Pre-pre-RFC: syntactic sugar for `Default::default()`

This reminds me of the conversation in Short enum variant syntax in some cases, where I tossed together this macro:

macro_rules! s {
    ( $($i:ident : $e:expr),* ) => {
        {
            let mut temp = Default::default();
            if false {
                temp // this tricks inference into working
            } else {
                $(
                    temp.$i = $e;
                )*
                temp
            }
        }
    }
}

With that, I think your example would be

let _: Foo = s! {
  bar: [s! {
    qux: [s! {
      zap: [s! {
         val: "val",
      }],
    }],
  }],
}

Having some sort of language feature (or even just a macro) like that could be cool, since it sounds like your situation is also one where it'd be nice to not have to type the name of the type.

It also makes me once again want the different version of FRU that can work with non_exhaustive types or those with private fields, so more things can use this kind of API if they want.

8 Likes