Just checking preprocessor rules

Monkey Forums/Monkey Programming/Just checking preprocessor rules

Gerry Quinn(Posted 2013) [#1]
I have been testing, and the following works:

Strict

#CHOICE = 1
' Strings work too, and True/False

Function Main:Int()
	#If CHOICE = 1
		Print "a"
	#Else
		Print "b"
	#End
	Return 0
End


Is this completely sorted now, i.e. it will automatically detect integers, strings or booleans, and work with all targets?

I just want to check as it is not documented as yet.


AdamRedwoods(Posted 2013) [#2]
As far as I know, it's still a little strange, but works reliably with simple "1/0" variable.

I believe the preprocessor works like this:
cannot do > or < or even <>. only =

if it's set once, it cannot be set again. all other sets will be ignored.

it uses "" it is not set, but the preprocessor will recognize the variable. i use this to set one variable in one module, and if the module is included, it activates another section in a different module.

if you use true or false, it'll give a string error. but you can set something to true, but you need to check for 1.
#TESTER = True

Import mojo

#if TESTER=True ''this will cause an error, but not if checking for 1
Const test:int=5
#Endif



Gerry Quinn(Posted 2013) [#3]
Okay. Looks like True is implemented internally as 1 (or maybe as not zero).

I'd guess the 'set once' rule is so you don't have to worry about which module (in which it is set) is loaded first. One could always use GUID-like constants if it is necessary to set one inside a module instead of globally for a program.

= covers the main uses of the preprocessor anyway, I think. > would be nice to have at some stage for serialisation (so you can easily load saves from previous versions), though.


Nobuyuki(Posted 2013) [#4]
Wish we had <> and logic operators, if we don't already. Seems to work, but maybe it's silently failing on me. Excluding certain targets from blocks of code would be lots easier.


Gerry Quinn(Posted 2013) [#5]
You're right. Though one or two targets can be excluded easily enough using #ELSEIF and #ELSE.

#IF TARGET = "android"
' Excluded
#ELSE
' Code here
#END