jsoniter-scala icon indicating copy to clipboard operation
jsoniter-scala copied to clipboard

Better derivation error when the constructor is private

Open MateuszKubuszok opened this issue 1 year ago • 4 comments

I was testing the behavior of Circe and Jsoniter by deriving the codecs for Out case class defined like this:

class NewType private (val value: Int)
object NewType {
  def unsafe(value: Int): NewType = new NewType(value)
}
final case class In1(int: NewType)
final case class In2(i11: In1, i12: In1)
final case class In3(i21: In2, i22: In2, i23: In2)
final case class In4(i31: In3, i32: In3, i33: In3, i34: In3)
final case class In5(i41: In4, i42: In4, i43: In4, i44: In4, i45: In4)
final case class Out(i1:  In5, i2:  In5, i3:  In5, i4:  In5, i5:  In5, i6:  In5)

I wanted to compare error messages and how good they are.

I noticed that Jsoniter simply throws exception in the macro:

Scala 2

[error] /Users/dev/Workspaces/GitHub/derivation-benchmarks/jsoniter-scala-semi/src/main/scala/example/JsoniterScalaSemi.scala:10:24: constructor NewType in class NewType cannot be accessed in <$anon: com.github.plokhotnyuk.jsoniter_scala.core.JsonValueCodec[example.model1.Out]> from <$anon: com.github.plokhotnyuk.jsoniter_scala.core.JsonValueCodec[example.model1.Out]>
[error]     JsonCodecMaker.make(CodecMakerConfig.withAllowRecursiveTypes(true))
[error]                        ^
[error] one error found

Scala 3

[error] -- [E173] Reference Error: /Users/dev/Workspaces/GitHub/derivation-benchmarks/jsoniter-scala-semi/src/main/scala/example/JsoniterScalaSemi.scala:10:23
[error] 10 |    JsonCodecMaker.make(CodecMakerConfig.withAllowRecursiveTypes(true))
[error]    |    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[error]    |constructor NewType cannot be accessed as a member of example.model1.NewType from module class JsoniterScalaSemi$.
[error] one error found

I believe that the error message could be improved.

MateuszKubuszok avatar Sep 20 '24 10:09 MateuszKubuszok