digitalmars.D.learn - Implicitly converting a newly allocated array to immutable
- Meta (14/14) Jan 05 2014 The following doesn`t work:
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (12/13) Jan 05 2014 A pure function is a workaround. The return value of a pure function is
- Meta (5/19) Jan 06 2014 Thanks, that does the trick. Also, is there any hack that I can
- H. S. Teoh (9/12) Jan 06 2014 Unfortunately, this is currently impossible due to the way AA's are
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
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
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: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.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 06 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
Jan 06 2014