This Atom is Under Construction

Reflection means taking an object and holding it up to a mirror, so it discovers things about itself.

For example, we often want to find out an object’s class name:

// Reflection/Name.kt package reflection fun className(a: Any): String = a::class.simpleName ?: ""

className() takes an Any object and produces that object’s class name. To do this, we call ::class, yielding a reference to a KClass object, which contains numerous properties and functions that allow you to discover information about, and to manipulate, that class. simpleName is one of those properties, producing the class name without any qualification. Because simpleName might produce a null, we handle it appropriately using Safe Calls & The Elvis Operator.

End of sample. See for full early-access book.

Previous          Next

©2018 Mindview LLC. All Rights Reserved.