two questions on enums
Chris Williams
yoreanon-chrisw at yahoo.co.jp
Fri Apr 4 18:28:06 PDT 2014
On Thursday, 3 April 2014 at 23:16:14 UTC, Eric wrote:
> 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'm not sure how you're using your enum. In Java, the only way to
perform a switch over an enum is if you know all the values
during compile time. Passing a type Enum into a function (i.e. as
opposed to "enum MyEnumType') requires using introspection to
pull the actual values out, and then a loop to scan through them
for your target value.
But if you've programmed a switch for a known enum type, then
that means the enum has already been implemented and there is no
chance for more than one type to be passed in.
So I'm confused.
More information about the Digitalmars-d-learn
mailing list