rust-typed-builder icon indicating copy to clipboard operation
rust-typed-builder copied to clipboard

Specify default field value for field with type that uses generic parameters with defaults

Open treysidechain opened this issue 1 year ago • 0 comments

Closes #44

This would support the example in #44

#[derive(TypedBuilder)]
struct Foo<T> {
    #[builder(default = 12)]
    x: T,
}

if and only if a default generic type is specified for T like so

#[derive(TypedBuilder)]
struct Foo<T = usize> {
    #[builder(default = 12)]
    x: T,
}

https://github.com/idanarye/rust-typed-builder/issues/44#issuecomment-761195682

Finally - this feels like breaking the Rust rule that the information about the types should be in the signatures, not in the implementation body. And yes, you can argue that the attributes are part of the signature, but #[builder(default)] acts as an implementing code generator and it feels wrong to have it affect the signature...

I totally agree with your assessment here. Specifying the default type for generic parameters in order to enable this feature feels like a happy medium since it keeps all the default type information in the struct signature.

I also added some tests and examples which show how this new feature interacts with more complex types that use both generic parameters with and without defaults. The code might not be the cleanest at the moment, just wanted to get a working model up first and then can refactor as needed. Let me know what you think!

treysidechain avatar Dec 07 '22 14:12 treysidechain