Search code examples
scalaargonaut

Argonaut casecodec3?


I'm trying this simple example from the Argonaut quickstart:

case class Address(street: String, number: Int, postcode: Int)

object Address {
  // Define codecs easily from case classes
  implicit def AddressCodecJson: CodecJson[Address] =
    casecodec3(Address.apply, Address.unapply)("street", "number", "post_code")
}

I'm getting a compilation error on casecodec3. I cloned the argonaut and scalaz repos and grepped the source, and I only see that in example code. Where is that symbol coming from and why can't I use it?


Solution

  • If your imports are in order then that should just work:

    import argonaut._, Argonaut._
    case class Address(street: String, number: Int, postcode: Int)
    
    object Address {
      implicit def AddressCodecJson: CodecJson[Address] =
      casecodec3(Address.apply, Address.unapply)("street", "number", "post_code")
    }
    

    In the REPL make sure to enter paste mode

    scala> :paste 
    // Entering paste mode (ctrl-D to finish)
    
    import argonaut._, Argonaut._
    case class Address(street: String, number: Int, postcode: Int)
    
    object Address {
      implicit def AddressCodecJson: CodecJson[Address] =
      casecodec3(Address.apply, Address.unapply)("street", "number", "post_code")
    }
    
    // Exiting paste mode, now interpreting.
    
    defined class Address
    defined object Address
    

    You don't see the source in github because the source is generated for example GeneratedCodecJsons.scala as defined in the build here https://github.com/argonaut-io/argonaut/blob/master/project/Boilerplate.scala