Create an array with immutable elements

Roman D. Boiko rb at d-coding.com
Thu Jun 14 17:19:27 PDT 2012


On Friday, 15 June 2012 at 00:14:11 UTC, bearophile wrote:
> Roman D. Boiko:
>
>> immutable struct Node{ string s; }
>> Node[] f()
>> {
>>  Node[] arr = ...?
>>  return arr;
>> }
>>
>> How to fill an array, if its elements are immutable? I want to 
>> assign values calculated by some function.
>
> In general sometimes it's not easy to build immutable data 
> structures.
>
> In D there are several ways to do something similar to what you 
> ask, some alternatives (maybe there are more possibilities):
>
Only the first example works, because assignment to immutable 
elements of array is not allowed. But thanks for reminding me 
that I can reserve desired number of elements :) This makes the 
first example a perfect solution for me.




More information about the Digitalmars-d-learn mailing list