"inline" conversion of array to immutable
FreeSlave via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Apr 22 04:17:20 PDT 2016
On Friday, 22 April 2016 at 11:07:47 UTC, Jeff Thompson wrote:
> On Friday, 22 April 2016 at 09:40:14 UTC, FreeSlave wrote:
>> On Friday, 22 April 2016 at 09:25:32 UTC, Jeff Thompson wrote:
>>> Hello. The following code compiles OK where func creates a
>>> mutable array and main assigns it to an immutable variable:
>>>
>>> [...]
>>
>> Probably this is what you look for
>> http://dlang.org/phobos/std_exception.html#.assumeUnique
>
> OK, we lose the compiler check for correctness. What if I put
> func directly in main with the hopes that the compiler will
> check correctness and also inline the function? But it won't
> assign to the immutable array. Why not? It's the same function.
>
> void main(string[] args) {
> int[] func(int x) pure {
> int[] result = new int[10];
> result[0] = x;
> return result;
> }
> immutable int[] array = func(1);
> }
Not sure why, but making func static fixes this:
void main(string[] args) {
static int[] func(int x) pure {
int[] result = new int[10];
result[0] = x;
return result;
}
immutable int[] array = func(1);
}
More information about the Digitalmars-d-learn
mailing list