Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Incorrect handling of Option[xxx] if None #453

@jcrowley66

Description

@jcrowley66

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)))
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions