`impl Trait` for statics and consts

This seems like something relatively simple compared to type_alias_impl_trait and does not require a typedef. e.g.

const DIGIT: impl Fn(&ParserState) -> Result<u8> = <my parser combinator here>;

Note that this already works and is optimized as expected:

const DIGIT: &dyn Fn(&ParserState) -> Result<u8> = &<my parser combinator here>;
2 Likes

This looks longer than just writing a function though. The point of type_alias_impl_trait is that you can give a name to the closure and store it in a type without using generics.

1 Like

This tracking issue already exists (but the current implementation is for let only).

3 Likes

This already compiles on nightly:

#![feature(type_alias_impl_trait)]

type MyFn = impl Fn();

#[define_opaque(MyFn)]
const THING: MyFn = || {};
6 Likes