Created
December 20, 2021 12:45
-
-
Save juanjovazquez/97df9794f7d2da9f7abcd5a274b893a4 to your computer and use it in GitHub Desktop.
erasedValue seems not work with opaque types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.compiletime.* | |
// this version works with `Field0` tuples | |
inline def getFirstLabel[T <: NonEmptyTuple](record: T): String = | |
inline erasedValue[T] match | |
case _: (Field0[l, v] *: t) => constValue[l] | |
// this version doesn't work with `Field1` tuples (built with opaque types) | |
inline def getFirstLabel1[T <: NonEmptyTuple](record: T): String = | |
inline erasedValue[T] match | |
case _: (Field1[l, v] *: t) => constValue[l] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Regular class | |
final case class Field0[L <: String & Singleton, V](label: L, value: V) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// opaque type alias | |
opaque type Field1[L <: String & Singleton, V] = (L, V) | |
object Field1: | |
def apply[L <: String & Singleton, V](label: L, value: V): Field1[L, V] = (label, value) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@main def Main(): Unit = | |
val record0 = (Field0("i", 10), Field0("s", "foo"), Field0("b", true)) | |
val record1 = (Field1("i", 10), Field1("s", "foo"), Field1("b", true)) | |
val firstLabel = getFirstLabel(record0) | |
println(firstLabel) // i OK! | |
val firstLabel1 = getFirstLabel1(record1) | |
//Compiling project (Scala 3.1.1-RC2, JVM) | |
//[error] ./Main.scala:6:21: cannot reduce inline match with | |
//[error] scrutinee: compiletime.erasedValue[ | |
//[error] (Field1[("i" : String), Int], Field1[("s" : String), String], | |
//[error] Field1[("b" : String), Boolean] | |
//[error] ) | |
//[error] ] : (Field1[("i" : String), Int], Field1[("s" : String), String], | |
//[error] Field1[("b" : String), Boolean] | |
//[error] ) | |
//[error] patterns : case _:*:[Field1[l @ _, v @ _], t @ _] | |
//[error] val firstLabel1 = getFirstLabel1(record1) | |
//[error] ^^^^^^^^^^^^^^^^^^^^^^^ | |
//Error compiling project (Scala 3.1.1-RC2, JVM) | |
//Compilation failed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment