Using static properties and methods of variable in its own initializer

Derek Fawcus dfawcus+dlang at employees.org
Thu Apr 10 15:52:33 UTC 2025


On Thursday, 10 April 2025 at 08:26:57 UTC, Dejan Lekic wrote:
>
> Is this not good enough for you?
> ```d
> void main(){
>    size_t y = size_t.sizeof;
> }
> ```

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

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




More information about the Digitalmars-d mailing list