Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problem encoding tagged types #31

Open
aryairani opened this issue Sep 24, 2015 · 3 comments
Open

problem encoding tagged types #31

aryairani opened this issue Sep 24, 2015 · 3 comments

Comments

@aryairani
Copy link

I have an example where I can't derive an instance for a case class even though I do have instances for all the members of the case class:

scalaVersion := "2.11.7"

resolvers += Resolver.sonatypeRepo("releases")

libraryDependencies +=
  "com.github.alexarchambault" %% "argonaut-shapeless_6.1" % "0.3.1"
import scalaz.{@@, Tag}
import argonaut._, Argonaut._, Shapeless._

trait MyTag

case class Foo(s: String)
case class Bar(s: String @@ MyTag)

object Main extends App {
  implicit def encodeTagged[A,T](implicit A: EncodeJson[A]): EncodeJson[A @@ T] =
    A.contramap(Tag.of[T].unwrap)

  implicitly[EncodeJson[Foo]] // ok
  implicitly[EncodeJson[String @@ MyTag]] // ok
  implicitly[EncodeJson[Bar]] // could not find argonaut.EncodeJson[Bar]

  println("hooray!")
}

above code is reproduced at http://scastie.org/12249

@aryairani
Copy link
Author

The derivation provided by argonaut does seem work though, whereas I can't seem to get argonaut-shapeless's to:

implicit val encodeBar: EncodeJson[Bar] = EncodeJson.derive[Bar]
implicitly[EncodeJson[Bar]] // ok!

http://scastie.org/12281

@alexarchambault
Copy link
Owner

This is related to milessabin/shapeless#309. Scalaz tagged types and shapeless records don't deal well together. milessabin/shapeless#309 gives a workaround, which would involve writing alternatives to hconsEncodeJson and hconsDecodeJson methods specifically taillored for scalaz tagged types. It should be possible fto write these manually, and put them in scope when deriving these codecs.

@aryairani
Copy link
Author

I wonder if it’s any different with shapeless tags instead of scalaz tags. I’m not really tied to the latter. Won’t have a chance to give it a try until next week though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants