Doable, but convoluted. Some languages simply ensure identifiers and keywords are never in the same namespace. Like prefixing keywords with @, or variables with $.
"Just make it a syntax error in older versions" is so much simpler and cleaner.
Actually, I kind of like it. Especially since
break = value
Is a clear syntax error that doesn't require a label.
However, for defensive purposes, I would suggest still requiring an implicit label definition, i.e.
let value = _: do { // Use _ as a throwaway label
if Bool.random() {
break = "Heads" // Returns from the `do` not the `if`, because the `if` has no label
}
break = "Tails"
}
You could say the if here is not an expression. But it could easily be:
let value = {() in // Single-line closure is interpreted as expression
_: if Bool.random() { // Expressions are "infectious": An `if` inside an expression is also an expression
break = "Heads" // Returns from `if`
}
else {
break = "Tails"
}
}()
Unless you disallow an expression without starting with let value = or break =, but the ship has sailed on that one.