www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - enums extension

reply "Comrad" <comrad.karlovich googlemail.com> writes:
Dear developers and community,
I want to draw you attention to the topic which was already 
discussed here:
http://forum.dlang.org/thread/9to6n8$12d6$1 digitaldaemon.com
and here:
http://forum.dlang.org/thread/bl1n0e$1km5$1 digitaldaemon.com

My question is: was something similar already done? are there 
some plans to do that?
May 15 2012
next sibling parent "Mehrdad" <wfunction hotmail.com> writes:
On Wednesday, 16 May 2012 at 04:22:18 UTC, Comrad wrote:
 Dear developers and community,
 I want to draw you attention to the topic which was already 
 discussed here:
 http://forum.dlang.org/thread/9to6n8$12d6$1 digitaldaemon.com
 and here:
 http://forum.dlang.org/thread/bl1n0e$1km5$1 digitaldaemon.com

 My question is: was something similar already done? are there 
 some plans to do that?

Dunno... but the way I'd use them would be more like: enum AccessMask { GENERIC_READ = 0x80000000, GENERIC_WRITE = 0x40000000, GENERIC_EXECUTE = 0x20000000, GENERIC_ALL = 0x10000000, //.... } enum FileAccessMask : AccessMask { FILE_READ_DATA = 0x0001, //... }
May 15 2012
prev sibling next sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Wed, 16 May 2012 06:22:15 +0200, Comrad  
<comrad.karlovich googlemail.com> wrote:

 Dear developers and community,
 I want to draw you attention to the topic which was already discussed  
 here:
 http://forum.dlang.org/thread/9to6n8$12d6$1 digitaldaemon.com
 and here:
 http://forum.dlang.org/thread/bl1n0e$1km5$1 digitaldaemon.com

 My question is: was something similar already done? are there some plans  
 to do that?

I have implemented this functionality in user code, with some caveats: module enumMagic; string EnumDefAsString(T)() if (is(T == enum)) { string result = ""; foreach (e; __traits(allMembers, T)) { result ~= e ~ " = T." ~ e ~ ","; } return result; } template ExtendEnum(T, string s) if (is(T == enum) && is(typeof({mixin("enum a{"~s~"}");}))) { mixin( "enum ExtendEnum {" ~ EnumDefAsString!T() ~ s ~ "}"); } unittest { enum Foo { a, b, c } alias ExtendEnum!( Foo, q{ d, e }) Bar; Bar b; b = Bar.a; // Look ma, I stole this from Foo! b = Bar.d; assert( Bar.a == Foo.a ); // Can compare the two. //b = Foo.a; // But cannot assign from one to the other. } void main( ) { } A library implementation with the missing features is possible, but would use a struct instead of an enum, an be a rather larger piece of code than this.
May 15 2012
prev sibling next sibling parent "Robert DaSilva" <spunit262 yahoo.com> writes:
On Wednesday, 16 May 2012 at 05:49:01 UTC, Mehrdad wrote:
 On Wednesday, 16 May 2012 at 04:22:18 UTC, Comrad wrote:
 Dear developers and community,
 I want to draw you attention to the topic which was already 
 discussed here:
 http://forum.dlang.org/thread/9to6n8$12d6$1 digitaldaemon.com
 and here:
 http://forum.dlang.org/thread/bl1n0e$1km5$1 digitaldaemon.com

 My question is: was something similar already done? are there 
 some plans to do that?

Dunno... but the way I'd use them would be more like: enum AccessMask { GENERIC_READ = 0x80000000, GENERIC_WRITE = 0x40000000, GENERIC_EXECUTE = 0x20000000, GENERIC_ALL = 0x10000000, //.... } enum FileAccessMask : AccessMask { FILE_READ_DATA = 0x0001, //... }

The inheritances notation implies that it's a subset and can be assigned to an instances of the base. This would not be true with enums. FileAccessMask foo = FileAccessMask.FILE_READ_DATA; AccessMask bar = foo; // Error
May 15 2012
prev sibling next sibling parent "Comrad" <comrad.karlovich googlemail.com> writes:
On Wednesday, 16 May 2012 at 05:54:45 UTC, Simen Kjaeraas wrote:
 On Wed, 16 May 2012 06:22:15 +0200, Comrad 
 <comrad.karlovich googlemail.com> wrote:

 Dear developers and community,
 I want to draw you attention to the topic which was already 
 discussed here:
 http://forum.dlang.org/thread/9to6n8$12d6$1 digitaldaemon.com
 and here:
 http://forum.dlang.org/thread/bl1n0e$1km5$1 digitaldaemon.com

 My question is: was something similar already done? are there 
 some plans to do that?

I have implemented this functionality in user code, with some caveats: module enumMagic; string EnumDefAsString(T)() if (is(T == enum)) { string result = ""; foreach (e; __traits(allMembers, T)) { result ~= e ~ " = T." ~ e ~ ","; } return result; } template ExtendEnum(T, string s) if (is(T == enum) && is(typeof({mixin("enum a{"~s~"}");}))) { mixin( "enum ExtendEnum {" ~ EnumDefAsString!T() ~ s ~ "}"); } unittest { enum Foo { a, b, c } alias ExtendEnum!( Foo, q{ d, e }) Bar; Bar b; b = Bar.a; // Look ma, I stole this from Foo! b = Bar.d; assert( Bar.a == Foo.a ); // Can compare the two. //b = Foo.a; // But cannot assign from one to the other. } void main( ) { } A library implementation with the missing features is possible, but would use a struct instead of an enum, an be a rather larger piece of code than this.

This is not really useful, because one can't pass Foo into a function if it's declared to take Bar...
May 17 2012
prev sibling next sibling parent "Mehrdad" <wfunction hotmail.com> writes:
On Wednesday, 16 May 2012 at 06:24:54 UTC, Robert DaSilva wrote:
 The inheritances notation implies that it's a subset and can be 
 assigned to an instances of the base. This would not be true 
 with enums.
 FileAccessMask foo = FileAccessMask.FILE_READ_DATA;
 AccessMask bar = foo; // Error

Actually, that^ *should* work in my case; that was my point...
May 17 2012
prev sibling parent "Mehrdad" <wfunction hotmail.com> writes:
On Thursday, 17 May 2012 at 18:27:07 UTC, Mehrdad wrote:
 On Wednesday, 16 May 2012 at 06:24:54 UTC, Robert DaSilva wrote:
 The inheritances notation implies that it's a subset and can 
 be assigned to an instances of the base. This would not be 
 true with enums.
 FileAccessMask foo = FileAccessMask.FILE_READ_DATA;
 AccessMask bar = foo; // Error

Actually, that^ *should* work in my case; that was my point...

Wait, no, scratch that, I misread it; you're right. It shouldn't work without a cast, but it should work with a cast.
May 17 2012