深入 Scala - 存在类型 [英] Scala in depth - Existential types

查看:37
本文介绍了深入 Scala - 存在类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在深入阅读 Scala,但我对存在类型的观点感到困惑.

I am currently reading Scala in depth and I struggle with a point about existential types.

使用这些来源:https://github.com/jsuereth/scala-in-depth-source/blob/master/chapter6/existential-types/existential.scala

使用 openjdk 7 和 Scala 2.10.3

with openjdk 7 and scala 2.10.3

以下说明给了我一个错误:

The following instructions gives me a error :

val x = new VariableStore[Int](12)
val d = new Dependencies {}
val t = x.observe(println)
d.addHandle(t)

<console>:14: error: method addHandle in trait Dependencies cannot be accessed in types.Dependencies
 Access to protected method addHandle not permitted because
 enclosing object $iw is not a subclass of 
 trait Dependencies in package types where target is defined
              d.addHandle(t)
                ^

而且我不知道我为什么以及如何到达这个错误.

And I can't find out why and how I arrive to this error.

编辑 1 :我从 Kihyo 的回答中添加了以下代码:

Edit 1 : I added the following code from Kihyo's answer :

class MyDependencies extends Dependencies {
  override def addHandle(handle: Ref) = super.addHandle(handle)
}

val x = new VariableStore[Int](12)
val d = new MyDependencies
val t = x.observe(println)
d.addHandle(t) //compiles

它使 addHandle 公开而不是受保护.

It make addHandle public instead of protected.

现在我收到以下错误消息:

Now I have the following error message :

type mismatch; found : x.Handle (which expands to) x.HandleClass required: d.Ref (which 
 expands to) x.Handle forSome { val x: sid.types.obs.Observable }

HandleClass 是一个句柄,Ref 是任何观察者的句柄(如果我猜对了),所以值 t 应该被接受为异常的正确类型.

HandleClass is a Handle and Ref is a Handle of any Observer (if I get it right) so the value t should be accepted as a correct type for the exception.

推荐答案

在特性Dependencies中,addHandle是这样定义的:

In the trait Dependencies, addHandle is defined like that:

protected def addHandle(handle : Ref) : Unit

protected 意味着,只有子类可以访问此方法,这就是您收到错误的原因.(这基本上告诉你确切的)

protected means, only subclasses can access this method and thats why you get the error. (which basically tells you exactly that)

当您创建一个使 addHandle 公开的子类时,您的代码可以工作:

Your code could work when you create a subclass that makes addHandle public:

class MyDependencies extends Dependencies {
  override def addHandle(handle: Ref) = super.addHandle(handle)
}

val x = new VariableStore[Int](12)
val d = new MyDependencies
val t = x.observe(println)
d.addHandle(t) //compiles

但我不知道那个例子以及你想用它做什么.

But I have no idea about that example and what you want to do with it.

@Edit1:

我遇到和你一样的错误,但我无法解释原因.当我扩展应用程序而不是主方法时,它对我有用:

I get the same error as you, but I can't explain why. It worked for me when I extend App instead of having a main-method:

object TestObs extends App {
  val x = new VariableStore[Int](12)
  val d = new MyDependencies
  val t = x.observe(println)
  d.addHandle(t)
}  

也许其他人对此有所了解.

Maybe someone else has some insight on this.

这篇关于深入 Scala - 存在类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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