opCall() @property

Jonathan M Davis jmdavisProg at gmx.com
Fri Jun 29 13:12:58 PDT 2012


On Friday, June 29, 2012 21:54:42 Zhenya wrote:
> I see, I just thought that opCall @ property equivalent opAssign
> and wanted to check it out, and now I would be interested to
> understand why it is not

I don't see how you could think that it _would_ be. The _only_ way that opCall 
can be invoked is by using the variable as if it were a function.

struct X
{
 bool _x;
 A opCall(bool x) {_x = x;return this;}
}

x(false);

Without those parens, the compiler has no idea that you're trying to use 
opCall. opCall is specifically for being able to call a variable as if it were 
a function. By using =, you're making the compiler look for opAssign

x = false;

because that's the function for overloading =. You're only going to be able to 
make a function a property when it would be used as a function if it wasn't 
declared as a property, and neither opCall or opAssign is used as a function 
(e.g x.opCall(), x.opAssign()). They're both overloading operators. @property 
is specifically for making a function act as if it were a variable, and 
overloaded operators aren't used as either functions or variables. They 
overload _operators_.

Off the top of my head, the _only_ overloaded operator that I can think of 
where it would make any sense to declare it @property would be opDispatch, 
because it's replacing function calls, but there are problems with that, 
because you can't have two opDispatches, so you can't use it for both 
properties and normal functions.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list