opCall() @property

Jonathan M Davis jmdavisProg at gmx.com
Fri Jun 29 13:05:00 PDT 2012


On Friday, June 29, 2012 21:08:05 Zhenya wrote:
> struct X
> {
> bool _x;
> A opCall(bool x) @property {_x = x;return this;}
> }
> 
> void main()
> {
> X a;
> x = false;//the same that x.opCall(false)?
> }
> 
> I thought that I could replace these opAssign, but the compiler
> does not agree with me.
> But why?

You're not actually using opCall anywhere. opCall as a property actually makes 
no sense, since the _only_ way that it's triggered is with parens. When 
compiling with -property, your opCall is probably uncallable except by calling 
it explicitly (e.g. x.opCall = false).

You could overload opAssign to do what you're trying to do, or you could use 
alias this.

struct X
{
 bool _x;

 X opAssign(bool value)
 {
 _x = value;
 return this;
 }
}

or

struct X
{
 bool _x;

 alias _x this;
}

If all you want is assignment though, then just overload opAssign, since alias 
enables a number of implicit conversions.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list