It took me quit some time to understand that you actually mean:
(1 != 1) && ((1 / 0) == 0) ? "T" : "F"
“Reads as English” does not help in anything! Especially when it comes to very basic operations. And if the code gets longer and you need to process and translate more words first it gets actually much harder to understand.
Code isn’t processed like language by our brains (only people who can’t program read it “as language”). Code is processed more like math, in an abstract symbolic way, according to fMRI scans. (Of course you still need language processing for the surface understanding as most programming syntax uses regular words. But I would bet a lot that this would go completely away if someone would read for example APL.)
It has reasons why math is mostly symbolic…
Also the whole “reads as English” thingy is moot when people don’t know English… Then “or” is as good as “||” or “或”. Makes exactly no difference.
Nevertheless I would strongly agree that “||” on Booleans should be (at least) a warning in Scala. I really don’t know when this isn’t an error.
Well, for example Swift can do that:
https://docs.swift.org/swift-book/documentation/the-swift-programming-language/advancedoperators/#Precedence-for-Custom-Infix-Operators
(And if I remember correctly also Agda can define custom operators with custom precedence; Coq can rewrite syntax, so it can do that also, but that’s kind of cheating).
But to be honest, I’m more in the “no operator precedence rules in programming languages” camp anyway. Make it read left to right (and maybe keep the swap around twist, as it’s just one simple rule) and else just use parens for everything.
People couldn’t even remember the most basic “Punkt vor Strich” in school, and don’t even ask people whether they still know the full “PEMDAS”. At the same time programming languages have much more rules! That’s not good design. Especially when it comes to teaching. And of course all the “funny” bugs you can spend really long in chasing, because messed up precedence is nothing easily spotted!
Pyret, a language especially made for teaching programming in the 21st century is (once more!) just right:
https://pyret.org/docs/latest/op-precedence.html#(part._.But_why_not_use_precedence_)
(Also look what they do about numbers, and arithmetic operations in general. Following some “traditions” form the stone age of computing, like most languages still do, isn’t really smart. They don’t. Scala OTOH fell for that trap, blindly following C-likes. Now it’s to late. But maybe Scala 4 could take inspiration from Pyret? )
Oh, and as we’re ranting about if-else syntax, I have also opinions (just not for Scala)!
If I would have a language I would not add IF-ELSE. Because for the “one branch version” that are just methods on Booleans; like in Small Talk (.ifTrue()
/ .ifFalse()
); and the “two branch version” is anyway just pattern-matching—which I would write with ?
for “match” and #
for “patterns”:
isThisPredicateTrue ?
#true => ifTrueBranch
#false => ifFalseBranch
No irregular IF-ELSE syntax. No special case. Simple and readable. Generalizes well.
(One would need to think here of course how to integrate the features form @LPTK’s proposal. But maybe (likely?) this could be done by mere syntax rewriting. I need to try, I guess.)
I agree here.
I really like syntax with less unnecessary noise. But the current Scala solution is subpar. Scala still does not have a clear concept of a “block of code”! This leads at the same time to the whole mess that the question about the colon is. That’s currently pure chaos. I still need to look this detail up constantly. Because there is no logic behind. Logic like for example “when a block follows it needs to be introduced by a colon”.
And actually the colon. Oh, man… It looks indeed good “on paper”, when code is written out without any usual IDE support like syntax highlighting and indent guides. But with syntax highlighting it looks actually quite odd. (And you of course never know where you need it besides for the body of “class likes”). Imho the above “rule” could be simplified to: “An indented region introduces a block. POINT”. And we’re done here! No colons, no special rules, but blocks as first class citizens. (And of course no “end markers”! That’s a really horrible part of the new syntax, even I like the look-and-feel of the rest. I would take braces-noise anytime instead of the horrible “end markers”. At least it’s just one useless symbol… I was even thinking lately to create a VSC extension to hide “end markers”. Nobody needs this nonsense anyway as IDEs show the header of the current scope, and together with indent guides it’s almost impossible to lose orientation even in deeply nested scopes. Works fine for Python—currently one of the most loved languages—so should also work fine for Scala).
The syntax rambling got quite long, I see! I should stop here…