Okay, Scala is a bit scary-powerful
May. 13th, 2009 02:18 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
I 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...
(no subject)
Date: 2009-05-13 06:48 pm (UTC)My favorite illustration that the theory of "conciseness => less code => on average, fewer bugs" breaks down after a certain point has always been Perl. Truly concise Perl can be terrifyingly difficult to eyeball / debug / maintain...
(no subject)
Date: 2009-05-13 08:17 pm (UTC)Ah, but one difference is that I don't think you can do those for two different user created types. So you can tell Foo how to act like a dict, a string, an int -- but no how to act like a Bar. (Or rather, I don't know how -- but then again, I can't think of when I've ever tried either, so I won't say that it's not possible.)
(no subject)
Date: 2009-05-13 10:45 pm (UTC)I don't know the Perl feature, but it sounds different -- this is much more general. (Which is typical of Scala -- the language designers hate special cases.) It actually converts from type A to type B automatically. You can use both freely: it just does the autoconversion when circumstances demand.
The feature is generally used in Scala when you want to add new features to existing types -- so far example, there is a RichInt subclass of Java's Int that adds lots more methods. The autoconverter means that if you try to use one of those methods on a plain Int, it converts it to a RichInt and keeps going. (This is affectionately known as the "Pimp My Type" pattern in the Scala community.)
In this particular case, I had a method that takes my own XMPPPacket type; I was passing in Packet. Now, XMPPPacket is a wrapper around Packet, but I wasn't doing any explicit conversion -- I was simply passing in a Packet. Moreover, the receiving code was clearly getting the right *kind* of XMPPPacket (which has two subclasses, XMPPIQ and XMPPMessage), which is clearly beyond even Scala's dangerously clever compiler.
In fact, it turned out that I had already written a converter method, which knows how to take a Packet, figure out which kind of packet it is, and wrap it in the appropriate subclass of XMPPPacket. The compiler saw that I was trying to pass a Packet into a method that takes XMPPPacket, so it simply assumed that it should apply the conversion. (Correctly, but it left me scratching my head for a while...)
Not that Scala
Date: 2009-05-13 09:17 pm (UTC)Re: Not that Scala
Date: 2009-05-13 11:43 pm (UTC)(no subject)
Date: 2009-05-13 10:15 pm (UTC)A few weeks ago I said to a coworker, "y'know, it's really hard to write good perl." He replied, "No -- it's just really easy to write *bad* perl."
I guess you're saying the same is true of Scala?
(no subject)
Date: 2009-05-13 10:51 pm (UTC)Now that I think of it, though, there *really* needs to be an Obfuscated Scala Contest. Even without C-style macros, I'd bet that you could produce really impressive results...
(no subject)
Date: 2009-05-13 10:56 pm (UTC)Can you do self-modifying/eval'ing code in Scala? If so, then you've got my private trifecta of ununderstandability/unmaintainability (overloaded syntax, leaky scoping, and self-modifying logic).
(no subject)
Date: 2009-05-14 01:00 am (UTC)(no subject)
Date: 2009-05-14 05:00 pm (UTC)(no subject)
Date: 2009-05-14 05:07 pm (UTC)Explicit casts might actually be a less scary approach, because it means that you are always aware when using them of what it implies.