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

Scala3: Deriving a JsonDecoder for a type having a phantom type doesn't seem to work #1178

Open
jivanic-demystdata opened this issue Oct 17, 2024 · 2 comments

Comments

@jivanic-demystdata
Copy link

I have this code:

final case class Example[State <: ValidationState](a: String, b: Int) derives JsonDecoder

object Example {
  type UnvalidatedExample = Example[ValidationState.Unvalidated]
  type ValidatedExample   = Example[ValidationState.Validated]
  
  enum ValidationState {
    case Unvalidated, Validated 
  }
  object ValidationState {
    type Unvalidated = Unvalidated.type
    type Validated   = Validated.type
  }
  
  // Doesn't compile
  given JsonDecoder[UnvalidatedExample] = JsonDecoder[Example[?]].asInstanceOf[JsonDecoder[UnvalidatedExample]]
}

which fails to compile with error:

No given instance of type zio.json.JsonDecoder[com.example.Example[?]] was found for parameter a of method apply in object JsonDecoder.
@johnhungerford
Copy link

Why are you trying to summon the decoder instead of deriving it? This compiles:

import zio.json.*
import Example.ValidationState


final case class Example[State <: ValidationState](a: String, b: Int) derives JsonDecoder

object Example {
  type UnvalidatedExample = Example[ValidationState.Unvalidated]
  type ValidatedExample   = Example[ValidationState.Validated]
  
  enum ValidationState {
    case Unvalidated, Validated 
  }
  object ValidationState {
    type Unvalidated = Unvalidated.type
    type Validated   = Validated.type
  }
  
  given JsonDecoder[UnvalidatedExample] = DeriveJsonDecoder.gen[Example[?]].asInstanceOf[JsonDecoder[UnvalidatedExample]]
}

@johnhungerford
Copy link

Never mind -- I missed that it's the deriving bit you were asking about. Not sure why that doesn't work, as JsonDecoder.derived just calls DeriveJsonDecoder.gen.

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

3 participants