Re: opCall() @property

2012-06-29 Thread Zhenya

On Friday, 29 June 2012 at 20:13:58 UTC, Jonathan M Davis wrote:

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

Thank you,I understood.



Re: opCall() @property

2012-06-29 Thread Jonathan M Davis
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


Re: opCall() @property

2012-06-29 Thread Jonathan M Davis
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


Re: opCall() @property

2012-06-29 Thread Zhenya

On Friday, 29 June 2012 at 19:37:50 UTC, Namespace wrote:

This works:

import std.stdio;

struct X {
private:
bool _x;

public:
this(bool x) {
_x = x;
}

@property
bool Get() inout {
return this._x;
}

alias Get this;

typeof(this) opAssign(bool x) {
this._x = x;

return this;
}
}

void main()
{
X a = false;
writeln(a);
a = true;
writeln(a);
}


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


Re: opCall() @property

2012-06-29 Thread Namespace

This works:

import std.stdio;

struct X {
private:
bool _x;

public:
this(bool x) {
_x = x;
}

@property
bool Get() inout {
return this._x;
}

alias Get this;

typeof(this) opAssign(bool x) {
this._x = x;

return this;
}
}

void main()
{
X a = false;
writeln(a);
a = true;
writeln(a);
}



opCall() @property

2012-06-29 Thread Zhenya

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?