Note that there are some explanatory texts on larger screens.

plurals
  1. POScala Macros: Checking for a certain annotation
    text
    copied!<p>Thanks to the answers to <a href="https://stackoverflow.com/q/17223213/397695">my previous question</a>, I was able to create a function macro such that it returns a <code>Map</code> that maps each field name to its value of a class, e.g.</p> <pre><code>... trait Model case class User (name: String, age: Int, posts: List[String]) extends Model { val numPosts: Int = posts.length ... def foo = "bar" ... } </code></pre> <p>So this command</p> <pre><code>val myUser = User("Foo", 25, List("Lorem", "Ipsum")) myUser.asMap </code></pre> <p>returns </p> <pre><code>Map("name" -&gt; "Foo", "age" -&gt; 25, "posts" -&gt; List("Lorem", "Ipsum"), "numPosts" -&gt; 2) </code></pre> <p>This is where <code>Tuple</code>s for the <code>Map</code> are generated (see Travis Brown's <a href="https://stackoverflow.com/a/17224392/397695">answer</a>):</p> <pre><code>... val pairs = weakTypeOf[T].declarations.collect { case m: MethodSymbol if m.isAccessor =&gt; val name = c.literal(m.name.decoded) val value = c.Expr(Select(model, m.name)) reify(name.splice -&gt; value.splice).tree } ... </code></pre> <p>Now I want to ignore fields that have <code>@transient</code> annotation. How would I check if a method has a <code>@transient</code> annotation?</p> <p>I'm thinking of modifying the snippet above as</p> <pre><code>val pairs = weakTypeOf[T].declarations.collect { case m: MethodSymbol if m.isAccessor &amp;&amp; !m.annotations.exists(???) =&gt; val name = c.literal(m.name.decoded) val value = c.Expr(Select(model, m.name)) reify(name.splice -&gt; value.splice).tree } </code></pre> <p>but I can't find what I need to write in <code>exists</code> part. How would I get <code>@transient</code> as an <code>Annotation</code> so I could pass it there?</p> <p>Thanks in advance!</p>
 

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