Skip to content

Struct member defaults #1696

@mikex-oss

Description

@mikex-oss

What's hard to do? (limit 100 words)

Sometimes you want to specify struct defaults within the struct definition itself.

C++ allows this, e.g.

struct MyStruct
{
    int x;
    int y {};
    int z { 2 };
};

So does SystemVerilog:

  typedef struct {
    logic[31:0] x;
    logic[31:0] y = '0;
    logic[31:0] z = 'd2;
  } my_struct_t;

Rust does not, though see discussion in rust-lang/rfcs#1806.

Current best alternative workaround (limit 100 words)

zero!<MyStruct>() works when everything can be zero-initialized. Otherwise, you can do something like (maybe even later attached to the struct with impl:

fn new_my_struct(x: u32) -> MyStruct {
    MyStruct {
        x: x
        y: u32:0,
        z: u32:2,
    }
}

which is sorta like implementing the Default trait in Rust in that you need to write a separate function that specifies the defaults.

Your view of the "best case XLS enhancement" (limit 100 words)

Even though the Rust RFC languished with mixed opinions, it'd be nice to support this in DSLX for readability and conciseness.

Metadata

Metadata

Assignees

No one assigned

    Labels

    dslxDSLX (domain specific language) implementation / front-endenhancementNew feature or request

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions