Auto wrap of () in Ok() for functions returning Result<(), Error_Type>

Assuming any block constructs were allowed, const fn foo() { ... } and fn foo() => const { ... } would mean different things, although being syntactically so similar.

This made me think about this syntax:

const OK: Result<(), !> = Ok(());

But the bottom type can't actually be used that way: while ! can always be substituted for MyError, Result<X, !> can't be substituted for Result<X, MyError>.

I'm not sure how changeable that is.

This could be done with C++ style generic consts (as opposed to const generics):

const OK<E>: Result<(), E> = Ok(());

The same could be achieved if associated consts could be imported with use:

impl<E> Result<(), E> {
    pub const OK: Self = Self::Ok(());
}

// somewhere downstream...
use Result::OK;

fn foo() -> Result<(), Error> {
    OK
}
5 Likes