Skip to content

[FEATURE] Default struct field values #310

@borup3

Description

@borup3

Is your feature request related to a problem? Please describe.
I'd like an ability to set a default value for struct fields. This is particularly useful for structs that are mixed into other structs, where you have to retroactively update multiple struct initializers or constructors to properly initialize the new mixed-in field.

Describe the solution you'd like

struct MyStruct {
    can_drop: bool = true;
}

struct MyStruct2 {
    use MyStruct;
    val: int;
}

impl MyStruct2 {
    fn new() -> Self {
        // Initializes can_drop to true because it's omitted
        return MyStruct2{ val: 10 };
    }
}

Describe alternatives you've considered

  1. Manually initializing the field in every struct that uses the mixin and retroactively updating their initializers to include new fields added later.
  2. Adding function init_my_struct(s: MyStruct*) { s.can_drop = true; } to call every time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions