foreach over enums?
%u
e at ee.com
Thu Sep 23 16:45:32 PDT 2010
== Quote from Simen kjaeraas (simen.kjaras at gmail.com)'s article
> %u <e at ee.com> wrote:
> > First question, shouldn't the first foreach be replaced by a simple for
> > loop:
> > for(int i = 0; i<num; i++ ) {
> It certainly could, and that would explain the presence of 'num', but
> it's not necessary, and should make no difference in the generated
> executable.
> > If I understand it correctly the foreach aggregates are actually
> > string-tuple
> > literals.
> Yes.
> > Then couldn't the second one be CT translated to char[][]?
> > That way the stringOf would be a simple array index.
> Indeed it could, though I will leave its implementation as an
> exercise for the reader :p. I also thought up a Duff's
> device-inspired contraption that works:
> string toString( ) {
> switch ( value ) {
> foreach ( i, e; T ) {
> case i:
> return T[i];
> }
> }
> }
> This should be as fast as the char[][].
I thought that would be more annoying to implement(string mixins etc).. didn't
know it could be that nice :)
> > And lastly, I probably need to make the whole struct a string mixin if I
> > want it's
> > type to be like an enum.. don't I? :'(
> I'm not sure what you're getting at here. In what way that you don't like
> it is it enum-unlike?
These two have distinctly different outputs ;P
alias defineEnum!( "A", "B", "C" ) Bar;
writefln( typeof(Bar.A).stringof );
enum Foo { A, B, C }
writefln( typeof(Foo.A).stringof );
Won't the compiler even choke on the type size when feeding defineEnum a hundred
elements or so?
More information about the Digitalmars-d-learn
mailing list