jastrachan | 17 Dec 22:48 2004
Picon

Re: tuple return

FWIW I'd like to support tuple assignment in Groovy so that you can, on 
the LHS of an expression do something like

     a, b, c = someListExpression

Its particularly useful for swapping variables

     a, b = b, a

Though this does beg the question; if we're gonna add LHS side tuples, 
should we support RHS tuple expressions. (LHS = left hand side, RHS = 
right hand side).

That opens a little can of worms in the parser I'd imagine? Also that 
begs the question...

     x = 1, 2

is x a list, or array etc?

In Python a tuple is read only; I've mused off and on as to whether we 
should map tuple syntax to Object[].

So

x = 1, 2, 3

would be logically quivalent to

     x = new Object[] { 1, 2, 3 }

so

     x = 1, 2, 3
     assert x instanceof Object[]
	
     x = [1, 2, 3]
     assert x instanceof java.util.List

Thoughts?

On 16 Dec 2004, at 19:35, Russel Winder wrote:
> Xavier,
>
> On Thu, 2004-12-16 at 13:08, Xavier Méhaut wrote:
>> Hi Russel,
>> Groovy tries to render less verbose the code in many areas. So the
>> idea was to do the same with multiple returns. Up to now in Java (and
>> groovy), when we want to return more than one value in a method, we
>> need to put them in one list, and then after calling it, we need to
>> retry the values from this list... I've found convenient to render the
>> code more concise ;)
>> The schema below shows a SADT-like (Idef0) data flow diagram... For
>> some kinds of applications we have such a structure and having this
>> possiblity in groovy should be nice ;)
>> regards
>> Xavier
>
> I think what has been suggested by others is probably the best bet to
> achieve the goal -- the procedure returns a list and then there is
> multiple assignment.  So your diagram would come out something like:
>
> 	out1, out2 = processA(in1, in2)
> 	process(out2)
>
> Ruby does this sort of thing.  If processA returns a scalar the out2
> gets the value nil and if processA return a list longer than 2 items 
> the
> first two would used and the rest ignored.  So
>
> #! /usr/bin/env ruby
> def swap(a, b)
>   print "A was " ; puts a
>   print "B was " ; puts b
>   [ b, a, 2 ]
> end
> a, b = swap(1, 2)
> print "A is " ; puts a
> print "B is " ; puts b
>
> results in:
>
> A was 1
> B was 2
> A is 2
> B is 1
>
> The Groovy equivalent might be:
>
> #! /usr/bin/env groovy
> def swap(a, b) {
>   print "A was " ; println a
>   print "B was " ; println b
>   [ b, a, 2 ]
> }
> a, b = swap(1, 2)
> print "A is " ; println a
> print "B is " ; println b
>
> As Guillaume has said this is not supported by Groovy as yet the
> question is whether it should be and when.  Would the best idea be to
> put an issue in Jira so that there is a focus for debate and if agreed
> scheduling?
>
> -- 
> Russel.
> =============================================
> Dr Russel Winder         +44 20 7585 2200
> 41 Buckmaster Road       +44 7770 465 077
> London SW11 1EN, UK      russel@...
>
>

James
-------
http://radio.weblogs.com/0112098/


Gmane