Skip to content

Opaque types with bounds #1272

@kamilkloch

Description

@kamilkloch

In the code below deriving JsonValueCodec[A] ends in a compilation error.
When definition of ReqId is changed to opaque type ReqId = Option[String], derivation succeeds.

object Wrappers {
    opaque type ReqId <: Option[String] = Option[String]

    object ReqId {
      inline def apply(x: Option[String]): ReqId = x

      given JsonValueCodec[ReqId] = JsonCodecMaker.make[Option[String]]
    }
}

object JsoniterOpaque extends App {
  import Wrappers.*

  println(writeToString(ReqId(None))) // null
  println(readFromString[ReqId]("null")) // None
  
  case class A(r: ReqId)

/** Exception occurred while executing macro expansion.
*    java.util.NoSuchElementException: head of empty list
*/
  // given JsonValueCodec[A] = JsonCodecMaker.make 
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions