two questions on enums
Eric
eric at makechip.com
Thu Apr 3 16:16:13 PDT 2014
On Thursday, 3 April 2014 at 22:34:19 UTC, bearophile wrote:
> Eric:
>
>> I disagree. If you just think of a class type enum as a class
>> type,
>> then what you say makes sense. But if you instead think of it
>> as a more powerful enum then it can enhance data safety in a
>> program.
>
> I was speaking about the current D enum, as implemented and as
> designed. Regarding your enhancements, I need some examples or
> a better explanation to understand your point.
>
Okay - I'm new to D, and I'm comming from a java background.
Suppose
you are designing an API, and you want the user to supply
arguments
as an enum. But the user needs to define the enum, so how can
the API
know in advance what enum to require? The solution is to require
an
enum that implements a known interface. This is easy to do in
java,
but I haven't yet tried it in D. I suspect it could be done with
CTFE
or something. An example where I use this is for electronics
software.
If I need the user to supply a set of pins, the pins are supplied
as an
enum which implements an interface. By using the interface, it
also
forces the user to include all of the attributes of each pin such
as
direction, max load, DC current, etc. Since class type enums are
references,
they are light, - and they should be immutable - so they are
thread safe aslo.
I hope this example illustrates where I'm comming from.
-Eric
More information about the Digitalmars-d-learn
mailing list