Why are immutable array literals heap allocated?

Patrick Schluter Patrick.Schluter at bbox.fr
Sun Jul 7 19:12:58 UTC 2019


On Saturday, 6 July 2019 at 09:56:57 UTC, ag0aep6g wrote:
> On 06.07.19 01:12, Patrick Schluter wrote:
>> On Friday, 5 July 2019 at 23:08:04 UTC, Patrick Schluter wrote:
>>> On Thursday, 4 July 2019 at 10:56:50 UTC, Nick Treleaven 
>>> wrote:
>>>> immutable(int[]) f() @nogc {
>>>>     return [1,2];
>>>> }
> [...]
>> 
>> and it cannot optimize it away because it doesn't know what 
>> the caller want to do with it. It might in another module 
>> invoke it and modify it, the compiler cannot tell. auto a=f(); 
>> a[0]++;
>
> f returns immutable. typeof(a) is immutable(int[]). You can't 
> do a[0]++.

You're right, I shouldn't post at 1 am.


More information about the Digitalmars-d-learn mailing list