# Is there anything like C# Caller Information in Kotlin?

**URL:** <https://discuss.kotlinlang.org/t/is-there-anything-like-c-caller-information-in-kotlin/11447>\
**Category:** Support\
**Created:** [February 5, 2019, 10:01am UTC](https://discuss.kotlinlang.org/t/is-there-anything-like-c-caller-information-in-kotlin/11447 "2019-02-05T10:01:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rupert.rawnsley](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/rupert.rawnsley/32/6030_2.png) [@rupert.rawnsley](https://discuss.kotlinlang.org/u/rupert.rawnsley)\
**Post date:** [February 5, 2019, 10:01am UTC](https://discuss.kotlinlang.org/t/is-there-anything-like-c-caller-information-in-kotlin/11447/1 "2019-02-05T10:01:39Z")

</div>

[Caller Information](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/caller-information) can help create powerful logging functionality with no burden on the caller, on runtime performance, or on compile time.

Is there anything like this in Kotlin?

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [February 5, 2019, 10:43am UTC](https://discuss.kotlinlang.org/t/is-there-anything-like-c-caller-information-in-kotlin/11447/2 "2019-02-05T10:43:19Z")

</div>

No, but I agree that it would be a useful feature.

---

<div class="post-metadata">

**Author:** ![marco89nish](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@marco89nish](https://discuss.kotlinlang.org/u/marco89nish)\
**Post date:** [February 5, 2019, 11:24am UTC](https://discuss.kotlinlang.org/t/is-there-anything-like-c-caller-information-in-kotlin/11447/3 "2019-02-05T11:24:55Z")

</div>

On JVM you can use `Thread.currentThread().getStackTrace()`. Not sure about the rest of the backends, though.

---

<div class="post-metadata">

**Author:** ![jstuyts](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jstuyts/32/2141_2.png) [@jstuyts](https://discuss.kotlinlang.org/u/jstuyts)\
**Post date:** [February 5, 2019, 11:30am UTC](https://discuss.kotlinlang.org/t/is-there-anything-like-c-caller-information-in-kotlin/11447/4 "2019-02-05T11:30:57Z")

</div>

Creating a `Throwable` is way more expensive at run-time than compile-time generated parameters.

This information is useful regularly, so it would be great if the compiler can simply insert this for you.

Maybe it can be built with a compiler plug-in?

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [February 5, 2019, 11:52am UTC](https://discuss.kotlinlang.org/t/is-there-anything-like-c-caller-information-in-kotlin/11447/5 "2019-02-05T11:52:50Z")

</div>

> [@jstuyts](#):
>
> Maybe it can be built with a compiler plug-in?

Pretty sure that this could be done. All you need to do is replace a property with the right filename, etc. It’s the same concept that was used for `coroutineContext` in the earlier iteration of the coroutine system, if I remember correctly.

* * *

Another option of getting this is to just write a KEEP proposal. I don’t think there would be much arguing about it. And that way it would have a far greater reach than a plug-in.

There is however one question to consider. How should this work with inline functions

```kotlin
fun foo() {
    bar()
}
inline fun bar() {
    println("$magicFilename$magicLineNumber")
}

```

There is an argument for it to print the file and line number of the callsite of bar. That way you can create your own logging functions. On the other hand you would no longer be able to use this from within other inline functions without confusion. So maybe it should report the file and linenumber of `println`. Maybe an annotation could be used to change this behaviour

```kotlin
fun foo() {
    bar()
}
inline fun bar() {
    log()
}
@InlineFileInfo
inline fun log() {
     println("$magicFilename$magicLineNumber")
}

```

Obviously those names should be improved.

---

<div class="post-metadata">

**Author:** ![marco89nish](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@marco89nish](https://discuss.kotlinlang.org/u/marco89nish)\
**Post date:** [February 5, 2019, 11:59am UTC](https://discuss.kotlinlang.org/t/is-there-anything-like-c-caller-information-in-kotlin/11447/6 "2019-02-05T11:59:09Z")

</div>

My bad, I didn’t look at example usage, from description it looked like runtime call.
