question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot create anonymous given Codec in Scala3

See original GitHub issue
import com.github.plokhotnyuk.jsoniter_scala.core.*
import com.github.plokhotnyuk.jsoniter_scala.macros.JsonCodecMaker.*

case class DeResult[T](isSucceed: Boolean, data: T, message: String)
case class RootPathFiles(files: List[String])

given JsonValueCodec[DeResult[Option[String]]] = make
given JsonValueCodec[DeResult[RootPathFiles]]  = make
Naming Error: xxx\endpoints\TestEndpoint.scala:16:8 
[error] 16 |  given JsonValueCodec[DeResult[RootPathFiles]]  = make
[error]    |        ^
[error]    |Double definition:
[error]    |final lazy given val given_JsonValueCodec_DeResult:
[error]    |  com.github.plokhotnyuk.jsoniter_scala.core.JsonValueCodec[
[error]    |    de.video.test.model.DeResult[Option[String]]
[error]    |  ] in object TestEndpoint at line 15 and
[error]    |final lazy given val given_JsonValueCodec_DeResult:
[error]    |  com.github.plokhotnyuk.jsoniter_scala.core.JsonValueCodec[
[error]    |    de.video.test.model.DeResult[de.video.test.model.RootPathFiles]
[error]    |  ] in object TestEndpoint at line 16

Sine the same case class with different type parameters, jsoniter use the same given var name.

A workaround is

given jsonValueCodecImplicit1: JsonValueCodec[DeResult[Option[String]]] = make
given jsonValueCodecImplicit2: JsonValueCodec[DeResult[RootPathFiles]]  = make

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
djx314commented, Jun 20, 2022

Yes, it’s the same problem. Now use

import com.github.plokhotnyuk.jsoniter_scala.core.*
import com.github.plokhotnyuk.jsoniter_scala.macros.JsonCodecMaker.*
import de.video.test.model.*

trait JsonCodec:

  private type DeAlias1 = DeResult[Option[String]]
  private type DeAlias2 = DeResult[RootPathFiles]
  private type DeAlias3 = List[String]
  private type DeAlias4 = RootFileNameRequest
  private type DeAlias5 = DirId

  given JsonValueCodec[DeAlias1] = make
  given JsonValueCodec[DeAlias2] = make
  given JsonValueCodec[DeAlias3] = make
  given JsonValueCodec[DeAlias4] = make
  given JsonValueCodec[DeAlias5] = make

end JsonCodec

object JsonCodec extends JsonCodec

and

import de.video.test.cases.model.JsonCodec.given

It’s more suitable for jsoniter’s style since that jsoniter not support nested Codec count.

0reactions
djx314commented, Jun 20, 2022

I see. Usually, when I need a Encoder[DeResult[List[Model1]]], actually I just need Encoder[DeResult[T]], Encoder[List[T]] and Encoder[Model1]. It does not seem to be a urgent issue in Scala3.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Scala 3 enums have limited customisation support #2336
If the enum has no fields the implementations are anonymous, however if the enum has fields then subclasses are created. As of v1.0.1,...
Read more >
Scala 3 macro: generic type from class - Stack Overflow
So I need a runtime compile feature. Code: object CodecMacro { import scala.quoted.staging.* given Compiler = Compiler.make(getClass.
Read more >
lampepfl/dotty - Gitter
I have no idea if the code I wrote is really dangerous or if there is a workaround to make dotc compile it....
Read more >
Anonymous Functions | Scala 3 — Book
For example, given a list like this: Scala 2 and 3. val ints = List(1, 2, 3). You can create a new list...
Read more >
Can't find a codec for class com.mongodb.client.model ...
It may not make sense for mongo - I am just trying to use the aggregate function..
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found