Re: Multi-assign
- Posted by mattlewis (admin) Dec 05, 2011
- 1660 views
integer a, b, c {a,b,c} = f(x) -- 1
A function (presumably) returns one value (sequence or atom).
If you are able to recover a, b & c then the {} are not acting as sequence forming operators in (1). (Otherwise: there would be a complaint about them being used before being initialised.)
The assumption is that the function will return values appropiate to the types of the LHS. What should those types be? The library functions often return different types (eg gets() returns -1 or a sequence).
Yes, this is a general issue, and so you should only use multi-assignment where applicable. In this aspect, it's no different than returning to an object and testing the type. The multi-assignment method will hopefully encourage using a specific element to declare success like value() does, since it's easier to detect invalid values.
{a,?,c} cannot be a sequence a slice [1..2] - what is its value?, its length?
Yes, that's an error. Don't do that. But if you do, euphoria will tell you about it.
f(x, ?, y): by the reasoning of {a, ?, c} the ? means 'disregard this value' not 'use the default'.
Disregard this value makes no sense applied to a function parameter.
I disagree. It's telling the function to disregard what's being passed there and use the default.
Confusion between {a,b,c} = {d,e,f} as L-value <- R-value and an expression (pure R-value).
{a,b,c} = {1,2,3} -- either a:=1, b:=2, c:=3 or {a=1,b=2,c=3} {1,2,3} = {a,b,c} -- either an error or {a=1,b=2,c=3}
The problem is not that you can't make this work. The problem is that syntactically they are identical. Therefore you cannot distinguish between an erroneous expression and a (correct) assignment and vice versa.
No, syntactically, they are not identical, any more than their atomic equivalents are.
Instead of using {a,b,c} why not use something like < a, b,c > or | a, b, c | for multi-assign? That way it is clear this is not a sequence, but a list of some kind.
I like the sequence notation, since it's a clear indicator that you're expecting a sequence with at least a certain number of elements. It's true that euphoria's assignment operator is the same as its equality operator, but I don't think this causes as much confusion as you imply.
I guess you could write some obfuscated code (I think PeteL wrote some of the best of this genre) where you write statements deceptively across lines, but I don't believe that the syntax is ambiguous itself.
Matt