1. Re: we need official libs : Swap
Gabriel Boehme wrote about {a,b} = foo():
{a,a} = {1,2} -- error!
{a[7], a[7]} = {1,2} -- error!
{a[7][2], a[7]} = {1,2} -- NASTY error!
{a[x], a[y]} = {1,2} -- legal only if x != y
{a[x][z], a[y]} = {1,2} -- legal only if x != y
{a[7], a[9]} = {a[9], a[7]} -- legal, swaps the variables
{a[x], a[y]} = {a[y], a[x]} -- legal only if x != y
{a[x][z], a[y]} = {a[y], a[x][z]} -- legal, swaps the variables
Others have done a lot of hand-wringing about how this syntax will make
Euphoria complex, and have lots of side effects that need to be examined in
great detail. It's not clear to me that *any* assumptions are warranted - my
own included.
Having recently written a fairly complete implementation of Euphoria (in
Euphoria), I can state with authority that I have *no* idea how the Euphoria
interpreter actually works. I suspect that others (discounting Robert and
his silent partner Junko) are also only making guesses as to how the feature
could be implemented.
Since, from the interpreter's point of view, the expression:
{ x1, x2 .. xN } = <expr>
is essentially syntactic sugar, Robert *could* simply choose to implement
this feature by expanding the expression back to it's original form. It's a
fairly common trick of the trade. So the expression:
{ a, a } = { 1, 2 }
would convert into:
tmp = { 1, 2 }
a = tmp[1]
a = tmp[2]
Each of the other cases can also be expanded to their 'original' form, and
shown to be perfectly legal, in the sense that they wouldn't generate
errors. On the other hand,
{a,a,a} = {1,2}
would simply generate the error:
subscript value 3 out of bounds, reading from length-2 sequence
-- quick digression --
While I'm on the topic, a quick request. Could the above error message be
changed? Whenever I see this message, it looks like I have a sequence with a
negative length, something that confused me dreadfully when I was learning
Euphoria.
-- end digression --
For those that argue that it shouldn't be added because it duplicates an
existing functionality, I'll point out that all these are identical, too:
"hello there"
{ 'h', 'e', 'l', 'l', 'o', ' ', 't', 'h', 'e', 'r', 'e' }
{ 104, 101, 108, 108, 32, 116, 104, 101, 114, 101 }
-- David Cuny