gpt4 book ai didi

scala - 如何在 dotty 中解压元组中的元素类型?

转载 作者:行者123 更新时间:2023-12-01 03:03:37 25 4
gpt4 key购买 nike

假设我有一个类定义为:

case class Box[A](a: A)

我想编写一个通用方法,将元组 (Box[A1](a1), .., Box[An](an)) 解包为类型为 (a1, .., an) 的元组 (A1, .., An)

我尝试了 Match Types 没有运气:
scala> type Unpack[Bs <: Tuple] <: Tuple = Bs match {
| case Unit => Unit
| case Box[a] *: bs => a *: Unpack[bs]
| }

scala> def unpack[Bs <: Tuple](bs: Bs): Unpack[Bs] = bs match {
| case () => ()
| case Box(a) *: as => a *: unpack(as)
| }
2 | case () => ()
| ^^
|Found: Unit
|Required: Unpack[Bs]
|
|where: Bs is a type in method unpack with bounds >: Unit(?1) | Unit(?2) and <: Tuple
3 | case Box(a) *: as => a *: unpack(as)
| ^^^^^^^^^^^^^^^
|Found: A$1 *: Unpack[Tuple]
|Required: Unpack[Bs]
|
|where: Bs is a type in method unpack with bounds >: (Any *: Tuple)(?3) and <: Tuple

最佳答案

我猜你测试了基于 Concat 的代码来自 the documentation 的示例.我测试了这个例子......但它不能在 0.21.0-RC1 上编译.

dotr -version
Starting dotty REPL...
Dotty compiler version 0.21.0-RC1 -- Copyright 2002-2019, LAMP/EPFL
scala> type Concat[+Xs <: Tuple, +Ys <: Tuple] <: Tuple = Xs match {
| case Unit => Ys
| case x *: xs => x *: Concat[xs, Ys]
| }
1 |type Concat[+Xs <: Tuple, +Ys <: Tuple] <: Tuple = Xs match {
| ^^^^^^^^^^^^
|covariant type parameter Xs occurs in invariant position in Xs match {
| case Unit => Ys
| case
| [x, xs <: Tuple] => scala.internal.MatchCase[x *: xs, x *: Concat[xs, Ys]]
|} <: Tuple

如果我删除了 +为了使其类似于您的示例,类型定义通过:

type Concat[Xs <: Tuple, Ys <: Tuple] <: Tuple = Xs match {
| case Unit => Ys
| case x *: xs => x *: Concat[xs, Ys]
| }

scala>

但我无法编写实现:

def concat[Xs <: Tuple, Ys <: Tuple](Xs: Xs, Ys: Ys): Concat[Xs, Ys] = Xs match {
| case () => Ys
| case x *: xs => x *: concat(xs, Ys)
| }
2 | case () => Ys
| ^^
|Found: (Ys : Ys)
|Required: Concat[Xs, Ys]
|
|where: Xs is a type in method concat with bounds >: (?1 : Unit) | (?2 : Unit) and <: Tuple
| Ys is a type in method concat with bounds <: Tuple
3 | case x *: xs => x *: concat(xs, Ys)
| ^^^^^^^^^^^^^^^^^^^
|Found: Any *: Concat[Tuple, Ys]
|Required: Concat[Xs, Ys]
|
|where: Xs is a type in method concat with bounds >: (?3 : Any *: Tuple) and <: Tuple
| Ys is a type in method concat with bounds <: Tuple



因此,让我们查阅文档。事情是......目前没有文档如何实现。有 a section telling us that things can be tricky .

那么它在实际代码中的表现如何呢? Concat源代码中的实现 looks currently like this :

 def dynamicConcat[This <: Tuple, That <: Tuple](self: This, that: That): Concat[This, That] = {
type Result = Concat[This, That]

// If one of the tuples is empty, we can leave early
(self: Any) match {
case self: Unit => return that.asInstanceOf[Result]
case _ =>
}

(that: Any) match {
case that: Unit => return self.asInstanceOf[Result]
case _ =>
}

val arr = new Array[Object](self.size + that.size)

// Copies the tuple to an array, at the given offset
inline def copyToArray[T <: Tuple](tuple: T, array: Array[Object], offset: Int): Unit = (tuple: Any) match {
case xxl: TupleXXL =>
System.arraycopy(xxl.elems, 0, array, offset, tuple.size)
case _ =>
tuple.asInstanceOf[Product].productIterator.asInstanceOf[Iterator[Object]]
.copyToArray(array, offset, tuple.size)
}

// In the general case, we copy the two tuples to an array, and convert it back to a tuple
copyToArray(self, arr, 0)
copyToArray(that, arr, self.size)
dynamicFromIArray[Result](arr.asInstanceOf[IArray[Object]])
}

当然,可能有人会争辩说这是出于性能原因(?),但似乎(直到有更好的方法被记录)匹配类型的值只能使用 .asInstanceOf 以某种hacky 方式实现。 .并且完全由您来确保值与推导的类型匹配(yuk!):

scala> def unpack[Bs <: Tuple](bs: Bs): Unpack[Bs] = bs match {
| case () => ().asInstanceOf[Unpack[Bs]]
| case Box(a) *: as => (a *: unpack(as)).asInstanceOf[Unpack[Bs]]
| }
def unpack[Bs <: Tuple](bs: Bs): Unpack[Bs]

scala> unpack( ( Box(1), Box("test") ) )
val res0: Int *: Unpack[Box[String] *: Unit] = (1,test)

scala>

希望一些 Dotty 贡献者可以提出更好的解决方案,但到目前为止,这是我认为可行的唯一方法。

关于scala - 如何在 dotty 中解压元组中的元素类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59565127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com