www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Allow static methods and fields for enum?

reply Profile Anaysis <PA gotacha.com> writes:
Why not make enum a comparable type to structs and classes?

They are static so they can't contain any mutable fields but 
surely they can contain methods? And especially they should be 
able to contain static methods!?
Jan 25 2017
next sibling parent reply drug <drug2004 bk.ru> writes:
26.01.2017 09:32, Profile Anaysis пишет:
 Why not make enum a comparable type to structs and classes?

 They are static so they can't contain any mutable fields but surely they
 can contain methods? And especially they should be able to contain
 static methods!?
What prevents you from using UFCS with enum members or templates with enum itself? (not tested)
Jan 25 2017
parent pineapple <meapineapple gmail.com> writes:
On Thursday, 26 January 2017 at 06:40:59 UTC, drug wrote:
 26.01.2017 09:32, Profile Anaysis пишет:
 Why not make enum a comparable type to structs and classes?

 They are static so they can't contain any mutable fields but 
 surely they
 can contain methods? And especially they should be able to 
 contain
 static methods!?
What prevents you from using UFCS with enum members or templates with enum itself? (not tested)
This is one solution, but it would still be useful as an organizational tool to be able to put those methods in the enum's own namespace.
Jan 26 2017
prev sibling next sibling parent Jonathan M Davis via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Thursday, January 26, 2017 06:32:10 Profile Anaysis via Digitalmars-d 
wrote:
 Why not make enum a comparable type to structs and classes?

 They are static so they can't contain any mutable fields but
 surely they can contain methods? And especially they should be
 able to contain static methods!?
If you want an enum type with methods, use a struct type that has methods. Enums don't have to be integral types. - Jonathan M Davis
Jan 26 2017
prev sibling parent Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d writes:
V Thu, 26 Jan 2017 06:32:10 +0000
Profile Anaysis via Digitalmars-d <digitalmars-d puremagic.com> napsáno:

 Why not make enum a comparable type to structs and classes?
 
 They are static so they can't contain any mutable fields but 
 surely they can contain methods? And especially they should be 
 able to contain static methods!?
 
struct EnumWithMethods { disable this(); static enum Senum { one, two, three, } alias Senum this; static getOne() { return this.one; } } void main() { import std.stdio; auto x = EnumWithMethods.one; auto y = EnumWithMethods.getOne(); writeln(x); writeln(y); }
Jan 26 2017