May 26, 2018, 03:33:59 AM

Author Topic: returning tuples?  (Read 106 times)

Offline meems

  • Full Member
  • ***
  • Posts: 227
returning tuples?
« on: May 15, 2018, 10:12:51 PM »
can bmax functions return tuples?
I sometimes find a function produces 2 or more values I'd like to keep. But I think bmax functions can only return one value, unless u pack them into a datatype instance.
Faced with this problem, I either create a global and stick the useful value in it, or I stick the useful value in an 'appendage ' field or unused field of some incidental datatype used in the function.

I don't bother with creating datatypes&instances just for the sake of passing multi values up from functions.

If not tuples, then for values of the same type, is it worth creating a quick array just to pass them up the function hierarchy?

Offline Derron

  • Hero Member
  • *****
  • Posts: 961
Re: returning tuples?
« Reply #1 on: May 15, 2018, 10:36:37 PM »
I have a generic data container for "simple stuff":
https://github.com/GWRon/Dig

So you do a
return new TData.AddNumber("a", 12).Add("object", myobject)


Another option is to use "var": it allows you to assign a new instance to the given parameter. Complex objects (no int, string, ...) are passed by reference already (so changes to their properties are visible outside already)

Code: BlitzMax
  1.  
  2. local p1:TMyType ...
  3. local p2:TMyType ...
  4. local otherType:TMyType ...
  5. local p3:string = "test"
  6. local p4:string = "othertest"
  7.  
  8. Function MyParams(param:TMyType var, param2:TMyType, param3:string, param4:string var)
  9.   param = otherType
  10.   param2 = otherType
  11.   param3 = ""
  12.   param4 = param3
  13. End Function
  14.  
  15. MyParams(p1, p2, p3, p4)
  16. 'p1 is now the same reference as otherType (so pointing to same object)
  17. 'p2 is still p1 once function is exited (but if you did a "param2.property = 2" the property would be changed)
  18. 'p3 is still "test" once the function is done
  19. 'p4 is now having the value of p3 (it's no object, so a bit different)
  20.  

I am pretty sure to have some errors in the code/logic but the Pro's here are eager to correct me then ;-)


bye
Ron

Offline Henri

  • Full Member
  • ***
  • Posts: 110
Re: returning tuples?
« Reply #2 on: May 16, 2018, 11:44:40 AM »
Hi,

like Ron said, what you are looking for is 'Var'. Types can already be accessed directly so no need for var.

Code: BlitzMax
  1. Function ReturnMultiplevalues(a:Int Var,b:Int Var,c:Int Var)
  2.         a=10
  3.         b=20
  4.         c=30
  5.         Return
  6. End Function
  7.  
  8. Local x:Int, y:Int, z:Int
  9.  
  10. ReturnMultipleValues(x,y,z)
  11.  
  12. Print "x="+x     '10
  13. Print "y="+y     '20
  14. Print "z="+z     '30
  15.  

Edit:   Arrays are also handy sometimes for this purpose.

-Henri
- Got 01100011 problems, but the bit ain't 00000001

Offline meems

  • Full Member
  • ***
  • Posts: 227
Re: returning tuples?
« Reply #3 on: May 19, 2018, 12:27:46 PM »
thanks Henri and Ron
:)

kinda makes return obsolete.