4 ms·
> For many years, the standard library has had an `Infallible` type to work around the unstable nature of the never type. It served the same semantic purpose as
by Georgelemental 13d ago
> For many years, the standard library has had an `Infallible` type to work around the unstable nature of the never type. It served the same semantic purpose as the never type, but did not have any special compiler support. Therefore, code using it would be technically correct but suboptimal (such as having an extra layer of tags in an enumeration or emitting dead code), because the optimizer would not always be able to remove references to `Infallible`.
This is incorrect. The compiler has always treated `Infallible` as uninhabited, and used that fact for optimizations. The downside of its lack of compiler support is losing out on the coercions. (The article is excellent otherwise)
- tialaramex 13d agoWhich makes sense because you could (and indeed still will be able to do) write your own uninhabited types very easily in Rust and indeed they're optimised accordingly. Because Rust has user-defined sum types you could simply write a sum of nothing: enum MyNeverType {} ...and it's uninhabited, the same way you can write the product of nothing struct MyUnitType {} ... and its size is zero.
- nextaccountic 13d agoNote, to be more clear, an enum with no variants has no values of that type (can't be constructed), and a struct with no fields has exactly one value of that type
- teiferer 13d agoJust like you would expect from algebra. The empty sum is 0, the empty product is 1.
- aissi 13d ago[flagged]
- nixpulvis 13d agoA reference to a value isn't the same as the value itself. I'm confused the point you're making here.
- horseduck 13d agotialaramex got it wrong even at the type theory level, a size of 0 is the wrong focus, the number of elements, 1, should have been the focus.
- kibwen 13d agoI wouldn't say they got it wrong exactly. Uninhabited types aren't equivalent to unit types, but in terms of Rust they're both classified as zero-size, and I suspect they were just pointing out that Rust has optimized and special-cased zero-sized types accordingly since time immemorial.
- red75prime 13d ago> remember to kill all pedophile murderers like... What's this bullshit?
- mattmcal 13d ago> (Note: Rust also uses exclamation marks to indicate calls to macros. The way the syntax is constructed, a place where it is valid to use the never type is not a valid place to put a macro invocation and vice versa. Yeah this statement is incorrect as well. Weird to have these minor technical inaccuracies in a relatively detailed article.
- tyushk 13d agoDo you have an example? I'm struggling to think of such a case where ! is ambiguous between a end of macro call and the Never type.
- Sharlin 13d agoPedantically, the phrasing in the article is incorrect because macro invocations are permissible in type positions. But as you said, the macro invocation syntax is nevertheless not ambiguous with !-as-type (or !-as-operator for that matter).