Scala untyped macro at infix position

In answer to this question , I went to implement a Haskell-like expression in Scala style using the macro-paradise branch. The code is available in scala-where . Now I can write something like the following:

val result = where ( f1(1) * f2(2), {
  def f1(x : Int) = x + 1
  def f2(x : Int) = x + 2
})

However, what I really would like to do is be able to call it in infix position:

val result = ( f1(1) * f2(2)) where {
  def f1(x : Int) = x + 1
  def f2(x : Int) = x + 2
}

Normally, this kind of thing would be easy, but I can't figure out how to do this when calling a macro. The expression (f1 (1) * f2 (2)) will not be entered until the macro is applied, so something like constructing an implicit class of values ​​does not work. Is there any way to get such syntax otherwise?

Otherwise, just having two lists of options that could be made:

val result = where (f1(1) * f2(2)) {
  def f1(x : Int) = x + 1
  def f2(x : Int) = x + 2
}

, . ?

+5
2

: , , ?

: , . , :

def myMacro(a: _)(b: _) = macro myMacro_impl

def myMacro_impl(c: Context)(a: c.Tree)(b: c.Tree): c.Tree = { ... }

:

myMacro(...)(...)
+2

: 2013-03-08 infix. scala -user :

. , "" foo (x: _) " - .

, , :

implicit class HasWhere(val exp : _) {
 def where(block : Unit) = macro whereInfix
}

def whereInfix(c : Context)(block : c.Expr[Unit]) = {
  import c.universe._

  val exp = Select(c.prefix.tree, TermName("exp"))
  val Expr(Block((inner, _))) = block
  val newinner = inner :+ exp
  Block(newinner : _*)
}
+1

All Articles