Static attributes & immutability, static attributes seen from instances

Michal Minich michal.minich at gmail.com
Sat Mar 6 03:33:02 PST 2010


On Fri, 05 Mar 2010 16:33:40 -0500, bearophile wrote:

> A bit later in the discussion div0 and Pelle M. have said/suggested that
> accessing static vars through an instance can be a bad thing, and it's
> better to allow the programmer to access them only through the
> class/struct name.

I think that calling static members through an instance should be 
disallowed. 

In my opinion this is serious misfeature. It causes semantic un-clearness 
on usage - As you shown with the example with immutable static member. 
Here is one with mutable member:

consider: struct S { static int s; int i }

and usage in some other module:
S s1, s2;
s1.s += 1;
s1.i += 1;

now s2.s == 1, s2.i == 0.

from usage is not apparent that 's' is a static member, in fact it seems 
it is instance member. This just makes program less clear. When one is 
presented with new code base and want to understand it, he must allays 
check if the member is not static - it should be apparent from usage and 
compiler should force it.

This would allow an additional feature: two "namespaces" in the class/
struct - one for static names and one for instance names, giving ability 
to "overload" members - i.e. have two opCall in struct - one for type and 
one for instance. But I cannot think of any example use case of this 
feature right now.



More information about the Digitalmars-d mailing list