自参考鸭子类型
我希望编写一个函数,对可以添加到其自身类型的其他成员的任何值进行操作(无论“添加”在上下文中意味着什么)。这种类型的明显(呵呵)定义:
type Addable = { def +(a : Addable) : Addable }
这给了我一个我根本不理解的错误:递归方法+需要结果类型
为什么最后一个 : Addable
不是结果类型?为什么它认为 + 是递归的?
但我发现了一个更普遍的问题,试图在它自己的定义中引用一个类型:
type T = { def f: T }
但后来我灵机一动:像在 Java 中那样解决它!
type T[T] = { def f: T }
这个编译了!
但现在我还有两个问题。
首先,我不知道如何使用类型 T。特别是,
def n(a:T) = a.f
给出了完全明智但令人沮丧的“类型 T 接受类型参数”错误。
其次,尝试将此模式应用于原始问题
type Addable[Addable] = { def +(a : Addable) : Addable }
会导致完全不可理解的“结构细化中的参数类型可能不会引用该细化之外定义的抽象类型”。 (实际的问题不在于它是“+”——感谢上帝和马丁,因为这会让我的头脑完全混乱——只是它需要一个 Addable 作为参数。)
那么
- 我如何定义一个鸭子类型的含义“有一个特定的函数返回相同类型的值”?
- 如何定义鸭子类型,意思是“具有采用相同类型的表达式作为参数的特定函数”?
我有一种宗教般的信念,认为这个问题是可以解决的。
I wish to write a function that operates on any value that can be added to other members of its own type (whatever "added" means in context). The obvious (heh-heh) definition of such a type:
type Addable = { def +(a : Addable) : Addable }
That gives me an error I don't understand at all: recursive method + needs result type
Why isn't that last : Addable
the result type? Why does it think + is recursive anyway?
But I found a more general problem, trying to refer to a type inside its own definition:
type T = { def f: T }
But then I had a brain-wave: solve it the way I would in Java!
type T[T] = { def f: T }
This compiled!
But now I have two more problems.
First, I have no idea how to use type T. In particular,
def n(a:T) = a.f
gives the wholly sensible yet frustrating "type T takes type parameters" error.
Second, attempting to apply this pattern to the original problem
type Addable[Addable] = { def +(a : Addable) : Addable }
leads to a completely incomprehensible "Parameter type in structural refinement may not refer to an abstract type defined outside that refinement". (The actual problem is not that it's "+" -- thank God and Martin, since that would complete mess up my head -- just that it takes an Addable as a parameter.)
So
- How do I define a duck-type meaning "has a particular function returning a value of the same type"?
- How do I define a duck-type meaning "has a particular function taking a expression of the same type as a parameter"?
I have a religious-like belief that this problem is solvable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些是不同的T。
当您编写时:
您有一个 Addable 类型,它采用单个类型参数,也称为 Addable。这是人们经常混淆的类似变体。
你的问题的实际答案是“你不能”,但我不想粉碎你的宗教信仰,所以我会说“结构类型以神秘的方式工作”。如果您想执行宗教任务,您可以访问这里,这也解释了为什么您不能这样做。
http://article.gmane.org/gmane.comp.lang.scala/7013
Those are different Ts.
When you write:
You have a type Addable which takes a single type parameter, also called Addable. Here's a similar variation people often confuse themselves with.
The actual answer to your question is "you can't" but I would hate to shatter your religious-like faith so instead I'll say "structural types work in mysterious ways." If you want to go on a religious mission you might visit here, which explains why you can't.
http://article.gmane.org/gmane.comp.lang.scala/7013