学习一些新的 Scala 3 编译时操作并对
Tuple
有点困惑(特别是在 *:
和 EmptyTuple
上使用类型匹配)
import scala.compiletime.*
imort cats.Show
transparent inline def showForTuple[T <: Tuple]: Show[T] =
inline erasedValue[T] match
case _: EmptyTuple => (new Show[EmptyTuple] {
override def show(n: EmptyTuple): String = ""
}).asInstanceOf[Show[T]]
case _: (t *: EmptyTuple) => (new Show[t *: EmptyTuple] {
val showHead = summonInline[Show[t]]
override def show(tup: t *: EmptyTuple): String = showHead.show(tup.head)
}).asInstanceOf[Show[T]]
case _: (t *: ts) => (new Show[t *: ts] {
val showHead = summonInline[Show[t]]
val showTail = showForTuple[ts]
override def show(tup: t *: ts): String =
showHead.show(tup.head) + ", " + showTail.show(tup.tail)
}).asInstanceOf[Show[T]]
这在 Scala 3.2.2 上按预期工作:
showForTuple[Int *: String *: EmptyTuple].show((1, "hola mundo"))
val res2: String = 1, hola mundo
但是失败了:
showForTuple[(Int, String)].show((1, "hola mundo"))
java.lang.VerifyError: Bad type on operand stack
Exception Details:
Location:
rs$line$28$$anon$1.show(Lscala/Product;)Ljava/lang/String; @16: invokevirtual
Reason:
Type 'scala/Product' (current frame, stack[2]) is not assignable to 'scala/Tuple2'
Current Frame:
bci: @16
flags: { }
locals: { 'rs$line$28$$anon$1', 'scala/Product', 'scala/Product' }
stack: { 'java/lang/StringBuilder', 'cats/Show', 'scala/Product' }
Bytecode:
0000000: bb00 2c59 122d b700 302a b600 322b 4d2c
0000010: b600 38b8 003e b800 42b9 0045 0200 b600
0000020: 4912 4bb6 0049 2ab6 004d 2b4e b200 522d
0000030: b600 55b6 0059 b900 4502 00b6 0049 b600
0000040: 5cb0
... 66 elided
学习自:https://docs.scala-lang.org/scala3/reference/metaprogramming/compiletime-ops.html 和其他资源(博客文章/视频)
编辑:感谢 Scala 的 Discord 中的 Il Totore,以下是一种解决方法,但对最初尝试时导致 java.lang.VerifyError 的原因感到困惑:
import scala.compiletime.*
import cats.Show
given Show[EmptyTuple] = _ => ""
lazy val given_Show_EmptyTuple: cats.Show[EmptyTuple]
given [A, T <: Tuple](using showA: Show[A], showT: Show[T]): Show[A *: T] =
_ match
case h *: EmptyTuple => showA.show(h)
case h *: t => showA.show(h) + ", " + showT.show(t)
transparent inline def showForTuple[T <: Tuple]: Show[T] =
inline erasedValue[T] match
case _: EmptyTuple => summonInline[Show[EmptyTuple]].asInstanceOf[Show[T]]
case _: (t *: EmptyTuple) => summonInline[Show[t *: EmptyTuple]].asInstanceOf[Show[T]]
case _: (t *: ts) => summonInline[Show[t *: ts]].asInstanceOf[Show[T]]
showForTuple[Int *: String *: EmptyTuple].show((1, "hola mundo")) // works
showForTuple[(Int, String)].show((1, "hola mundo")) // Also works?
这是使用内置方法
scala.compiletime.summonAll
和类型级操作Tuple.Map
、Zip
等的另一种解决方法
type Ev[T <: Tuple, A] = Tuple.Union[Tuple.Map[T, [_] =>> A]] =:= A
inline def showForTuple[T <: Tuple]: Show[T] =
new Show[T]:
override def show(t: T): String =
summonFrom {
case ev: Ev[Tuple.Zip[Tuple.Map[T, Show], T], String] =>
ev.liftCo[List](
summonAll[Tuple.Map[T, Show]]
.zip(t)
.map[[_] =>> String]([a] => (x: a) =>
type InstVal[b] = (Show[b], b)
x match
case (s, v): InstVal[?] => s.show(v)
).toList
).reduce(_ + ", " + _)
}
我不得不使用
scala.compiletime.summonFrom
因为编译器不知道Tuple.Union[Tuple.Map[T, [_] =>> A]] =:= A
.
更简单的实现是用
mkString
代替reduce
inline def showForTuple[T <: Tuple]: Show[T] =
new Show[T]:
override def show(t: T): String =
summonAll[Tuple.Map[T, Show]]
.zip(t)
.map[[_] =>> String]([a] => (x: a) =>
type InstVal[b] = (Show[b], b)
x match
case (s, v): InstVal[?] => s.show(v)
)
.toList
.mkString(", ")