-
Notifications
You must be signed in to change notification settings - Fork 77
Incorrect handling of Option[xxx] if None #453
Description
Apologies if this is a duplicate, but several searches did not turn up this problem. [Using JSON format]
With this definition:
case class PickleNone(test:Option[String])
this sequence works OK:
val test = PickleNone(None)
val roundTrip = test.pickle.value.unpickle[PickleNone]
gives test == roundTrip
but this sequence:
val test = PickleNone(None)
val roundTrip = test.pickle.value.unpickle[AnyRef]
results in test != roundTrip
It appears in the second case that the unpickle step sees the scala.None.type and instantiates a new instance for None. Unfortunately, Scala expects None to be a singleton, and any None == None test is reduced to object identity, which now fails.
Scala 2.11.8 Pickling 0.10.1
Full test case follows
package pickletests
import scala.pickling._
import scala.pickling.Defaults._
import scala.pickling.json._
case class PickleNone(testOption:Option[String])
object PickleNoneTest extends App {
val test = PickleNone(None)
val testPickled = test.pickle.value
val testUnpickled = testPickled.unpickle[PickleNone]
val testAnyRef = testPickled.unpickle[AnyRef]
val testAnyRefAsPickleNone = testPickled.unpickle[AnyRef].asInstanceOf[PickleNone]
Console.println("test == testUnpickled is: " + (test == testUnpickled))
Console.println(" test == testAnyRef is: " + (test == testAnyRef))
Console.println(" test == asInstanceOf is: " + (test == testAnyRefAsPickleNone))
Console.println("")
Console.println("")
Console.println(" test.testOption is: " + test.testOption.toString + " @%8X".format(System.identityHashCode(test.testOption)))
Console.println(" testUnpickled.testOption is: " + testUnpickled.testOption.toString + " @%8X".format(System.identityHashCode(testUnpickled.testOption)))
Console.println(" testAnyRef.testOption is: " + testAnyRef.asInstanceOf[PickleNone].testOption.toString + " @%8X".format(System.identityHashCode(testAnyRef.asInstanceOf[PickleNone].testOption)))
Console.println("testAnyRefAsPickleNone.testOption is: " + testAnyRefAsPickleNone.testOption.toString + " @%8X".format(System.identityHashCode(testAnyRefAsPickleNone.testOption)))
}