# Class references

**URL:** <https://discuss.kotlinlang.org/t/class-references/358>\
**Category:** Uncategorized\
**Created:** [January 3, 2015, 10:03pm UTC](https://discuss.kotlinlang.org/t/class-references/358 "2015-01-03T22:03:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nbilyk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nbilyk/32/1483_2.png) [@nbilyk](https://discuss.kotlinlang.org/u/nbilyk)\
**Post date:** [January 3, 2015, 10:03pm UTC](https://discuss.kotlinlang.org/t/class-references/358/1 "2015-01-03T22:03:09Z")

</div>

I'm looking for recommendations on how to design something without the use of class references.

Kotlin has javaClass() for compatibility with Java code, but otherwise, there are no class references (unless I’m missing something).  
What would the best way to design an API such as the following:

```kotlin
fun <T> setLoader(type:Class<T>, loader: Loader<T>) { loaderMap.put(type, loader) }
fun load(file:String, type:Class) { loaderMap.get(type).load(file) }
```

---

<div class="post-metadata">

**Author:** ![orangy](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/orangy/32/1462_2.png) [@orangy](https://discuss.kotlinlang.org/u/orangy)\
**Post date:** [January 3, 2015, 10:29pm UTC](https://discuss.kotlinlang.org/t/class-references/358/2 "2015-01-03T22:29:54Z")

</div>

Reflection and class "literals" are coming, though there is still a lot of work ahead. Meanwhile, you can use recently introduced reified type parameters for inline functions.

&nbsp;

```kotlin
inline fun LoaderRegistry.setLoaderFor<reified T>(loader: Loader<T>) = setLoader(javaClass<T>(), loader)
```

`
and use it like this
`

```kotlin
registry.setLoaderFor<MyEntity>(EntityLoader())
```

`
depending on how rich is your domain space, there could be more DSLs around it, such as:
`

` ```kotlin
// core traits
trait LoaderRegistry {
    fun setLoader<T>(klass: Class<T>, loader: Loader<T>)
    fun setStrategy<T>(klass: Class<T>, strategy: LoaderStrategy)
}
trait Loader<T>
enum class LoaderStrategy {
  Singleton
  Transient
}
// DSL
class LoaderRegistryDescriptor<T>(val registry: LoaderRegistry, val klass: Class<T>)
inline fun LoaderRegistry.describe<reified T>(body: LoaderRegistryDescriptor<T>.() -> Unit)
  = LoaderRegistryDescriptor(this, javaClass<T>()).body()
fun <T> LoaderRegistryDescriptor<T>.loader(loader: Loader<T>) = registry.setLoader<T>(klass, loader)
fun <T> LoaderRegistryDescriptor<T>.strategy(value: LoaderStrategy) = registry.setStrategy<T>(klass, value)
// domain objects
class MyEntity
class EntityLoader : Loader<MyEntity>
fun fn(registry: LoaderRegistry) {
  registry.describe<MyEntity> {
        loader(EntityLoader())
  strategy(LoaderStrategy.Singleton)
  }
}

```
`

---

<div class="post-metadata">

**Author:** ![nbilyk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nbilyk/32/1483_2.png) [@nbilyk](https://discuss.kotlinlang.org/u/nbilyk)\
**Post date:** [January 3, 2015, 10:42pm UTC](https://discuss.kotlinlang.org/t/class-references/358/3 "2015-01-03T22:42:04Z")

</div>

Thanks for the quick response. I should have clarified -- It would need to work for both JVM and JS, so using javaClass() won't work.

---

<div class="post-metadata">

**Author:** ![nbilyk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nbilyk/32/1483_2.png) [@nbilyk](https://discuss.kotlinlang.org/u/nbilyk)\
**Post date:** [January 4, 2015, 1:40am UTC](https://discuss.kotlinlang.org/t/class-references/358/4 "2015-01-04T01:40:51Z")

</div>

Is there any way to get the class reference in JS?

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [January 4, 2015, 7:56am UTC](https://discuss.kotlinlang.org/t/class-references/358/5 "2015-01-04T07:56:49Z")

</div>

Currently Kotlin/JS has no means of reflection. It will be supported a little later later than Kotlin-specific reflection on the JVM.

---

<div class="post-metadata">

**Author:** ![nbilyk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nbilyk/32/1483_2.png) [@nbilyk](https://discuss.kotlinlang.org/u/nbilyk)\
**Post date:** [January 4, 2015, 10:10pm UTC](https://discuss.kotlinlang.org/t/class-references/358/6 "2015-01-04T22:10:07Z")

</div>

I had a bit of an "Aha" moment. Classes with class objects can be passed around a bit like a class reference. So in my case, where I want to map a type to an instance, I can use the reference to the class object.

class Foo {  
&nbsp;&nbsp;class object  
}

map.put(Foo, true) // Works
