Other programming languages that support implicits “a la Scala”

点点圈 提交于 2019-12-21 07:12:42

问题


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.


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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.




回答5:


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



回答6:


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.



来源:https://stackoverflow.com/questions/3069432/other-programming-languages-that-support-implicits-a-la-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!