Scastie (scala 3.5.0-RC2) with -source:3.3
:
import compiletime.{erasedValue, constValue}
// java apis
trait S
class I extends S
class Str(s: Int) extends S
class F[A <: S](s: A)
// scala wrapper
opaque type Sized[N <: Int] <: F[Str] = F[Str]
type SS[T] <: S = T match
case Int => I
case Sized[t] => Str
transparent inline def s[T]: SS[T] = inline erasedValue[T] match
case _: Int => I()
// error: cannot reduce summonFrom with
// patterns : case given ev @ _:ValueOf[Nothing]
case _: Sized[t] => Str(valueOf[t])
// error: Nothing is not a constant type; cannot take constValue
case _: Sized[t] => Str(constValue[t])
@main def run() = s[Sized[5]]
My aim was to create a Sized
refined type over F[Str]
. I’m getting an error trying to match 5
at compile time with both valueOf
and constValue
.
Is there a way to represent this function using an opaque type
? It works with valueOf
if I use a case class instead:
case class Sized[N <: Int](f: F[Str]) extends AnyVal
P.S. Is there a way to define the match type SS
without running afoul of SIP-56?