支持隐式“a la Scala”的其他编程语言也可以。

发布于 2024-09-06 00:49:52 字数 189 浏览 2 评论 0原文

Scala 隐式函数非常强大。我很好奇它们是否是 Scala 的新/独特功能,或者其他编程语言中已经存在的概念。

谢谢。

编辑

为了澄清我的问题,是的,我正在谈论这个具体的实现。起初,到处都是“隐式事物”似乎很奇怪,但使用了一段时间并看到其他人如何使用它后,我对其效果印象深刻。

Scala implicits are very powerfull. I'm curious if they are a new/unique feature of Scala, or the concept already existed in other programming languages.

Thanks.

EDIT:

To clarify my question, yes, I'm talking about this concrete implementation. Having "implicit things" all around seemed strange at first, but having used it for a while and seeing how others use it, I'm impressed by how well it works.

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

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

发布评论

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

评论(6

青瓷清茶倾城歌 2024-09-13 00:49:52

看起来灵感来自 Haskell 的类型类。至少一篇博客文章声称隐含有它们起源于 Haskell 类型类;该文章引用了 Martin Odersky 于 2006 年发表的论文,题为 Poor Man's Type Classes 。 Daniel Sobral 最近写了一篇关于如何模拟类型类的文章与隐式

Looks like the inspiration was Haskell's type classes. At least one blog article claims that implicits have their origin in Haskell type classes; the article refers to a 2006 paper by Martin Odersky entitled, Poor Man's Type Classes. And Daniel Sobral wrote a recent article on how to simulate type classes with implicits.

dawn曙光 2024-09-13 00:49:52

2000 年,《编程语言原理》(POPL) 上有一篇非常好的论文,其中介绍了 隐式参数。它们已在 Haskell 中实现。我确信 Scala 的设计师 Martin Odersky 知道这项工作。 (Martin 是 POPL 的经常参与者和贡献者,受到欢迎。)

There was a really good paper at Princples of Programming Lanages (POPL) in 2000, which introduced implicit parameters. They've been implemented in Haskell. I'm sure Martin Odersky, the designer of Scala, was aware of this work. (Martin is a frequent and welcome participant in and contributor to POPL.)

相权↑美人 2024-09-13 00:49:52

如果我从 http://patricklogan.blogspot.com/2007/ 正确理解隐式06/scala-implicits.html 那么是的,有几种语言支持它。

最好的例子是 C# 扩展方法。我最近使用它们的一个例子:

我经常不得不在两个点之间进行距离计算。 Point 没有方法计算到另一个点的距离,因此我将以下代码添加到我的项目中:

class MyPointExtension
{
  public static Double GetDistance(this Point p1, Point p2)
  {
    return /* the pythagoras code */
  }
}

然后我可以这样做:

Point unitPosition = new Point(x,y);
Point target = new Point(x2,y2);
Double distance = unitPosition.GetDistance(target);

If i understand implicits correctly from http://patricklogan.blogspot.com/2007/06/scala-implicits.html then yes, there are several languages that support it.

Best example are C# Extension methods. A recent example where i used them:

I often had to do distance calculations between two Points. A Point has no method to calculate the distance to another point so i added the following code to my project:

class MyPointExtension
{
  public static Double GetDistance(this Point p1, Point p2)
  {
    return /* the pythagoras code */
  }
}

I could then do:

Point unitPosition = new Point(x,y);
Point target = new Point(x2,y2);
Double distance = unitPosition.GetDistance(target);
我家小可爱 2024-09-13 00:49:52

另一个参考:“将类作为对象和隐式类型” (2010 ),作者:Bruno C. d. S.奥利维拉、阿德​​里安·摩尔斯和马丁·奥德斯基。

通过此推文转发推文

Another reference: "Type Classes as Objects and Implicits" (2010), by Bruno C. d. S. Oliveira, Adriaan Moors and Martin Odersky.

Via this tweet and re-tweet.

腻橙味 2024-09-13 00:49:52

这取决于您想要将“支持隐式”一词延伸到多广的范围。 Scala 中隐式的一个令人信服的原因是本质上是向现有类(您无权访问)添加方法。这在其他语言中可以通过不同的构造实现:例如,Smalltalk、Ruby 和 Objective-C 都支持向您无法控制的类添加方法。

It depends on how broadly you want to stretch the phrase "supports implicits". One of the compelling reasons for implicits in Scala is to essentially add methods to an existing class (that you don't have access to). This is possible in other languages through different constructs: for example, Smalltalk, Ruby, and Objective-C all support adding methods to classes you don't control.

清风夜微凉 2024-09-13 00:49:52

尽管不像 Scala 隐式那么强大,C++ 已经有了转换运算符和复制构造函数,它们都可以导致隐式类型转换。与定义二元运算符的能力(Scala 不允许的)相结合,这提供了 Scala 隐式的一些功能。

Although not as powerful as Scala implicits C++ already had conversion operators and copy constructors that could both lead to implicit type conversions. In combination with the ability to define binary operators (something Scala does not allow) this delivered some of the power of Scala implicits.

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