enum pointers or class references limitation

Q. Schroll via Digitalmars-d digitalmars-d at puremagic.com
Fri Sep 1 16:13:50 PDT 2017


On Friday, 1 September 2017 at 21:08:20 UTC, Ali Çehreli wrote:
> [snip]
> > assert(!([1,2,3] is [1,2,3]));
> >
> > Which is exactly what enum expands to and totally expected.
> Where is the
> > surprise?

This is not a surprise. Array literals are not identical.

> In the surprising case foo is a symbol, seemingly of a 
> variable. Failing the 'is' test is surprising in that case. 
> I've just remembered that the actual surprising case is the 
> following explicit check:
>
>     assert(!(foo.ptr is foo.ptr)); // Passes
>
> I find it surprising because it looks like an entity does not 
> have a well-behaving .ptr.
>
> (Aside: I think your code might be surprising to at least 
> newcomers as well.)

That's a good reason to unrecommend/disallow enums with 
indirections. The compiler should recommend/suggest using static 
immutable instead as it does not have such oddities. The only 
advantage of enum is being guaranteed to be known at compile-time 
and they can be templatized (can be also done for static 
immutable via eponymous template).
I'd vote for a warning/error when the type of an enum has 
indirections together with a pragma to switch the warning off for 
the rare case you know exactly what you do.
Just as Scott Meyers said: make it easy to use correctly and hard 
to use incorrectly. Today it's easy to use incorrectly.



More information about the Digitalmars-d mailing list