4 ms·
Some languages have good alternatives to this. Examples: * Rust has an unreachable!() macro that tells readers and static analyzers that it should be dead code
by progval 7y ago
Some languages have good alternatives to this. Examples:
* Rust has an unreachable!() macro that tells readers and static analyzers that it should be dead code; but raises an error if executed at runtime. https://doc.rust-lang.org/std/macro.unreachable.html https://doc.rust-lang.org/std/macro.unreachable.html
* Languages with pattern-matching will either crash at compile-time if the set of clauses is non-exhaustive, or at run time if a case is missing. That is, if you don't have a catch-all clause.
- ryandrake 7y agoCool language feature! I’ve done this in other languages using a code style policy where all “naked” else statements and ‘default’ inside a switch statement must throw an uncaught exception or otherwise deliberately crash. if(option==1) do_somethimg(); else do_something_else(); ...must then be rewritten as: if(option==1) do_somethimg(); else if(option==2) do_something_else(); else abort(); This kind of defensive coding would have helped to prevent the famous ‘goto fail’ bug from a few years ago.
- rtpg 7y agoTypescript has the `never` type, which is meant for values that are never used. You can combine that with other stuff to get something _like_ `unreachable!` but it's a bit hard to get working right to be honest.