digitalmars.D.learn - What is the state of opAssign?
- BCS (5/5) Oct 19 2007 Is it or will it be possible, in v1.0 or v2.0, to define overloads sutch...
- Jarrett Billingsley (3/8) Oct 19 2007 It's called opImplicitCast, and yes, it will be in D2.
- BCS (13/27) Oct 19 2007 Hmmm. I was hoping for something along the lines of opAssing_r because I...
- Nathan Reed (5/20) Oct 20 2007 I'd be very suspicious of using the return value of a function as an lva...
- BCS (19/40) Oct 20 2007 Depends on what I'm doing with it. In the simple case, the above would w...
- Nathan Reed (9/54) Oct 20 2007 Frankly, I think this is a very bad idea. With the usual semantics of
- BCS (5/14) Oct 21 2007 Point taken. However In the case I'm using, the semantics would be as ex...
Is it or will it be possible, in v1.0 or v2.0, to define overloads sutch that this works? class C{} auto c = new C; real r = c;
Oct 19 2007
"BCS" <BCS pathlink.com> wrote in message news:ffbf1l$cbo$1 digitalmars.com...Is it or will it be possible, in v1.0 or v2.0, to define overloads sutch that this works? class C{} auto c = new C; real r = c;It's called opImplicitCast, and yes, it will be in D2.
Oct 19 2007
Reply to Jarrett,"BCS" <BCS pathlink.com> wrote in message news:ffbf1l$cbo$1 digitalmars.com...Hmmm. I was hoping for something along the lines of opAssing_r because I'm hoping to use the original value of the lvalue for something. would this work class C { static C opImplicitCastFrom(real r){...} C opAssign(B b){...} } class B {} auto b = new B; real r = 5; r = b; //opImplicitCastFrom(r).opAssign(b);Is it or will it be possible, in v1.0 or v2.0, to define overloads sutch that this works? class C{} auto c = new C; real r = c;It's called opImplicitCast, and yes, it will be in D2.
Oct 19 2007
BCS wrote:would this work class C { static C opImplicitCastFrom(real r){...} C opAssign(B b){...} } class B {} auto b = new B; real r = 5; r = b; //opImplicitCastFrom(r).opAssign(b);I'd be very suspicious of using the return value of a function as an lvalue! What is the value in 'r' after this code has executed?? Thanks, Nathan Reed
Oct 20 2007
Reply to Nathan,BCS wrote:Depends on what I'm doing with it. In the simple case, the above would work as expected but would be able to also do something with r. struct C { real* rr; static C opImplicitCastFrom(ref real r) { C ret; ret.rr = & r; return ret; } C opAssign(B b) { //... uses rr //*rr = cast(real) b; } } class B {}would this work class C { static C opImplicitCastFrom(real r){...} C opAssign(B b){...} } class B {} auto b = new B; real r = 5; r = b; //opImplicitCastFrom(r).opAssign(b);I'd be very suspicious of using the return value of a function as an lvalue! What is the value in 'r' after this code has executed?? Thanks, Nathan Reed
Oct 20 2007
BCS wrote:Reply to Nathan,Frankly, I think this is a very bad idea. With the usual semantics of assignment are it *doesn't* depend on the current value of the lhs. Overloading an operator such that its semantics violates the assumptions programmers normally make about it is never good imho. This is why D doesn't allow commutative operators to be overloaded as non-commutative, etc... Thanks, Nathan ReedBCS wrote:Depends on what I'm doing with it. In the simple case, the above would work as expected but would be able to also do something with r. struct C { real* rr; static C opImplicitCastFrom(ref real r) { C ret; ret.rr = & r; return ret; } C opAssign(B b) { //... uses rr //*rr = cast(real) b; } } class B {}would this work class C { static C opImplicitCastFrom(real r){...} C opAssign(B b){...} } class B {} auto b = new B; real r = 5; r = b; //opImplicitCastFrom(r).opAssign(b);I'd be very suspicious of using the return value of a function as an lvalue! What is the value in 'r' after this code has executed?? Thanks, Nathan Reed
Oct 20 2007
Reply to Nathan,Frankly, I think this is a very bad idea. With the usual semantics of assignment are it *doesn't* depend on the current value of the lhs. Overloading an operator such that its semantics violates the assumptions programmers normally make about it is never good imho. This is why D doesn't allow commutative operators to be overloaded as non-commutative, etc... Thanks, Nathan ReedPoint taken. However In the case I'm using, the semantics would be as expected because I'd be modeling a different semantic system. OTOH, I think I might be able to avoid that issue by defining the assignment from the left side (I might be forced to anyway by other constrains).
Oct 21 2007