Re: Setting field of struct object

2024-02-01 Thread TheTeaLady via Digitalmars-d-learn

On Monday, 22 January 2024 at 11:31:11 UTC, zjh wrote:

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


```d
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`.


Apparently comments about module visibility issues are no longer 
allowed on the D forum.


I wonder why your comment was not deleted though ??

No wonder there is now a fork of D.


Re: Setting field of struct object

2024-02-01 Thread Anonymouse via Digitalmars-d-learn

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

writeln(p);
}
```


I had reason to need this to work a while ago and `opDispatch` 
came in very handy. I was able to cook up one that forwarded 
calls to other members in a struct, while returning `this` by 
ref, allowing for chaining calls. I use it with UDAs.


(Scroll to the unit tests for examples.)

```d
/++
Mixin template generating an `opDispatch` redirecting calls 
to members whose
names match the passed variable string but with an underscore 
prepended.

 +/
mixin template UnderscoreOpDispatcher()
{
ref auto opDispatch(string var, T)(T value)
{
import std.traits : isArray, isAssociativeArray, 
isSomeString;


enum realVar = '_' ~ var;
alias V = typeof(mixin(realVar));

static if (isAssociativeArray!V)
{
// Doesn't work with AAs without library solutions
}
else static if (isArray!V && !isSomeString!V)
{
mixin(realVar) ~= value;
}
else
{
mixin(realVar) = value;
}

return this;
}

auto opDispatch(string var)() inout
{
enum realVar = '_' ~ var;
return mixin(realVar);
}
}

///
unittest
{
struct Foo
{
int _i;
string _s;
bool _b;
string[] _add;
alias wordList = _add;

mixin UnderscoreOpDispatcher;
}

Foo f;
f.i = 42; // f.opDispatch!"i"(42);
f.s = "hello";// f.opDispatch!"s"("hello");
f.b = true;   // f.opDispatch!"b"(true);
f.add("hello");   // f.opDispatch!"add"("hello");
f.add("world");   // f.opDispatch!"add"("world");

assert(f.i == 42);
assert(f.s == "hello");
assert(f.b);
assert(f.wordList == [ "hello", "world" ]);

auto f2 = Foo()
.i(9001)
.s("world")
.b(false)
.add("hello")
.add("world");

assert(f2.i == 9001);
assert(f2.s == "world");
assert(!f2.b);
assert(f2.wordList == [ "hello", "world" ]);
}
```

You could trivially adapt it to use a `withName`, `withEmail` 
calling scheme instead of the underscore thing.


Re: Setting field of struct object

2024-02-01 Thread TheTeaLady via Digitalmars-d-learn

On Monday, 22 January 2024 at 15:47:23 UTC, bachmeier wrote:

On Monday, 22 January 2024 at 15:45:45 UTC, zjh wrote:

On Monday, 22 January 2024 at 15:33:01 UTC, ryuukk_ wrote:

it only took me 1 project to never want to touch C++ again..



D language used to have no `copy constructor`, isn't it now 
added in again?


You have to admit the good aspects of `C++`.
You should take a look at the `latest C++`. C++ has already 
learned many advantages of `D`, but D has not made 
`significant progress`!
As a user, `C++` is really not much different from D, and even 
surpasses D `in many aspects`.
`RAII `, `variable parameter` template, `coroutine, concept`, 
`value semantics`, very easy to understand.
Moreover, the `inheritance` of C++ is very enjoyable to use in 
many aspects.


Sounds like you should be using C++. Why are you here?


Everyone who uses D should be welcome, even if they have a 
preference for another language, or a preference for features in 
another language.


This is how D will evolve and improve.

And this should include people who don't like the untyped 
universe of the D module (specifically, its lack of an optional 
mechanism for information hiding within a module). People can 
think of it as if it were typed, but this is just an illusion. 
This illusion can easily lead to type confusion and inconsistent 
and erroneous interactions - i.e. bugs.
To avoid this in D one needs to literally reorganise that untyped 
universe into into a typed system. Now your types impose 
constraints which will help to enforce their correctness. 
Organisation is important of course, but for using types in D, 
its vital - if strong typing is your objective.


I think D is clumsy in many other areas as well.

btw. I too prefer multiple inheritance.


Re: Setting field of struct object

2024-01-25 Thread zjh via Digitalmars-d-learn

On Thursday, 25 January 2024 at 08:46:34 UTC, Renato wrote:



```d
void main() {
Person p = { "Joe", "j...@ab.com", 30};
writeln(p);
}
```

I just tested it and it works. It's `great`!



Re: Setting field of struct object

2024-01-25 Thread Renato via Digitalmars-d-learn

On Monday, 22 January 2024 at 11:31:11 UTC, zjh wrote:

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


```d
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`.


You know you can use struct literals in initializers, right?

```d
import std.stdio;
struct Person {
string name, email;
ulong age;
}

void main() {
Person p = {name: "Joe", email: "j...@example.com", age: 30};
writeln(p);
}
```


Re: Setting field of struct object

2024-01-22 Thread bachmeier via Digitalmars-d-learn

On Monday, 22 January 2024 at 15:56:59 UTC, zjh wrote:

On Monday, 22 January 2024 at 15:51:37 UTC, zjh wrote:


I spent `too much time` on D.


And some of the inherent `drawbacks` of `C++` are too hateful.


It's a package deal. Everything in C++ is there because there 
were benefits when they added it, but those benefits came with 
downsides. D will end up in the same place if it emulates C++.


Re: Setting field of struct object

2024-01-22 Thread zjh via Digitalmars-d-learn

On Monday, 22 January 2024 at 15:51:37 UTC, zjh wrote:


I spent `too much time` on D.


And some of the inherent `drawbacks` of `C++` are too hateful.



Re: Setting field of struct object

2024-01-22 Thread zjh via Digitalmars-d-learn

On Monday, 22 January 2024 at 15:47:23 UTC, bachmeier wrote:


Sounds like you should be using C++. Why are you here?



I spent `too much time` on D.


Re: Setting field of struct object

2024-01-22 Thread zjh via Digitalmars-d-learn

On Monday, 22 January 2024 at 15:14:32 UTC, Bkoie wrote:

D is totally different from C++ in D you usually you wont 
construct the struct directly use alias as.



Stop being `unconventional` and quickly copy their `good things`.
Otherwise, the `development speed` of the D language is really 
`too slow`!





Re: Setting field of struct object

2024-01-22 Thread bachmeier via Digitalmars-d-learn

On Monday, 22 January 2024 at 15:45:45 UTC, zjh wrote:

On Monday, 22 January 2024 at 15:33:01 UTC, ryuukk_ wrote:

it only took me 1 project to never want to touch C++ again..



D language used to have no `copy constructor`, isn't it now 
added in again?


You have to admit the good aspects of `C++`.
You should take a look at the `latest C++`. C++ has already 
learned many advantages of `D`, but D has not made `significant 
progress`!
As a user, `C++` is really not much different from D, and even 
surpasses D `in many aspects`.
`RAII `, `variable parameter` template, `coroutine, concept`, 
`value semantics`, very easy to understand.
Moreover, the `inheritance` of C++ is very enjoyable to use in 
many aspects.


Sounds like you should be using C++. Why are you here?


Re: Setting field of struct object

2024-01-22 Thread zjh via Digitalmars-d-learn

On Monday, 22 January 2024 at 15:33:01 UTC, ryuukk_ wrote:

it only took me 1 project to never want to touch C++ again..



D language used to have no `copy constructor`, isn't it now added 
in again?


You have to admit the good aspects of `C++`.
You should take a look at the `latest C++`. C++ has already 
learned many advantages of `D`, but D has not made `significant 
progress`!
As a user, `C++` is really not much different from D, and even 
surpasses D `in many aspects`.
`RAII `, `variable parameter` template, `coroutine, concept`, 
`value semantics`, very easy to understand.
Moreover, the `inheritance` of C++ is very enjoyable to use in 
many aspects.




Re: Setting field of struct object

2024-01-22 Thread ryuukk_ via Digitalmars-d-learn
I should note that it only took me 1 project to never want to 
touch C++ again.. that must be telling something, either about 
the language, or me, or both lol


Re: Setting field of struct object

2024-01-22 Thread ryuukk_ via Digitalmars-d-learn

On Monday, 22 January 2024 at 11:31:11 UTC, zjh wrote:

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


```d
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`.


I used to want this feature too, but i then got hit by a bug when 
i reordered the fields in the struct.. i don't want to deal with 
that stuff anymore


But we now have named arguments, so this feature could be make 
use of it, it's similar with enums, perhaps one day this could be 
revived:


https://github.com/dlang/DIPs/blob/e2ca557ab9d3e60305a37da0d5b58299e0a9de0e/DIPs/DIP1044.md

There is even a working implementation: 
https://github.com/dlang/dmd/pull/14650





Re: Setting field of struct object

2024-01-22 Thread Bkoie via Digitalmars-d-learn

On Monday, 22 January 2024 at 11:31:11 UTC, zjh wrote:

On Monday, 22 January 2024 at 08:54:54 UTC, zjh wrote:
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`.


i think D module base system is fine wished some other lang had 
the same you can avoid circular import ref.


dry? well he can easily turn that into a generic config static 
factory which its look like hes trying to do.


D is totally different from C++ in D you usually you wont 
construct the struct directly use alias as.


Re: Setting field of struct object

2024-01-22 Thread zjh via Digitalmars-d-learn

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


```d
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`.



Re: Setting field of struct object

2024-01-22 Thread zjh via Digitalmars-d-learn

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`.


Re: Setting field of struct object

2024-01-22 Thread FeepingCreature via Digitalmars-d-learn

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("joel...@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);
}
```




Re: Setting field of struct object

2024-01-22 Thread Danilo via Digitalmars-d-learn

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

VS:`C++`

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


It's not much different in D. ;)

```d
import std;

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

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


Re: Setting field of struct object

2024-01-22 Thread Danilo via Digitalmars-d-learn

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:
```d
import std;

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

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

writeln(p);
}
```



Re: Setting field of struct object

2024-01-22 Thread Joel via Digitalmars-d-learn

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

writeln(p);
}
```


VS:`C++`

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


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



Re: Setting field of struct object

2024-01-22 Thread zjh via Digitalmars-d-learn

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

writeln(p);
}
```


VS:`C++`

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




Re: Setting field of struct object

2024-01-22 Thread Danilo via Digitalmars-d-learn

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:

```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() {
auto p = (new Person).withName("Tom")
 .withEmail("joel...@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:
```d
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("joel...@gmail.com")
 .withAge(44);

writeln(p);
}
```
It's common OOP style in some frameworks.


Re: Setting field of struct object

2024-01-22 Thread Joel via Digitalmars-d-learn

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.


```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("joel...@gmail.com").withAge(44);

writeln(p);
}
```


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



Setting field of struct object

2024-01-22 Thread Joel via Digitalmars-d-learn
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("joel...@gmail.com").withAge(44);
writeln(p);
}
```