Types for Safety

BlitzPlus Forums/BlitzPlus Programming/Types for Safety

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Just letting a few ideas/things i do out for discussion.

A difficult error in blitz can be where a variable is spelled wrongly, causing a newly created variable of the same name to be used instead, with the value 0.
This kind of bug can be hard to track.

As many may, using types almost exclusively, greatly reduces this risk as if you mispel either the type or the field name, you get a syntax error.

But what if, by accident (Im mainly a C++ programmer so this is something i do), you do something like:
Type RectType
field Left,Top,Bottom,Right   ; just ignore that Left is an inbuilt function as this is just an example.
end type;

Function SetRect(this.RectType,inLeft,inTop,inRight,inBottom)
	left = inLeft
	Top = inTop
	Right = inRight
	Bottom = inBottom
End function

By accident, instead of:
Function SetRect(this.RectType,inLeft,inTop,inRight,inBottom)
	this\left = inLeft
	this\Top = inTop
	this\Right = inRight
	this\Bottom = inBottom
End function


Well...oops.
Here i have a questionable solution to this, whereby a compile time error would be generated by the first example.
Steps:
1.Define a type that wont be used or anything.
2.Create a global variable of that type for each field in your types, and set to Null.
And the Result is=>
If you try and use a variable with that name that is not a type field, you get an arithmetic syntax error as the operation you expect wont be applicable to that type.
So, for example, the above would (IMO) be more safely written as:

Type SyntaxErrorChecker
	Field FieldSyntaxErrorChecker
End Type


Type RectType
field Left,Top,Bottom,Right
end type
global Left.SyntaxErrorChecker=Null
global Top.SyntaxErrorChecker=Null
global Bottom.SyntaxErrorChecker=Null
global Right.SyntaxErrorChecker=Null

Function SetRect(this.RectType,inLeft,inTop,inRight,inBottom)
	this\left = inLeft
	this\Top = inTop
	this\Right = inRight
	this\Bottom = inBottom
End function


Any thoughts/opinions on this idea?