Passing 'boxing' values
Monkey Forums/Monkey Programming/Passing 'boxing' values
| ||
I would think that an advantage to boxing would be to create a mutable Integer type when passing values... ?Class IntBox Field value:Int Method New( value:Int ) Self.value=value End Method ToInt:Int() Return value End Method ToString:String() Return String(value) End End Function DoThis(x:Int) ''doesn't work with x:IntBox either x=x+1 End Function Main() Local testx:IntBox = New IntBox testx = 1 DoThis(testx) '' x=x+1 Print (testx) '' why isn't this 2? End |
| ||
I'm new to Monkey but it looks as though DoThis() should have a return statement. I assume Monkey parameters are passed by value and not be reference. |
| ||
You are correct, but I was trying to leverage 'boxing' where you can place an object in place of an Int type. I'm starting to think monkey will convert IntBox to an Int, using the ToInt() method, thus ignoring the object. I was THINKING that monkey trans did it the OTHER way, where it would sub x in DoThis() with IntBox.value-- but I understand what's going on now. If I added a method to IntBox called AddOne() Method AddOne() value += 1 End Then I could pass IntBox around like an Int, but use the method AddOne() as well. |