static array literal syntax request: auto x=[1,2,3]S;
Mehrdad
wfunction at hotmail.com
Fri Jun 15 00:19:28 PDT 2012
On Friday, 15 June 2012 at 07:15:50 UTC, Jonathan M Davis wrote:
> On Friday, June 15, 2012 09:09:48 Don Clugston wrote:
>> On 10/06/12 23:43, Jonathan M Davis wrote:
>> > On Sunday, June 10, 2012 23:23:57 Mehrdad wrote:
>> >> I honestly don't see the POINT of having a "dynamic array
>> >> literal".
>> >>
>> >> What's the point of making the literals dynamic?
>> >>
>> >> They should all be static, and only converted to dynamic if
>> >> necessary from the context.
>> >>
>> >> But I really don't see the benefit of allocating them on
>> >> the heap
>> >> just because we can... perhaps someone can enlighten me?
>> >
>> > In the vast majority of cases where an array literal is
>> > used, it's
>> > assigned to a dynamic array.
>>
>> I doubt that very much. I know it's not true in my code, I use
>> array
>> literals almost exclusively for immutable values.
>> Usually if you are initializing an array, where you will
>> modify the
>> elements later, you want all values to be the same.
>>
>> I argued that array literals should be immutable, just as
>> string
>> literals are. But I lost.
>
> What does immutability have to do with static vs dynamic?
>
> immutable a = [0, 1, 2, 3];
>
> results in an immutable(int[]), not immutable(int[4]).
>
> - Jonathan M Davis
Just a guess here, but I think what Don meant was this:
An immutable _literal_ is quite often "static __gshared" (it's
pretty pointless to make it an instance member...), which means
it's always in memory, which means it should be static (as
opposed to dynamic), since slicing it wouldn't cause problems.
More information about the Digitalmars-d
mailing list