Thread overview
Implicitly converting a newly allocated array to immutable
Jan 06, 2014
Meta
Jan 06, 2014
Ali Çehreli
Jan 07, 2014
Meta
Jan 07, 2014
H. S. Teoh
January 06, 2014
The following doesn`t work:

immutable(string[]) strArr = new string[](10);

But I feel that it probably should work. I know we have assumeUnique, but I remember awhile ago that some work was done toward making the result of unique expressions (like those using new) implicitly convertible to immutable, const, shared, etc. For example, the following works:

class Test
{
    int i;
}

immutable(Test) test = new Test();

(I realize that this will break if Test contains any non-value type like an array)
January 06, 2014
On 01/05/2014 05:19 PM, Meta wrote:> The following doesn`t work:
>
> immutable(string[]) strArr = new string[](10);

A pure function is a workaround. The return value of a pure function is implicitly convertible to immutable:

pure string[] foo()
{
    return new string[](10);
}

void main()
{
    immutable(string[]) strArr = foo();
}

Ali

January 07, 2014
On Monday, 6 January 2014 at 04:10:04 UTC, Ali Çehreli wrote:
> On 01/05/2014 05:19 PM, Meta wrote:> The following doesn`t work:
> >
> > immutable(string[]) strArr = new string[](10);
>
> A pure function is a workaround. The return value of a pure function is implicitly convertible to immutable:
>
> pure string[] foo()
> {
>     return new string[](10);
> }
>
> void main()
> {
>     immutable(string[]) strArr = foo();
> }
>
> Ali

Thanks, that does the trick. Also, is there any hack that I can use to build an AA at compile time? I have a module level variable that's a string[][string] and I'd like to initialize it without resorting to static this.
January 07, 2014
On Tue, Jan 07, 2014 at 12:01:33AM +0000, Meta wrote:
[...]
> Also, is there any hack that I can use to build an AA at compile time? I have a module level variable that's a string[][string] and I'd like to initialize it without resorting to static this.

Unfortunately, this is currently impossible due to the way AA's are implemented.

I do have a few ideas of how it might be done, but the last time I tried I couldn't get far enough to actually have it work.


T

-- 
The two rules of success: 1. Don't tell everything you know. -- YHL