Note that there are some explanatory texts on larger screens.

plurals
  1. POScala Macros: Convert/parse a Tree to a Name
    primarykey
    data
    text
    <p>This is a simplified example but the problem remains the same.</p> <p>I want to achieve this using macros (scala based pseudocode):</p> <pre><code>(a: Int) =&gt; { val z = "toShort" a.z } </code></pre> <p>If I reify it, I would obtain something similar to this:</p> <pre><code>Function( List( ValDef( Modifiers(Flag.PARAM), newTermName("a"), Ident(scala.Int), EmptyTree ) ), Block( List( ValDef( Modifiers(), newTermName("z"), TypeTree(), Literal(Constant("toShort")) ) ), Apply( Select( Ident(newTermName("a")), newTermName("toShort") ), List() ) ) ) </code></pre> <p>I dont know how to access to a value and then use it as a TermName.</p> <p>I tried replacing <code>newTermName("toShort")</code> with <code>newTermName(c.Expr[String](Select(Ident(newTermName("z")))).splice)</code> but the compiler doesn't seem to like:</p> <blockquote> <p>exception during macro expansion: java.lang.UnsupportedOperationException: the function you're calling has not been spliced by > the compiler. this means there is a cross-stage evaluation involved, and it needs to be invoked explicitly. if you're sure this is not an oversight, add scala-compiler.jar to the classpath, import <code>scala.tools.reflect.Eval</code> and call <code>&lt;your expr&gt;.eval</code> instead.</p> </blockquote> <p>I've also tried 'eval' as suggested by compiler: <code>newTermName(c.eval(c.Expr[String](...))</code> but neither worked.</p> <p>How could I convert a tree like <code>Select(Ident(newTermName("z")))</code> (which is a access to a value of a local val) to <strike>a Name</strike> a string which can be used as a parameter for <code>newTermName</code>? Is it possible?</p> <p><strong>UPDATE:</strong></p> <p>Here the real problem brought to you as a <a href="https://gist.github.com/glerchundi/b5c0fef88fb0bb222fde#file-validation-scala-L196" rel="nofollow">gist</a>!</p> <p>Thanks in advance,</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload