Constant Objects?

Monkey Forums/Monkey Programming/Constant Objects?

SHiLLSiT(Posted 2013) [#1]
I've tried this, but this doesn't work:

Const SOME_COLOR:COLOR = New Color(255,255,255)


Is there an alternative to creating constant objects that I am not aware of?


Markus(Posted 2013) [#2]
?
Const  i:Int=$ffffffFF



Polan(Posted 2013) [#3]
Int is not an object in Blitz laungages.
You could always do Global SOME_COLOR:COLOR and pretend it to be constant. The same way we are pretending some class variables are private.
And with monkey being case sensitive? you can say "variable with ALL_CAPS is constant, do not change it".


Gerry Quinn(Posted 2013) [#4]
Or make the object private, with public functions that access copies of it or its fields.

But at the end of the day, it's a lot of cruft to avoid people doing stuff that they should know better than to do anyway. Giving it a name that indicates its intended constant nature is probably the best approach. For many people, ALL_CAPS indicates a constant.


computercoder(Posted 2013) [#5]
Int is not an object in Blitz laungages.

You CAN use int as an Object in Monkey by using the IntObject class. Per Monkey Help:
The IntObject box class can be used to encapsulate an int value inside an object.



Polan(Posted 2013) [#6]
Sure, but it's just class-wrapper, small class with 1 field.
Class IntObject
   Field val%
EndClass

And that make instance of it an object, and object can't be constant right?


rIKmAN(Posted 2013) [#7]
I know you wanted Consts but how about:

Global COLOR_RED:Float[]
Global COLOR_GREEN:Float[]
Global COLOR_BLUE:Float[]

...

COLOR_RED = [255.0, 0.0, 0.0]
COLOR_GREEN = [0.0, 255.0, 0.0]
COLOR_BLUE = [0.0, 0.0, 255.0]

....

Cls(COLOR_RED[0], COLOR_RED[1], COLOR_RED[2]
Cls(COLOR_GREEN[0], COLOR_GREEN[1], COLOR_GREEN[2]
Cls(COLOR_BLUE[0], COLOR_BLUE[1], COLOR_BLUE[2]

etc



Markus(Posted 2013) [#8]
my example showed how he can use a rgba value as a constant in relation of his row of source code.


computercoder(Posted 2013) [#9]
And that make instance of it an object, and object can't be constant right?

Thats right. However, who said you couldn't do the following:
Class IntObj
  Const val% = $FFFFFF
End


You would achieve the same result as a constant, but within an object! :)


ziggy(Posted 2013) [#10]
Private
Global MyPrivateColor:=New Color(255,255,255)
Public
Function MYCOLOR:Color();
    Return MyPrivateColor;
End

MYCOLOR acts like a constant in this scenario.


Gerry Quinn(Posted 2013) [#11]
So long as Color has no public methods that can change it (or public fields)!