Default initialization no longer a thing
Salih Dincer
salihdb at hotmail.com
Tue Oct 24 17:58:18 UTC 2023
On Tuesday, 24 October 2023 at 15:39:09 UTC, Hipreme wrote:
> The only problem I ever had with D was float being initialized
> to nan instead of 0. This has been talked a lot of times now.
> If you read the post from Sonke, you can see that D is not
> being how simple it was from before from the addition of lot of
> keywords: `pure`, `nothrow`, `@nogc`, `scope`, `return`. I
> don't want any more verbosity than that, so all hail default
> initialization :D
I don't like using so many attributes either. Fortunately, your
code usually works even when you don't use it :)
On Tuesday, 24 October 2023 at 01:35:16 UTC, deadalnix wrote:
> @disable this plain doesn't work. It prevent things in some
> cases, but utltimately fails to enforce the invariant it sets
> out to enforce.
The default constructor doesn't always work the way I want. Let's
talk some code. Test1 and test2 are ok but the third one is
something I came across recently:
```d
void main()
{
string data;
assert(data is null);
/** TEST 1 **/
struct String
{
string data;
}
String s;
assert(s.data is null);
auto s2 = String();
assert(s2.data is null);
/** TEST 2 **/
struct Ztring
{
string data;
this(string data)
{
this.data = data;
}
}
Ztring z;
assert(z.data is null);
auto z2 = Ztring("D");
assert(!(z2.data is null));
/** TEST 3 **/
struct Foo
{
int[] data;
this(int[] data)
{
this.data = data;
if(this.data.capacity < 4)
{
this.data.length = 20;
}
}
}
auto l = Foo([1, 2, 3]);
assert(l.data.capacity > 4);
}
```
I don't want my code to run with the default feature, but it
shouldn't take any constructor parameters either. What I want is
to increase the capacity of one of the members at compile time
while it is initialized.
SDB at 79
More information about the Digitalmars-d
mailing list