更高种类类型缺失的清单

发布于 2024-12-17 10:23:57 字数 682 浏览 4 评论 0原文

我在 scalac 中遇到问题,找不到我想要使用的更高级内部类型的清单。考虑一些带有构造函数的类似列表的类型:

trait L[S, A]

def emptyList[S, A: Manifest]: L[S, A] = ???

现在另一种类型从它的伴生对象构造出来,如下所示:

object Tree {
  private class Leaf[S, A]
  private class Impl[S, A](l: L[S, Leaf[S, A]]) extends Tree[S, A]

  def empty[S, A]: Tree[S, A] = new Impl(emptyList[S, Leaf[S, A]])
}
trait Tree[S, A]

它失败了,因为 scalac 想要类型 Leaf[S, A] 的清单。为什么不可用?我不想通过要求传递清单参数来放松 Leaf 的可见性并弄乱构造函数。

我完全不明白这一点——我认为从 JVM 的角度来看,在 L 中构造的数组可以归结为 Array[Leaf[_, _]]又名 Array[java.lang.Object],那么这个拒绝有什么意义呢?

有什么办法可以找到清单吗?

I am having trouble with scalac not finding a manifest for a higher-kinded internal type I want to use. Consider some list like type with a constructor:

trait L[S, A]

def emptyList[S, A: Manifest]: L[S, A] = ???

Now another type which gets constructed from its companion object like this:

object Tree {
  private class Leaf[S, A]
  private class Impl[S, A](l: L[S, Leaf[S, A]]) extends Tree[S, A]

  def empty[S, A]: Tree[S, A] = new Impl(emptyList[S, Leaf[S, A]])
}
trait Tree[S, A]

It fails because scalac wants a manifest for type Leaf[S, A]. Why isn't that available? I don't want to relax visibility of Leaf and clutter the constructor by asking to pass in a manifest argument.

I don't understand this at all—I thought that from the JVM perspective, the arrays that are constructed in L boil down to Array[Leaf[_, _]] aka Array[java.lang.Object], so what's the point of this refusal?

Any ways to find the manifest?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞清仓 2024-12-24 10:23:57

您必须为所有参数提供清单,实际上创建一个完整的清单链:

def emptyList[ S, A : Manifest ] {}
class Leaf[ S, A ]
def emptyTree[ S: Manifest, A : Manifest ] { emptyList[ S, Leaf[ S, A ]]}

You have to provide manifest for all your parameters, in effect creating a complete manifest chain:

def emptyList[ S, A : Manifest ] {}
class Leaf[ S, A ]
def emptyTree[ S: Manifest, A : Manifest ] { emptyList[ S, Leaf[ S, A ]]}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文