In an article I read, written by Martin Odersky ( "Unifying Functional and Object-Oriented Programming with Scala" , Communications of the ACM, Vol. 57 No. 4, Pages 76-86), in the conclusions he indicated the following:
Another important abstraction mechanism in Scala is implicit parameters that let one emulate the essential capabilities of Haskell's type classes.
I understand how implicit arguments work in scala and I know haskell's type classes . But I don't quite understand how to "emulate" type classes in scala using implicit arguments.
Does anyone have any scala examples that can illustrate this mechanism?
Well, I don't know Scala well enough to give an answer with absolute certainty, but I do know Haskell and one of the biggest concepts regarding type classes is dictionary translation or dictionary translation. passing"). This is an implementation technique in which the compiler, given a function whose source code type requires a class, translates the function into intermediate code in which classes do not exist. A source function like the following:
...would translate to an intermediate code more like this:
...where
EqDict a
is a class dictionary— a dispatch table containing the class methodsEq
for the type parametera
. This is a low-level mechanism in compilers, and not a feature of Haskell itself, but it's not hard to see that armed with this concept we can take any program that uses type classes and reformulate it without them, if we write our own "dictionaries". " and pass them as arguments to all functions that need them. In the Haskell world every now and then someone recommends doing this instead of using type classes; This article (in English) seems to me to be the best known that recommends this technique.Likewise, this shows us that in a functional language that doesn't support type classes we can nevertheless simulate them, by building our own dictionaries and passing them as explicit arguments to all functions that need them. We can do it in object-oriented languages too, for example in Java:
The downside is that the functions get more complicated: in the example above, the function
sameList
with classes the type takes two arguments, but after translation to dictionaries it takes three. It would be very annoying to repeat this over and over again in our source programs.And this is where the implicit arguments come into play. With such a mechanism we can use this technique of dictionaries but without having to constantly repeat them in the program. A function that requires a dictionary (or two, or three, or five) declares them as implicit arguments, and thus, when we use it from another that also requires the same implicit arguments, the language automatically takes care of communicating them.
Note however that implicit arguments have advantages and disadvantages over type classes. The big difference is that type classes guarantee that given the same class and the same type, we always get the same dictionary. Implicit arguments, on the other hand, allow different dictionaries in different parts of the program to be associated with the same type. This is a complicated topic that I cannot go into, but the cost of the additional flexibility of implicit arguments is that it also makes it easier to build bad programs.