Get name of defining val

筅森魡賤 提交于 2019-12-12 10:48:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!