How to write down the type of a trait function?


#1

I have the following code:

pub enum Lazy<T, F: Fn() -> T> {
    Uninitialized(F),
    Initialized(T),
}

impl<T, F: Fn() -> T> Lazy<T, F> {
    pub fn new(init: F) -> Lazy<T, F> {
        Lazy::Uninitialized(init)
    }
}

impl<T: Default> Lazy<T, _> {
    pub fn new_default() -> Lazy<T, _> {
        Lazy::Uninitialized(T::default)
    }
}

The second impl block is obviously invalid as is (with underscores for type parameters). What I’m trying to do is figure out how to write down the type of <T as Default>::default, which should implement Fn() -> T, and put that type where the underscores are now, but I can’t figure out how to write down its type explicitly.


#2

You can refer to T::default as a plain function, like Lazy<T, fn() -> T>.


#3

Ah cool that worked; thanks!