Setting field of struct object

FeepingCreature feepingcreature at gmail.com
Mon Jan 22 09:53:29 UTC 2024


On Monday, 22 January 2024 at 08:54:54 UTC, zjh wrote:
> On Monday, 22 January 2024 at 08:27:36 UTC, Joel wrote:
>
>> ```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);
>> }
>> ```
>
> VS:`C++`
>
> ```d
> struct Person {
>     string name, email;
>     ulong age;
> }
> Person a{"n","email",33};
> ```

D:

```d
import std.stdio;

struct Person {
     string name, email;
     ulong age;
}

void main() {
     Person p = Person(name: "n", email: "email", age: 33);
     writefln!"%s"(p);
}
```




More information about the Digitalmars-d-learn mailing list