# 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:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![medium](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/medium/32/10404_2.png) [@medium](https://discuss.kotlinlang.org/u/medium)\
**Post date:** [March 8, 2016, 3:27pm UTC](https://discuss.kotlinlang.org/t/nameof-to-get-names-of-program-elements/1518/1 "2016-03-08T15:27:04Z")

</div>

C# has a really nice feature called [`nameof`](https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6#nameof-expressions). With this expression you can get the name of a program element in a safe way without repeating it as a string.

Example:

```
fun process(customer: Customer) {
    checkArgument(customer.isTrustworthy, "customer must be trustworthy")
    ...
}

```

Here you have to repeat the parameter name `customer` in the exception message. That is error prone.

It would be nice to have a feature like `nameof` here:

```
fun process(customer: Customer) {
    checkArgument(customer.isTrustworthy, "${nameof(customer)} must be trustworthy")
    ...
}

```

Is something like this on the roadmap?

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [March 8, 2016, 4:44pm UTC](https://discuss.kotlinlang.org/t/nameof-to-get-names-of-program-elements/1518/2 "2016-03-08T16:44:35Z")

</div>

No, we don’t have anything like that on the roadmap. Given that IntelliJ IDEA’s refactorings do a fairly good job of updating string literals, adding a special language feature just for this case seems to be a bit overkill.

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [March 9, 2016, 5:06pm UTC](https://discuss.kotlinlang.org/t/nameof-to-get-names-of-program-elements/1518/3 "2016-03-09T17:06:59Z")

</div>

> [@yole](#):
>
> refactorings do a fairly good job of updating string literals

I disagree. String literal search may find many unrelated usages especially for common identifier names, so it couldn’t be trusted really.

---

<div class="post-metadata">

**Author:** ![marchy](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@marchy](https://discuss.kotlinlang.org/u/marchy)\
**Post date:** [February 10, 2017, 8:12pm UTC](https://discuss.kotlinlang.org/t/nameof-to-get-names-of-program-elements/1518/4 "2017-02-10T20:12:23Z")

</div>

Absolutely agree with @ilya.gorbunov. Ruby has long had the :thing syntax which is used wildley different than “thing”.

Semantism is a core pinnacle of strongly-typed languages which Kotlin strongly firmly in. To suggest a string literal solution and leave it up to “IDE refactoring” is literally against the whole belief system which Kotlin is built upon, let alone being a huge known [Magic Strings](https://en.wikipedia.org/wiki/Magic_string) anti-pattern. This is why we have enums and the strongly-typed ‘when’ (Kotlin) / ‘switch’ (Swift) statements, to get AWAY from string-based, non-typed workarounds.

I wouldn’t expect this answer from an intermediate developer, let alone the you guys.

---

<div class="post-metadata">

**Author:** ![elizarov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/elizarov/32/2164_2.png) [@elizarov](https://discuss.kotlinlang.org/u/elizarov)\
**Post date:** [February 12, 2017, 1:11pm UTC](https://discuss.kotlinlang.org/t/nameof-to-get-names-of-program-elements/1518/5 "2017-02-12T13:11:49Z")

</div>

This feature actually exists for properties and function names, but not for local variables (yet). Try this:

```auto
class Data(val namedProp: String)
fun namedFunction(): String = ""

fun main(args: Array<String>) {
    println("My property name is '${Data::namedProp.name}'")
    println("My function name is '${::namedFunction.name}'")
}

```

Compiler actually inlines the corresponding invocations and substitutes the actual names, so there is no runtime reflection in this code.

As for variables: [https://youtrack.jetbrains.com/issue/KT-16303](https://youtrack.jetbrains.com/issue/KT-16303)

P.S. There is also a minor performance problem related to existing `.name` support in compiler, but that is not the reason for not using this feature if you needed it, and definitely not a reason to invent any kind of `nameof` (because `.name` is the Kotlin way!): [https://youtrack.jetbrains.com/issue/KT-16304](https://youtrack.jetbrains.com/issue/KT-16304)

---

<div class="post-metadata">

**Author:** ![a.kosenkov](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@a.kosenkov](https://discuss.kotlinlang.org/u/a.kosenkov)\
**Post date:** [November 22, 2017, 11:21am UTC](https://discuss.kotlinlang.org/t/nameof-to-get-names-of-program-elements/1518/6 "2017-11-22T11:21:29Z")

</div>

Another related use case for variable names:

```kotlin
val `Some Kafka Topic Name` = magicallyDefine<Key, Value>("Some Kafka Topic Name")

```

Obviously, repeating the name is not super convenient. I could imagine, if `magicallyDefine` is an `inline` function, it could potentially access the outer variable name using similar syntax and get the `String` using the proposed `.name` semi-reflection:

```kotlin
val `Some Kafka Topic Name` = magicallyDefine<Key, Value>()

```

Where the name is deducted using syntax like `val topic: String = ::return.name`, which looks amazingly bad and is not always available.

I guess at this level of complexity it is much better to make a normal named class.

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [November 22, 2017, 6:36pm UTC](https://discuss.kotlinlang.org/t/nameof-to-get-names-of-program-elements/1518/7 "2017-11-22T18:36:02Z")

</div>

@a.kosenkov You can achieve something like that with the delegated properties: [http://kotlinlang.org/docs/reference/delegated-properties.html](http://kotlinlang.org/docs/reference/delegated-properties.html)

---

<div class="post-metadata">

**Author:** ![robstoll](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/robstoll/32/4488_2.png) [@robstoll](https://discuss.kotlinlang.org/u/robstoll)\
**Post date:** [April 13, 2018, 9:54am UTC](https://discuss.kotlinlang.org/t/nameof-to-get-names-of-program-elements/1518/8 "2018-04-13T09:54:20Z")

</div>

Another use case is to retrieve names of object properties/functions. Something like `nameOf(a::foo)`.  
I have created a feature request: [https://youtrack.jetbrains.com/issue/KT-23777](https://youtrack.jetbrains.com/issue/KT-23777)

---

<div class="post-metadata">

**Author:** ![shanepcarr](https://avatars.discourse-cdn.com/v4/letter/s/f07891/32.png) [@shanepcarr](https://discuss.kotlinlang.org/u/shanepcarr)\
**Post date:** [July 17, 2019, 5:57pm UTC](https://discuss.kotlinlang.org/t/nameof-to-get-names-of-program-elements/1518/9 "2019-07-17T17:57:09Z")

</div>

> [@ilya.gorbunov](#):
>
> agree. String literal search may find many unrelated usages especially for common identifier names, so it couldn’t be trusted really.

Agree too. one must remember to run a refactoring. With nameof more is automatic. Big value proposition of Kotlin is that there is actually innovation and new features being added. This is one that unquestionably should be added. I use this a lot in C#. it is a great feature.

---

<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.

---

<div class="post-metadata">

**Author:** ![medium](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/medium/32/10404_2.png) [@medium](https://discuss.kotlinlang.org/u/medium)\
**Post date:** [July 31, 2019, 12:04pm UTC](https://discuss.kotlinlang.org/t/nameof-to-get-names-of-program-elements/1518/11 "2019-07-31T12:04:36Z")

</div>

The idea is not bad, but it won’t work for parameter names. And it would add some (not too big, I think) runtime overhead whereas a `nameof` operator would be evaluated at compile time.

---

<div class="post-metadata">

**Author:** ![dan.lugg](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dan.lugg/32/6256_2.png) [@dan.lugg](https://discuss.kotlinlang.org/u/dan.lugg)\
**Post date:** [February 11, 2022, 4:13pm UTC](https://discuss.kotlinlang.org/t/nameof-to-get-names-of-program-elements/1518/12 "2022-02-11T16:13:55Z")

</div>

This would be ideal for use in some annotation parameters. Since annotation parameters with interpolated strings (_with compile time constants_) are already supported, `nameof` would be very useful in these circumstances. For example, with AspectJ:

```auto
// This predictably fails because MyAnnotation::class.simpleName is not
// a compile-time constant
@Around("@annotation(${MyAnnotation::class.simpleName})")

// This would presumably work, as nameof(MyAnnotation) should resolve
// to a compile-time constant
@Around("@annotation(${nameof(MyAnnotation)})")

```

---

<div class="post-metadata">

**Author:** ![jfontsaballs](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@jfontsaballs](https://discuss.kotlinlang.org/u/jfontsaballs)\
**Post date:** [June 8, 2022, 4:06pm UTC](https://discuss.kotlinlang.org/t/nameof-to-get-names-of-program-elements/1518/13 "2022-06-08T16:06:35Z")

</div>

`nameof` would be very useful in combination with `kotlin-react`:

Instead of writing:

```auto
val Login = FC<Props>("Login") {}

val Dashboard = FC<Props>("Dashboard") {}

```

We could easily link the display name of the component to the variable name:

```auto
val Login = FC<Props>(nameof(Login)) {}

val Dashboard = FC<Props>(nameof(Dashboard)) {}

```

Please be aware that in this case using `::Login.name` does not compile.

---

<div class="post-metadata">

**Author:** ![xyzboom](https://avatars.discourse-cdn.com/v4/letter/x/f0a364/32.png) [@xyzboom](https://discuss.kotlinlang.org/u/xyzboom)\
**Post date:** [April 16, 2025, 5:06am UTC](https://discuss.kotlinlang.org/t/nameof-to-get-names-of-program-elements/1518/14 "2025-04-16T05:06:45Z")

</div>

I think this requirement is just to avoid hard coding.  
So I wrote a KSP project to temporarily solve the problem of hard coding with constant names. Use a `@Konst` annotation on the declarations that you want to use its name.

```kotlin
@Konst class MyClass

```

The variable `MyClass_qName` and `MyClass_sName` will be generated at the top-level in the same package of `MyClass`.  
See more information at my toy project: [GitHub - XYZboom/konst-names](https://github.com/XYZboom/konst-names).

---

<div class="post-metadata">

**Author:** ![madmax1028](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/madmax1028/32/3335_2.png) [@madmax1028](https://discuss.kotlinlang.org/u/madmax1028)\
**Post date:** [April 16, 2025, 9:50am UTC](https://discuss.kotlinlang.org/t/nameof-to-get-names-of-program-elements/1518/15 "2025-04-16T09:50:27Z")

</div>

Yeah. Besides the oblivious lack of support for function parameters all those `.name` references are basically useless in annotations where only constants are acceptable.

---

<div class="post-metadata">

**Author:** ![MIha\_Markic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/miha_markic/32/11397_2.png) [@MIha\_Markic](https://discuss.kotlinlang.org/u/MIha_Markic)\
**Post date:** [July 17, 2026, 10:37am UTC](https://discuss.kotlinlang.org/t/nameof-to-get-names-of-program-elements/1518/16 "2026-07-17T10:37:25Z")

</div>

I really wonder why is nameof not implemented at this time. Seems like a low hanging fruit which would make code more robust.
