Accessing compiler-generated apply$default$1 from inside the companion object

32 views
Skip to first unread message

Daniel Armak

unread,
Feb 27, 2015, 4:33:49 AM2/27/15
to scala-user

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

Som Snytt

unread,
Feb 27, 2015, 12:55:28 PM2/27/15
to Daniel Armak, scala-user
I offered a partial answer on SO. Now, off to my day job.

There's an example for entering default params in a macro:

https://github.com/scala/scala/blob/v2.11.5/test/files/run/macro-default-params/Macros_1.scala

Probably the macro folks want to make it easier, over to time, to interact with the pipeline in this way.

It would be nice if a macro could say, Let me view this module symbol after the default-args micro-phase.





--
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.

Daniel Armak

unread,
Feb 27, 2015, 5:28:15 PM2/27/15
to Som Snytt, scala-user

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.


Daniel Armak
Reply all
Reply to author
Forward
0 new messages