« The Expression Problem and Other Mysteries of Life | Main | Axes of Systems - Performance, Capacity and Scalability »

June 22, 2009


Ricardo Lima

The problem with 2 + 3 * 5 = 25 is that it breaks the "principle of least astonishment". Like "The Spanish Inquisition", nobody expects that + and * have the same priority. In Haskell, + and * are function calls, but * still have higher precedence. This is not about purity, but usability of the language.


@Ricardo - Speaking of least astonishment: http://dcsobral.blogspot.com/2009/06/parser-surprise.html

@Tinou - I share your sentiment. I like Scala's power, but really love Newspeak's purity and elegance. However, I think there's no "right" and "wrong" in this debate because Newspeak and Scala have different targets, and slightly different design philosophies. Gilad Bracha mentioned operator precedence implementation being considered as part of a "skin" (http://gbracha.blogspot.com/2008/09/skinning-newspeak.html) so I don't think it's lack of pragmatism that led to Newspeak design, but rather the observation that convenience opens a slippery slope to confusion, like the link above illustrates. Now, many people may prefer convenience over clarity, so it may be a pragmatic choice if a language aims to quickly become widely popular. But as I already noted, Scala and Newspeak, both being great languages, aren't perusing exactly the same goals.



I disagree. Operators originate in maths, and there operator precedence is a fundermental concept. So the "pure" solution would mean 2 + 3 * 5 = 17.

Purity does not mean applying the same rule everywhere. From a maths perspective, it would be impure to treat operators differently than their defined mathematical meaning.


Please consider that your example really means 2.+(3.*(5)) in Scala since 2, 3 and 5 are not simple data types - Int is a class! This means you are calling a method named "+" or "*" as these are no keywords in Scala. Everything else is just syntactic sugar.


In Haskell, which you list on the pure side, the result is 17. Scala's approach to operator precedence is similar (if not based on) Haskell's. So as you say, this is a trivial example. Scala's raison d'être is the bridging of the object and functional worlds on a robust platform, which it does very well.

The comments to this entry are closed.