Thread overview
Re: Undefined identifier error with enum in the separate module
Feb 01, 2014
Philippe Sigaud
Feb 01, 2014
bearophile
Feb 02, 2014
Philippe Sigaud
Feb 02, 2014
bearophile
Feb 02, 2014
Philippe Sigaud
Feb 02, 2014
bearophile
February 01, 2014
> // Reply.d
> import CodeEnum;
>
> unittest
> {
>         auto.e = CodeEnum.OK; // Error: undefined identifier 'OK'
> }
>
>
> What I am doing wrong?

The module and your enum have the same name. When the compiler sees the `CodeEnum` symbol, it considers you're referring to the module. This module does not have an `OK` member, hence the error.

In D, do not use the same symbol for a module and one of its inner symbols.
February 01, 2014
Philippe Sigaud:

> In D, do not use the same symbol for a module and one of its inner symbols.

I have seen other people hit this problem (like problems caused by having a "set.d" module containing a "Set" struct plus a "set" helper function).

Is this worth warning against (with a dmd warning/error)?

Bye,
bearophile
February 02, 2014
Bearophile:
> I have seen other people hit this problem (like problems caused by having a "set.d" module containing a "Set" struct plus a "set" helper function).
>
> Is this worth warning against (with a dmd warning/error)?

I'd prefer an error, with the same kind of nice message you get when
you forget to import std.stdio.
Ideally, the error should be caught as soon as the module is compiled,
like shadowing errors (which it is, in a way).

"module foo contains a member named foo. Module members cannot shadow module names."
February 02, 2014
Philippe Sigaud:

> I'd prefer an error, with the same kind of nice message you get when
> you forget to import std.stdio.
> Ideally, the error should be caught as soon as the module is compiled,
> like shadowing errors (which it is, in a way).
>
> "module foo contains a member named foo. Module members cannot shadow module names."

This is the ER I have opened, if you have comments or you don't agree with my ideas, please comment there:
https://d.puremagic.com/issues/show_bug.cgi?id=12059

Bye,
bearophile
February 02, 2014
> This is the ER I have opened, if you have comments or you don't agree with my ideas, please comment there: https://d.puremagic.com/issues/show_bug.cgi?id=12059

Well, I really thought it was forbidden (as in: the code will never work). I guess you can define foo inside module foo.d and use it internally... So OK for your proposal.
February 02, 2014
Philippe Sigaud:

> Well, I really thought it was forbidden (as in: the code will never
> work). I guess you can define foo inside module foo.d and use it
> internally... So OK for your proposal.

You can use it externally too, if you qualify it correctly.

Bye,
bearophile