Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: ThinBASIC OOPification

  1. #1
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    55
    Posts
    8,665
    Blog Entries
    2
    Rep Power
    10

    ThinBASIC OOPification

    Hi all,

    in next thinBasic version (I'm almost ready) there will be a little more OOPification of standard ThinBasic TYPEs.
    As you may already aware, you can define Functions inside TYPEs more or less like having methods in classes for languages supporting OOP.

    The new features I've added will be:

    • type extension (similar to class inheritance)
      You can create a TYPE that Extends another TYPE functionality
    • a kind of functions polymorphic when same function name is defined in derived types
      Extended TYPEs will inherit base TYPE elements and functions. Base Functions can be redefined in Extended type


    In case you would like to test new features:
    http://www.thinbasic.biz/projects/th...c_1.9.12.0.zip

    I still need to develop error checking so you might experience some GPF for those new features if you do not follow the rules but it should work.
    Example in \thinBasic\SampleScripts\OOP\Example_030_Extends.tbasic


    A little example count more than 1000 words:
    Uses "console"
     
    '-----------------------------------------------
    '-----------------------------------------------
    Type t2D
      x As Long
      y As Long
     
      Init      As Function
      ToString  As Function
    End Type
     
    Function t2D.Init(ByVal x As Long, ByVal y As Long) As Long
      Me.X = x
      Me.Y = y
    End Function
     
    Function t2D.ToString() As String
      Function = "(" & Me.x & "," & Me.Y & ")"
    End Function
     
    '-----------------------------------------------
    ' t3D extends t2D type adding a new element/property
    ' and redefining 2 type functions
    '-----------------------------------------------
    Type t3D Extends t2d
      Z As Long
     
      Init      As Function
      ToString  As Function
    End Type
     
    Function t3D.Init(ByVal x As Long, ByVal y As Long, ByVal z As Long) As Long
      Me.X = x
      Me.Y = y
      Me.Z = z
    End Function
     
    Function t3D.ToString(Optional ByVal sSep As String) As String
      If sSep = "" Then sSep = ","
      Function = "(" & Me.x & sSep & Me.Y & sSep & Me.z & ")"
    End Function
     
     
    '-----------------------------------------------
    '-----------------------------------------------
     
    Dim p3D As t3D
    Dim p2D As t2D
     
    p3D.Init(10, 20, 30)
    p2D.Init(100, 200)
     
    PrintL "SizeOf p2D    =", SizeOf(p2D)
    PrintL "p2D ToString  =", p2D.ToString
    PrintL
    PrintL "SizeOf p3D    =", SizeOf(p3D)
    PrintL "p3D ToString  =", p3D.ToString("-")
     
    PrintL "----------------------------"
    PrintL "All done, press a key to end"
    WaitKey
    
    Last edited by ErosOlmi; 16-03-2014 at 14:48.
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  2. #2
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    52
    Posts
    1,484
    Blog Entries
    2
    Rep Power
    165
    that sounds like some good news


  3. #3
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,104
    Blog Entries
    5
    Rep Power
    729
    This is good news!

    The OOPification is great, code units I design at work are all encapsulated in "super types".
    I like the ME., it is shorter than THIS. and much more... personal


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  4. #4
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    52
    Posts
    1,484
    Blog Entries
    2
    Rep Power
    165
    Quote Originally Posted by Petr Schreiber View Post
    ...I like the ME., it is shorter than THIS...
    Just to make sure,
    ME <> THIS !
    ME indicates an element of some TYPE inside a type-function, THIS indicates a member of a CLASS inside a class-method. Just so sorry the other languages don't have to care about that difference since none of them has functional types as tB has them

    @Eros,

    for the "init" - which is a certainly often used function to any type - (guess I suggested something similar already long time ago...) - how about some implemented default-Init-Function for all Type's?

    Somewhat alike this:

    Type t_2D
       X as Long
       Y as Long
    End Type
    ' there's no "Init as Function"
    ' simple variables as Long in Type...
    
    ' the built-in Init-function to the Type would work somewhat alike this:
    Function t_2D.Init(Optional Byval X as Long, Optional Byval Y as Long) As DWord
      ' assume there's something alike "Local Me As t_2D At VarPtr(FunctionCalledFrom)"
      Me.X = X
      Me.Y = Y
      Function = Varptr(Me)  ' could return Me-VarPointer for further usage..., <>0 means success...
    
    End Function
    
    ' I (user) just would call it then
    Dim myPoint1 as t_2D
    myPoint1.Init(12,34) ' so X will be 12, Y 34 then
    'or
    Dim myPoint2 as t_2D
    myPoint2.Init() ' here X and Y are 0...
    
    so pretty much the function takes all subsets (like X as Long, Y as Long...) of the type-declaration as Optional function-parameters.

    Now extend this to types with other types embedded

    Type t_2D 
    ' the embedded type
       X as Long
       Y as Long
    End Type
    
    Type t_Maintype
      A As Byte
      B As Byte
      Pos as t_2D ' now this is no "simple variable"...
      C As Byte
    End Type
    ' neither Init here, because built-in...
    
    Dim Example(123) as t_Maintype
    
    ' I could call it like this:
    Local myOverlay As t_Maintype At Example(42).Init(1,2, MKL$(45, 67), 3 )
    
    how would the "built-in Init-function to all types" take my parameters here?

    Perhaps change from "Init" to "Set" or "Be" ?
    Last edited by ReneMiner; 14-03-2014 at 10:09.


  5. #5
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    55
    Posts
    8,665
    Blog Entries
    2
    Rep Power
    10
    Rene,

    I was so happy to see you posting again ... ... but after this post ... not so happy
    I will think about it.
    I already have some nice additional implementations.

    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  6. #6
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    52
    Posts
    1,484
    Blog Entries
    2
    Rep Power
    165
    -oh I was only absent because I was hooked to some excessive "game-testing" for a while (as excessive as I use to code at other times) since I have no real idea what useful script to write next...
    Did the "Like"-expression-developement get any further in the meantime so it will allow UDTs soon?


  7. #7
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    52
    Posts
    1,484
    Blog Entries
    2
    Rep Power
    165
    and... - forget about the init/set/be- we still can use
    With Example(42)
    ...
    


    And to make sure I've understood:
    I need to re-write the functions and re-introduce them for the extended type (t3d) only if I don't want it to use the same function as the base-type (t2D) but to be "user-defined"?
    Last edited by ReneMiner; 14-03-2014 at 19:48.


  8. #8
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    55
    Posts
    8,665
    Blog Entries
    2
    Rep Power
    10
    Yes

    Extended TYPE will get all the items (properties) and functions (methods) of the base TYPE
    If you create in the extended TYPE a function with the same name of the base Type, in that case the new function will win.
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  9. #9
    Hi Eros,

    Really nice implementation. You don't even need to use the 'class' word!

    My thoughts on inheritance in general:

    Inheritance turns out to be less useful as a major concept of OOP than was originally conceived. Petr mentioned the term 'Supertypes' which I take to mean a collection of object types which can be totally different but have the same interfaces, same function names. For example you could have diverse objects like text, sounds, textures, backgrounds, 3d objects which could all have create() set(), get() render() and delete() methods in common but may be structurally unrelated on the inside.
    Last edited by Charles Pegge; 15-03-2014 at 09:21.

  10. #10
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,104
    Blog Entries
    5
    Rep Power
    729
    Hi Charles!,

    I actually use the term supertypes for any type with functions currently. Your idea of interfaces is good one, and could be pushed further syntactically (Like TYPE GameObject Implements IRenderable, ITransformable )

    Hi Rene!,

    great to see you around again!
    Just to make sure,
    ME <> THIS !
    ME indicates an element of some TYPE inside a type-function, THIS indicates a member of a CLASS inside a class-method.
    Traditionally, BASIC languages use ME for the same thing as THIS, just have a look at VB.NET. I don't differentiate between supertype and class, they are both tools for achieving object oriented design.


    Petr
    Last edited by Petr Schreiber; 15-03-2014 at 08:51.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •