Constant Objects?
Monkey Forums/Monkey Programming/Constant Objects?
| ||
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? |
| ||
?Const i:Int=$ffffffFF |
| ||
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". |
| ||
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. |
| ||
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. |
| ||
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? |
| ||
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 |
| ||
my example showed how he can use a rgba value as a constant in relation of his row of source code. |
| ||
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! :) |
| ||
Private Global MyPrivateColor:=New Color(255,255,255) Public Function MYCOLOR:Color(); Return MyPrivateColor; End MYCOLOR acts like a constant in this scenario. |
| ||
So long as Color has no public methods that can change it (or public fields)! |