3 ms·
Scala is one of the more misunderstood languages, partly due to its higher learning curve compared to most language. The implicits you mentioned are a good exa
by valenterry 6y ago
Scala is one of the more misunderstood languages, partly due to its higher learning curve compared to most language.
The implicits you mentioned are a good example. It is one feature and consists of two parts:
1. Implicits at use-site: they define that a not explicitly provided value can be filled in implicitly by the compiler
2. Implicits at definition-site: they mark those values that the compiler is allowed to fill in at use-site when a value is not explicitly provided
That's all there is to it and one without the other would be utterly useless, so it really is _one_ feature.
But that one feature can be used for multiple things and I assume that this is what you meant. For example, they can be used for method extension syntax (which is a distinct feature in Kotlin) or derivation (e.g. serialize a structure into json, which in Java is usually done with reflection or annotations) or ad-hoc polymorphism (emulating typeclasses similar to Haskell).
Mind that these use-cases were not foreseen when Scala was created! But because implicits are such a powerful language feature, Scala can now have method extension syntax without any changes in the language (well, besides some minor optional syntactic sugar, but that's it).
Java cannot easily have this syntax and Kotlin only has it because it was added from the beginning.
Implicits are a beautiful example how a well thought and flexible language feature covers multiple features of other programming languages at once.
The underscore character is a different thing, because that isn't really a language feature, it is syntax. But I agree that it is overused in Scala and is quite confusing in the beginning.