Implicit conversions seem to be a major and controversial feature of Scala, while they seem to have far less prominence in C#. What’s the difference between them in the two languages? Is there anywhere I’m forced to use implicits in Scala, or can I always choose whether I want the conversion to be implicit or explicit as in C#? Often in C# I liked to make conversions explicit so as to maintain type-checking of the programmers intent, even if the consumer is myself.
Am I right in saying that neither C# or Scala can implicitly split or combine method / function parameters? As in def myMethod(v1: Int, v2: Int) will not accept a Tuple2[Int, Int] as its parameter list and a def yourMethod(v1: Tuple2[Int, Int]) will not accept two ints as its parameter list. Presumably implicit/ explicit parameter splitting / combining must have been considered by the language designers. I do find this feature desirable when using the multiple but similar graphics library’s point structures.
I have somehow missed the controversy, so I doubt it is a major one.
Implicit conversions are available, but generally frowned upon by experient Scala developers, depending on the specifics.
When an implicit conversion exists to add a method to a type through an “extension” class, then it’s accepted. This extension class is not a type used as a type for parameters, definitions or variables, nor do its methods return itself, but the original type where applicable.
When an implicit conversion converts between two types that are normally used in a program, it is considered a bad thing. In fact, Scala 2.10 will come with warnings against this kind of usage.
The distinction can clearly be seen in the two packages available to convert between Java and Scala collections: scala.collection.JavaConversions and scala.collection.JavaConverters. The latter exists precisely because the implicit conversion style used in the former was considered bad.
Scala does allow you to make an implicit conversion not available inside a specific scope, but I have rarely seen it used. On the other hand, the flag
-Xlog-implicit-conversionsallows you to keep track of where implicit conversions are happening.There aren’t many implicit type conversions in the default scope in Scala. A
Stringcan be seen as aSeq[Char], and “primitive” numeric types have type widening — that is, smaller types can be seen as larger types. There might be others I don’t remember right now, but, generally speaking, you’ll have to import an implicit conversion to use it.