default property - bug or feature

Monkey Forums/Monkey Programming/default property - bug or feature

dmaz(Posted 2011) [#1]
Function Main()
	Local t:test = New test
	Local f:Float

	'why does this work?
	t = 8.0
	
	t.Echo

' sanity check
	Local t2 := New test(5.0)
	
	t = t2
	
	t.Echo

End


Class test
	Field value:Float
	
	Method New( value:Float )
		Self.value = value
	End

	Method Set( value:Float )
		Self.value = value
	End

	Method Get:Float()
		Return value
	End
	
	Method Echo()
		Print value
	End
End
the New method is required to make this work and while the Set method works the Get doesn't seem to. ie t += 1.0 does not work. I was all excited as I have class that wraps a float and this would have been really cool.


marksibly(Posted 2011) [#2]
Hi,

It's part of the 'boxing' mechanism.

An int/float/string can be converted to an object if the object's class has a constructor that takes an int/float/string.

To be able to 'unbox', objects should have a ToInt, ToFloat or ToString method.

Take a look at the 'monkey.boxes' module for an example of boxing/unboxing objects.

This stuff isn't well docced right now - will fix very soon.


dmaz(Posted 2011) [#3]
this is awesome! found one problem though... while the unboxing works for t = t + 1.5, it doesn't for t += 1.5 and just seems to hang.