Skip to content

Add quotes.Type.valueOfConstant #11715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion library/src-bootstrapped/scala/quoted/Type.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,31 @@ object Type:

/** Show a source code like representation of this type without syntax highlight */
def show[T <: AnyKind](using Type[T])(using Quotes): String =
import quotes.reflect._
import quotes.reflect.*
TypeTree.of[T].show

/** Return a quoted.Type with the given type */
@compileTimeOnly("Reference to `scala.quoted.Type.of` was not handled by PickleQuotes")
given of[T <: AnyKind](using Quotes): Type[T] = ???


/** Extracts the value of singleton constant type, None otherwise.
*
* Example usage:
* ```scala
* ... match
* case '{ $mirrorExpr : Mirror.Sum { type MirroredLabel = label } } =>
* Type.valueOfConstant[label] // Option[String]
* }
* ```
* @syntax markdown
*/
def valueOfConstant[T](using Type[T])(using Quotes): Option[T] =
import quotes.reflect.*
def valueOf(tpe: TypeRepr): Option[T] =
tpe.dealias.widenTermRefByName match
case ConstantType(const) => Some(const.value.asInstanceOf[T])
case _ => None
valueOf(TypeRepr.of[T])

end Type
25 changes: 25 additions & 0 deletions tests/run-macros/from-type.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Some(true)
Some(false)
Some(1)
Some(2)
Some(3)
Some(4)
Some(5)
Some(6.0)
Some(7.0)
Some(a)
Some(abc)
Some(10)
Some(11)
None
None
None
None
None
None
None
None
None
None
None
None
8 changes: 8 additions & 0 deletions tests/run-macros/from-type/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import scala.quoted.*

inline def testValueOfType[T]: Unit = ${ testValueOfTypeExpr[T] }

private def testValueOfTypeExpr[T: Type](using Quotes): Expr[Unit] =
val value = Type.valueOfConstant[T]
val strExpr = Expr(value.toString)
'{ println($strExpr) }
34 changes: 34 additions & 0 deletions tests/run-macros/from-type/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

@main def Test: Unit =
testValueOfType[true]
testValueOfType[false]
testValueOfByte[1]
testValueOfShort[2]
testValueOfType[3]
testValueOfType[4]
testValueOfType[5L]
testValueOfType[6d]
testValueOfType[7f]
testValueOfType['a']
testValueOfType["abc"]
val x: 10 = 10
testValueOfType[x.type]
type A = 11
testValueOfType[A]


testValueOfType[Boolean]
testValueOfType[Byte]
testValueOfType[Short]
testValueOfType[Int]
testValueOfType[Long]
testValueOfType[Double]
testValueOfType[Float]
testValueOfType[Char]
testValueOfType[String]
testValueOfType[Null]
testValueOfType[Any]
testValueOfType[Some[1]]

transparent inline def testValueOfByte[B <: Byte] = testValueOfType[B]
transparent inline def testValueOfShort[S <: Short] = testValueOfType[S]