File tree 4 files changed +88
-1
lines changed
library/src-bootstrapped/scala/quoted
4 files changed +88
-1
lines changed Original file line number Diff line number Diff line change @@ -13,11 +13,31 @@ object Type:
13
13
14
14
/** Show a source code like representation of this type without syntax highlight */
15
15
def show [T <: AnyKind ](using Type [T ])(using Quotes ): String =
16
- import quotes .reflect ._
16
+ import quotes .reflect .*
17
17
TypeTree .of[T ].show
18
18
19
19
/** Return a quoted.Type with the given type */
20
20
@ compileTimeOnly(" Reference to `scala.quoted.Type.of` was not handled by PickleQuotes" )
21
21
given of [T <: AnyKind ](using Quotes ): Type [T ] = ???
22
22
23
+
24
+ /** Extracts the value of singleton constant type, None otherwise.
25
+ *
26
+ * Example usage:
27
+ * ```scala
28
+ * ... match
29
+ * case '{ $mirrorExpr : Mirror.Sum { type MirroredLabel = label } } =>
30
+ * Type.valueOfConstant[label] // Option[String]
31
+ * }
32
+ * ```
33
+ * @syntax markdown
34
+ */
35
+ def valueOfConstant [T ](using Type [T ])(using Quotes ): Option [T ] =
36
+ import quotes .reflect .*
37
+ def valueOf (tpe : TypeRepr ): Option [T ] =
38
+ tpe.dealias.widenTermRefByName match
39
+ case ConstantType (const) => Some (const.value.asInstanceOf [T ])
40
+ case _ => None
41
+ valueOf(TypeRepr .of[T ])
42
+
23
43
end Type
Original file line number Diff line number Diff line change
1
+ Some(true)
2
+ Some(false)
3
+ Some(1)
4
+ Some(2)
5
+ Some(3)
6
+ Some(4)
7
+ Some(5)
8
+ Some(6.0)
9
+ Some(7.0)
10
+ Some(a)
11
+ Some(abc)
12
+ Some(10)
13
+ Some(11)
14
+ None
15
+ None
16
+ None
17
+ None
18
+ None
19
+ None
20
+ None
21
+ None
22
+ None
23
+ None
24
+ None
25
+ None
Original file line number Diff line number Diff line change
1
+ import scala .quoted .*
2
+
3
+ inline def testValueOfType [T ]: Unit = $ { testValueOfTypeExpr[T ] }
4
+
5
+ private def testValueOfTypeExpr [T : Type ](using Quotes ): Expr [Unit ] =
6
+ val value = Type .valueOfConstant[T ]
7
+ val strExpr = Expr (value.toString)
8
+ ' { println($strExpr) }
Original file line number Diff line number Diff line change
1
+
2
+ @ main def Test : Unit =
3
+ testValueOfType[true ]
4
+ testValueOfType[false ]
5
+ testValueOfByte[1 ]
6
+ testValueOfShort[2 ]
7
+ testValueOfType[3 ]
8
+ testValueOfType[4 ]
9
+ testValueOfType[5L ]
10
+ testValueOfType[6d ]
11
+ testValueOfType[7f ]
12
+ testValueOfType['a' ]
13
+ testValueOfType[" abc" ]
14
+ val x : 10 = 10
15
+ testValueOfType[x.type ]
16
+ type A = 11
17
+ testValueOfType[A ]
18
+
19
+
20
+ testValueOfType[Boolean ]
21
+ testValueOfType[Byte ]
22
+ testValueOfType[Short ]
23
+ testValueOfType[Int ]
24
+ testValueOfType[Long ]
25
+ testValueOfType[Double ]
26
+ testValueOfType[Float ]
27
+ testValueOfType[Char ]
28
+ testValueOfType[String ]
29
+ testValueOfType[Null ]
30
+ testValueOfType[Any ]
31
+ testValueOfType[Some [1 ]]
32
+
33
+ transparent inline def testValueOfByte [B <: Byte ] = testValueOfType[B ]
34
+ transparent inline def testValueOfShort [S <: Short ] = testValueOfType[S ]
You can’t perform that action at this time.
0 commit comments