digitalmars.D - ? valid opAssign
- Thomas Kuehne (9/9) Nov 16 2004 Is the code below valid?
- Daniel Keep (9/21) Nov 16 2004 Ok, I could be wrong, BUT, I think that it's valid.
- Regan Heath (9/26) Nov 16 2004 Or, is the order reversed, i.e. assigns b to b, then a to b.
- Daniel Keep (5/41) Nov 17 2004 Hmm... my intuition tells me it's right to left. Assignment *should* be...
Is the code below valid? The reason I ask is that this code passes the compiler, but I can't find and documentation on it's behaviour. Thomas
Nov 16 2004
Ok, I could be wrong, BUT, I think that it's valid. The trick here is that (unless Walter changed it from C/C++), assignment can be used in an expression. Specifically, (a=b) first assigns the value of b to a, then returns a. So `return b=b=a;' is assigning a to b, b to itself, then returning the value of b. Maybe :P -- Daniel Thomas Kuehne wrote:Is the code below valid? The reason I ask is that this code passes the compiler, but I can't find and documentation on it's behaviour. Thomas
Nov 16 2004
On Tue, 16 Nov 2004 19:11:30 +1100, Daniel Keep <daniel.keep dummy.gmail.com> wrote:Ok, I could be wrong, BUT, I think that it's valid. The trick here is that (unless Walter changed it from C/C++), assignment can be used in an expression. Specifically, (a=b) first assigns the value of b to a, then returns a. So `return b=b=a;' is assigning a to b, b to itself, then returning the value of b.Or, is the order reversed, i.e. assigns b to b, then a to b. Of course in both cases the result is the same, b=a. The real question is, is the order defined? If so, what is it? ReganThomas Kuehne wrote:-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/The reason I ask is that this code passes the compiler, but I can't find and documentation on it's behaviour. Thomas
Nov 16 2004
Hmm... my intuition tells me it's right to left. Assignment *should* be right associative--it computes the right hand side first (thus, performing all the assignments right to left). But then again, IANALL Regan Heath wrote:On Tue, 16 Nov 2004 19:11:30 +1100, Daniel Keep <daniel.keep dummy.gmail.com> wrote:Ok, I could be wrong, BUT, I think that it's valid. The trick here is that (unless Walter changed it from C/C++), assignment can be used in an expression. Specifically, (a=b) first assigns the value of b to a, then returns a. So `return b=b=a;' is assigning a to b, b to itself, then returning the value of b.Or, is the order reversed, i.e. assigns b to b, then a to b. Of course in both cases the result is the same, b=a. The real question is, is the order defined? If so, what is it? ReganThomas Kuehne wrote:The reason I ask is that this code passes the compiler, but I can't find and documentation on it's behaviour. Thomas
Nov 17 2004