How about some __initialize magic?

Stanislav Blinov stanislav.blinov at gmail.com
Mon Nov 29 08:35:06 UTC 2021


On Sunday, 28 November 2021 at 22:00:05 UTC, russhy wrote:
> On Sunday, 28 November 2021 at 19:30:11 UTC, Stanislav Blinov 
> wrote:
>
>> This topic has nothing to do with what you're talking about.
>
> It does, you just don't understand what "we could improve it" 
> mean; relaxing its rules, and reusing the syntax for doing what 
> you ask for

Oh I have no doubt that there is indeed some lack of 
understanding here. So I'm going to try one last time. The 
problem in question lies in the assignment operator, __not__ 
whatever's on the right hand side of it. It's absolutely 
irrelevant here how you spell the initializer.

First please understand the difference between initialization and 
assignment. Then read up on 
https://dlang.org/spec/declaration.html#void_init and then try to 
understand that assigning to uninitialized structs that have an 
explicit or implicit `opAssign` defined would involve using 
uninitialized values, which may lead to UB. And that is just one 
of the problems that existing library solutions address. The rest 
is spelled out in the first post.

Have fun with this little program:

```d
import std.stdio;

void main() {
     File file = void;
     file = File.init; // File.init, .{}, BANANAS - doesn't 
matter, it's UB
}
```

So once again, if you want to discuss initializer syntax, feel 
free to create a topic for that as that is not what's in question 
here.


More information about the Digitalmars-d mailing list