Using enum constant from different modules

simendsjo via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 10 14:00:35 PDT 2014


On 07/10/2014 10:47 PM, "Marc Schütz" <schuetzm at gmx.net>" wrote:
> On Thursday, 10 July 2014 at 20:27:39 UTC, Jacob Carlborg wrote:
>> Here's a code example:
>>
>> module main;
>>
>> import foo;
>>
>> enum Get = "GET";
>>
>> void bar (string a)
>> {
>>     assert(a is Get);
>> }
>>
>> void main ()
>> {
>>     asd();
>> }
>>
>> module foo;
>>
>> import main;
>>
>> void asd()
>> {
>>     bar(Get);
>> }
>>
>> Running the above code will cause an assert error in the function
>> "bar". But if I move the function "asd" into the "main" module and
>> completely skip the "foo" module the assert passes.
>>
>> I don't know if I'm thinking completely wrong here but this seems like
>> a bug to me.
> 
> No, this is equivalent to:
> 
> void bar (string a)
> {
>     assert(a is "GET");
> }
> 
> void asd()
> {
>     bar("GET");
> }
> 
> Enums behave as if their values are copy-n-pasted everywhere they are
> used (you probably know that).
> 
> The compiler probably conflates the two identical strings when they're
> in the same module. This is safe for immutable data. I'm sure there's
> something in the spec about it...

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");
}

changing a and b to enum gives the same results.


More information about the Digitalmars-d-learn mailing list