Using enum constant from different modules

simendsjo via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 10 22:27:30 PDT 2014


On 07/11/2014 01:08 AM, sigod wrote:
> On Thursday, 10 July 2014 at 20:59:17 UTC, simendsjo wrote:
>> Strings behaves a bit odd with is(). The following passes:
>>
>> import std.stdio;
>> void f(string a, string b) {
>>     assert(a is b); // also true
>> }
>> void main() {
>>     string a = "aoeu";
>>     string b = "aoeu";
>>     assert(a is b); // true
>>     f(a, b);
>>     writeln("passed");
>> }
> 
> ```d
> import std.stdio;
> void f(string a, string b) {
>    writeln("a: ", a.ptr, ", b: ", b.ptr);
>    assert(a is b); // also true
> }
> void main() {
>    string a = "aoeu";
>    string b = "aoeu";
>    writeln("a: ", a.ptr, ", b: ", b.ptr);
>    assert(a is b); // true
>    f(a, b);
>    writeln("passed");
> }
> ```
> 
> Output:
> ```
> a: 4210A0, b: 4210A0
> a: 4210A0, b: 4210A0
> passed
> ```
> 
> Seems legit to me.

I forgot to check for compiler optimizations (even without -O).

immutable(int)[] a = [1];
immutable(int)[] b = [1];
assert(a is b); // fails as .ptr is different.

So it looks like string literals is cached by the compiler and reused.
Changing "aoeu" to 10.to!string for instance breaks this optimization.

But the fact that immutable(char)[] behaves different from
immutable(int)[] is a bit strange.


More information about the Digitalmars-d-learn mailing list