digitalmars.D - final switch traps and improvements
- bearophile (42/42) Dec 23 2012 Recently Ada 2012 was accepted as ISO standard, and on the good
- Andrei Alexandrescu (9/30) Dec 24 2012 Yes, this is a bug in the language design (as much as the "method is
- Adam D. Ruppe (14/17) Dec 24 2012 Note btw:
- Andrei Alexandrescu (4/19) Dec 24 2012 Yah, sorry for getting out of touch with the state of the art.
Recently Ada 2012 was accepted as ISO standard, and on the good Lambda the Ultimate blog there is a small thread about it, with an interesting post: http://lambda-the-ultimate.org/node/4661#comment-73731 The post makes two simple valid points, worth considering. Currently in D this code compiles with no errors: void main() { bool b; final switch (b) { case true: break; } } And gives at run-time: core.exception.SwitchError test(3): No appropriate switch clause found It's one of my top fifteen bug reports (it was mislabelled as enhancement request): http://d.puremagic.com/issues/show_bug.cgi?id=5713 In my opinion It's a topic worth discussing and fixing. The simplest solution is to statically disallow "final switch" to operate on anything bug enums. A better solution is to make it work correctly where it can, and statically disallow the other situations. This means a final switch on bool must require both true and false cases. If the final switching is on a "uint % 3" expression then the compiler must statically require all the 0,1,2 cases and nothing else. If part of all of this enhancement request gets accepted, then other useful final switch situations are worth supporting: http://d.puremagic.com/issues/show_bug.cgi?id=596 One common use case is with Nullable, but syntactically speaking this is not good enough: import std.typecons: Nullable; void main() { auto n = Nullable!int(5); final switch (n) { case Nullable(false, _): ... case Nullable(true, _): ... } } Bye, bearophile
Dec 23 2012
On 12/24/12 2:11 AM, bearophile wrote:Recently Ada 2012 was accepted as ISO standard, and on the good Lambda the Ultimate blog there is a small thread about it, with an interesting post: http://lambda-the-ultimate.org/node/4661#comment-73731 The post makes two simple valid points, worth considering. Currently in D this code compiles with no errors: void main() { bool b; final switch (b) { case true: break; } } And gives at run-time: core.exception.SwitchError test(3): No appropriate switch clause found It's one of my top fifteen bug reports (it was mislabelled as enhancement request): http://d.puremagic.com/issues/show_bug.cgi?id=5713 In my opinion It's a topic worth discussing and fixing.Yes, this is a bug in the language design (as much as the "method is hidden" exception that I recall we got rid of).The simplest solution is to statically disallow "final switch" to operate on anything bug enums.I'd say we eradicate the exception altogether. Again, it reflects incompleteness in language's approach. Requiring people to add a default: {} whenever they don't care about certain cases is a low price to pay for ensuring everybody stays in sync. Andrei
Dec 24 2012
On Monday, 24 December 2012 at 16:01:31 UTC, Andrei Alexandrescu wrote:I'd say we eradicate the exception altogether. Again, it reflects incompleteness in language's approach. Requiring people to add aNote btw: void main() { int a = 10; switch(a) { case 10: } } test7.d(3): Error: non-final switch statement without a default is deprecated final switch lets you get away without a default because it is supposed to ensure you have a case in there for everything. That final switch(b) {case false: } compiles at all is the bug.
Dec 24 2012
On 12/24/12 11:10 AM, Adam D. Ruppe wrote:On Monday, 24 December 2012 at 16:01:31 UTC, Andrei Alexandrescu wrote:Oh, that's great!I'd say we eradicate the exception altogether. Again, it reflects incompleteness in language's approach. Requiring people to add aNote btw: void main() { int a = 10; switch(a) { case 10: } } test7.d(3): Error: non-final switch statement without a default is deprecatedfinal switch lets you get away without a default because it is supposed to ensure you have a case in there for everything. That final switch(b) {case false: } compiles at all is the bug.Yah, sorry for getting out of touch with the state of the art. Andrei
Dec 24 2012