On 21 October 2013 11:48, Walter Bright <[email protected]> wrote:

> On 10/20/2013 5:59 PM, Jonathan M Davis wrote:
>
>> If that paradigm is frequent enough, it might be worth wrapping it in a
>> struct. Then, you'd probably get something like
>>
>> StaticArray!(int, 10) tmp(n);
>> int[] a = tmp[];
>>
>> which used T[10] if n was 10 or less and allocated T[] otherwise. The
>> destructor could then deal with freeing the memory.
>>
>
> Sounds like a good idea - and it should fit in with Andrei's nascent
> allocator design.
>

I use this pattern all over the place.
I don't love it though. It doesn't feel elegant at all and it wastes stack
space, but it's acceptable, and I'd really like to see this pattern
throughout phobos, especially where strings and paths are concerned.
System interface functions that pass zero-terminated strings through to the
OS are the primary offender, needless garbage, those should be on the stack.

I like to use alloca too where it's appropriate. I'd definitely like if D
had a variable-sized static array syntax for pretty-ing alloca.
I thought about something similar using alloca via a mixin template, but
that feels really hackey!

Reply via email to