自参考鸭子类型

发布于 2024-10-05 18:13:51 字数 935 浏览 1 评论 0原文

我希望编写一个函数,对可以添加到其自身类型的其他成员的任何值进行操作(无论“添加”在上下文中意味着什么)。这种类型的明显(呵呵)定义:

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 作为参数。)

那么

  1. 我如何定义一个鸭子类型的含义“有一个特定的函数返回相同类型的值”?
  2. 如何定义鸭子类型,意思是“具有采用相同类型的表达式作为参数的特定函数”?

我有一种宗教般的信念,认为这个问题是可以解决的。

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

  1. How do I define a duck-type meaning "has a particular function returning a value of the same type"?
  2. 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 技术交流群。

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

发布评论

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

评论(1

拥抱我好吗 2024-10-12 18:13:51

这些是不同的T。

scala> type T[T] = { def f: T  } 
defined type alias T

scala> var x: T[Int] = null
x: T[Int] = null

scala> x = new AnyRef { def f = 5 }
x: T[Int] = $anon$1@44daa9f1

当您编写时:

type Addable[Addable] = { def +(a : Addable) : Addable }

您有一个 Addable 类型,它采用单个类型参数,也称为 Addable。这是人们经常混淆的类似变体。

scala> def f[Int](x: Int) = x * x
<console>:7: error: value * is not a member of type parameter Int
       def f[Int](x: Int) = x * x
                              ^

你的问题的实际答案是“你不能”,但我不想粉碎你的宗教信仰,所以我会说“结构类型以神秘的方式工作”。如果您想执行宗教任务,您可以访问这里,这也解释了为什么您不能这样做。

http://article.gmane.org/gmane.comp.lang.scala/7013

Those are different Ts.

scala> type T[T] = { def f: T  } 
defined type alias T

scala> var x: T[Int] = null
x: T[Int] = null

scala> x = new AnyRef { def f = 5 }
x: T[Int] = $anon$1@44daa9f1

When you write:

type Addable[Addable] = { def +(a : Addable) : Addable }

You have a type Addable which takes a single type parameter, also called Addable. Here's a similar variation people often confuse themselves with.

scala> def f[Int](x: Int) = x * x
<console>:7: error: value * is not a member of type parameter Int
       def f[Int](x: Int) = x * x
                              ^

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

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