derive-new icon indicating copy to clipboard operation
derive-new copied to clipboard

Add the capability of 'init-only' fields.

Open chilabot opened this issue 2 years ago • 0 comments

I'd be nice to have 'init-only' fields, so you can use them in field initialization:

#[derive(new)]
struct Test
{
    #[new(init)] // <---
    active: bool,

    #[new(value = "Other::new(active)")]
    other: Other
}

Like https://docs.python.org/3/library/dataclasses.html#init-only-variables

I don't think I'd add too much complexity and I'd be really useful. The workaround for this is to declare the init-field after 'other' and to store it in the struct, which also forces a copy if the initialized class takes the parameter by value.

chilabot avatar Mar 06 '23 15:03 chilabot