简体   繁体   English

Scala宏是否有可能约束方法的类型?

[英]Is it possible to have a scala macro constrain the type of a method?

Basically I'm imagining the following. 基本上,我在想象以下内容。

trait A[T] {
  def get(idx: Int): U
}

The macro comes in the use. 使用宏。 Ideally I want to be able to use get(0), get(1) as macros where the proper return type will be known. 理想情况下,我希望能够将get(0),get(1)用作可知道正确返回类型的宏。 Is this possible? 这可能吗?

Yes. 是。 Just specify the return type as Any and make your macro whitebox ( http://docs.scala-lang.org/overviews/macros/blackbox-whitebox.html#blackbox_and_whitebox_macros ). 只需将返回类型指定为Any ,然后将宏设置为白盒( http://docs.scala-lang.org/overviews/macros/blackbox-whitebox.html#blackbox_and_whitebox_macros )。 Whitebox macro expansions assume their actual type, not the declared type of the macro definition. 白盒宏扩展假定其实际类型,而不是宏定义的声明类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM