Type safe constants :)

BlitzMax Forums/BlitzMax Programming/Type safe constants :)

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()


Welcome to the world of, erm, Type Safe Consts. Please close the gate behind you.

lol,

Dont tell anyone ;)


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.)

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)