How to ensure two parameters in an inline type match (scala 3) refer to the same type

Viewed 42

In scala 3, when using an inline match, how do I make sure two type params are the same.

class Div[A,B]
transparent inline def simplify [A](a: QuantityUnit[A]) = 
  inline a match {
    // only match if a and b are the same
    case _: Div[a,b] /* a =:= b */ => "string" 
    case _ => 24
  }

// should produce 24 because the type params are different
val a: String = simplify(new Div[Boolean,Int])

// should produce "string" because the type params are the same
val b: Int = simplify(new Div[Boolean,Boolean])

This is the key line, I would like to express what is commented out:

case _: Div[a,b] /* a =:= b */ => "string"

The best I've come up with is something like this:

case _: Div[a,b] => inline erasedValue[b] match {
  case _: a => "string"
  case _ => ...
}

But it forces me to break out of the main match statement.

1 Answers

I stumbled upon something that seems to work. The most straightforward version:

// type alias
type SameParamDiv[A] = Div[A,A]

...

case _: SameParamDiv[a] => "string" 

And my attempt to inline the same thing:

 case _ : (([a] =>> Div[a,a])[_]) => {

I'm not sure exactly about the syntax, but it seems to work :D

For completeness, here's something that didn't work:

// never matches
case _ : Div[a,b] if erasedValue[a] == erasedValue[b] => 
Related