- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Strange behaviour with case objects and default parameter in superclass
Sat, 2012-01-07, 22:17
Hi,
can anyone explain what's going on here?
In the session below the resulting value of a list of singleton objects depends on whether I am accessing one of those objects before accessing the list. I am using the REPLs paste mode, so you can try verify it easily (I'm running it on OS X):
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29).Type in expressions to have them evaluated.Type :help for more information.
scala> :paste// Entering paste mode (ctrl-D to finish)
class IdAndMsg(val id: Int, val msg: String = "")
case object ObjA extends IdAndMsg(1)case object ObjB extends IdAndMsg(2)
object IdAndMsg { val values = List(ObjA , ObjB)}
// Exiting paste mode, now interpreting.
defined class IdAndMsgdefined module ObjAdefined module ObjBdefined module IdAndMsg
scala>
scala> ObjAres0: ObjA.type = ObjA
scala> IdAndMsg.valuesres1: List[Product with Serializable with IdAndMsg] = List(null, ObjB)
Why does IdAndMsg.values contains a null value?
If I reverse the last 2 steps everything is ok:
... (pasting the code) ...scala> IdAndMsg.valuesres0: List[Product with Serializable with IdAndMsg] = List(ObjA, ObjB)
scala> ObjAres1: ObjA.type = ObjA
If I do not rely on the default parameter in the base class, i.e. I define ObjA as follows
case object ObjA extends IdAndMsg(1,"")
... then the evaluation order doesn't matter and the list does not contain null.
Am I missing something?
ThanksSoeren
can anyone explain what's going on here?
In the session below the resulting value of a list of singleton objects depends on whether I am accessing one of those objects before accessing the list. I am using the REPLs paste mode, so you can try verify it easily (I'm running it on OS X):
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29).Type in expressions to have them evaluated.Type :help for more information.
scala> :paste// Entering paste mode (ctrl-D to finish)
class IdAndMsg(val id: Int, val msg: String = "")
case object ObjA extends IdAndMsg(1)case object ObjB extends IdAndMsg(2)
object IdAndMsg { val values = List(ObjA , ObjB)}
// Exiting paste mode, now interpreting.
defined class IdAndMsgdefined module ObjAdefined module ObjBdefined module IdAndMsg
scala>
scala> ObjAres0: ObjA.type = ObjA
scala> IdAndMsg.valuesres1: List[Product with Serializable with IdAndMsg] = List(null, ObjB)
Why does IdAndMsg.values contains a null value?
If I reverse the last 2 steps everything is ok:
... (pasting the code) ...scala> IdAndMsg.valuesres0: List[Product with Serializable with IdAndMsg] = List(ObjA, ObjB)
scala> ObjAres1: ObjA.type = ObjA
If I do not rely on the default parameter in the base class, i.e. I define ObjA as follows
case object ObjA extends IdAndMsg(1,"")
... then the evaluation order doesn't matter and the list does not contain null.
Am I missing something?
ThanksSoeren
Wed, 2012-01-11, 14:01
#2
Re: Strange behaviour with case objects and default parameter i
Hi Sören,
this is almost certainly a bug - please file a ticket on https://issues.scala-lang.org/If you manage to reproduce / demonstrate it without the interpreter, that would be a great help.
Thanks: Lukas
On Sat, Jan 7, 2012 at 22:17, Sören Kress <soeren.kress@gmail.com> wrote:
this is almost certainly a bug - please file a ticket on https://issues.scala-lang.org/If you manage to reproduce / demonstrate it without the interpreter, that would be a great help.
Thanks: Lukas
On Sat, Jan 7, 2012 at 22:17, Sören Kress <soeren.kress@gmail.com> wrote:
Hi,
can anyone explain what's going on here?
In the session below the resulting value of a list of singleton objects depends on whether I am accessing one of those objects before accessing the list. I am using the REPLs paste mode, so you can try verify it easily (I'm running it on OS X):
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29).Type in expressions to have them evaluated.Type :help for more information.
scala> :paste// Entering paste mode (ctrl-D to finish)
class IdAndMsg(val id: Int, val msg: String = "")
case object ObjA extends IdAndMsg(1) case object ObjB extends IdAndMsg(2)
object IdAndMsg { val values = List(ObjA , ObjB)}
// Exiting paste mode, now interpreting.
defined class IdAndMsgdefined module ObjAdefined module ObjBdefined module IdAndMsg
scala>
scala> ObjAres0: ObjA.type = ObjA
scala> IdAndMsg.valuesres1: List[Product with Serializable with IdAndMsg] = List(null, ObjB)
Why does IdAndMsg.values contains a null value?
If I reverse the last 2 steps everything is ok:
... (pasting the code) ...scala> IdAndMsg.valuesres0: List[Product with Serializable with IdAndMsg] = List(ObjA, ObjB)
scala> ObjAres1: ObjA.type = ObjA
If I do not rely on the default parameter in the base class, i.e. I define ObjA as follows
case object ObjA extends IdAndMsg(1,"")
... then the evaluation order doesn't matter and the list does not contain null.
Am I missing something?
ThanksSoeren
It does look like a bug, like Lukas said, but the reason why you see
the behavior change is that objects are lazily initialized. Before you
actually use the object in some way, its initialization will not be
run. So, when you use objA (call toString on it through the REPL)
before IdAndMsg, you are initializing it. Still, I see nothing that
would justify the null. And I think I saw this bug before, so you
might want to *search* the issues database before opening a ticket.
On Sat, Jan 7, 2012 at 19:17, Sören Kress wrote:
> Hi,
>
> can anyone explain what's going on here?
>
> In the session below the resulting value of a list of singleton objects
> depends on whether I am accessing one of those objects before accessing the
> list. I am using the REPLs paste mode, so you can try verify it easily (I'm
> running it on OS X):
>
> Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM,
> Java 1.6.0_29).
> Type in expressions to have them evaluated.
> Type :help for more information.
>
> scala> :paste
> // Entering paste mode (ctrl-D to finish)
>
> class IdAndMsg(val id: Int, val msg: String = "")
>
> case object ObjA extends IdAndMsg(1)
> case object ObjB extends IdAndMsg(2)
>
> object IdAndMsg {
> val values = List(ObjA , ObjB)
> }
>
> // Exiting paste mode, now interpreting.
>
> defined class IdAndMsg
> defined module ObjA
> defined module ObjB
> defined module IdAndMsg
>
> scala>
>
> scala> ObjA
> res0: ObjA.type = ObjA
>
> scala> IdAndMsg.values
> res1: List[Product with Serializable with IdAndMsg] = List(null, ObjB)
>
> Why does IdAndMsg.values contains a null value?
>
> If I reverse the last 2 steps everything is ok:
>
> ... (pasting the code) ...
> scala> IdAndMsg.values
> res0: List[Product with Serializable with IdAndMsg] = List(ObjA, ObjB)
>
> scala> ObjA
> res1: ObjA.type = ObjA
>
> If I do not rely on the default parameter in the base class, i.e. I define
> ObjA as follows
>
> case object ObjA extends IdAndMsg(1,"")
>
> ... then the evaluation order doesn't matter and the list does not contain
> null.
>
> Am I missing something?
>
> Thanks
> Soeren
>