Static attributes & immutability, static attributes seen from instances

Adam D. Ruppe destructionator at gmail.com
Sat Mar 6 17:36:43 PST 2010


On Sun, Mar 07, 2010 at 12:36:11AM +0000, Michal Minich wrote:
> I never used opAssign, but doesn't it solves this problem? Considering 
> that both opCall and opAssign work with "=" their interaction must be 
> probably quite complex...

opAssign only worked after the struct has been initialized.

===
import std.stdio;

struct S {
/*
	static S opCall(int a) {
		S tmp;
		writefln("opCall(%d);", a);
		return tmp;
	}
*/
	S* opAssign(int a) {
		writefln("opAssign(%d);", a);
		return &this;
	}
}

void main() {
	S a = 10; // Error: cannot implicitly convert expression (10) of type int to S
	a = 20;
	a(30);
}
===

Uncomment the opCall, and it compiles, outputting:
opCall(10);
opAssign(20);
opCall(30);


If you add a second opCall that is not static, but otherwise identical,
then it fails to compile either the first nor third lines, because:

c.d(19): Error: function c.S.opCall called with argument types:
        ((int))
matches both:
        c.S.opCall(int a)
and:
        c.S.opCall(int a)
c.d(21): Error: function c.S.opCall called with argument types:
        ((int))
matches both:
        c.S.opCall(int a)
and:
        c.S.opCall(int a)


(one being the static opCall, and the other of course being the instance one).


Pretty annoying. This is the #1 thing that got in my way of creating a
JavascriptObject struct right after opDispatch came out - emulating a
delegate and accepting assignment at declaration were mutually incompatible.

-- 
Adam D. Ruppe
http://arsdnet.net



More information about the Digitalmars-d mailing list