问题
I would like to catch the name of variable that output of my macro is assigned to. Exactly like project in build.sbt. I would prefer out of the box solution(library) if there is one, because it looks like pretty general use case.
Here is small example
val someValue = myMacro()
and as an output of myMacro() I would like to get string "someValue".
回答1:
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
来源:https://stackoverflow.com/questions/41160751/get-name-of-defining-val