Methods in enums
H. S. Teoh
hsteoh at qfbox.info
Tue Aug 15 02:19:04 UTC 2023
On Tue, Aug 15, 2023 at 12:38:38AM +0000, Alexandru Ermicioi via Digitalmars-d wrote:
> On Monday, 14 August 2023 at 23:40:33 UTC, WebFreak001 wrote:
> > ...
> > They would of course need a bit of new syntax, like possibly
> > separating the enum values and methods with `;`, like what Java
> > does, or having a more D-like syntax that just allows methods
> > anywhere in the enum. I think the concept itself would fit into the
> > language design very nicely.
>
> You could use plain private struct as an enum type, then you'd have
> methods on enum members.
[...]
Proof of concept:
struct EnumMember {
int value;
void myMethod() {
import std.stdio;
writefln("myMethod: %s", value);
}
}
enum MyEnum : EnumMember {
hooray = EnumMember(1),
boo = EnumMember(2),
haha = EnumMember(3),
}
void main() {
MyEnum e;
e.myMethod();
}
T
--
The diminished 7th chord is the most flexible and fear-instilling chord. Use it often, use it unsparingly, to subdue your listeners into submission!
More information about the Digitalmars-d
mailing list