# Nameof to get names of program elements

**URL:** <https://discuss.kotlinlang.org/t/nameof-to-get-names-of-program-elements/1518>\
**Category:** Language Design\
**Created:** [March 8, 2016, 3:27pm UTC](https://discuss.kotlinlang.org/t/nameof-to-get-names-of-program-elements/1518 "2016-03-08T15:27:04Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![series0ne](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/series0ne/32/6772_2.png) [@series0ne](https://discuss.kotlinlang.org/u/series0ne)\
**Post date:** [July 31, 2019, 9:26am UTC](https://discuss.kotlinlang.org/t/nameof-to-get-names-of-program-elements/1518/10 "2019-07-31T09:26:02Z")

</div>

Out of curiosity, what’s wrong with some first-class functions to handle this?

```auto
fun nameOf(callable: KCallable<*>): String = callable.name
fun nameOf(clazz: KClass<*>): String = clazz.simpleName!!
fun nameOf(clazz: Class<*>): String = clazz.simpleName

```

Use like so…

```auto
data class MyAwesomeClass(val myAwesomeProperty: Int) {
    fun helloWorld() = Unit
}

fun main(vararg args: String) {
    println(nameOf(MyAwesomeClass::class))
    println(nameOf(MyAwesomeClass::myAwesomeProperty))
    println(nameOf(MyAwesomeClass::helloWorld))
}

```

Works for classes, properties, functions…the only case I’ve found where it won’t work is local fields.

---

_[View the full topic](https://discuss.kotlinlang.org/t/nameof-to-get-names-of-program-elements/1518)._
