News for hackers. Stuff that matters.
Re:Principle of Least Astonishment by jameson on Monday April 06, 2020 @03:14PM (#22796844)
In JavaScript, "0 < -1 < 2" evaluates to true.

The coercion rules were a big surprise to me when I first started to work in JS. Sometimes codes were obvious but the underlying interpretation is a surprise call.

Re:Principle of Least Astonishment by skrebbel on Monday April 06, 2020 @04:52PM (#22797742)
0 < -1 < 2

To be fair, is there any mainstream language other than Python where that expression makes sense at all?

And even Python had to make explicit, syntactical support for it, and IMO it's a pretty non-obvious language hack. I mean, there's an invisible AND in there, that's pretty astonishing to me at least :-)

Re:Principle of Least Astonishment by xeeeeeeeeeeenu on Monday April 06, 2020 @06:22PM (#22798466)
Re:Principle of Least Astonishment by ghettoimp on Monday April 06, 2020 @05:54PM (#22798284)
Works beautifully in Common Lisp. Of course, it is not mainstream.

? (< 0 1 2)

T

? (< 0 -1 2)

NIL

Re:Principle of Least Astonishment by jdminhbg on Monday April 06, 2020 @08:48PM (#22799366)
In Clojure:

   user=> (< 0 1 2)
   true
And

   user=> (< 0 -1 2)
   false
Re:Principle of Least Astonishment by benatkin on Monday April 06, 2020 @05:17PM (#22797989)
It's a big ask, to have a binary operator and ternary operator that use the same symbol.
Re:Principle of Least Astonishment by Doxin on Tuesday April 07, 2020 @03:04AM (#22800970)
It's honestly some fairly trivial AST-twiddling to support that feature. the compiler can simply detect cases where the left-hand-side of a comparison is /also/ a comparison and change the AST to make it two comparisons combined with an AND statement.

That said I find comparison chaining rather confusing and unexpected so I make a point of avoiding its use. Typing it out as two comparisons hardly saves any effort in any case.

Re:Principle of Least Astonishment by benatkin on Tuesday April 07, 2020 @01:26PM (#22805351)
I know, but it isn't a very useful feature, so why bother?
Re:Principle of Least Astonishment by geerlingguy on Monday April 06, 2020 @03:19PM (#22796884)
Reminds me of https://www.destroyallsoftware.com/talks/wat

Javascript is convenient for many use cases, but it definitely violates POLA on many levels, in many situations!

Re:Principle of Least Astonishment by saagarjha on Monday April 06, 2020 @03:58PM (#22797181)
And it does in C as well!
Re:Principle of Least Astonishment by r00fus on Monday April 06, 2020 @06:24PM (#22798477)
So does this mean that JS does satisfy the principle of least astonishment for an experienced c-coder (something important when it was developed in the early 2000s)?

In what language (other than a RPN based language like Lisp) would this expect to return something meaningful?

Re:Principle of Least Astonishment by saagarjha on Wednesday April 08, 2020 @05:56AM (#22811529)
Yes, you could say that. It preserves the order of operations and “reverse-truthiness” (i.e. boolean-to-integer) conversions from C.