Using enum constant from different modules

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 10 13:47:01 PDT 2014


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...


More information about the Digitalmars-d-learn mailing list