I have a fundamental language design talking point for you. It's not specific to D. I claim that, most of the time, a programmer cannot, and shouldn't have to, make the decision of whether to allocate on stack or heap. For example:

void func(T)()
{
    auto t = <allocate T from heap or stack?>
    ...
}

The question of whether variable t should lay in heap or stack, depends not only on the sizeof(T), but also on the context in which func is called at. If func is called at a context in which allocating T on stack would cause a stack overflow, then t should be allocated from the heap. On the other hand, if func is called at a context where T still fits nicely on the stack, it would probably be better and faster to allocate t on stack.

So, the question of whether to allocate that variable t on stack or heap is something that only the compiler (or runtime) can answer. Is there any language where you have the ability to say "allocate T from wherever it's best"?

I wonder if it would be possible in D to let the compiler allocate dynamic arrays on stack when it can statically guarantee that it's safe to do so (no dangling references, never increasing the size of the array, etc).

Reply via email to