~sircmpwn/hare-users

2 2

Embedded struct initialization feels cumbersome?

Details
Message ID
<3232566.44csPzL39Z@veleda>
DKIM signature
pass
Download raw message
As I understand, this is how you initialize it currently.

```
type Animal = struct {
        id: uint
};

type Dog = struct {
        base: Animal,
        legs: u8
};

export fn main() void = {
        let dog = Dog {
                base = Animal {
                        id = 1,
                },
                legs = 4, 
        };
};
```

but I'd expect it to work like so:

```
export fn main() void = {
        let dog = Dog {
                base.id = 0,
                legs = 4, 
        };
};
```

As "base" is already user-defined, why would struct specification be
needed?
Lassi Pulkkinen <lassi@pulk.fi>
Details
Message ID
<20230109051545.040661F636@pulk.fi>
In-Reply-To
<3232566.44csPzL39Z@veleda> (view parent)
DKIM signature
pass
Download raw message
I'm not sure if this applies to your actual use case, but you can embed
a struct without giving it a field name, and its fields will appear in
the outer struct:

type Animal = struct {
	id: uint,
};

type Dog = struct {
	Animal,
	legs: u8,
};

export fn main() void = {
	let dog = Dog {
		id = 1,
		legs = 4, 
	};
};

Note that this still has the same layout semantics as a named field
would, as opposed to just copying the fields into the struct definition.
Details
Message ID
<CPTMC1UT1HT5.1QZ884JMF9RPO@taiga>
In-Reply-To
<3232566.44csPzL39Z@veleda> (view parent)
DKIM signature
pass
Download raw message
I would be open to a spec update and a harec patch that makes this work:

On Wed Jan 4, 2023 at 1:11 PM CET, Maurice Huuskes wrote:
> export fn main() void = {
>         let dog = Dog {
>                 base.id = 0,
>                 legs = 4, 
>         };
> };
> ```
Reply to thread Export thread (mbox)