1
1
package tastyquery
2
2
3
+ import scala .annotation .switch
4
+
3
5
import scala .compiletime .asMatchable
4
6
5
7
import tastyquery .Contexts .*
@@ -20,7 +22,30 @@ object Constants {
20
22
final val NullTag = 11
21
23
final val ClazzTag = 12
22
24
23
- class Constant (val value : Matchable , val tag : Int ) {
25
+ final class Constant private (val value : Matchable , val tag : Int , internal : Boolean ) {
26
+ @ deprecated(" this constructor is unsafe; use the `apply` methods in the companion instead" , since = " 1.1.0" )
27
+ def this (value : Matchable , tag : Int ) =
28
+ this (value, tag, internal = true )
29
+
30
+ // Check that the tag matches the value:
31
+ val valid = (tag : @ switch) match
32
+ case UnitTag => value == ()
33
+ case BooleanTag => value.isInstanceOf [Boolean ]
34
+ case CharTag => value.isInstanceOf [Char ]
35
+ case ByteTag => value.isInstanceOf [Byte ]
36
+ case ShortTag => value.isInstanceOf [Short ]
37
+ case IntTag => value.isInstanceOf [Int ]
38
+ case LongTag => value.isInstanceOf [Long ]
39
+ case FloatTag => value.isInstanceOf [Float ]
40
+ case DoubleTag => value.isInstanceOf [Double ]
41
+ case StringTag => value.isInstanceOf [String ]
42
+ case NullTag => value == null
43
+ case ClazzTag => value.isInstanceOf [Type ]
44
+ case _ => false
45
+
46
+ require(valid, s " Illegal combination of value and tag for Constant( $value, $tag) " )
47
+ end this
48
+
24
49
def wideType (using Context ): Type = tag match
25
50
case UnitTag => defn.UnitType
26
51
case BooleanTag => defn.BooleanType
@@ -160,18 +185,18 @@ object Constants {
160
185
}
161
186
162
187
object Constant {
163
- def apply (x : Null ): Constant = new Constant (x, NullTag )
164
- def apply (x : Unit ): Constant = new Constant (x, UnitTag )
165
- def apply (x : Boolean ): Constant = new Constant (x, BooleanTag )
166
- def apply (x : Byte ): Constant = new Constant (x, ByteTag )
167
- def apply (x : Short ): Constant = new Constant (x, ShortTag )
168
- def apply (x : Int ): Constant = new Constant (x, IntTag )
169
- def apply (x : Long ): Constant = new Constant (x, LongTag )
170
- def apply (x : Float ): Constant = new Constant (x, FloatTag )
171
- def apply (x : Double ): Constant = new Constant (x, DoubleTag )
172
- def apply (x : String ): Constant = new Constant (x, StringTag )
173
- def apply (x : Char ): Constant = new Constant (x, CharTag )
174
- def apply (x : Type ): Constant = new Constant (x, ClazzTag )
188
+ def apply (x : Null ): Constant = new Constant (x, NullTag , internal = true )
189
+ def apply (x : Unit ): Constant = new Constant (x, UnitTag , internal = true )
190
+ def apply (x : Boolean ): Constant = new Constant (x, BooleanTag , internal = true )
191
+ def apply (x : Byte ): Constant = new Constant (x, ByteTag , internal = true )
192
+ def apply (x : Short ): Constant = new Constant (x, ShortTag , internal = true )
193
+ def apply (x : Int ): Constant = new Constant (x, IntTag , internal = true )
194
+ def apply (x : Long ): Constant = new Constant (x, LongTag , internal = true )
195
+ def apply (x : Float ): Constant = new Constant (x, FloatTag , internal = true )
196
+ def apply (x : Double ): Constant = new Constant (x, DoubleTag , internal = true )
197
+ def apply (x : String ): Constant = new Constant (x, StringTag , internal = true )
198
+ def apply (x : Char ): Constant = new Constant (x, CharTag , internal = true )
199
+ def apply (x : Type ): Constant = new Constant (x, ClazzTag , internal = true )
175
200
176
201
def unapply (c : Constant ): Constant = c
177
202
}
0 commit comments