Good evening,
I have an idea: would it be nice to have some kind of "small object oriented programming" by using types?
Here is an example:
TYPE Monster
HitPoints%
Speed%
Strength%
Statima%
ENDTYPE
TYPE Hydra INHERITS FROM Monster
NumberOfHeads%
ENDTYPE
TYPE Medusa INHERITS FROM Monster
ChanceToTurnToStone#
LengthOfHair%
NumberOfSnakes%
ENDTYPE
In the example the Hydra would have the additional variable NumberOfHeads% and the Medusa would additionally have ChanceToTurnToStone#, LengthOfHair% and NumberOfSnakes%.
This would make the code better structured and easier to read by the programmer.
What do you think about the idea?
Best wishes,
Miroslav
Well, inheritance is OOP at its core and thus not supported. I guess you have tried it, but since you can include type variables inside another type you can do:
TYPE Monster
HitPoints%
Speed%
Strength%
Statima%
ENDTYPE
TYPE Hydra
BaseStats AS Monster
NumberOfHeads%
ENDTYPE
TYPE Medusa
BaseStats AS Monster
ChanceToTurnToStone#
LengthOfHair%
NumberOfSnakes%
ENDTYPE
Then access the basic data of a monster with something like Medusa.BaseStats.HitPoints and so on.
I like the idea, and I wish to use "simple OOP" in GLBasic too. I'm thinking to make a pre-processor for the language that takes OOP-GLBasic and outputs common GLBasic code, but it's very complex :S. I proprose this syntax like in VB.Net:
QuoteTYPE Hydra
INHERITS Monster
NumberOfHeads%
ENDTYPE
Quote from: Ruidesco on 2012-Jul-17
Well, inheritance is OOP at its core and thus not supported. I guess you have tried it, but since you can include type variables inside another type you can do:
TYPE Monster
HitPoints%
Speed%
Strength%
Statima%
ENDTYPE
TYPE Hydra
BaseStats AS Monster
NumberOfHeads%
ENDTYPE
TYPE Medusa
BaseStats AS Monster
ChanceToTurnToStone#
LengthOfHair%
NumberOfSnakes%
ENDTYPE
Then access the basic data of a monster with something like Medusa.BaseStats.HitPoints and so on.
Well said. I've been using this method and it's quite nice.
The only limitation is polymorphism. Having an array of mixed subtypes remains impossible. However, it's not such a huge deal if you just organize your objects properly.
some off topic: GLBScript supports OOP. Inheritance and late binding is fully supported (even ABSTRACT) and of course methods.