Returning two values

Monkey Forums/Monkey Beginners/Returning two values

ratking(Posted 2016) [#1]
I'd like to create a simple function that returns two values, let's say two Floats. I found out that Monkey-X has no functionality for that (no surprise), but also no Structs and no ByRef (or similar) function parameters. The only solution seems to be to create a Class specifically for that function which holds the two Floats, and within the function create a new object and return it. This seems a bit much for such a simple task, and probably isn't very good for the GC if you have to use that class often. Is there any other solution that doesn't over-complicate things?

The ideal would be that some other person would be able to use that function without thinking about pooling or boxing the values.


Playniax(Posted 2016) [#2]
You could just return an array.

Return [x,y]


ratking(Posted 2016) [#3]
Hmm, I thought arrays are objects too?

(It also doesn't work when the 2 values have different types, I guess.)


Goodlookinguy(Posted 2016) [#4]
Do not assume anything about the underlying structure of Monkey because it varies from platform-to-platform. In several languages arrays are not objects. I know in java they are.

Edit: I missed that part about not thinking about pooling or boxing. That is not a realistic premise. These people are programmers. Learning about and understanding boxing and pooling is vital. Don't simplify past the point of realistic.

Here's a solution to your problem. Although I decided to return 4 different value types instead of 2. As a recommendation, if you get overly concerned with speed, you may want to pool several boxed objects and clear their value when they're first accessed.



Edit: I decided to provide multiple examples based on what you were saying.


ratking(Posted 2016) [#5]
Edit: I missed that part about not thinking about pooling or boxing. That is not a realistic premise. These people are programmers. Learning about and understanding boxing and pooling is vital. Don't simplify past the point of realistic.


That's not the problem. But trying to make a simple library adds a lot of unnecessary complexity to it which have to be communicated. The more you have such boxing and unboxing, the less readable the code becomes (IMHO). The verbosity in your examples really cries for a simple Struct thing. Especially if it's something simple like a Point() structure, or similar.

Thankfully MX2 will have Structs, but it's too bad MX1 won't have them.

I would also be content with ByRef parameters.


PixelPaladin(Posted 2016) [#6]
You could create arrays for the return values and pass them to the function. Then the function can write the return values into the arrays and you can read from them afterwards:




ratking(Posted 2016) [#7]
Yeah, that's probably the way I'll go.

Thanks for all the replies! Helped me to understand Monkey a bit better.


Gerry Quinn(Posted 2016) [#8]
Yes, what PixelPaladin says is the solution if you want to minimise memory allocation. You could also define a class to hold the data, but pass a single instance of the class that you use over and over, instead of constructing and returning a new data object in every call.

If you have a utility class you use a lot, you might choose to allow for both options. My Rect class even adds a third, to cater for all coding situations:

	' Returns X-coordinate of centre 
	Method XCentre:Int()
		Return loc.x + size.w / 2
	End
	
	' Returns Y-coordinate of centre
	Method YCentre:Int()
		Return loc.y + size.h / 2
	End
	
	' Returns a new point representing central point of rectangle
	Method Centre:Point()
		Return New Point( loc.x + size.w / 2, loc.y + size.h / 2 )
	End
			
	' Set point to central point of rectangle
	Method GetCentre:Void( _centre:Point )
		_centre.Set( loc.x + size.w / 2, loc.y + size.h / 2 )
	End


...though it would be nice to be able to inline the first two!