I'm back for a while from my jaunt into .netland.
Anyway, now that I'm coding some blitzmas the main things I miss are private fields and properties.
We've talked about this before, but I'd like to try again.
Now, private fields can be evil, it is true. It's annoying when the field is there, you just can't access it. However, they serve a very good purpose - prevent the user from inadvertently setting the field to an invalid or null value. This will result in an exception somewhere along the line. It might not even show up except in rare circumstances, if the field is not used much.
It is possible to prevent this by peppering your code with if statements checking the value. This is slower and ugly to boot.
If a field is private then it can only be accessed from the class's methods which presumably deal with it in a safe manner. A well written class can guarrantee that the values of its fields will not be out of range.
However, now with all these private fields if you want a reasonably direct method of modifying them, you need to create get/set proxy methods which rather clutter up the class. This is where properties come in. They allow get/set methods to look like fields. Most importantly, the set method can make sure the value of a field is not set to null.
Properties are also useful for things that aren't even fields. For example I might make a 'Rectangle' class and have fields storing position and size. I could have a property "BottomRight", which returns and receives a point class.
My suggestion for a syntax would be:
Oh, I almost forgot, it'd be nice to have a "getter" attribute for fields which autocreates a get-only property. Also a 'property' attribute, which for non-base types makes sure it can't be nullable. This cuts down the majority of most get/set method clutter.
Anyway, now that I'm coding some blitzmas the main things I miss are private fields and properties.
We've talked about this before, but I'd like to try again.
Now, private fields can be evil, it is true. It's annoying when the field is there, you just can't access it. However, they serve a very good purpose - prevent the user from inadvertently setting the field to an invalid or null value. This will result in an exception somewhere along the line. It might not even show up except in rare circumstances, if the field is not used much.
It is possible to prevent this by peppering your code with if statements checking the value. This is slower and ugly to boot.
If a field is private then it can only be accessed from the class's methods which presumably deal with it in a safe manner. A well written class can guarrantee that the values of its fields will not be out of range.
However, now with all these private fields if you want a reasonably direct method of modifying them, you need to create get/set proxy methods which rather clutter up the class. This is where properties come in. They allow get/set methods to look like fields. Most importantly, the set method can make sure the value of a field is not set to null.
Properties are also useful for things that aren't even fields. For example I might make a 'Rectangle' class and have fields storing position and size. I could have a property "BottomRight", which returns and receives a point class.
My suggestion for a syntax would be:
Property BottomRight Method Get:Point() return Point.Create(x+w, y+h) End Method Method Set(val:Point) If val <> null Then w = val.x - x h = val.y - y Endif End Method End PropertyConverted by blitzmax compiler to:
Method get_BottomRight:Point() return Point.Create(x+w, y+h) End Method Method set_BottomRight(val:Point) If val <> null Then w = val.x - x h = val.y - y Endif End MethodTo use the property:
Print a.BottomRight.XConverted to:
Print a.get_BottomRight().XOh, and constructors with parameters would be nice! To mark: Pretty please? should only take a day or two.
Oh, I almost forgot, it'd be nice to have a "getter" attribute for fields which autocreates a get-only property. Also a 'property' attribute, which for non-base types makes sure it can't be nullable. This cuts down the majority of most get/set method clutter.