www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - in not working with enum'd AA

reply "Nathan M. Swan" <nathanmswan gmail.com> writes:
I don't think this should happen:

        private enum KeywordsToTypes = [
            "import"d    : TT.KwIMPORT,
            "public"d    : TT.KwPUBLIC,
            "protected"d : TT.KwPROTECTED,
            "private"d   : TT.KwPRIVATE,
            "static"d    : TT.KwSTATIC,
        ];

        static this() {
            // writes "0"
            std.stdio.writeln("import"d in KeywordsToTypes);
        }

Is there a reason why, or is this a bug?
Feb 27 2012
next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday, February 27, 2012 09:34:49 Nathan M. Swan wrote:
 I don't think this should happen:
 
         private enum KeywordsToTypes = [
             "import"d    : TT.KwIMPORT,
             "public"d    : TT.KwPUBLIC,
             "protected"d : TT.KwPROTECTED,
             "private"d   : TT.KwPRIVATE,
             "static"d    : TT.KwSTATIC,
         ];
 
         static this() {
             // writes "0"
             std.stdio.writeln("import"d in KeywordsToTypes);
         }
 
 Is there a reason why, or is this a bug?

No, that shouldn't happen, and I think that there's already a bug report on a similar issue. But I would point out that the value of an enum is effectively copy-pasted wherever it is used, so you're going to end up with a new instance of the AA everywhere that you use it. So, having an enum which is an AA is a _bad_ idea. It would be far better to use immutable. - Jonathan M Davis
Feb 27 2012
prev sibling next sibling parent "Daniel Murphy" <yebblies nospamgmail.com> writes:
"Nathan M. Swan" <nathanmswan gmail.com> wrote in message 
news:jubnbnovcksfyagtamzn forum.dlang.org...
I don't think this should happen:

        private enum KeywordsToTypes = [
            "import"d    : TT.KwIMPORT,
            "public"d    : TT.KwPUBLIC,
            "protected"d : TT.KwPROTECTED,
            "private"d   : TT.KwPRIVATE,
            "static"d    : TT.KwSTATIC,
        ];

        static this() {
            // writes "0"
            std.stdio.writeln("import"d in KeywordsToTypes);
        }

 Is there a reason why, or is this a bug?

I think there's a bug with dstring lookup in AAs, but there are plenty of other AA bugs too. Have you tried with char strings?
Feb 27 2012
prev sibling next sibling parent "Nathan M. Swan" <nathanmswan gmail.com> writes:
On Monday, 27 February 2012 at 09:13:24 UTC, Jonathan M Davis 
wrote:
 It would be far better to use immutable.

 - Jonathan M Davis

That doesn't work either, as it says: Error: non-constant expression ["import"d:cast(TokenType)2,"public"d:cast(TokenType)3,"protected"d:cast(TokenType)4,"private"d:cast(TokenType)5,"static"d:cast(TokenType)6]
Feb 27 2012
prev sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Monday, February 27, 2012 18:19:59 Nathan M. Swan wrote:
 On Monday, 27 February 2012 at 09:13:24 UTC, Jonathan M Davis
 
 wrote:
 It would be far better to use immutable.
 
 - Jonathan M Davis

That doesn't work either, as it says: Error: non-constant expression ["import"d:cast(TokenType)2,"public"d:cast(TokenType)3,"protected"d:cast(Tok enType)4,"private"d:cast(TokenType)5,"static"d:cast(TokenType)6]

You have to initialize the AA in a static constructor (and use a _shared_ static constructor, since the AA is immutable and therefore shared across threads). You can't initialize it directly, because you can't have memory which is used at runtime continue to compile time. The only type that can do that on any level is a dynamic array. That can't be done with anything more complicated yet (like an AA or a class). - Jonathan M Davis
Feb 27 2012