www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Implicitly converting a newly allocated array to immutable

reply "Meta" <jared771 gmail.com> writes:
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)
Jan 05 2014
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Jan 05 2014
parent reply "Meta" <jared771 gmail.com> writes:
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.
Jan 06 2014
parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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
Jan 06 2014