A Question Asker
A Question Asker

Reputation: 3343

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?

Upvotes: 0

Views: 30

Answers (1)

Eugene Burmako
Eugene Burmako

Reputation: 13048

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.

Upvotes: 1

Related Questions