Kotlin has quite a lot of goodies, so I was wondering, if it has the feature, which I have last seen when working with C/C++ macros:
I would like to be able to get the expression passed to a parameter of a called function. This would mostly be used for validation / logging purposes. E.g.:
fun checkNotNull(val value: Any?) {
if (value == null)
throw IllegalArgumentException("${value::passedExpression} is null")
}
val foo = Person(name = "Ada", surname = null)
checkNotNull(foo.surname)
So this would throw "foo.surname is null"
. This would save quite a lot of writing, which seems to be Kotlin's mission (or one of). It may have limitations, e.g. only return the expression if it's simple, like object graph navigation, null otherwise.
Is there any such feature? Or a feature request? Or perhaps pre-compiler addon?