Hi,
I asked this on SO but there were no answers, so I’ll try here.
When a method (in this example a constructor, apply
) has a parameter with a default value, scala generates a synthetic method on the companion object named [method]$default$[index]
where index is the parameter’s position.
I can access this method from outside the companion object, but not from inside it. This works:
case class C(i: Int = 1)
object C
def x: Int = C.apply$default$1
But this doesn’t:
case class C(i: Int = 1)
object C {
def x: Int = C.apply$default$1
}
scalac complains that value apply$default$1 is not a member of object C
. Presumably the method is generated later than my own def x
is processed.
I need to access this method from inside the companion object because I’m generating implicit typeclasses from a macro. Is there a way to do this?
Right now I’m generating code that uses runtime reflection to call the method, but that is inefficient and unsatisfying.
Thanks!
Daniel Armak
--
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/d/optout.
Thanks Som, but I don’t understand what this macro (in Macros_1) has to do with the problem at hand. The macro creates a class C
with a method f
with a parameter a
with a default value. So what?
I’ve been trying to follow up on your hint on SO. Unfortunately, I don’t understand Scala compiler internals either! You suggest that I “substitute your own namer in the attachment to enter your additional methods at the same time”. At the same time as what? Adding what methods?
I got my macro to find the ConstructorDefaultsAttachment and its Namer, and I should be able to use it to add methods to the module.
Thanks you for your help, really. I’m sorry I need more instructions, but it would probably take me way too long to figure out by myself everything that’s going on here.