Idea: Simple "type (object) oriented programming" with GLBasic?

Previous topic - Next topic

Miroslav-Stimac

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
PC: AMD Phenom II X4 955 BE, Microsoft Windows 8 64 Bit, 12 GB DDR3-RAM, 2 TB hard disk, NVidia GTX 560

Home computers:
1. Commodore 64, 1541-II floppy, Final Cartridge 3
2. Commodore Amiga 1200, Blizzard 1230-IV, FPU 50 MHz, 16 MB fast memory, 4 GB Flash HD
3. Atari Mega ST2

Ruidesco

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:

Code (glbasic) Select
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.

hardyx

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

theprotocol

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:

Code (glbasic) Select
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.

metzzo

some off topic: GLBScript supports OOP. Inheritance and late binding is fully supported (even ABSTRACT) and of course methods.
That's no Bug, that's my project!

http://programming-with-design.at/