暴露来自单例类型的路径依赖类型 [英] Exposing a path-dependent type coming from a singleton type

查看:43
本文介绍了暴露来自单例类型的路径依赖类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让 Scala 为来自单例类型的路径相关类型找到正确的类型.

I'm trying to make Scala find the right type for a path-dependent type coming from a singleton type.

首先,这里是示例的类型容器和一个实例:

First, here is the type container for the example, and one instance:

trait Container {
  type X
  def get(): X
}

val container = new Container {
  type X = String
  def get(): X = ""
}

我可以在第一次尝试中看到字符串(所以我已经有了一个工作场景):

I can see the String in this first attempt (so I already have a working scenario):

class WithTypeParam[C <: Container](val c: C) {
  def getFromContainer(): c.X = c.get()
}

val withTypeParam = new WithTypeParam[container.type](container)

// good, I see the String!
val foo: String = withTypeParam.getFromContainer()

但是当没有类型参数时,这不再起作用.

But when there is no type parameter, this does not work anymore.

class NoTypeParam(val c: Container) {
  def getFromContainer(): c.X = c.get()
}

val noTypeParam = new NoTypeParam(container)

// this does *not* compile
val bar: String = noTypeParam.getFromContainer()

有人知道为什么需要类型参数吗?

Does anybody know why the type parameter is needed?

推荐答案

参见 this 关于 Scala 内部的线程,特别是 Adriaan 的解释.

See this thread on scala-internals, in particular, Adriaan's explanation.

这篇关于暴露来自单例类型的路径依赖类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆