that is exactly the proposal I’ve started to work on (except that I would not restrict the patterns in any way, i.e. not remove the ability to put in “where” clauses).
Currently I’m gathering examples where I think this switch expression would help readability.
Thanks for the idea of using this as inline pattern matching for simple cases (one pattern only)!
Am 07. Januar 2016 um 08:24 schrieb Chris Lattner via swift-evolution email@example.com:
On Jan 6, 2016, at 8:35 PM, Paul Ossenbruggen via swift-evolution firstname.lastname@example.org wrote:
Chris, already said he did not like ? being used for ternary because it was already used for optionals. I think because of historical precedent it may be acceptable here. I have tried combos earlier with ! and got no support, admittedly not with the same usage as yours.
Hi Paul (and others),
Here’s an updated opinion from me - keep in mind that this isn’t gospel, just my opinion, and subject to change
Basically I’ve settled on the idea that there is nothing better-enough that would be worth changing the ternary ?: for. Anything we came up with would be “weird” and diverge from any existing practice out there, and therefore be impossible to justify. That’s why I updated the commonly_proposed.md list.
That said, I think that the terseness that ?: provides is important. If you can agree to both points, it sounds like that means that ?: is here to stay, with its existing spelling. This also means that the dream of divorcing ?: from optional syntax has failed.
That leaves us with the question of what to do with switch-as-expression. I’m still pretty unmotivated by any of the ML/haskell like syntaxes that lead to giant hanging statements-like things indented to the right. Instead, the idea of embracing the weirdness that is ?: and extending it to pattern matching feels like the best path. Something akin to:
let x = someValue ?
case : result1
case : result2
case : result3
would make sense: it is totally unambiguous (‘?’ followed by ‘case’ drives this, and goes until a default case). The presence of the (strongly syntax highlighted) case statements means that people can flow this onto a single line if they want, and I also like it that provides a way to get the power of “if case/else” inline in an expression as well for the simplest pattern matching cases (e.g. checking for one enum value, or matching a string value against a regex someday).
If we went with this, I think it would make sense to keep colons, and use refutable patterns (as seen in switch statements) as the pattern grammar. I’d lean towards not allowing where clauses on the patterns, and the results would obviously have to be single expressions (without a return or other statement) just like ?:. Perhaps that syntax should require or allow commas between the cases, I don’t have a strong opinion about that.
So in terms of the ideas you are kicking around, this one seems promising to me. That said, there is still a huge and hard question to answer here: would this feature pay for itself? It is clear that any feature of this ilk will be making the language more complex. Would code that uses it actually be better (e.g. more clear) than code that isn’t using it today? What ways can be measured to justify adding this to Swift?
swift-evolution mailing list