Compiler doesn't generate a field for implicit val when an implicit val with the same type is present in the superclass

37 views
Skip to first unread message

Alexey Romanov

unread,
Aug 21, 2015, 3:37:35 AM8/21/15
to scala-user
Also asked at http://stackoverflow.com/questions/32118877/field-not-generated-for-an-implicit-val-class-constructor-parameter.

Consider this definition:

    class Elem[A]
   
    abstract class BaseDef[T](implicit val selfType: Elem[T])
   
    case class Foo[A, T]()(implicit val eA: Elem[A], val eT: Elem[T]) extends BaseDef[A]

I expect Foo to have fields eA and eT, but it only has eT:

    > val fields = classOf[Foo[_, _]].getDeclaredFields

    > println(fields.mkString("\n"))

private final scalan.Elem scalan.Foo.eT

It seems the compiler decides it can reuse selfType field for eA, which would be great if I didn't want to access eA with scala-reflect. Is there a work-around to force the compiler to generate the field? Should this be reported as a bug?


Jason Zaugg

unread,
Aug 21, 2015, 7:16:42 AM8/21/15
to Alexey Romanov, scala-user
The compiler avoids redundant fields in subclasses if it statically determines that the value is stored in a field in a super class that exposes it via an accessible accessor.

To prevent this, you might be able to change the superclass constructor by removing the val keyword and add another val to that class that stores the parameter.

-jason

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

Alexey Romanov

unread,
Aug 21, 2015, 8:59:43 AM8/21/15
to scala-user, alexey.v...@gmail.com
Thank you!
Reply all
Reply to author
Forward
0 new messages