@@ -2,6 +2,8 @@ package dotty.tools
22package dotc
33package core
44
5+ import scala .reflect .Typeable
6+
57import Types .* , Contexts .* , Symbols .* , Constants .* , Decorators .*
68import config .Printers .typr
79import reporting .trace
@@ -26,33 +28,22 @@ object TypeEval:
2628 if tp1.isStable then tp1.fixForEvaluation else tp
2729 case tp => tp
2830
29- def constValue (tp : Type ): Option [Any ] = tp.fixForEvaluation match
30- case ConstantType (Constant (n)) => Some (n)
31- case _ => None
31+ extension (tp : Type ) def constant [T : Typeable ]: Option [T ] =
32+ TypeComparer .constValue(tp.fixForEvaluation).collect { case Constant (c : T ) => c }
33+
34+ def constValue (tp : Type ): Option [Any ] = tp.constant[Any ]
3235
33- def boolValue (tp : Type ): Option [Boolean ] = tp.fixForEvaluation match
34- case ConstantType (Constant (n : Boolean )) => Some (n)
35- case _ => None
36+ def boolValue (tp : Type ): Option [Boolean ] = tp.constant[Boolean ]
3637
37- def intValue (tp : Type ): Option [Int ] = tp.fixForEvaluation match
38- case ConstantType (Constant (n : Int )) => Some (n)
39- case _ => None
38+ def intValue (tp : Type ): Option [Int ] = tp.constant[Int ]
4039
41- def longValue (tp : Type ): Option [Long ] = tp.fixForEvaluation match
42- case ConstantType (Constant (n : Long )) => Some (n)
43- case _ => None
40+ def longValue (tp : Type ): Option [Long ] = tp.constant[Long ]
4441
45- def floatValue (tp : Type ): Option [Float ] = tp.fixForEvaluation match
46- case ConstantType (Constant (n : Float )) => Some (n)
47- case _ => None
42+ def floatValue (tp : Type ): Option [Float ] = tp.constant[Float ]
4843
49- def doubleValue (tp : Type ): Option [Double ] = tp.fixForEvaluation match
50- case ConstantType (Constant (n : Double )) => Some (n)
51- case _ => None
44+ def doubleValue (tp : Type ): Option [Double ] = tp.constant[Double ]
5245
53- def stringValue (tp : Type ): Option [String ] = tp.fixForEvaluation match
54- case ConstantType (Constant (n : String )) => Some (n)
55- case _ => None
46+ def stringValue (tp : Type ): Option [String ] = tp.constant[String ]
5647
5748 // Returns Some(true) if the type is a constant.
5849 // Returns Some(false) if the type is not a constant.
0 commit comments