Static operator overloads are possible why?

Manfred Nowak svv1999 at hotmail.com
Thu Jul 5 06:59:59 PDT 2007


Giles Bathgate wrote

> how to access an lvalue in a static operator overload.
operators declared `static' can only access entities that are actual 
parameters or declared `static' too. Therefore change

|    public char[] Name;

to

     public static char[] Name;

Or as a complete example:

public class Test
{
	static char[] baseName = "Test";
	char[] name;

	static void opAddAssign(Test value)
	{
		writefln(value.name);
		Test.baseName~= ".".dup;
		baseName~= value.name.dup;
	}
	static void opCall(){
		writefln( ":", baseName);
	}
} 

void main()
{
	Test b = new Test();
	b.name = "foo";

	Test += b;
	Test();

     Test t;
     t(); 
}
import std.stdio;

Or in other words:
for the entities of a class that are declared `static' the D-compiler 
automatically establishes a singleton and that singleton can be 
accessed 
1) by the name of the class or
2) every instance of the class

So:  your questions
> 'should static operator overloads even be allowed?' and if they
> were to be allowed should they not have two arguments?
have the answers yes and no respectively. 
-manfred  



More information about the Digitalmars-d mailing list