Question: value of multi-statement expression?
Posted: 03 Oct 2016, 11:16
Lexikos, as v2 development continues, can I rely on the behavior that a multi-expression in a return statement will continue to be allowed (although I know it doesn't return multiple values) and will always return the final sub-expression as the return value? I know it might be bad form, but in short utility functions, sometimes it's nice/quick to set the byrefs on a single line on the way out...
For example, in the following code, can I rely on the fact that answer will always be assigned false?
I wasn't sure because I didn't see that this specific behavior was guaranteed in the documentation. Maybe I didn't look hard enough, but I don't think I read it in the "Return" or "Operators in Expressions" or "Returning Values to Caller" help sections. Thanks again for the assistance!
EDIT: To try to answer my own question, it occurred to me to try the more general case: answer := (x:=1, y:=2), which verified my assumption that answer is in fact assigned 2. I think it works because of the left-to-right evaluation of the multi-statement, such that a multi-statement expression, when used as part of a larger expression, evaluates to the value of its last sub-expression. I might have missed it somewhere, but I didn't see this general principle specifically documented anywhere, so now I've got myself wondering if that behavior also can be relied upon as v2 progresses?
For example, in the following code, can I rely on the fact that answer will always be assigned false?
Code: Select all
example(byref x, byref y, byref z) {
return x:=1, y:=2, z:=3, false
}
answer := example(x,y,z) ; answer is 0/false
MsgBox % x '`n' y '`n' z '`n' answer
EDIT: To try to answer my own question, it occurred to me to try the more general case: answer := (x:=1, y:=2), which verified my assumption that answer is in fact assigned 2. I think it works because of the left-to-right evaluation of the multi-statement, such that a multi-statement expression, when used as part of a larger expression, evaluates to the value of its last sub-expression. I might have missed it somewhere, but I didn't see this general principle specifically documented anywhere, so now I've got myself wondering if that behavior also can be relied upon as v2 progresses?