digitalmars.D.learn - opCall() property
- Zhenya (13/13) Jun 29 2012 struct X
- Namespace (26/26) Jun 29 2012 This works:
- Zhenya (4/30) Jun 29 2012 I see, I just thought that opCall @ property equivalent opAssign
- Jonathan M Davis (26/29) Jun 29 2012 I don't see how you could think that it _would_ be. The _only_ way that ...
- Zhenya (2/47) Jun 29 2012 Thank you,I understood.
- Jonathan M Davis (25/40) Jun 29 2012 You're not actually using opCall anywhere. opCall as a property actually...
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?
Jun 29 2012
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); }
Jun 29 2012
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
Jun 29 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 notI 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
Jun 29 2012
On Friday, 29 June 2012 at 20:13:58 UTC, Jonathan M Davis wrote:On Friday, June 29, 2012 21:54:42 Zhenya wrote:Thank you,I understood.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 notI 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
Jun 29 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
Jun 29 2012