scala3 icon indicating copy to clipboard operation
scala3 copied to clipboard

Inline match seems premature for type parameters

Open unthingable opened this issue 2 years ago • 0 comments

Compiler version

3.2.1-RC1

Possibly related: https://github.com/lampepfl/dotty/issues/14146

Minimized example

Not sure if bug or feature request or possible misuse, also please pardon my pedestrian/inaccurate understanding of both the mechanism involved and the following explanation.

inline def cast[T](obj: Any): Option[T] =
  inline obj match
    case t: T => Some(t)
    case _    => None

cast[Int](1) // <- Some(1) 👍 

class CastTest[A](val a: A):
  val c: Option[Int] = cast(a)

CastTest[Int](1).c // <- None

After trying a different inlining strategies a pattern seems to be that the inlined match in val c = is actually seeing A as the type and not Bar (evident by attempting to obtain a given ClassTag for obj in cast: No ClassTag available for A). Thus inline matching on a parameter doesn't seem a viable option, unless there was a way to defer inlining until object construction when A is known instead of inlining at class compilation. Worst of all, this code fails silently.

Expectation

One of:

  • match inlining deferred until the concrete type of parameter is known (at class instantiation)
  • compiler warning that the inline match on a parameter is going to surprise me

unthingable avatar Sep 10 '22 22:09 unthingable