AA literals/initialisation
Jonathan M Davis
jmdavisProg at gmx.com
Mon Nov 11 21:30:59 PST 2013
On Tuesday, November 12, 2013 01:07:21 TheFlyingFiddle wrote:
> On Monday, 11 November 2013 at 12:14:10 UTC, Jonathan M Davis
>
> wrote:
> > Every time you use an enum, it's replaced with its value. So,
> > if an enum is an
> > AA, then that literal is copy-pasted everywhere that the enum
> > is used. So, it
> > would almost certainly be foolish to use it anywhere other than
> > to assign to a
> > variable (and then all of the operations are done on the
> > variable). Really,
> > having an enum that's an AA is almost certainly a foolish thing
> > to do. It's
> > one case where the behavior of enums doesn't help and
> > definitely hurts.
> >
> > - Jonathan M Davis
>
> I have found it to be kind of usefull atleast when i'm only using
> the AA at compiletime. Like a simple vector swizzle like the code
> below. I store rgba/xyzw characters in the table with diffrent
> offsets into a static array.
>
> Vector!(s.length, T) opDispatch(string s)()
> if(s.length > 1)
> {
> Vector!(s.length, T) res;
> foreach(i; staticIota!(0, s.length)) {
> enum offset = swizzleTable[s[i]];
> res.data[i] = data[offset];
> }
> return res;
> }
>
> However with your statement above i'm now a little worried does
> this mean that the line enum offset = swizzleTable[s[i]]; will
> not be able to pick the correct constant at compiletime? And will
> instead do some runtime AA hash lookup?
All enum's must be known at compile time. They are never calculated at
runtime. It's the _value_ of an enum that gets copy-pasted, not its text. So,
if you have
enum offset = swizzleTable[s[i]];
then the result will be calculated at compile time. Whether calculating it is
at all efficient is quite another matter, but the calculation will be done at
compile time.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list