Skip to content

Commit 12a277c

Browse files
Specialize Constant constructor
1 parent 2de2b0e commit 12a277c

File tree

1 file changed

+45
-19
lines changed

1 file changed

+45
-19
lines changed

compiler/src/dotty/tools/dotc/core/Constants.scala

+45-19
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,10 @@ object Constants {
2323
final val EnumTag = 13
2424
final val ScalaSymbolTag = 14
2525

26-
case class Constant(value: Any) extends printing.Showable {
26+
class Constant(val value: Any, val tag: Int) extends printing.Showable with Product1[Any] {
2727
import java.lang.Double.doubleToRawLongBits
2828
import java.lang.Float.floatToRawIntBits
2929

30-
val tag: Int = value match {
31-
case null => NullTag
32-
case x: Unit => UnitTag
33-
case x: Boolean => BooleanTag
34-
case x: Byte => ByteTag
35-
case x: Short => ShortTag
36-
case x: Int => IntTag
37-
case x: Long => LongTag
38-
case x: Float => FloatTag
39-
case x: Double => DoubleTag
40-
case x: String => StringTag
41-
case x: Char => CharTag
42-
case x: Type => ClazzTag
43-
case x: Symbol => EnumTag
44-
case x: scala.Symbol => ScalaSymbolTag
45-
case _ => throw new Error("bad constant value: " + value + " of class " + value.getClass)
46-
}
47-
4830
def isByteRange: Boolean = isIntRange && Byte.MinValue <= intValue && intValue <= Byte.MaxValue
4931
def isShortRange: Boolean = isIntRange && Short.MinValue <= intValue && intValue <= Short.MaxValue
5032
def isCharRange: Boolean = isIntRange && Char.MinValue <= intValue && intValue <= Char.MaxValue
@@ -235,5 +217,49 @@ object Constants {
235217
h = mix(h, equalHashValue.##)
236218
finalizeHash(h, length = 2)
237219
}
220+
221+
override def toString = s"Constant($value)"
222+
def canEqual(x: Any) = true
223+
def get = value
224+
def isEmpty = false
225+
def _1 = value
226+
}
227+
228+
object Constant {
229+
def apply(x: Null) = new Constant(x, NullTag)
230+
def apply(x: Unit) = new Constant(x, UnitTag)
231+
def apply(x: Boolean) = new Constant(x, BooleanTag)
232+
def apply(x: Byte) = new Constant(x, ByteTag)
233+
def apply(x: Short) = new Constant(x, ShortTag)
234+
def apply(x: Int) = new Constant(x, IntTag)
235+
def apply(x: Long) = new Constant(x, LongTag)
236+
def apply(x: Float) = new Constant(x, FloatTag)
237+
def apply(x: Double) = new Constant(x, DoubleTag)
238+
def apply(x: String) = new Constant(x, StringTag)
239+
def apply(x: Char) = new Constant(x, CharTag)
240+
def apply(x: Type) = new Constant(x, ClazzTag)
241+
def apply(x: Symbol) = new Constant(x, EnumTag)
242+
def apply(x: scala.Symbol) = new Constant(x, ScalaSymbolTag)
243+
def apply(value: Any) =
244+
new Constant(value,
245+
value match {
246+
case null => NullTag
247+
case x: Unit => UnitTag
248+
case x: Boolean => BooleanTag
249+
case x: Byte => ByteTag
250+
case x: Short => ShortTag
251+
case x: Int => IntTag
252+
case x: Long => LongTag
253+
case x: Float => FloatTag
254+
case x: Double => DoubleTag
255+
case x: String => StringTag
256+
case x: Char => CharTag
257+
case x: Type => ClazzTag
258+
case x: Symbol => EnumTag
259+
case x: scala.Symbol => ScalaSymbolTag
260+
}
261+
)
262+
263+
def unapply(c: Constant) = c
238264
}
239265
}

0 commit comments

Comments
 (0)