RETRACTION ALERT: See the very end for the good news
I might be jumping the gun on this one (I was, see the “Retraction” at the end of this post) but I think I’ve come across my first Scala niggle. Having range support build into a language is
really nice, and fair play to Scala for having it. I’m not sure I like the syntax however (and this might be where I’m missing something – please comment if I have). What I mean is this:
and this:
That’s great, but in Ruby (and IIRC, Groovy) the same end (albeit without the immutability) is achieved by:
Now I’ve done some small-scale (i.e. not exhaustive) digging, and I know there is also “range” support built into many of the collections classes and the Iterator class too. My beef however is around legibility, and “least surprise”, especially with the second Scala example. To my eyes, when I see the Ruby examples in code, it just
reads. That’s what I want from a modern language.
It’s not all doom and gloom however. One thing I
disliked about Ruby ranges (and again I think Groovy ranges too) was the exclusive version of the syntax:
Spot the difference? No? ITS ONE DOT! That’s a potential source of bugs if ever I saw one. Compare with Scala:
Now you could argue that to have the inclusive set should be the default, but then you should argue that array indexes should start at 1 – the battle is lost. but this, I like.
One final thing; I know I have the power available to me in Scala to allow me to write a function which will work like the Ruby syntax (in fact it’d be a good exercise, and one I’ll aim to come back to) but should I have to? Not if I can avoid it.
Retraction
I just got to the AboutLists Koan. Hidden away in koan("You can create a
list from a range") is a sweet little Range literal:
That’s nice. I like that even more than the Ruby syntax. I should have kept
my big mouth shut. Well done Scala, well done.
No comments:
Post a Comment