Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get name of defining val

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

like image 231
Krever Avatar asked Dec 15 '16 09:12

Krever


1 Answers

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
like image 96
Jasper-M Avatar answered Oct 05 '22 11:10

Jasper-M