Multiple value, pass-by-reference, or object?

Monkey Forums/Monkey Programming/Multiple value, pass-by-reference, or object?

Ken(Posted 2013) [#1]
Hi All,

I'm writing an object that takes a string, steps through the string letter by letter, adds the points required for the given letter to a list, and finally returns the list of points required to draw that word to the screen.

Ideally, I'd like to have a method that will return both the width, and the list of points to generate a given letter.

It doesn't seem Monkey can return more than one type.
In C you'd get around that using pass-by-reference, and alter a thing in the caller, but I don't 'think Monkey does pass-by-reference.

So, does that leave objects as the only way out? How heavyweight are objects? In this instance, I'd be creating a new object for every character that's drawn, and that just sounds expensive in terms of garbage collection.

Is there a more elegant way that this?

Class LetterResult
	Field width:Int
	Field points:IntList
	
	Method New()
		width=0
		points=new IntList
	End
End

Class GoalWord
	Method drawChar:LetterResult(c:Char, x:Int, y:Int)
		Local lr:LetterResult = new LetterResult
		lr.width=56
		lr.points.Add(1)
		' generate more points
		return lr
	End
	
	Method drawString:Void(str:String)
		local x:Int = 10
		local y:Int = 50
		For local c:Char = Eachin drawString			
			local lr:LetterResult = drawChar(c, x, y)
			x += lr.width
			' do something with the points...is there a list.AppendList?
		End
	End
End



skid(Posted 2013) [#2]
Why not make the result a member of the object?


Samah(Posted 2013) [#3]
If you want to do it that way, just create one instance of your "result" class and pass it in as a parameter. No need for a return value at all.


MikeHart(Posted 2013) [#4]
Objects and arrays are passed by reference, the rest as value.


Samah(Posted 2013) [#5]
@MikeHart: Objects are passed by reference, the rest as value.

We've already had this discussion, and I'm not going into semantics again.
http://www.monkeycoder.co.nz/Community/posts.php?topic=4987

Ken:
Since you're obviously a C programmer, primitives are just primitives, and object references are like (Object *). There is no (int *), etc. in Monkey.


Gerry Quinn(Posted 2013) [#6]
"So, does that leave objects as the only way out? How heavyweight are objects? In this instance, I'd be creating a new object for every character that's drawn, and that just sounds expensive in terms of garbage collection."

If you're worried about object creation, you're looking in the wrong place. Your IntList will generate a new object for every point.

The issue with creating objects is that if you create enough, the system will periodically have to go through them and free the memory of objects that are no longer reachable. This is called garbage collection. If your game is not an arcade game, I doubt whether the odd garbage collection stutter which may occur will matter, so just code whatever works.

Otherwise, you can reduce the amount of objects by using (and re-using if possible) an array of primitives (ints or floats) for your points. An array of primitives is a single object. If you are always drawing only one letter at a time, and are then finished with the points, you could allocate a big array (or a stack) at the start, and just keep on re-using that.


Shinkiro1(Posted 2013) [#7]
Shoot for the most straight forward solution possible (an array or an object in this case) and only think about optimization once it becomes a problem.

The most important optimization lies in your lifetime variable.


Samah(Posted 2013) [#8]
@Shinkiro1: ...only think about optimization once it becomes a problem.

This.

Just remember, the compiler is usually smarter than you. :)


ziggy(Posted 2013) [#9]
Just remember, the compiler is usually smarter than you. :)
I'll believe this when I see the compiler assembling any Ikea furniture. XD

Now, seriously, if it ever gets a problem, you can add a sort of object pooling or the like. I've one ready in my own framework but still haven't had any need for it, so I would say it's quite safe to assume it is not usually a problem. the platform that has worse GC is XNA, and even in this platform, being a bit conservative on object creation is usually enough.


Ken(Posted 2013) [#10]
Thanks for the input guys. I'll cross the proverbial when I get there.

-Ken