Types for Safety
BlitzPlus Forums/BlitzPlus Programming/Types for Safety
| ||
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? |
| ||
if you mispel either Hehe. That's my thoughts on it... |
| ||
lol...Well you can see i can even misspell misspell. But my idea was about the second thing (Functions updating types) and automatic 0 value instances being the accidental result of forgetting that in a function habdling a type you have to use the types name then the field name. hm..I suppose i could just write something that parses my blitz files and ensures that things are 'strict' ie identifiers only declared with const, function, type, local and global..and dim...and labels. And field. And all the automatic ones...and userlibs. A further mistake i dont like risking is mispelling a const. But using a type there would be a great waste. |