Book Image

Programming Kotlin

Book Image

Programming Kotlin

Overview of this book

Quickly learn the fundamentals of the Kotlin language and see it in action on the web. Easy to follow and covering the full set of programming features, this book will get you fluent in Kotlin for Android.
Table of Contents (20 chapters)
Programming Kotlin
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface

Useful KClass properties


A KClass fully describes a particular class including its type parameters, superclasses, functions, constructors, annotations, and properties. Let's define a toy class:

    class Sandwich<F1, F2>() 

Now we can inspect the KClass for this and find out the types of parameters it declares. We do this using the typeParameters property available on the KClass instance:

    val types = Sandwich::class.typeParameters 

From here, we can get the label of the type parameter, and the upper bounds, if any have been defined (otherwise Any):

    types.forEach { 
      println("Type ${it.name} has upper bound ${it.upperBounds}") 
    } 

In the case of Sandwich, this would output the following:

    Type F1 has upper bound [kotlin.Any?] 
    Type F2 has upper bound [kotlin.Any?] 

Next, let's show the superclasses for a given type. Firstly, we need a type that has many parents:

    class ManyParents : Serializable, Closeable, java.lang.AutoCloseable...