Get val definition name

I would like to catch the name of the variable to which the output of my macro is assigned. Just like projectin build.sbt. I would prefer to use this solution (library) if there is one, because it looks like a normal general use case.

Here is a small example

val someValue = myMacro()

and as an output, myMacro()I would like to get a string "someValue".

+4
source share
1 answer

You are looking for SourceCode .

Example:

scala> def myName(implicit name: sourcecode.Name) = name.value
myName: (implicit name: sourcecode.Name)String

scala> val foo = myName
foo: String = foo
+8
source

Source: https://habr.com/ru/post/1663823/


All Articles