Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reifying the function implementation instead of the reference

I need to get the AST of the implementation of a lambda function. And I get stuck because reify works on the argument and not on it's value.

val x = (a: Int) => println("a")
val t = showRaw(reify(thevalueof x)) //something here should change
println(t)

should print:

Expr(Function(List(ValDef(Modifiers(PARAM), newTermName("a"), Ident(scala.Int), EmptyTree)), Apply(Select(Select(This(newTypeName("scala")), newTermName("Predef")), newTermName("println")), List(Literal(Constant("a"))))))

I supposed there should be a trick with splice or so, but I just can't manage to do this.

like image 762
Ingdas Avatar asked Mar 05 '14 13:03

Ingdas


People also ask

What is a reified function?

"reified" is a special type of keyword that helps Kotlin developers to access the information related to a class at runtime. "reified" can only be used with inline functions. When "reified" keyword is used, the compiler copies the function's bytecode to every section of the code where the function has been called.

What are reified Generics?

Reified Generics is a generics system that makes generics type information available at runtime. C# is one language that supports Reified Generics natively, as opposed to Java's Type-Erased Generics.

What is inline Kotlin?

An Inline function is a kind of function that is declared with the keyword "inline" just before the function declaration. Once a function is declared inline, the compiler does not allocate any memory for this function, instead the compiler copies the piece of code virtually at the calling place at runtime.

How do I use generic Kotlin?

If a generic type has several type parameters, each of them can be projected independently. For example, if the type is declared as interface Function<in T, out U> you could use the following star-projections: Function<*, String> means Function<in Nothing, String> . Function<Int, *> means Function<Int, out Any?> .


1 Answers

Currently there is no robust way of getting ASTs of the program outside of a macro application (and reify is a macro, so it abides by the same rules). However, we are experimenting with a next-gen macro engine that might fix this problem: http://scalamacros.org/news/2014/03/02/project-palladium.html.

like image 111
Eugene Burmako Avatar answered Oct 27 '22 03:10

Eugene Burmako