Search code examples
scalataggingscalaz

How to make scalaz.Tagged work with class class primitive attributes?


Consider the following example:

import scalaz._

object TaggedExample {
  sealed trait Test

  def Test[A](a: A): A @@ Test = Tag[A, Test](a)
}

case class TaggedAttribute(l: Long @@ TaggedExample.Test)

It will fail to compile with next reason:

scalac: type mismatch;
 found   : Double
 required: AnyRef
Note: an implicit exists from scala.Double => java.lang.Double, but
methods inherited from Object are rendered ambiguous.  This is to avoid
a blanket implicit which would convert any scala.Double to any AnyRef.
You may wish to use a type ascription: `x: java.lang.Double`.

To my understanding, it is happens due to some details in case class compiler code generation (because simple def test(l: Long @@ TaggedExample.Test) = l compiles just fine).

If we change case class definition to

case class TaggedAttribute(l: java.lang.Long @@ TaggedExample.Test)

compilation will succeed.

The question is: Is there a way to avoid this scalac error without changing type of l to java.lang.Long (which, in turn, will allow l to be null etc.)?


Update

Found this Tagged type : type mismatch question and answer to it right after posting, but still: may be there is a way to avoid use of java.lang.* box types.


Solution

  • Know bug: Compiler error when using tagged types and case classes

    Workaround: use box types.