Setting field of struct object

Joel joelcnz at gmail.com
Mon Jan 22 08:27:36 UTC 2024


I've been watching a video (YouTube - "Pipeline-oriented 
programming - Scott Wlaschin - NDC Porto 2023") with something 
like the following code. This only sets the first method call, so 
I'm wanting to know how to make this work, for the subsequent 
methods.

```d
import std;

struct Person {
     string name, email;
     ulong age;
     auto withName(string name) { this.name=name; return this; }
     auto withEmail(string email) { this.email=email; return this; 
}
     auto withAge(ulong age) { this.age=age; return this; }
}

void main() {
     Person p;
     p.withName("Tom").withEmail("joelcnz at gmail.com").withAge(44);
     writeln(p);
}
```



More information about the Digitalmars-d-learn mailing list