eed3si9n / sjson-new

a typeclass based JSON codec that's backend independent

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

iso custom types example doesn't compile

jroper opened this issue · comments

Copied from the example for custom types in the README:

Welcome to Scala 2.12.3 (OpenJDK 64-Bit Server VM, Java 1.8.0_131).
Type in expressions for evaluation. Or try :help.

scala> import sjsonnew._, LList.:*:
import sjsonnew._
import LList.$colon$times$colon

scala> import BasicJsonProtocol._
import BasicJsonProtocol._

scala> case class Person(name: String, value: Int)
defined class Person

scala> implicit val personIso = LList.iso(
     |          { p: Person => ("name", p.name) :*: ("value", p.value) :*: LNil },
     |          { case (_, name) :*: (_, value) :*: LNil => Person(name, value) })
<console>:22: error: missing parameter type for expanded function
The argument types of an anonymous function must be fully known. (SLS 8.5)
Expected type was: ? => ?
                { case (_, name) :*: (_, value) :*: LNil => Person(name, value) })
                ^

I'm also wary of whether this is even useful at all - doesn't the builder in this case require the order of the JSON properties to be the order that appears in the LList? If that's so, it's not a compliant JSON converter, since compliant JSON converters should allow properties to appear in any order.

In 2.12.10, even simple examples don't compile:

case class Test(x: String)
implicit val testIso = LList.iso(
    { t: Test => ("x", t.x) :*: LNil },
    { case (_, x) :*: LNil => Test(x) }
)
// error: missing parameter type for expanded function
// The argument types of an anonymous function must be fully known. (SLS 8.5)
// ...

The low-level API still works as expected, though, so that's what I used as a workaround for now.

I've found that you can actually make the high-level API work if you help out Scala's type inference:

LList.iso[Test, Int :*: LNil](
    { test: Test => ("test", test.x) :*: LNil },
    { case (_, x) :*: LNil => Test(x) }
)