[macro] Splicing a list of parameters with default values

161 views
Skip to first unread message

Woody Robert

unread,
Jul 4, 2013, 6:22:20 AM7/4/13
to scala...@googlegroups.com
Hi,

We have a macro that generates a value having a structural type, so we generate each of its members. One of them is a method with default parameter values.

E.g., we want the following invokation:

case class User(name: String, age: Int)
val user = User("Peter", 42)

val result = myMacro(user)


To generate a value with a method copy like the following:

new {
  def copy(name: String = get(user, "name"), age: Int = get(user, "age")): User = ???
}


So we can then write result.copy or result.copy(age = 0) and so on.

Here is a relevant part of our code:

val paramsCopy = for(param <- params) yield {
  val paramName = param.name.toTermName
  val paramNameString = param.name.toString
  val paramType = param.typeSignature
  q"val $paramName: $paramType = get($obj , $paramNameString)"
}

val defCopy = q"""def copy(..${paramsCopy.reverse}) = ???"""

We have two questions:

1. We defined each parameter with a quasiquote containing a val def. If we replace it with just q"$paramName: $paramType = get($obj , $paramNameString)", we get a syntax error when the macro is expanded (';' expected but '=' found). With methods having parameters with no default value, if we remove the leading val keyword we get an AST type error (found Typed, required: ValDef). What is the right way to build a parameter list?
2. The default parameters don't work. I can call result.copy("John", 0), though. Are default parameters filled before macro invocations?

Thanks for your help!

Eugene Burmako

unread,
Jul 4, 2013, 6:40:29 AM7/4/13
to Woody Robert, scala-user
Could you please elaborate on defaults not working? What error messages are you getting?


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Woody Robert

unread,
Jul 4, 2013, 8:56:43 AM7/4/13
to scala...@googlegroups.com, Woody Robert
We have this result :

result.copy(name = "John", age = 0) // Ok
result.copy(name = "John")                   // Error: not enough arguments for method copy; Unspecified value parameter age
result.copy                                                 // Error: not enough arguments for method copy; Unspecified value parameters name, age

Thanks for helping.

Lukas Rytz

unread,
Jul 4, 2013, 2:08:22 PM7/4/13
to scala...@googlegroups.com, Woody Robert
Maybe it's due to some flags (DEFAULTPARAM) that are not set by the quasiquote parser?

  q"val $paramName: $paramType = get($obj , $paramNameString)"

Reply all
Reply to author
Forward
0 new messages