1. Multi-assign
- Posted by mattlewis (admin) Nov 15, 2011
- 2761 views
For 4.1, I started working on multiple assignment. Basically, it would look like this:
{ x, y } = { 1, "foo" } { $, x } = value( y )
It's really syntactic sugar, but can make some code a lot easier to read. My implementation currently uses dollar signs to ignore values. There's been some concern on the dev list that this might be confusing, so I'd like to get feedback from the wider community.
Does a dollar make sense? Should we use a different symbol? Should we just put extra commas in?
For those interested, the code is currently in the 'multi-assign' branch in the Mercurial repo.
Matt
2. Re: Multi-assign
- Posted by BRyan Nov 15, 2011
- 2723 views
For 4.1, I started working on multiple assignment. Basically, it would look like this:
{ x, y } = { 1, "foo" } { $, x } = value( y )
It's really syntactic sugar, but can make some code a lot easier to read. My implementation currently uses dollar signs to ignore values. There's been some concern on the dev list that this might be confusing, so I'd like to get feedback from the wider community.
Does a dollar make sense? Should we use a different symbol? Should we just put extra commas in?
For those interested, the code is currently in the 'multi-assign' branch in the Mercurial repo.
Matt
I don't really understand what multiple assignment is; but if you use a dollar sign in a sequence could it be confused with the last position of a sequence for example: seq[1..$]
4. Re: Multi-assign
- Posted by mattlewis (admin) Nov 15, 2011
- 2682 views
I don't really understand what multiple assignment is; but if you use a dollar sign in a sequence could it be confused with the last position of a sequence for example: seq[1..$]
Multiple assignment is a way to take the result of an expression that is a sequence, probably most commonly the return value of a sequence, and assign it in one go, without having to use a temporary variable. So:
-- instead of: x = value( something ) my_number = x[2] -- you could do: { $, my_number } = value( something )
The confusion was the argument brought up, but I'm honestly not sure how one would confuse this. Also, $ can already be used as a list terminator:
constant A = 1, B = 2, $ sequence s = { 1, 2, 3, $ }
So, it's basically a place holder. Personally, I like it better than something like an underscore or a dash or a dot because as a glyph, it's fairly substantial and easy to spot and differentiate from other things.
Matt
5. Re: Multi-assign
- Posted by wynco Nov 15, 2011
- 2688 views
The $ sign might be confusing to newer programers, but I don't really see a problem after getting used to it. The underscore would make sence progamatically, but as BRyan said it would not be very visible. How about the '~', in older programs that could not read long file names they were shortened with the tilde to indicate 'missing' chars. Would that make sence? to indicate missing variables. Just a thought.
6. Re: Multi-assign
- Posted by mattlewis (admin) Nov 16, 2011
- 2618 views
For 4.1, I started working on multiple assignment. Basically, it would look like this:
I've documenting what I've done. It currently looks like this:
Multiple Assignment
Special sequence notation on the left hand side of an assignment can be made to assign to multiple variables with a single statement. This can be useful for using functions that return multiple values in a sequence, such as value.
atom success, val { success, val } = value( "100" ) -- success = GET_SUCCESS -- val = 100
It is also possible to ignore some of the values in the right hand side. Any elements beyond the number supplied on the left hand side are ignored. Other values can also be ignored by using a dollar sign ('$') instead of a variable name:
{ $, val } = value( "100" )
Variables may only appear once on the left hand side, however, they may appear on both the left and right hand side. For instance, to swap the values of two variables:
{ a, b } = { b, a }
7. Re: Multi-assign
- Posted by _tom (admin) Nov 16, 2011
- 2619 views
This is how I understand multiple-assign so far...
Multiple assignment:
demo | Newway | Oldway |
---|---|---|
swap |
{a,b} = {b,a} |
object temp temp = a a = b b = temp |
mutiple return |
{ myfirst_number, mysecond_number} = value(something) |
object temp = value( something ) ? temp -- { 100, 200 } myfirst_number = temp[1] mysecond_number = temp[2] |
Potential multiple return syntax when used with an omitted variable:
Symbol | Sample | Thoughts | ||
---|---|---|---|---|
1 | 'none' |
{ , mysecond_number} = value(something) |
too bare, easy to miss, bad for the eyes | |
2 | $ |
{ $ , mysecond_number} = value(something) |
symbol in use as end marker | |
3 | # |
{ # , mysecond_number} = value(something) |
suggests 'comment' hence blank, bad used as #FF hex number | |
4 | ! |
{ ! , mysecond_number} = value(something) |
suggests 'not' as in `!=` hence blank | |
5 | ~ |
{ ~ , mysecond_number} = value(something) |
used in DOS as 'missing' |
Of these choices the $ symbol is the worst choice because:
Prior Use | Demo | |
---|---|---|
1 | sequence end marker -- length |
seq[ 1 .. $ ] |
2 | enum end marker |
enum A, B, C, $ |
3 | list end marker |
sequence s = { 1, 2, 3, $ } |
Clearly, $ means "end marker" in Euphoria. Using $ for other purposes is OK, but using $ for "other purposes" within a sequence is very bad--an entrenched pattern is being broken.
Any marker, other than $, makes more sense.
Consider extending the marker idea to "missing arguments" in a routine:
foo( 1, 2,, 4) becomes foo(1,2,#,4)
since "invisible" syntax is hard to use.
_tom
8. Re: Multi-assign
- Posted by jeremy (admin) Nov 16, 2011
- 2658 views
Does a dollar make sense? Should we use a different symbol? Should we just put extra commas in?
I dislike extra commas. They are hard to read, easy to get wrong and can make you look for hours for bad code. What about a - ?
{ -, last } = parse_name("John Doe")
I know extra commas are used for parameters but I'd like to see that be deprecated and whatever we decide here be used there as well.
Jeremy
9. Re: Multi-assign
- Posted by mattlewis (admin) Nov 16, 2011
- 2574 views
This is how I understand multiple-assign so far...
Yes.
Of these choices the $ symbol is the worst choice because:
...
Clearly, $ means "end marker" in Euphoria. Using $ for other purposes is OK, but using $ for "other purposes" within a sequence is very bad--an entrenched pattern is being broken.
Any marker, other than $, makes more sense.
Well, I still disagree. There are currently two uses of $: length shortcut in subscripting, and list terminator. I see adding it here as similar to its list termination duties, and like it in part because it's already used that way.
But it looks like I may be out-voted. Of the other options, I think a tilde '~' is the next best choice.
Matt
10. Re: Multi-assign
- Posted by jimcbrown (admin) Nov 16, 2011
- 2646 views
Any marker, other than $, makes more sense.
Consider extending the marker idea to "missing arguments" in a routine:
foo( 1, 2,, 4) becomes foo(1,2,#,4)
since "invisible" syntax is hard to use.
_tom
I think that using the hash is a bad idea, as it's (imvho) more prone to being the result of a typo than the dollar sign would be.
What if the person meant <eucode> foo( 1, 2, #A, 4) </eucode> but missed the 'a'?
Of course, for a LHS of a multi-assign that's not an issue, since we can't have literal values (such as hexadecimals) in there anyways.
11. Re: Multi-assign
- Posted by euphoric (admin) Nov 16, 2011
- 2594 views
$ is fine by me (or the underscore) to serve as a placemarker meaning "ignore me."
What about the case where there are not enough RHS items to complete LHS requests? Or vice versa?
{ a } = { 1, 2, 3 } { a, b, c } = { 1 }
12. Re: Multi-assign
- Posted by Insolor Nov 16, 2011
- 2612 views
I vote for underscore. In some languages there is the anonymous _ variable, which is used when the value doesn't matter.
13. Re: Multi-assign
- Posted by mattlewis (admin) Nov 16, 2011
- 2615 views
$ is fine by me (or the underscore) to serve as a placemarker meaning "ignore me."
I vote for underscore. In some languages there is the anonymous _ variable, which is used when the value doesn't matter.
In euphoria v4, an underscore, by itself is actually a valid variable name.
What about the case where there are not enough RHS items to complete LHS requests? Or vice versa?
{ a } = { 1, 2, 3 } { a, b, c } = { 1 }
In the first case, 1 is assigned to a, and the rest of the sequence is ignored. In the second case, you get a subscript error.
Matt
14. Re: Multi-assign
- Posted by jimcbrown (admin) Nov 16, 2011
- 2531 views
In euphoria v4, an underscore, by itself is actually a valid variable name.
Which doesn't make it all that different from other keywords that we have added (like 'label' or 'loop') that were previously common variable names.
Even in 4.0 code, I doubt the underscore is a popular variable/routine name. In fact, I wonder if it has been used at all this way by any code.
15. Re: Multi-assign
- Posted by mattlewis (admin) Nov 16, 2011
- 2544 views
In euphoria v4, an underscore, by itself is actually a valid variable name.
Which doesn't make it all that different from other keywords that we have added (like 'label' or 'loop') that were previously common variable names.
Even in 4.0 code, I doubt the underscore is a popular variable/routine name. In fact, I wonder if it has been used at all this way by any code.
It has been used at least in some of the euphoria unit tests. Specifically, t_net_dns.e and and t_socket.e.
Matt
16. Re: Multi-assign
- Posted by jimcbrown (admin) Nov 16, 2011
- 2551 views
It has been used at least in some of the euphoria unit tests. Specifically, t_net_dns.e and and t_socket.e.
Matt
That shouldbe fairly easy to fix up if necessary.
17. Re: Multi-assign
- Posted by mattlewis (admin) Nov 16, 2011
- 2547 views
It has been used at least in some of the euphoria unit tests. Specifically, t_net_dns.e and and t_socket.e.
Matt
That shouldbe fairly easy to fix up if necessary.
Ugh. I'd rather not. I think we'd have to add some special handling in the scanner. I think it's more useful to leave it the way it is.
Matt
18. Re: Multi-assign
- Posted by jimcbrown (admin) Nov 16, 2011
- 2534 views
It has been used at least in some of the euphoria unit tests. Specifically, t_net_dns.e and and t_socket.e.
Matt
That shouldbe fairly easy to fix up if necessary.
Ugh. I'd rather not. I think we'd have to add some special handling in the scanner. I think it's more useful to leave it the way it is.
Matt
Wait, what? Why?
19. Re: Multi-assign
- Posted by mattlewis (admin) Nov 16, 2011
- 2581 views
It has been used at least in some of the euphoria unit tests. Specifically, t_net_dns.e and and t_socket.e.
Matt
That shouldbe fairly easy to fix up if necessary.
Ugh. I'd rather not. I think we'd have to add some special handling in the scanner. I think it's more useful to leave it the way it is.
Matt
Wait, what? Why?
I'm going from memory, but basically, an underscore is a valid symbol character. We used to say that symbols couldn't start with an underscore, but that restriction was removed at some point during 4.0 development. The naked underscore name is just a variable that starts with an underscore, but has no more characters. Maybe not a big deal, and I'm blowing it out of proportion.
I still like the $.
Matt
20. Re: Multi-assign
- Posted by jimcbrown (admin) Nov 16, 2011
- 2594 views
I'm going from memory, but basically, an underscore is a valid symbol character. We used to say that symbols couldn't start with an underscore, but that restriction was removed at some point during 4.0 development. The naked underscore name is just a variable that starts with an underscore, but has no more characters. Maybe not a big deal, and I'm blowing it out of proportion.
We could make it a one-character keyword (or other sort of reserved word). I don't think that using the underscore for this feature is very complicated from a technical perspective.
In any case, the aim should be to do what's easiest for users, not what's easiest to implement. Unless the effort to implement this is unreasonable, I wouldn't discount it (at least not for technical reasons).
Another idea would be to use a new keyword, like 'empty' or 'blank' instead of a special symbol (like the dollar sign or the hash character).
Hmm.
Nah, I'm totally against that.
21. Re: Multi-assign
- Posted by euphoric (admin) Nov 16, 2011
- 2608 views
Just use $.
Quick. Easy. Solved. Next issue!
22. Re: Multi-assign
- Posted by jeremy (admin) Nov 16, 2011
- 2552 views
I still like -,
{ -, lastname } = parse_name("John Doe")
Sure, it is used in math but given the context it's pretty easy to determine it's not subtracting any numbers. Also consider my previous statement of making this newly chosen character (whatever it is) valid for function calls instead of just an extra comma:
some_func("John", -, "Doe") -- vs some_func("John", $, "Doe")
Besides, $ requires a shift, - doesn't, that's reason enough
Jeremy
23. Re: Multi-assign
- Posted by euphoric (admin) Nov 16, 2011
- 2568 views
Besides, $ requires a shift, - doesn't, that's reason enough
Yeah, I was looking for a shiftless character. Then you came around. No, wait... What I meant was...
The minus sign might work, but the dollar sign is more conspicuous- more full-bodied.
24. Re: Multi-assign
- Posted by DerekParnell (admin) Nov 16, 2011
- 2580 views
I'd prefer not to use '$' as it is already loaded with two meanings and to add a third is probably asking for future problems.
I think the dash '-' is fine for the LHS but not so good on the RHS. It could be a typo on the RHS where someone meant to key negative number as an parameter.
A new keyword is a really bad idea.
The preference for a shift-less character is not so important as the feature would not be a commonly used one. Also, keyboards vary as to which characters are shifted. For example, the tilde '~' is very awkward on some keyboards.
I think we need to have a glyph of some sort, and a visually prominent one would be preferable. We should try to avoid characters that are potentially typos.
So I think we are left with the single characters ... "@%^&*?" , and maybe combinations like "[]".
{ A, @, B } = SomeFunc(1, @, 2) { A, %, B } = SomeFunc(1, %, 2) { A, ^, B } = SomeFunc(1, ^, 2) { A, &, B } = SomeFunc(1, &, 2) { A, *, B } = SomeFunc(1, *, 2) { A, ?, B } = SomeFunc(1, ?, 2) { A, [], B } = SomeFunc(1, [], 2)
25. Re: Multi-assign
- Posted by jimcbrown (admin) Nov 16, 2011
- 2615 views
A new keyword is a really bad idea.
I agree with you, but I am curious as to why you think that.
I just thought using a keyword would be too confusing, as it'd be too easy to think it was a variable name or something.
The preference for a shift-less character is not so important as the feature would not be a commonly used one.
We use shifted characters all the time for more commonly used expressions, like and'ing two sequences together.
I think we need to have a glyph of some sort, and a visually prominent one would be preferable. We should try to avoid characters that are potentially typos.
Agreed. This arguably takes the underscore out of the equation as well (as it could be a typo on either side - variable named "_math" turned into "_,math" or something).
So I think we are left with the single characters ... "@%^&*?" , and maybe combinations like "[]".
{ A, [], B } = SomeFunc(1, [], 2)
This one I really like. It stands out to me.
Problem is, it could be a double typo ("[]" when "p[0]" was meant for example).
{ A, @, B } = SomeFunc(1, @, 2)
I could live with this.
{ A, %, B } = SomeFunc(1, %, 2) { A, ^, B } = SomeFunc(1, ^, 2) { A, &, B } = SomeFunc(1, &, 2) { A, *, B } = SomeFunc(1, *, 2) { A, ?, B } = SomeFunc(1, ?, 2)
These all look like operations to me. Even the caret (we don't use it, but it reminds me of power() in other languages).
26. Re: Multi-assign
- Posted by kinz Nov 17, 2011
- 2536 views
Multi-assign in BASIC is:
a,b,c,d,e = 1
So you need just:
object a,b,c,d,e = 1 --- is it possible in 4.* now ? -- And then you can do: procedure ma(object a, object b) if sequence(a) then for i=1 to length(a) do a[i]= b end for elsif atom(a) and atom(b) then a = b else end if end procedure ma({a,b,e}, 2) ?a ?b ?c ?d ?e
Regards
kinz
27. Re: Multi-assign
- Posted by kinz Nov 17, 2011
- 2482 views
Multi-assign in BASIC is:
a,b,c,d,e = 1
Sorry, my bad, it doesn't work
28. Re: Multi-assign
- Posted by mattlewis (admin) Nov 17, 2011
- 2544 views
A new keyword is a really bad idea.
I agree with you, but I am curious as to why you think that.
I just thought using a keyword would be too confusing, as it'd be too easy to think it was a variable name or something.
I agree with those reasons, and also believe that we should generally be parsimonious about adding keywords.
I think we need to have a glyph of some sort, and a visually prominent one would be preferable. We should try to avoid characters that are potentially typos.
Agreed. This arguably takes the underscore out of the equation as well (as it could be a typo on either side - variable named "_math" turned into "_,math" or something).
Yes, I agree with these. However, I think I may be the only one who still thinks the $ is perfect.
{ A, @, B } = SomeFunc(1, @, 2)
I could live with this.
This is not awful, though I'd always imagined using @ for implicit routine id's or something in the future.
{ A, %, B } = SomeFunc(1, %, 2) { A, ^, B } = SomeFunc(1, ^, 2) { A, &, B } = SomeFunc(1, &, 2) { A, *, B } = SomeFunc(1, *, 2) { A, ?, B } = SomeFunc(1, ?, 2)
These all look like operations to me. Even the caret (we don't use it, but it reminds me of power() in other languages).
Yes. I was thinking modulo (%) and bitwise xor (^).
I think my preferences, so far, in order, are: $, ~, @.
Matt
29. Re: Multi-assign
- Posted by jeremy (admin) Nov 17, 2011
- 2513 views
I think we may be worrying about multiple uses of a character a bit too much. Just learn the rules of Euphoria and what special characters do. No need to make it overly complex but knowing that $ is "ignore" in a LHS is a pretty easy rule.
One thing people did not comment on is making this "new" ignore character valid in function calls for greater clarity:
procedure greet(sequence name="World", sequence greeting="Hello") printf(1, "%s, %s!\n", { greeting, name }) end procedure greet(,"Goodbye") -- pretty bad IMHO greet( $, "Goodbye") -- much nicer
Discussing this may have an effect on what character we choose.
Jeremy
30. Re: Multi-assign
- Posted by irv Nov 17, 2011
- 2482 views
The ~ by all means!
It's not currently used by Euphoria, so it's the best, least misunderstandable, most visible possibility. The fact that it is also used to indicate the user's home folder in Linux filenames isn't going to cause a problem.
Just read it as "whatever..."
31. Re: Multi-assign
- Posted by jaygade Nov 17, 2011
- 2472 views
Is this meant to be just a placeholder for multi-assign, or is it also meant to be a generic bit bucket variable as some have asked for in the past?
My 2 cents:
$ is okay. I kinda like the way that [] stands out and it meets many other criteria, I don't think that it would be a common typo unless scanning something like "foo[]" becomes an issue.
{[], result} = value(number) [] = command_line() -- ignore the command line
32. Re: Multi-assign
- Posted by mattlewis (admin) Nov 17, 2011
- 2469 views
Is this meant to be just a placeholder for multi-assign, or is it also meant to be a generic bit bucket variable as some have asked for in the past?
My original proposal was just for multi-assign. It seems to be growing into a generic bit bucket.
Matt
33. Re: Multi-assign
- Posted by jimcbrown (admin) Nov 17, 2011
- 2447 views
One thing people did not comment on is making this "new" ignore character valid in function calls for greater clarity
Discussing this may have an effect on what character we choose.
Well, I see this as being not a big deal, beyond that some characters (such as the dash or the hash mark) are more likely to be typos on the RHS than the LHS.
Other than that minor (if pesky) issue, it has no effect on the debate.
34. Re: Multi-assign
- Posted by jaygade Nov 17, 2011
- 2456 views
My original proposal was just for multi-assign. It seems to be growing into a generic bit bucket.
Matt
I was just wondering because the two ideas seemed kind of related. Not multi-assign and bit bucket, but placeholder and bit bucket.
35. Re: Multi-assign
- Posted by jimcbrown (admin) Nov 17, 2011
- 2467 views
My original proposal was just for multi-assign. It seems to be growing into a generic bit bucket.
Matt
I was just wondering because the two ideas seemed kind of related. Not multi-assign and bit bucket, but placeholder and bit bucket.
This seems like a natural development to me.
36. Re: Multi-assign
- Posted by petelomax Nov 18, 2011
- 2374 views
I'd prefer not to use '$' as it is already loaded with two meanings and to add a third is probably asking for future problems.
So I think we are left with the single characters ... "@%^&*?" , and maybe combinations like "[]".
{ A, ?, B } = SomeFunc(1, ?, 2)
That one makes the most sense to me.
Regarding $ on the RHS, to indicate omitted parameters, you cannot do that as it already has valid meaning:
sequence s s = {1,5,7} function f(object o) return o end function ? s[f($)] -- prints 7, as it should if getc(0) then end if
Regards, Pete
37. Re: Multi-assign
- Posted by mattlewis (admin) Nov 18, 2011
- 2367 views
Regarding $ on the RHS, to indicate omitted parameters, you cannot do that as it already has valid meaning:
? s[f($)]
I wouldn't call that valid. I'd say you've found a new bug in the parser.
Matt
38. Re: Multi-assign
- Posted by Insolor Nov 18, 2011
- 2320 views
Regarding $ on the RHS, to indicate omitted parameters, you cannot do that as it already has valid meaning:
? s[f($)]
I wouldn't call that valid. I'd say you've found a new bug in the parser.
Matt
I used such trick in some programs:
s[rand($)]
39. Re: Multi-assign
- Posted by jeremy (admin) Nov 18, 2011
- 2306 views
I used such trick in some programs:
s[rand($)]
I'd vote for that being a bug. Fun, interesting but something that needs to be fixed.
Jeremy
40. Re: Multi-assign
- Posted by DerekParnell (admin) Nov 18, 2011
- 2306 views
I used such trick in some programs:
s[rand($)]
I'd vote for that being a bug. Fun, interesting but something that needs to be fixed.
Jeremy
Except that there is a problem with calling it a bug; it's documented as legal. Look at the section in the documentation on "Slicing of Sequences". In there we see ...
s[$][1..floor($/2)] -- first half of the last element of s
Now although it may not be what some intended, it is documented, and what harm or problem does it cause?
41. Re: Multi-assign
- Posted by mattlewis (admin) Nov 18, 2011
- 2308 views
Except that there is a problem with calling it a bug; it's documented as legal. Look at the section in the documentation on "Slicing of Sequences". In there we see ...
s[$][1..floor($/2)] -- first half of the last element of s
Now although it may not be what some intended, it is documented, and what harm or problem does it cause?
Fascinating. As soon as I saw it, I realized how it worked. And now that you've shown that, I recall that being the original spec. OK, so we don't want to break that, and so if we want to use the same symbol for multi-assign and for "use the default arg", then we obviously can't use $, so I relent.
Matt
42. Re: Multi-assign
- Posted by BRyan Nov 18, 2011
- 2280 views
Fascinating. As soon as I saw it, I realized how it worked. And now that you've shown that, I recall that being the original spec. OK, so we don't want to break that, and so if we want to use the same symbol for multi-assign and for "use the default arg", then we obviously can't use $, so I relent.
Matt
Now that you agree with me :)
What about the ! explanation point ?
Bernie
43. Re: Multi-assign
- Posted by jimcbrown (admin) Nov 18, 2011
- 2302 views
Regarding $ on the RHS, to indicate omitted parameters, you cannot do that as it already has valid meaning:
sequence s s = {1,5,7} function f(object o) return o end function ? s[f($)] -- prints 7, as it should if getc(0) then end if
Regards, Pete
That's not insurmountable. Using $ on the LHS is still unambiguous, and when used on the right side the $ only means omitted when that is the only possible interpretation. If both interpretations are possible, then use the length-of-sequence meaning over the omitted-parameter meaning. That's ok since the user can still just use nothing at all (e.g. "s[f(a, , c)]") to omit the parameter in these cases.
(I was not a fan of using $ in either of these meanings, anyways. $ reminds me too much of M$.)
44. Re: Multi-assign
- Posted by jimcbrown (admin) Nov 19, 2011
- 2245 views
OK, so we don't want to break that, and so if we want to use the same symbol for multi-assign and for "use the default arg", then we obviously can't use $, so I relent.
What about $$$ ? That wouldn't break anything, iiuc.
45. Re: Multi-assign
- Posted by Fernando Nov 19, 2011
- 2338 views
I'd prefer not to use '$' as it is already loaded with two meanings and to add a third is probably asking for future problems.
I think the dash '-' is fine for the LHS but not so good on the RHS. It could be a typo on the RHS where someone meant to key negative number as an parameter.
A new keyword is a really bad idea.
The preference for a shift-less character is not so important as the feature would not be a commonly used one. Also, keyboards vary as to which characters are shifted. For example, the tilde '~' is very awkward on some keyboards.
I think we need to have a glyph of some sort, and a visually prominent one would be preferable. We should try to avoid characters that are potentially typos.
So I think we are left with the single characters ... "@%^&*?" , and maybe combinations like "[]".
{ A, @, B } = SomeFunc(1, @, 2) { A, %, B } = SomeFunc(1, %, 2) { A, ^, B } = SomeFunc(1, ^, 2) { A, &, B } = SomeFunc(1, &, 2) { A, *, B } = SomeFunc(1, *, 2) { A, ?, B } = SomeFunc(1, ?, 2) { A, [], B } = SomeFunc(1, [], 2)
The symbol may represent something that absorbs everything. In physics, this reminds me a black hole. A black hole could be formed by a collapsed star which in turn becomes a little star. 'Little star' is the meaning of the word 'asterisk' (http://en.wiktionary.org/wiki/asterisk).
The symbol of asterisk (asterisk character) is already known and used as "don't care" as in specifying file names (or paths) in CP/M, DOS, Microsoft Windows and Unix-like operating systems. In digital logic, a similar form symbol is used as "don't care": x.
- Fernando
46. Re: Multi-assign
- Posted by Fernando Nov 19, 2011
- 2233 views
Some interesting possibilities with LHS symbolic sequences:
- Initializing several variables with the same value:
{a,b,c} = 0
- Incrementing several variables:
{a,b,c} += 1
- Adding different values for different variables:
{a,b,c} += {d,e,f}
- Doubling several variables:
{a,b,c} *= 2
- Fernando
47. Re: Multi-assign
- Posted by DerekParnell (admin) Nov 19, 2011
- 2263 views
Some interesting possibilities with LHS symbolic sequences:
- Initializing several variables with the same value:
{a,b,c} = 0
When the RHS is an atom, we might be able to get away with this, but once the RHS is a sequence, we strike ambiguities.
Consider ...
{a,b,c} = 1 -- Assign 1 to a, b, and c {a,b,c} = {d,e,f} -- Assign {d,e,f} to a, b, and c or -- assign d to a, e to b, and f to c?
48. Re: Multi-assign
- Posted by Fernando Nov 19, 2011
- 2251 views
Some interesting possibilities with LHS symbolic sequences:
- Initializing several variables with the same value:
{a,b,c} = 0
When the RHS is an atom, we might be able to get away with this, but once the RHS is a sequence, we strike ambiguities.
Consider ...
{a,b,c} = 1 -- Assign 1 to a, b, and c {a,b,c} = {d,e,f} -- Assign {d,e,f} to a, b, and c or -- assign d to a, e to b, and f to c?
IMHO, one of the intentions of multi-assign is to simplify the access to the elements of a returned sequence of a function. Then:
{a,b,c} = {d,e,f} -- assign d to a, e to b, and f to c
To satisfy the other case:
{a,b,c} = { {d,e,f} , {d,e,f} , {d,e,f} }
- Fernando
49. Re: Multi-assign
- Posted by DerekParnell (admin) Nov 19, 2011
- 2205 views
Some interesting possibilities with LHS symbolic sequences:
- Initializing several variables with the same value:
{a,b,c} = 0
When the RHS is an atom, we might be able to get away with this, but once the RHS is a sequence, we strike ambiguities.
Consider ...
{a,b,c} = 1 -- Assign 1 to a, b, and c {a,b,c} = {d,e,f} -- Assign {d,e,f} to a, b, and c or -- assign d to a, e to b, and f to c?
IMHO, one of the intentions of multi-assign is to simplify the access to the elements of a returned sequence of a function. Then:
{a,b,c} = {d,e,f} -- assign d to a, e to b, and f to c
To satisfy the other case:
{a,b,c} = { {d,e,f} , {d,e,f} , {d,e,f} }
- Fernando
Yes, but the problem I see is the inconsistency that might happen.
If {a,b,c} = 1 means assign to each symbol on the LHS the value on the RHS, then why does {a,b,c} = {1,2,3} mean something different? It looks the same; a list of symbols on the LHS and a single value on the RHS ( a single sequence).
Or more to the point ...
{a,b,c} = func()
Would operate differently depending on whether func() returned an atom or a sequence.
50. Re: Multi-assign
- Posted by Fernando Nov 19, 2011
- 2205 views
Some interesting possibilities with LHS symbolic sequences:
- Initializing several variables with the same value:
{a,b,c} = 0
When the RHS is an atom, we might be able to get away with this, but once the RHS is a sequence, we strike ambiguities.
Consider ...
{a,b,c} = 1 -- Assign 1 to a, b, and c {a,b,c} = {d,e,f} -- Assign {d,e,f} to a, b, and c or -- assign d to a, e to b, and f to c?
IMHO, one of the intentions of multi-assign is to simplify the access to the elements of a returned sequence of a function. Then:
{a,b,c} = {d,e,f} -- assign d to a, e to b, and f to c
To satisfy the other case:
{a,b,c} = { {d,e,f} , {d,e,f} , {d,e,f} }
- Fernando
Yes, but the problem I see is the inconsistency that might happen.
If {a,b,c} = 1 means assign to each symbol on the LHS the value on the RHS, then why does {a,b,c} = {1,2,3} mean something different?
My first thought was: because this is the Euphoric way. Comparing:
s += 1
with:
s += {a,b,c}
In the first case, each element of s is incremented. In the second case, first element of s is added by a, second element of s is added by b, ... and NOT the first element of s is added by {a,b,c}, ... But, the assignment operator '=' does not work as the assignment WITH operator (+=, *= , etc). We cannot assign an atom to a sequence. Thus, I think you're right: there would be an inconsistency in that form of initialization similar with the inconsistency in assignment operator with assignment with operator.
It looks the same; a list of symbols on the LHS and a single value on the RHS ( a single sequence).
Or more to the point ...
{a,b,c} = func()
Would operate differently depending on whether func() returned an atom or a sequence.
Yes, the assignment would operate differently depending on the type: atom or sequence.
51. Re: Multi-assign
- Posted by petelomax Nov 19, 2011
- 2169 views
Consider ...
{a,b,c} = 1 -- Assign 1 to a, b, and c {a,b,c} = {d,e,f} -- Assign {d,e,f} to a, b, and c or -- assign d to a, e to b, and f to c?
My take on this was:
{a,b,c} = 1 -- illegal/error {a,b,c} @= 1 -- a=1, b=1, c=1 {a,b,c} = {d,e,f} -- a=d, b=e, c=f {a,b,c} @= {d,e,f} -- a={d,e,f}, b={d,e,f}, c={d,e,f}
Pete
52. Re: Multi-assign
- Posted by mattlewis (admin) Dec 03, 2011
- 1883 views
I'd prefer not to use '$' as it is already loaded with two meanings and to add a third is probably asking for future problems.
So I think we are left with the single characters ... "@%^&*?" , and maybe combinations like "[]".
{ A, ?, B } = SomeFunc(1, ?, 2)
That one makes the most sense to me.
I think I have come around to this idea. I've updated the code, and added eubins for Windows / Linux, 32 and 64-bit here.
I also updated it so that you can use ? for default parameters in routine calls.
Matt
53. Re: Multi-assign
- Posted by DerekParnell (admin) Dec 03, 2011
- 1850 views
I also updated it so that you can use ? for default parameters in routine calls.
I like that this is optional (doesn't break existing code). You could issue a warning when -strict is in effect and the ? is missing from a parameter list.
54. Re: Multi-assign
- Posted by mattlewis (admin) Dec 03, 2011
- 1909 views
I also updated it so that you can use ? for default parameters in routine calls.
I like that this is optional (doesn't break existing code). You could issue a warning when -strict is in effect and the ? is missing from a parameter list.
Yes, I didn't put in the warning yet, but that was the plan.
Matt
56. Re: Multi-assign
- Posted by bill Dec 05, 2011
- 1706 views
I have viewed this thread with dismay.
Everyone seems to see only the positive (multi-assignment is good!). Yes it is useful (marginally). It provides a simple swap {a, b} = {b, a} and it allows the dreadful {?, x} = value(Y).
Consider:
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).
{a,?,c} cannot be a sequence a slice [1..2] - what is its value?, its length?
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.
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.
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.
57. Re: Multi-assign
- Posted by mattlewis (admin) Dec 05, 2011
- 1659 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
58. Re: Multi-assign
- Posted by jimcbrown (admin) Dec 05, 2011
- 1684 views
I have viewed this thread with dismay.
A function (presumably) returns one value (sequence or atom).
Why?
What's wrong with having a function return multiple values?
Everyone seems to see only the positive (multi-assignment is good!). Yes it is useful (marginally).
What are the negative effects?
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.)
Agreed. This is not the creation-of-sequence operator. This is in fact the inverse, it is desequencing.
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).
Desequencing implies that the return type has to be a sequence (before the sequence can be desequenced) and to have a different type is to cause an error.
{a,?,c} cannot be a sequence a slice [1..2] - what is its value?, its length?
Well, you have a return value that is a sequence s that is desequenced such that a is assigned to s[1] and c is assigned to s[3] and s[2] is ignored.
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.
So that symbol has at least two meanings. What of it?
I see these two technical meanings as very closely related. In both usages, the user is telling the language that the user does not care about the 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 answer:
{a,b,c} = {1,2,3} -- a:=1, b:=2, c:=3 -- conceptually shorthand for sequence temp_s = {1,2,3} a = s[1] b = s[2] c = s[3] delete temp_s {1,2,3} = {a,b,c} -- an error
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.
I'm not clear on what you mean here.
It is technically possible to distinguish between the two. I know this because the code to do so has already been written and tested. The interpreter/translator/parser is able to do this.
Perhaps you mean that you think the concept is too confusing for a user?
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 don't care much one way or the other on what symbols or characters are used, but changing that would not change the fundamental nature of desequencing.
A function would still return a sequence, and the operators would still desequence it.
59. Re: Multi-assign
- Posted by DerekParnell (admin) Dec 05, 2011
- 1613 views
I have viewed this thread with dismay.
Good. Not that I agree with you here but its good that we speak up when we have an issue about the direction of the language.
Everyone seems to see only the positive (multi-assignment is good!). Yes it is useful (marginally). It provides a simple swap {a, b} = {b, a} and it allows the dreadful {?, x} = value(Y).
Consider:
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.)
I can see how the LHS use of the brace-pair looks like a sequence creation operation when it actually isn't that at all. This is a classic reuse of the limited character set available to use to mean different things depending on the context its used in. There are already a number of such re-uses in Euphoria, such as '=', which can mean an assignment or an equality test depending on where one uses it. Another is the parenthesis-pair, which can delineate a set of function arguments or impose a precedence order for expression evaluation.
We chose the brace-pair for this new feature to hint to the code reader that a sequence is involved, but in this case a sequence is being de-constucted rather than being constructed.
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).
The idea is that one would use multi-assign when it makes sense and when it would not fail. So if one uses it for functions that return an atom, you're doing it wrong.
{a,?,c} cannot be a sequence a slice [1..2] - what is its value?, its length?
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 like to think of it more as a placeholder. So I regard it more as saying ... 'this position in the list is not being used by me'.
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.
Not quite accurate. The parser can distinguish between the two uses, just as it can with the '=' symbol. The rule is simply when the scanner finds '{' as the first character at the start of a statement, it assumes that as multi-assignment is being written. It then goes on to validate that assumption and if true, generates the appropriate Intermediate Language code.
A multi-assignment LHS list is illegal if it contains any literals. Thus {1,2,3} will fail.
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.
As I said earlier, we could have used something else, but we wanted to keep some link to a sequencing sort of operation - in this case a de-sequencing one.