logoalt Hacker News

WalterBrightyesterday at 9:23 PM1 replyview on HN

Let's try this:

    constexpr std::vector<int> f() { return {1,2,3}; }
    constinit std::vector<int> p = f(); // error
in D!

    const(int)[] f() { return [1,2,3]; }
    immutable int[] aaa = f();
And the object file, look ma, aaa[] is statically allocated:

    internal:
        db      001h,000h,000h,000h,002h,000h,000h,000h ;........
        db      003h,000h,000h,000h,000h,000h,000h,000h ;........
    __D5test63aaayAi:
        db      003h,000h,000h,000h,000h,000h,000h,000h ;........
        db      000h,000h,000h,000h,000h,000h,000h,000h ;........

Replies

tialaramexyesterday at 11:03 PM

But why though? What is the purpose of the immutable "dynamic" array in your D?

We're unavoidably going to learn at compile time how big the array is, and knowing how big it is will always be the same or better, so we should always do that.

show 2 replies