GLBasic forum

Main forum => GLBasic - en => Topic started by: Miroslav-Stimac on 2012-Jul-17

Title: Idea: Simple "type (object) oriented programming" with GLBasic?
Post by: Miroslav-Stimac on 2012-Jul-17
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
Title: Re: Idea: Simple "type (object) oriented programming" with GLBasic?
Post by: 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.
Title: Re: Idea: Simple "type (object) oriented programming" with GLBasic?
Post by: hardyx on 2012-Jul-18
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
Title: Re: Idea: Simple "type (object) oriented programming" with GLBasic?
Post by: theprotocol on 2012-Jul-18
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.
Title: Re: Idea: Simple "type (object) oriented programming" with GLBasic?
Post by: metzzo on 2012-Jul-19
some off topic: GLBScript supports OOP. Inheritance and late binding is fully supported (even ABSTRACT) and of course methods.