Okay, Scala is a bit scary-powerful
May. 13th, 2009 02:18 pmI just had my first brush with what I gather is a common issue in Scala programming: trying to figure out why my program is working. I was passing type A into my method, which takes closely-related-but-not-identical type B. And yet, it was simply working.
As it turns out, I was slightly ahead of myself -- apparently, last week I wrote an implicit converter from A to B, which I'd forgotten about. Implicits are one of those power features in Scala that can produce remarkably concise code, but must be used with great care, for exactly the reason I just found. Essentially, an implicit converter does exactly what it sounds like: it declares how to go from A to B, so you can use A wherever a B is expected. The compiler simply takes care of the conversion silently.
Very neat feature, and I think it'll be useful to me. But I'm going to have to be really careful with it, lest I confuse myself even more. I suspect it should be used sparingly.
In general, I'm finding that I'm often preferring to be a little more verbose than Scala requires -- for example, it permits me to omit return types most of the time (the compiler susses them automatically), but I actually prefer to have them there, for clarity and self-documentation, if the method is more than a one-liner. It's one of many examples that Scala is a language of remarkable expressiveness, but that just makes it *easier* to write bad code if you aren't sensitive to the issues. It's also easier to write good code, mind -- the language comes closer to the DWIM ideal than anything else I've ever seen -- but it's up to you...
As it turns out, I was slightly ahead of myself -- apparently, last week I wrote an implicit converter from A to B, which I'd forgotten about. Implicits are one of those power features in Scala that can produce remarkably concise code, but must be used with great care, for exactly the reason I just found. Essentially, an implicit converter does exactly what it sounds like: it declares how to go from A to B, so you can use A wherever a B is expected. The compiler simply takes care of the conversion silently.
Very neat feature, and I think it'll be useful to me. But I'm going to have to be really careful with it, lest I confuse myself even more. I suspect it should be used sparingly.
In general, I'm finding that I'm often preferring to be a little more verbose than Scala requires -- for example, it permits me to omit return types most of the time (the compiler susses them automatically), but I actually prefer to have them there, for clarity and self-documentation, if the method is more than a one-liner. It's one of many examples that Scala is a language of remarkable expressiveness, but that just makes it *easier* to write bad code if you aren't sensitive to the issues. It's also easier to write good code, mind -- the language comes closer to the DWIM ideal than anything else I've ever seen -- but it's up to you...