Krever
Krever

Reputation: 1467

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

Upvotes: 6

Views: 1164

Answers (1)

Jasper-M
Jasper-M

Reputation: 15086

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

Upvotes: 9

Related Questions