strict and superstrict

BlitzMax Forums/BlitzMax Beginners Area/strict and superstrict

yossi(Posted 2014) [#1]
what is the difference between strict and superstrict ?


degac(Posted 2014) [#2]
Fast example
Strict
Local i=10
Print i

SuperStrict
Local i:int=10 'required to specity :Int type
Print i


Strict
Function Calc(val)
	Print val+2
End Function


Calc(2)



SuperStrict
Function Calc:int(val:int)
	Print val+2
End Function
Calc(2)


Strict requires to the user to define any single variables.
With SuperStrict you need to specify the type of any variables, and return value from functions.

The best way is to use SuperStrict


yossi(Posted 2014) [#3]
thank you.


dawlane(Posted 2014) [#4]
To clarify what degac says.
If you didn't use Strict or SuperStrict you don't have to declare variables. You can just write code and declare variables on the fly.
The problem with this method is that you can write code like so
For k=1 To 10
	sum=num+k ' num should be sum.
Next
Print sum
but if you miss spell a variable like the example above. Then it will assume it to be a new variable and give it an initial value that will lead you to spend hours debugging.

Now Strict force you to declare variables before use. It says this in the documentation (Language Reference->BASIC Compatibility). This solves the miss spelling problem as an error would be generated, but not if you forget to declare it's type.
Strict
Local k=0
Local sum=0.0 ' didn't declare a type of float here 
For k=1 To 10
	sum=sum+k
Next
Print sum ' Wanted to output a floating point number
Now SuperStrict hasn't got a good example or explanation in the documentation. What it does is to forces you to not only to declare a variable before use, but also it's data type. It also forces you specify the expected data type in the parameters of a function as well as a return data type from a functions.
SuperStrict ' Enforce declarations and types
Local k=0 ' Throws a compile error replace with k:Int
Local sum:Float=0.0
For k=1 To 10
	sum=Add(sum, k)
Next

Function Add:Float( s:Float, k:Float ) ' We can cast k from a int to a float
	Print "k:="+k
	Return s+k
End Function

Print sum ' Wanted to output a floating point number

Using SuperStrict is the best way to go, and even more so when you wish to use C/C++ code or pre-compiled libraries with you BlitzMax programs.