引用Scala中内部类的类型

发布于 2024-08-20 03:37:11 字数 1013 浏览 3 评论 0原文

以下代码尝试模仿 DSL 的多态嵌入:而不是给出Inner 中的行为,它在其封闭类的 useInner 方法中进行编码。我添加了 enclosure 方法,以便用户只需保留对 Inner 实例的引用,但始终可以获取其封闭实例。通过这样做,来自特定 Outer 实例的所有 Inner 实例仅绑定到一种行为(但这里需要它)。

abstract class Outer {
  sealed class Inner {
    def enclosing = Outer.this
  }
 def useInner(x:Inner) : Boolean
}

def toBoolean(x:Outer#Inner) : Boolean = x.enclosing.useInner(x)

它无法编译,scala 2.8 抱怨:

type mismatch; found: sandbox.Outer#Inner
               required: _81.Inner where val _81:sandbox.Outer

来自 Scala 编程:嵌套类Scala 之旅:内部类,在我看来,问题在于useInner 期望来自特定 Outer 实例的 Inner 实例作为参数。

真正的解释是什么以及如何解决这个问题?

The following code tries to mimic Polymorphic Embedding of DSLs: rather than giving the behavior in Inner, it is encoded in the useInner method of its enclosing class. I added the enclosing method so that user has only to keep a reference to Inner instances, but can always get their enclosing instance. By doing this, all Inner instances from a specific Outer instance are bound to only one behavior (but it is wanted here).

abstract class Outer {
  sealed class Inner {
    def enclosing = Outer.this
  }
 def useInner(x:Inner) : Boolean
}

def toBoolean(x:Outer#Inner) : Boolean = x.enclosing.useInner(x)

It does not compile and scala 2.8 complains about:

type mismatch; found: sandbox.Outer#Inner
               required: _81.Inner where val _81:sandbox.Outer

From Programming Scala: Nested classes and A Tour of Scala: Inner Classes, it seems to me that the problem is that useInnerexpects as argument an Inner instance from a specific Outer instance.

What is the true explanation and how to solve this problem ?

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

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

发布评论

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

评论(3

清晰传感 2024-08-27 03:37:11

我想 Inner 类型就像 this.Inner 类型。 Outer#Inner 独立于外部实例(不是路径依赖类型)。

abstract class Outer {
  sealed class Inner {
    def enclosing = Outer.this
  }
  def useInner(x:Outer#Inner) : Boolean
}

def toBoolean(x:Outer#Inner) : Boolean = x.enclosing.useInner(x)

I suppose the type Inner is like the type this.Inner. Outer#Inner is independent of the outer instance (not a path-dependent type).

abstract class Outer {
  sealed class Inner {
    def enclosing = Outer.this
  }
  def useInner(x:Outer#Inner) : Boolean
}

def toBoolean(x:Outer#Inner) : Boolean = x.enclosing.useInner(x)
瑶笙 2024-08-27 03:37:11

问题正如您所描述的, useInner 需要特定 Outer 实例的 Inner 。由于 enclosure 返回一个通用的 Outer,据我所知,确实没有办法将两者联系在一起。

但是,您可以强制它:

def toBoolean(x: Outer#Inner): Boolean = {
  val outer = x.enclosing
  outer.useInner(x.asInstanceOf[outer.Inner])
}

The problem is as you describe, that useInner is expecting an Inner of a specific Outer instance. Since enclosing returns a generic Outer, there is really no way to tie both together that I know of.

You can force it, however:

def toBoolean(x: Outer#Inner): Boolean = {
  val outer = x.enclosing
  outer.useInner(x.asInstanceOf[outer.Inner])
}
故人爱我别走 2024-08-27 03:37:11

你也可以这样定义你的成员:

def useInner(x:Outer#Inner) : Boolean

或者你可以这样写:

abstract class Outer {
    class InnerImpl {
        def enclosing = Outer.this
    }
    final type Inner = Outer#InnerImpl
    def useInner(x:Inner) : Boolean
}

You can also define your member like this:

def useInner(x:Outer#Inner) : Boolean

Or you can write like this:

abstract class Outer {
    class InnerImpl {
        def enclosing = Outer.this
    }
    final type Inner = Outer#InnerImpl
    def useInner(x:Inner) : Boolean
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文