How to get a type in Scala AST of underscored lambda function parameter?

407 views Asked by At

Not digging in details of DSL, I could write (based on that example):

def InputLine = rule { Number ~ zeroOrMore("+" ~ Number ~> ((x: Int, y: Int) => x + y)) ~ EOI }

I need to call lambda function from macro. Scala AST representation of it is as follows:

List(
   ValDef(Modifiers(PARAM), newTermName("x"), TypeTree().setOriginal(Select(Ident(scala), scala.Int)), EmptyTree), 
   ValDef(Modifiers(PARAM), newTermName("y"), TypeTree().setOriginal(Select(Ident(scala), scala.Int)), EmptyTree))

Important to note is that both x and y has types in AST. This works fine.

Next step is to use type inference, and simplify lambda as follows:

def InputLine = rule { Number ~ zeroOrMore("+" ~ Number ~> ((_:Int) + _)) ~ EOI }

Scala AST for that lambda:

List(
    ValDef(Modifiers(PARAM | SYNTHETIC), newTermName("x$1"), TypeTree().setOriginal(Select(Ident(scala), scala.Int)), EmptyTree), 
    ValDef(Modifiers(PARAM | SYNTHETIC), newTermName("x$2"), TypeTree(), EmptyTree))

Note that x$2 param has no explicit type. I need that to call the function. Where should I get it? The solution "just pass parameter of type Any to a function" will not satisfy scalac as it requires Int.

This question is logically followed by that one.

The code is available at parboled2@gihub.

1

There are 1 answers

0
Alexander Myltsev On BEST ANSWER
Number ~> ((_:Int) + _)

is transformed to something like this

pimpActionOp[...](rule).~>.apply[...](function)

type classes of pimpActionOp and apply contain all necessary information to restore types.