File tree 4 files changed +55
-0
lines changed
compiler/src/dotty/tools/dotc
4 files changed +55
-0
lines changed Original file line number Diff line number Diff line change @@ -5214,6 +5214,10 @@ object Types extends TypeUtils {
5214
5214
def thatReducesUsingGadt (tp : Type )(using Context ): Boolean = tp.underlyingMatchType match
5215
5215
case mt : MatchType => mt.reducesUsingGadt
5216
5216
case _ => false
5217
+
5218
+ object Normalizing :
5219
+ def unapply (tp : Type )(using Context ): Option [Type ] =
5220
+ Some (tp.tryNormalize).filter(_.exists)
5217
5221
}
5218
5222
5219
5223
enum MatchTypeCasePattern :
Original file line number Diff line number Diff line change @@ -135,6 +135,7 @@ object TypeTestsCasts {
135
135
def recur (X : Type , P : Type ): String = trace(s " recur( ${X .show}, ${P .show}) " ) {
136
136
(X <:< P ) ||| P .dealias.match
137
137
case _ : SingletonType => " "
138
+ case MatchType .Normalizing (tp) => recur(X , tp)
138
139
case _ : TypeProxy
139
140
if isAbstract(P ) => i " it refers to an abstract type member or type parameter "
140
141
case defn.ArrayOf (tpT) =>
Original file line number Diff line number Diff line change
1
+ import scala .reflect .TypeTest
2
+
3
+ type Matcher [A ] = A match { case String => A }
4
+
5
+ def patternMatch [A ](a : Any )(using tt : TypeTest [Any , Matcher [A ]]): Option [Matcher [A ]] = {
6
+ // type T = RDF.Triple[Rdf]
7
+ a match {
8
+ case res : Matcher [A ] => Some (res)
9
+ case _ => None
10
+ }
11
+ }
12
+
13
+ def patternMatchWithAlias [A ](a : Any )(using tt : TypeTest [Any , Matcher [A ]]): Option [Matcher [A ]] = {
14
+ type T = Matcher [A ]
15
+ a match {
16
+ case res : T => Some (res)
17
+ case _ => None
18
+ }
19
+ }
20
+
21
+ type S = String
22
+ type MS = Matcher [S ]
23
+
24
+ type S2 = MS
25
+ type MS2 = Matcher [S2 ]
26
+
27
+ type Mstuck = Matcher [Nothing ]
Original file line number Diff line number Diff line change
1
+
2
+ @ main def main = {
3
+ println(patternMatch[String ](" abc" ))
4
+ println(patternMatchWithAlias[String ](" abc" ))
5
+ println(patternMatch[String ](" abc" )(using (s : Any ) => {
6
+ if s.isInstanceOf [Matcher [String ]] then Some [s.type & Matcher [String ]](s.asInstanceOf [s.type & Matcher [String ]]) else None }))
7
+ println(patternMatchWithAlias[String ](" abc" )(using (s : Any ) => {
8
+ if s.isInstanceOf [Matcher [String ]] then Some [s.type & Matcher [String ]](s.asInstanceOf [s.type & Matcher [String ]]) else None }))
9
+
10
+ println(patternMatch[String ](1 ))
11
+ println(patternMatchWithAlias[String ](1 ))
12
+
13
+ println(patternMatch[String ](" abc" )(using (s : Any ) => {
14
+ if s.isInstanceOf [S ] then Some [s.type & Matcher [String ]](s.asInstanceOf [s.type & Matcher [String ]]) else None }))
15
+ println(patternMatch[String ](" abc" )(using (s : Any ) => {
16
+ if s.isInstanceOf [MS ] then Some [s.type & Matcher [String ]](s.asInstanceOf [s.type & Matcher [String ]]) else None }))
17
+ println(patternMatch[String ](" abc" )(using (s : Any ) => {
18
+ if s.isInstanceOf [S2 ] then Some [s.type & Matcher [String ]](s.asInstanceOf [s.type & Matcher [String ]]) else None }))
19
+ println(patternMatch[String ](" abc" )(using (s : Any ) => {
20
+ if s.isInstanceOf [MS2 ] then Some [s.type & Matcher [String ]](s.asInstanceOf [s.type & Matcher [String ]]) else None }))
21
+ println(patternMatch[String ](" abc" )(using (s : Any ) => {
22
+ if s.isInstanceOf [Mstuck ] then Some [s.type & Matcher [String ]](s.asInstanceOf [s.type & Matcher [String ]]) else None })) // warn
23
+ }
You can’t perform that action at this time.
0 commit comments