p0nce / d-idioms

"Effective D" without the haircut.
http://p0nce.github.io/d-idioms/
82 stars 17 forks source link

Allocating static arrays. #156

Closed veelo closed 5 years ago

veelo commented 5 years ago

Hi and thank you for your idioms.

Regarding https://p0nce.github.io/d-idioms/#One-does-not-simply-call-new-for-static-arrays:

Although there is no syntax in the language to new a static array, I think I have found a nice idiom for allocating static arrays on the GC heap (and elsewhere), using std.experimental.allocator:

import std.experimental.allocator;

void main()
{
    alias T = int[4];

    // T* t = new T;              // Won't work.
    T* t = theAllocator.make!T;   // Fine!
}

In places where use of dynamic arrays are not straightforward such as in translations from other languages, this can be a practical solution.

Do you want to include this, or do you prefer a PR?

Cheers, Bastiaan.

p0nce commented 5 years ago

Hello, I don't have much time to update the d-idioms myself, so if you want to contribute you are welcome. Please follow the guidelines in README.md, that until now noone has ever followed.

p0nce commented 5 years ago

Done