5 ms·
Rust actually has a type like this in addition to a bottom type (which is !). It is the Infallible type which is an enum type (a sum type) with no variants and
by slaymaker1907 6y ago
Rust actually has a type like this in addition to a bottom type (which is !). It is the Infallible type which is an enum type (a sum type) with no variants and is thus not constructible. It's purpose is to indicate that an operation always succeeds when that operation returns a Result type. But defining it that way, the compiler can use this information to remove error checks. While it doesn't implement every trait automatically, I found it to be pretty easy to implement every trait I cared about since the it just had to typecheck and not do anything useful.
- Sharlin 6y ago`Infallible` only exists because an arbitrary empty type was needed to signal a `Result` that's always `Ok` and the "official" never type `!` was not yet implemented/stable. If it weren't for backward compatibility, `Infallible` could be removed from the standard library right now and its uses replaced with `!`. However, `!` is not a real bottom type either, not yet anyway, because it is not the subtype of every type. As of now it is an unsolved problem how exactly one would go about making it one.
- EE84M3i 6y agoI'm not too familiar with rust's syntax -- isn't `!` normally used after identifiers to indicate macro invocation? Or is it part of the identifier that happens to name a macro? More accurately, when I have a function that returns `!`, is that `!` a special syntax construct at the parser level, a keyword, or a name from the prelude? Or is there some special zero-name macro that is invoked? Can I put a bang in my own type name? https://doc.rust-lang.org/reference/types/never.html https://doc.rust-lang.org/reference/types/never.html seems to answer the question "it's special" I guess the docs are: https://doc.rust-lang.org/std/primitive.never.html https://doc.rust-lang.org/std/primitive.never.html
- DougBTX 6y ago> isn't `!` normally used after identifiers to indicate macro invocation? I'm not sure where that's explicitly documented, but the docs for procedural macros call it the "macro invocation operator": https://doc.rust-lang.org/reference/procedural-macros.html#function-like-procedural-macros https://doc.rust-lang.org/reference/procedural-macros.html#f... > Also, I tried searching but I couldn't find an entry for `!` in the docs. Yeah, that's one reason I'd prefer that it had an explicit name, rather than a single character.
- nicoburns 6y agoA special syntax construct at the parser level I think. In fact currently the `-> !` syntax for functions is it's own special syntax distinct (although v. similar in meaning) from the `!` type. It's not a macro (macro invocations just use the same character), and you can't have a '!' in your own type names.
- Sharlin 6y agoYeah, in current stable Rust `-> !` is just a syntactic construct. But it is being reified into a proper first-class type [1], indeed in nightly you've been able to use it in most type positions for a while already (I actually forgot that the "basic" implementation is not yet stable!) [1] https://github.com/rust-lang/rust/issues/35121 https://github.com/rust-lang/rust/issues/35121
- vmchale 6y agoThat's a void type, not a bottom type.