HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Returning multiple values

08-15-2008, 04:37 PM#1
Vexorian
That's about one of the worst limitations in Jass, everything must return a single value. The question is, is it possible to fix it in vJass? Reference arguments seem like quite unimplementable so:

Collapse JASS:

function orus takes real x, real y returns real,real
    set x=(x-y)*(x-y)
    set y=5.0*x-x*x
 return (x,y)
endfunction

function mah takes nothing returns nothing
 local real x = 0.2
 local real y = 13.0
 local real z
 local real w

    set (z,w)= orus(x,y)
endfunction
Collapse result:
globals
    real f__result_real1
    real f__result_real2
endglobals

function orus takes real x, real y returns nothing
    set x=(x-y)*(x-y)
    set y=5.0*x-x*x
 set f__result_real1=x
 set f__result_real2=x
endfunction


function mah takes nothing returns nothing
 local real x = 0.2
 local real y = 13.0
 local real z
 local real w

    call orus()
    set z=f__result_real1
    set w=f__result_real2
endfunction
???

suggestions welcome.
08-15-2008, 04:52 PM#2
PurplePoot
Why not keep the return and thus decrease the number of globals by one (Also a minor syntax edit for the function declaration to how I see it being practical (far shorter declarations), but perhaps a bit more confusing...?)?

Collapse vJass:
function orus takes real x, real y returns real(2) //if you wanted other types, just add them,
                                                                        //eg: real(3),integer(2),unit
    set x=(x-y)*(x-y)
    set y=5.0*x-x*x
 return (x,y)
endfunction

function mah takes nothing returns nothing
 local real x = 0.2
 local real y = 13.0
 local real z
 local real w

    set (z,w)= orus(x,y)
endfunction

Collapse result:
globals
    real f__result_real
endglobals

function orus takes real x, real y returns real
    set x=(x-y)*(x-y)
    set y=5.0*x-x*x
 set f__result_real=y
 return x
endfunction


function mah takes nothing returns nothing
 local real x = 0.2
 local real y = 13.0
 local real z
 local real w

    set z=orus()
    set w=f__result_real
endfunction

EDIT: One more thing:

Collapse vJass:
function mah takes nothing returns nothing
 local real x = 0.2
 local real y = 13.0
 local real z,w = orus(x,y)
endfunction

Collapse result:
function mah takes nothing returns nothing
 local real x = 0.2
 local real y = 13.0
 local real z=orus()
 local real w=f__result_real
endfunction
08-15-2008, 06:06 PM#3
Ammorth
Ive used structs to return multiple values before, but the creation/destruction was a bit of a turn-off (aswell as creating a whole struct just for 1 return). This I like.
08-15-2008, 07:59 PM#4
darkwulfv
Looks cool. So, if this comes to creation, it'll be like (the only reference in coding I know) Python, where you could go (it's been forever, so pardon the crap syntax)
Code:
a, b, c, d = 1, 3, 5, 7

I would go for it.
08-15-2008, 08:09 PM#5
Troll-Brain
Vexorian you did an error.
Shouldn't be that : set f__result_real2=y instead of set f__result_real2=x

Also i'm agree with PurplePoot but maybe it's too hard to code ?
08-15-2008, 08:26 PM#6
Ammorth
I personally like vexorian's return syntax ( returns real, real, real, integer, integer, unit ) its easy enough to type and it makes it easier to follow.
08-15-2008, 09:58 PM#7
PurplePoot
I don't know... it just strikes me as unnecessarily unoptimized. Readability is definitely important, yes, but in some ways real(3), integer(2), unit is more readable than real, real, real, integer, integer, unit, especially since scrolling a page or two sideways just to read the function declaration can be annoying (though returning more than two values would admittedly be unlikely).

On a related note (darkwulfv reminded me), we've had local integer a,b for well over a year now (to my knowledge), but still lack local integer a,b=3,5, and set a,b=3,5. Finally, setting multiple variables to the same value would be handy, for example set a,b,c=3.
08-15-2008, 10:19 PM#8
Vexorian
I feel like not letting people ever return more than two values. But if I allow more than two, real(3) is out of the question, we don't do takes integer (4) ...
08-15-2008, 11:58 PM#9
PurplePoot
Fair enough, the syntax is indeed a bit weird for the rest of Jass.

However, on the same note about local integer a,b,c (blah, I suppose this is getting more and more off-topic), what about having takes integer a,b,c?
08-16-2008, 03:50 AM#10
Jazradel
real(3) is horrible. I'm not sure the () are necessary, set (z,w)= orus(x,y) could just as well be set z,w = orus(x,y).

Also since when can we do integer a,b,c?
08-16-2008, 04:12 AM#11
PurplePoot
I was under the impression we could, after further testing it appears it just deletes variables after the ,. I got the theory in the first place from the older pages of the JassHelper thread.
08-16-2008, 11:57 AM#12
Themerion
Quote:
Originally Posted by Vexorian
I feel like not letting people ever return more than two values. But if I allow more than two, real(3) is out of the question, we don't do takes integer (4) ...

Not the best comparison. Takes values are named, return values are not.