Compile Time Parameter for Macro Expansion

落花浮王杯 提交于 2019-12-11 05:29:47

问题


I would like to write an annotation macro that adds an extends <sometype> to traits where <sometype> can be specified at compile time.

How can a compile time parameter be passed to a macro expansion? Ideally I would like to specify a command line argument at the compiler call.


回答1:


Macro annotations don't have access to the command-line flags passed to scalac. However, one possible way to achieve this might be to use system properties.

For example, in the macro annotation implementation

// MyMacro.scala
val someType = sys.props.getOrElse("myapp.sometype", ???)

Then pass the type as a command line option

// command-line
scalac -Dmyapp.sometype=foobar Code.scala

Similarly, it's possible to run sbt -Dsometype=foobar compile. However, note that the JVM process needs to start with the system property flag, so setting scalacOptions += "-Dsometype=foobar" may not work.



来源:https://stackoverflow.com/questions/42969692/compile-time-parameter-for-macro-expansion

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