Woody Robert
unread,Jul 4, 2013, 6:22:20 AM7/4/13Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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!