amnaredo / test

0 stars 0 forks source link

Serialization of case classes with hierarchy gives : ambiguous implicit values #150

Open amnaredo opened 2 years ago

amnaredo commented 2 years ago

I have created small project where I isolated project and described quite strange behaviour: https://github.com/jarekratajski/upickleproblem

dev/upikle2/src/main/scala/sample/CR.scala:10: ambiguous implicit values: [error] both value derive$macro$12 of type => upickle.default.Writer[sample.CR] [error] and value derive$macro$13 of type => upickle.default.Writer[sample.PI] [error] match expected type upickle.default.Writer[T1] [error] writeJs(this) [error] ^ [error] one error found error Compilation failed

We have project with lot of case classes and this issue is really causing problems (we move class or change small field and compilation fails).

ID: 118 Original Author: jarekratajski

amnaredo commented 2 years ago

So, thats an interesting consequence of having def toJSValue(): upickle.Js.Value functions inside of the same case classes being targets for upickling.

Basically, what ends up happening is that the implicit Writer definition for classes like CR end up being generated twice, which actually is ambiguous. The solution to this problem is to prevent the macro from trying to generate an implicit for you the second time, ie do this:

  def toJSValue()(implicit writer: upickle.default.Writer[CR]): upickle.Js.Value = {
    writer.write(this)
  }

Original Author: Voltir

amnaredo commented 2 years ago

Another example, in the Ammonite console

@ upickle.json.read("")
Compilation Failed
ambiguous implicit values:
 both value derive$macro$125 of type => pprint.PPrint[upickle.Js.Value]
 and value derive$macro$127 of type => pprint.PPrint[upickle.Js.Value]
 match expected type pprint.PPrint[upickle.Js.Value]
ambiguous implicit values:
 both value derive$macro$125 of type => pprint.PPrint[upickle.Js.Value]
 and value derive$macro$127 of type => pprint.PPrint[upickle.Js.Value]
 match expected type pprint.PPrint[upickle.Js.Value]

Original Author: lihaoyi

amnaredo commented 2 years ago

Bug bankruptcy Original Author: lihaoyi