google / google/xls

Struct member defaults

Open
#1,696 2 comments 1 reaction 0 assignees View on GitHub
dslx enhancement
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.