Struct member defaults
- Dominant language
- C++
- Stars
- 1.9k
- Forks
- 283
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 135
Description
### 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 https://github.com/rust-lang/rfcs/pull/1806.
### Current best alternative workaround (limit 100 words)
`zero!()` 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.
Contributor guide
Assessment
This issue has not been assessed yet.