Type safe constants :)

BlitzMax Forums/BlitzMax Programming/Type safe constants :)

fredborg(Posted 2006) [#1]
Hi,

I just discovered you can do type safe constants... pretty sweet :)
Strict

Type bob

	Const IS_HAPPY:Int	= 1
	Const IS_HUNGRY:Int	= 2

	Field mood:Int

	Method New()
		mood = Rand(0,3)
	EndMethod

	Method PrintMood()
		
		If mood | IS_HAPPY
			Print "I'm happy"
		EndIf
		
		If mood | IS_HUNGRY
			Print "I'm hungry"
		EndIf
		
	EndMethod

EndType

' Won't work :)
' Print IS_HAPPY
' Will work :)
' Print bob.IS_HAPPY

Local b:bob = New bob
Print "Bob says:"
b.PrintMood()



Diablo(Posted 2006) [#2]
Welcome to the world of, erm, Type Safe Consts. Please close the gate behind you.


H&K(Posted 2006) [#3]
lol,

Dont tell anyone ;)


WendellM(Posted 2006) [#4]

Executing:untitled1.exe
Bob says:
I'm happy
I'm hungry

Process complete

Poor Bob seems to have an eating disorder....

(And yes, Consts inside Types are pretty sweet.)


Dreamora(Posted 2006) [#5]
What you found there is known as Scoping and has been used by many to group constants etc into usefull logical groups as a type is a seperated namespace (for const, global and function)