The following codes (see the two ‘ERR’ lines below) cannot be compiled using Scala 3.3.0-RC3
type TestMatchType[T] = T match
case "String1" => Int
case "String2" => String
def testMatchType[T](input: T): TestMatchType[input.type] =
case "String1": "String1" => 1 // ERR: '=>' expected, but ':' found
case "String2": "String2" => "Two" // ERR: '=>' expected, but ':' found
Basically scala has two types of patterns (can’t recall the proper names for them though):
“variable patterns”, e.g. case x => ... case x: Int => ...
“value/extractor patterns”, e.g. case 1 => ... case Some(1) => ... case x @ Some(1) => ...
Note that only variable patterns can have type ascriptions (like : Int) and only value/extractor patterns can have name bindings (like x @).
Extractor patterns can have other patterns embedded in them, e.g. case Some(x: Int) => but this doesn’t.
Before it happened to be possible to write things like case "abc": "abc" => ... but it caused some syntactic problems and didn’t seem to have any semantic benefits as in general checking the type and checking the value could be performed separately.
Match types are a feature which still isn’t very popular so there still might be some corner cases which could be handled in a smoother way (and combining match types with matches on values is one of them).