简体   繁体   中英

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. 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 ). Whitebox macro expansions assume their actual type, not the declared type of the macro definition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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