开发者

Other programming languages that support implicits "a la Scala"

开发者 https://www.devze.com 2023-01-03 18:30 出处:网络
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.

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.


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.


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.)


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.


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.


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);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号