views:

290

answers:

6

Hi.

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  A: 

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.

mipadi
+1  A: 

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);
dbemerlin
Scala implicits are more powerfull than C# extension methods. In Odersky's words *"[in C#] you can only add methods, not fields or interfaces to a class"*. See here http://www.artima.com/weblogs/viewpost.jsp?thread=179766
german1981
I agree with @german1981, extension methods are one of the uses for Scala implicits, but it has none of the mechanics of implicits themselves.
Daniel
+11  A: 

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.

Brian Clapper
Indeed, one of the things that impresses me more are that *type classes thing* used in `Ordering` and `Numeric`. Thank you for the references!
german1981
Implicits are a lot more general than type classes, though. For example, you can always choose to explicitly pass an implicit parameter, instead of letting the compiler fill it in for you. Whereas in Haskell typeclasses are always done by the compiler.
Seth Tisue
A: 

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.

Silvio Bierman
+2  A: 

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

Norman Ramsey
A: 

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.

german1981