Simplified syntax for simple functions

If we're making a simplified syntax for putting functions on one line, I'd personally get more use out of something like this for making trait implementations easier:

pub struct Foo { .. }
impl Foo {
    pub const fn new() -> Self { .. }
}

impl Default for Foo {
    // Declares that `Foo::default()` has the same parameters, return type,
    // and body as `Foo::new`.
    // 
    // Equivalent to `fn default() -> Self { Self::new() }`.
    fn default = Self::new;
}

I write this exact setup often because I want types that can be constructed in a const context, but also implement Default, and this is the best way to ensure that both options exist and are in-sync with each other.

As another upside, this avoids having to deal with type inference, since it can just copy the return type from the referenced function.

18 Likes