Jump to page: 1 2 3
Thread overview
Setting field of struct object
Jan 22
Joel
Jan 22
Joel
Jan 22
Danilo
Jan 22
Danilo
Jan 22
zjh
Jan 22
Joel
Jan 22
Danilo
Jan 22
zjh
Jan 22
zjh
Jan 22
Bkoie
Jan 22
zjh
Jan 22
Bkoie
Jan 22
ryuukk_
Jan 22
ryuukk_
Jan 22
zjh
Jan 22
bachmeier
Jan 22
zjh
Jan 22
zjh
Jan 22
bachmeier
Jan 25
Renato
Jan 25
zjh
January 22

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.

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@gmail.com").withAge(44);
    writeln(p);
}
January 22

On Monday, 22 January 2024 at 08:27:36 UTC, Joel wrote:

>

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.

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@gmail.com").withAge(44);
    writeln(p);
}

I've lost interest in the video, looks like horrible syntax (F#).

January 22

On Monday, 22 January 2024 at 08:35:01 UTC, Joel wrote:

>

I've lost interest in the video, looks like horrible syntax (F#).

Nonetheless, this usually used with Objects (new class/struct instances), like so:

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() {
    auto p = (new Person).withName("Tom")
                         .withEmail("joelcnz@gmail.com")
                         .withAge(44);
    writeln(p);
}

If you convert it to a class, add an static opCall for initialization,
and a toString() method, it's even nicer:

module app;

import std;

class Person {
    private string name, email;
    private 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; }

    static Person opCall() => new Person();

    override string toString() {
        return "Person{ name: "~name~", age: "~age.to!string~", email: "~email~" }";
    }
}

void main() {
    auto p = Person()
                 .withName("Tom")
                 .withEmail("joelcnz@gmail.com")
                 .withAge(44);

    writeln(p);
}

It's common OOP style in some frameworks.

January 22

On Monday, 22 January 2024 at 08:27:36 UTC, Joel wrote:

>
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@gmail.com").withAge(44);
    writeln(p);
}

VS:C++

struct Person {
    string name, email;
    ulong age;
}
Person a{"n","email",33};
January 22

On Monday, 22 January 2024 at 08:54:54 UTC, zjh wrote:

>

On Monday, 22 January 2024 at 08:27:36 UTC, Joel wrote:

>
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@gmail.com").withAge(44);
    writeln(p);
}

VS:C++

struct Person {
    string name, email;
    ulong age;
}
Person a{"n","email",33};

What about in D:
auto a=Person(“n”, “email”, 33);

January 22

On Monday, 22 January 2024 at 08:54:21 UTC, Danilo wrote:

>

It's common OOP style in some frameworks.

With latest D you can also just use named parameters:

import std;

struct Person {
    /*private*/ string name, email;
    /*private*/ ulong age;
}

void main() {
    auto p = Person(
        name: "Tom",
        email: "joelcnz@gmail.com",
        age: 44,
    );

    writeln(p);
}
January 22

On Monday, 22 January 2024 at 08:54:54 UTC, zjh wrote:

>

VS:C++

struct Person {
    string name, email;
    ulong age;
}
Person a{"n","email",33};

It's not much different in D. ;)

import std;

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

void main() {
    auto p = Person("Tom", "joelcnz@gmail.com", 44);
    writeln(p);
}
January 22

On Monday, 22 January 2024 at 08:54:54 UTC, zjh wrote:

>

On Monday, 22 January 2024 at 08:27:36 UTC, Joel wrote:

>
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@gmail.com").withAge(44);
    writeln(p);
}

VS:C++

struct Person {
    string name, email;
    ulong age;
}
Person a{"n","email",33};

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);
}
January 22

On Monday, 22 January 2024 at 08:54:54 UTC, zjh wrote:

>
struct Person {
    string name, email;
    ulong age;
}
Person a{"n","email",33};

C++ can achieve ultimate simplicity without violating DRY,
And here, D violates the DRY principle!
Moreover, as the package level, module level, class level, member level, D language violates integrity.
Because D has no class level limit.
These are all not serious states.

January 22

On Monday, 22 January 2024 at 11:31:11 UTC, zjh wrote:
D language violates integrity.

>

Because D has no class level limit.
These are all not serious states.

It seems that D language is not professional.

« First   ‹ Prev
1 2 3