Void initialization

bearophile bearophileHUGS at lycos.com
Tue Dec 20 14:19:06 PST 2011

Stewart Gordon:

> I don't quite understand - why not just use float as it is? OK, so abbreviating it to TF 
> saves 9 characters on that line, but the alias declaration and its trailing line break 
> take up 16 characters, so you're not saving space at all.

It's not a way to save chars, it's a way to avoid bugs, making the code my DRY.

> Moreover, the style guide discourages meaningless type aliases.

That's also why I have said a better solution is to wrap that code into a function template, so there is no need for an alias.

> import std.stdio, std.gc;
> void main() {
>      size_t x = 42;
>      float[] f = cast(float[]) std.gc.malloc(x*4);
>      writefln(f.length);
>      alias float TF;
>      f = (cast(TF*)std.gc.malloc(x * TF.sizeof))[0 .. x];
>      writefln(f.length);
> }

I didn't know this. It's handy.


More information about the Digitalmars-d-learn mailing list