{"action":"created","issue":{"url":"https://api.github.com/repos/scala/pickling/issues/339","labels_url":"https://api.github.com/repos/scala/pickling/issues/339/labels{/name}","comments_url":"https://api.github.com/repos/scala/pickling/issues/339/comments","events_url":"https://api.github.com/repos/scala/pickling/issues/339/events","html_url":"https://github.com/scala/pickling/issues/339","id":88303449,"number":339,"title":"Value class problems","user":{"login":"DylanMLG","id":12161233,"avatar_url":"https://avatars.githubusercontent.com/u/12161233?v=3","gravatar_id":"","url":"https://api.github.com/users/DylanMLG","html_url":"https://github.com/DylanMLG","followers_url":"https://api.github.com/users/DylanMLG/followers","following_url":"https://api.github.com/users/DylanMLG/following{/other_user}","gists_url":"https://api.github.com/users/DylanMLG/gists{/gist_id}","starred_url":"https://api.github.com/users/DylanMLG/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DylanMLG/subscriptions","organizations_url":"https://api.github.com/users/DylanMLG/orgs","repos_url":"https://api.github.com/users/DylanMLG/repos","events_url":"https://api.github.com/users/DylanMLG/events{/privacy}","received_events_url":"https://api.github.com/users/DylanMLG/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-06-15T03:39:37Z","updated_at":"2015-08-12T19:00:48Z","closed_at":"2015-08-12T19:00:47Z","body":"I'm having a bit of trouble whenever I deal with value classes. I'm not sure if these should be classified as bugs, I'm doing something incorrectly, or if it's something not supported. Here are some of my observations with scala-pickling 0.10.1 and scala 2.11.6\r\n\r\nI've only been using Scala Pickling a couple of days so I may be missing some understanding. I'd appreciate any clarification if possible. Thanks!\r\n\r\n##### Ok. Both fields extend AnyVal\r\n```scala\r\nimport scala.pickling._\r\nimport scala.pickling.binary._\r\nimport scala.pickling.Defaults._\r\nimport scala.pickling.static._\r\n\r\ncase class A(value: Int) extends AnyVal\r\ncase class B(value: Int) extends AnyVal\r\ncase class Test(a: A, b: B)\r\n\r\nobject Example {\r\n  def main(args: Array[String]): Unit = {\r\n    val test = Test(A(1), B(2))\r\n    val pickled = test.pickle\r\n    println(pickled.unpickle[Test])\r\n  }\r\n}\r\n```\r\n```\r\nTest(A(1),B(2))\r\n[success] Total time: 1 s, completed 15/06/2015 3:00:29 PM\r\n```\r\n\r\n##### Fails when a single property doesn't extend AnyVal\r\n```scala\r\nimport scala.pickling._\r\nimport scala.pickling.binary._\r\nimport scala.pickling.Defaults._\r\nimport scala.pickling.static._\r\n\r\ncase class A(value: Int) extends AnyVal\r\ncase class B(value: Int)\r\ncase class Test(a: A, b: B)\r\n\r\nobject Example {\r\n  def main(args: Array[String]): Unit = {\r\n    val test = Test(A(1), B(2))\r\n    val pickled = test.pickle\r\n    println(pickled.unpickle[Test])\r\n  }\r\n}\r\n```\r\n\r\n```\r\n[error] (run-main-72) java.lang.IllegalArgumentException: Can not set final int field Test.a to A\r\njava.lang.IllegalArgumentException: Can not set final int field Test.a to A\r\n        at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)\r\n        at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)\r\n        at sun.reflect.UnsafeQualifiedIntegerFieldAccessorImpl.set(UnsafeQualifiedIntegerFieldAccessorImpl.java:100)\r\n        at java.lang.reflect.Field.set(Field.java:764)\r\n        at Example$TestUnpickler$macro$4$2$.unpickle(Example.scala:14)\r\n        at scala.pickling.Unpickler$class.unpickleEntry(Pickler.scala:79)\r\n        at Example$TestUnpickler$macro$4$2$.unpickleEntry(Example.scala:14)\r\n        at scala.pickling.functions$.unpickle(functions.scala:11)\r\n        at scala.pickling.UnpickleOps.unpickle(Ops.scala:23)\r\n        at Example$.main(Example.scala:14)\r\n        at Example.main(Example.scala)\r\n        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\r\n        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n        at java.lang.reflect.Method.invoke(Method.java:497)\r\n```\r\n\r\n##### Fine when they are made final\r\n```scala\r\nimport scala.pickling._\r\nimport scala.pickling.binary._\r\nimport scala.pickling.Defaults._\r\nimport scala.pickling.static._\r\n\r\nfinal case class A(value: Int) extends AnyVal\r\nfinal case class B(value: Int)\r\ncase class Test(a: A, b: B)\r\n\r\nobject Example {\r\n  def main(args: Array[String]): Unit = {\r\n    val test = Test(A(1), B(2))\r\n    val pickled = test.pickle\r\n    println(pickled.unpickle[Test])\r\n  }\r\n}\r\n```\r\n\r\n```\r\nTest(A(1),B(2))\r\n[success] Total time: 1 s, completed 15/06/2015 2:57:56 PM\r\n```\r\n\r\n##### Breaks if one is made an Option\r\n```scala\r\nimport scala.pickling._\r\nimport scala.pickling.binary._\r\nimport scala.pickling.Defaults._\r\nimport scala.pickling.static._\r\n\r\nfinal case class A(value: Int) extends AnyVal\r\nfinal case class B(value: Int)\r\ncase class Test(a: A, b: Option[B])\r\n\r\nobject Example {\r\n  def main(args: Array[String]): Unit = {\r\n    val test = Test(A(1), Some(B(2)))\r\n    val pickled = test.pickle\r\n    println(pickled.unpickle[Test])\r\n  }\r\n}\r\n```\r\n```\r\n[error] (run-main-82) java.lang.IllegalArgumentException: Can not set final int field Test.a to A\r\njava.lang.IllegalArgumentException: Can not set final int field Test.a to A\r\n        at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)\r\n        at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)\r\n        at sun.reflect.UnsafeQualifiedIntegerFieldAccessorImpl.set(UnsafeQualifiedIntegerFieldAccessorImpl.java:100)\r\n        at java.lang.reflect.Field.set(Field.java:764)\r\n        at Example$TestUnpickler$macro$7$2$.unpickle(Example.scala:14)\r\n        at scala.pickling.Unpickler$class.unpickleEntry(Pickler.scala:79)\r\n        at Example$TestUnpickler$macro$7$2$.unpickleEntry(Example.scala:14)\r\n        at scala.pickling.functions$.unpickle(functions.scala:11)\r\n        at scala.pickling.UnpickleOps.unpickle(Ops.scala:23)\r\n        at Example$.main(Example.scala:14)\r\n        at Example.main(Example.scala)\r\n        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\r\n        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n        at java.lang.reflect.Method.invoke(Method.java:497)\r\n```\r\n\r\nI'm confused at this point. So I start looking at pickling Options.\r\n```scala\r\nimport scala.pickling._\r\nimport scala.pickling.binary._\r\nimport scala.pickling.Defaults._\r\nimport scala.pickling.static._\r\n\r\nfinal case class A(value: Int)\r\n\r\nobject Example {\r\n  def main(args: Array[String]): Unit = {\r\n    PicklerUnpickler.generate[Option[A]]\r\n  }\r\n}\r\n```\r\n```\r\n[error] Example.scala:10: cannot generate fully static pickler because: 'scala.Some' allows unknown subclasses (it is no\r\nt sealed or final isCaseClass=false isEffectivelyFinal=false isSealed=false directSubclasses=List())\r\n[error]     PicklerUnpickler.generate[Option[A]]\r\n```\r\nI don't really understand why since Some appears to be final. It turns out that removing the `scala.pickling.static._` import makes it compile. \r\n\r\n##### Ok. I'll try to remove the static import to get Options to work with AnyVal\r\n```scala\r\nimport scala.pickling._\r\nimport scala.pickling.binary._\r\nimport scala.pickling.Defaults._\r\n\r\nfinal case class A(value: Int) extends AnyVal\r\nfinal case class B(value: Int) extends AnyVal\r\ncase class Test(a: A, b: Option[B])\r\n\r\nobject Example {\r\n  def main(args: Array[String]): Unit = {\r\n    val test = Test(A(1), Some(B(2)))\r\n    val pickled = test.pickle\r\n    println(pickled.unpickle[Test])\r\n  }\r\n}\r\n```\r\n```\r\n[error] (run-main-8e) java.lang.IllegalArgumentException: Can not set final int field Test.a to A\r\njava.lang.IllegalArgumentException: Can not set final int field Test.a to A\r\n        at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)\r\n        at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)\r\n        at sun.reflect.UnsafeQualifiedIntegerFieldAccessorImpl.set(UnsafeQualifiedIntegerFieldAccessorImpl.java:100)\r\n        at java.lang.reflect.Field.set(Field.java:764)\r\n        at Example$TestUnpickler$macro$7$2$.unpickle(Example.scala:13)\r\n        at scala.pickling.Unpickler$class.unpickleEntry(Pickler.scala:79)\r\n        at Example$TestUnpickler$macro$7$2$.unpickleEntry(Example.scala:13)\r\n        at scala.pickling.functions$.unpickle(functions.scala:11)\r\n        at scala.pickling.UnpickleOps.unpickle(Ops.scala:23)\r\n        at Example$.main(Example.scala:13)\r\n        at Example.main(Example.scala)\r\n        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\r\n        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n        at java.lang.reflect.Method.invoke(Method.java:497)\r\n```\r\n##### It only works without a property extending AnyVal\r\n```scala\r\nimport scala.pickling._\r\nimport scala.pickling.binary._\r\nimport scala.pickling.Defaults._\r\n\r\nfinal case class A(value: Int)\r\nfinal case class B(value: Int)\r\ncase class Test(a: A, b: Option[B])\r\n\r\nobject Example {\r\n  def main(args: Array[String]): Unit = {\r\n    val test = Test(A(1), Some(B(2)))\r\n    val pickled = test.pickle\r\n    println(pickled.unpickle[Test])\r\n  }\r\n}\r\n```\r\n```\r\nTest(A(1),Some(B(2)))\r\n[success] Total time: 1 s, completed 15/06/2015 3:30:29 PM\r\n```"},"comment":{"url":"https://api.github.com/repos/scala/pickling/issues/comments/130412601","html_url":"https://github.com/scala/pickling/issues/339#issuecomment-130412601","issue_url":"https://api.github.com/repos/scala/pickling/issues/339","id":130412601,"user":{"login":"jsuereth","id":29006,"avatar_url":"https://avatars.githubusercontent.com/u/29006?v=3","gravatar_id":"","url":"https://api.github.com/users/jsuereth","html_url":"https://github.com/jsuereth","followers_url":"https://api.github.com/users/jsuereth/followers","following_url":"https://api.github.com/users/jsuereth/following{/other_user}","gists_url":"https://api.github.com/users/jsuereth/gists{/gist_id}","starred_url":"https://api.github.com/users/jsuereth/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jsuereth/subscriptions","organizations_url":"https://api.github.com/users/jsuereth/orgs","repos_url":"https://api.github.com/users/jsuereth/repos","events_url":"https://api.github.com/users/jsuereth/events{/privacy}","received_events_url":"https://api.github.com/users/jsuereth/received_events","type":"User","site_admin":false},"created_at":"2015-08-12T19:00:47Z","updated_at":"2015-08-12T19:00:47Z","body":"This is actually a bug in scala-reflection I just discovered.   The code you list works in 0.11.x branch now since we use the new `scala.pickling.internal.Reflect` method to find private members.\r\n\r\nBasically previously, scala-reflection *fails* to correctly discover private val/vars.  I'm not sure how you hit this bug, but I just ran a test against master (with the new macros/reflection) and it all appears to be solved.\r\n\r\nThanks for the report!  cc> @phaller "}}