Why is this not allowed?
Salih Dincer
salihdb at hotmail.com
Tue Oct 8 18:01:45 UTC 2024
On Monday, 7 October 2024 at 17:20:00 UTC, ryuukk_ wrote:
>
> We went from trying to do that:
>
> ```d
> struct EntityDef
> {
> struct
> {
> int hp;
> } stats;
> }
> ```
>
>
> to getting suggested to do that instead:
>
> ```d
> struct Foo
> {
> int bar;
>
> //struct {/*
> Baz baz;
> struct Baz
> {
> auto opAssign(int value)
> => baz = value;//*/
> int baz;
> }
> }
>
> void main()
> {
> Foo foo;
> foo.bar = 7;
> foo.baz = 42;
>
> imported!"std.stdio".writeln(foo); /*
> with opAssign() Anonymous
> Foo(7, Baz(42)) or Foo(7, 42) */
> }
> ```
I understand you, but you misunderstood me. You probably brought
up a problem that can't be solved quickly (or ever). I wanted to
acknowledge D's praise and bring a different perspective. Would
this solution work for you?
```d
template Foo() { int hp; }
struct EntityDef
{
mixin Foo stats;
}
void main()
{
auto num = EntityDef(41);
assert(num.stats.hp == 41);
with(num)
{
stats.hp = 42;
assert(stats.hp == 42);
}
}
```
SDB at 79
More information about the Digitalmars-d-learn
mailing list