I've been wondering whether transparent implicit
conversions are really such a good idea and whether it might actually be better to use implicits more, um, explicitly. For example, suppose I have a method which accepts a Date
as a parameter and I have an implicit conversion which turns a String
into a Date
:
implicit def str2date(s: String) : Date = new SimpleDateFormat("yyyyMMdd").parse(s) private def foo(d: Date)
Then obviously I can call this with a transparent implicit
conversion:
foo("20090910")
Would it be better to make the fact that I am converting the string into a date more explicit?
class DateString(val s: String) { def toDate : Date = new SimpleDateFormat("yyyyMMdd").parse(s) } implicit def str2datestr(s: String) : DateString = new DateString(s)
So then the usage looks more like:
foo("20090910".toDate)
The advantage of this is that it is clearer later on what is happening - I've been caught out a few times now by transparent implicit
conversions I should know about (Option
to Iterable
anyone?) and this usage still allows us to take advantage of the power of implicit
s.
I believe that the more "explicit" way of doing implicit conversions is much better in terms of readability than the fully transparent one, at least in this example.
In my opinion, using implicit
s fully transparently from type A
to type B
is OK when you can always view an object of type A
as being able to be used whenever and object of type B
is needed. For example, the implicit conversion of a String
to a RandomAccessSeq[Char]
always makes sense - a String
can always, conceptually, be viewed as a sequence of characters (in C, a string is just a sequence of characters, for example). A call to x.foreach(println)
makes sense for all String
s.
On the other hand, the more explicit conversions should be used when an object of type A
can sometimes be used as an object of type B
. In your example, a call to foo("bar")
does not makes sense and throws an error. As Scala does not have checked exceptions, a call to foo(s.toDate)
clearly signals that there might be an exception thrown (s
might not be a valid date). Also, foo("bar".toDate)
clearly looks wrong, while you need to consult documentation to see why foo("bar")
might be wrong. An example of this in the Scala standard library is conversions from String
s to Int
s, via the toInt
method of the RichString
wrapper (String
s can be seen as Int
s, but not all the time).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With