Thread overview
Using static properties and methods of variable in its own initializer
6 days ago
Dave P.
5 days ago
Dejan Lekic
5 days ago
Dave P.
1 day ago
Atila Neves
5 days ago
Derek Fawcus
5 days ago
user1234
5 days ago
Dave P.
6 days ago
size_t a = a.sizeof; // This works
void main(){
    size_t y = y.sizeof; // this fails
}

In y’s initializer, y hasn’t yet been inserted into the scope. This prevents bugs like

int a = a;

but prevents patterns like:

int* x = malloc((*x).sizeof);

although you can still do:

int* x; x = malloc((*x).sizeof);

More concerningly, you get unintended references to globals if you happen to reuse the name:

char y;
void main(){
    size_t y = y.sizeof;
    assert(y == y.sizeof); // fails, y == 1
}

You can also get weird code if your variable has the same name as a type:

struct foo {
}
void main(){
    auto foo = foo();
    auto foo2 = foo(); // Error: struct `foo` does not overload ()
}

In my opinion, it should be fine to refer to static properties or methods and so

void main(){
    size_t y = y.sizeof;
}

should work, but because of how this was implemented it does slightly change the semantics if you happen to shadow a global.

Should this be fixed? or is this the intended design of the language?

5 days ago

On Wednesday, 9 April 2025 at 19:47:43 UTC, Dave P. wrote:

>

In my opinion, it should be fine to refer to static properties or methods and so

void main(){
    size_t y = y.sizeof;
}

should work, but because of how this was implemented it does slightly change the semantics if you happen to shadow a global.

Should this be fixed? or is this the intended design of the language?

Is this not good enough for you?

void main(){
   size_t y = size_t.sizeof;
}

In short, types few properties too, .sizeof being one of them. Other properties are .alignof and .init . More about it: https://dlang.org/spec/property.html#type

5 days ago

On Wednesday, 9 April 2025 at 19:47:43 UTC, Dave P. wrote:

>

[...]
Should this be fixed? or is this the intended design of the language?

  1. I think that the pattern should never be accepted, so the very first example, the one at the global scope, must be an error.
  2. There's no shadowing as long as you can disambiguate, using a qual chain (or even the global scope operator), the right decl. to use.

Let's look at the other proglangs... C is permissive, C++ is not (unless -fpermissive).

5 days ago

On Thursday, 10 April 2025 at 08:26:57 UTC, Dejan Lekic wrote:

>

On Wednesday, 9 April 2025 at 19:47:43 UTC, Dave P. wrote:

>

[...]

Is this not good enough for you?

void main(){
   size_t y = size_t.sizeof;
}

In short, types few properties too, .sizeof being one of them. Other properties are .alignof and .init . More about it: https://dlang.org/spec/property.html#type

This is just a trivial example. In general, repeating the type twice is error prone.

5 days ago

On Thursday, 10 April 2025 at 10:39:43 UTC, user1234 wrote:

>

On Wednesday, 9 April 2025 at 19:47:43 UTC, Dave P. wrote:

>

[...]
Should this be fixed? or is this the intended design of the language?

  1. I think that the pattern should never be accepted, so the very first example, the one at the global scope, must be an error.
  2. There's no shadowing as long as you can disambiguate, using a qual chain (or even the global scope operator), the right decl. to use.

Let's look at the other proglangs... C is permissive, C++ is not (unless -fpermissive).

I don’t know what you’re trying to show with your godbolt links. C++ allows what I am talking about, those errors are due to it disallowing implicit conversions from void*, which D also disallows.

5 days ago

On Thursday, 10 April 2025 at 08:26:57 UTC, Dejan Lekic wrote:

>

Is this not good enough for you?

void main(){
   size_t y = size_t.sizeof;
}

I suspect not, I commonly use the C equivalent of one of the prior examples:

void foo() {
   struct foo *fp = malloc(sizeof *fp);
}

Specifically such that when the code is updated, and the struct is renamed, the allocation is still of the correct size. The struct is likely to be renamed as its role may have changed slightly, and/or that changing often the name helps flush out missed updates.

Basically using the size of the variable helps avoid errors, as the type in the sizeof can then never get out of sync with the actual variable.

1 day ago

On Thursday, 10 April 2025 at 14:35:49 UTC, Dave P. wrote:

>

On Thursday, 10 April 2025 at 08:26:57 UTC, Dejan Lekic wrote:

>

On Wednesday, 9 April 2025 at 19:47:43 UTC, Dave P. wrote:

>

[...]

Is this not good enough for you?

void main(){
   size_t y = size_t.sizeof;
}

In short, types few properties too, .sizeof being one of them. Other properties are .alignof and .init . More about it: https://dlang.org/spec/property.html#type

This is just a trivial example. In general, repeating the type twice is error prone.

Yes, which is why I would recommend:

const y = size_t.sizeof;