dao
dao copied to clipboard
switch syntax defect
switch (var x = ...) or switch (invar x = ...) work, but not switch (x = ...). This omission looks inconsistent with for and while syntax.
The inconsistency is superficial. for and while do not really support for/while( x = expr1 ), they support for/while( x = expr1; expr2 ). And, unlike in for and while, var and invar in switch actually declare a new variable in each case block according its case type.
The only way to make them consistent is to add a new syntax for declaring case-wise variable for type switch.
Well, from the user's point of view, switch demands the use of var while for, while and, actually, if allow to omit it in a very similar case. Even if that's a simplified perception of things, it is likely the most apparent one, and it requires the user to remember this 'exceptional' case which is not very convenient.
It's true it doesn't look consistent even though it's superficial from the implementation point of view.