Results 1 to 3 of 3

Thread: Automagical override, something for discussion

  1. #1
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Automagical override, something for discussion

    I just discovered the member variable duplication is checked when using inheritance, but in case of functions it is not the case.

    This allows cool tricks like:
    Uses "Console"
    
    Type Vector2D
      x As Single
      y As Single
      
      ToString As Function
    End Type            
    
    Function Vector2D.ToString() As String
      Return Format$(Me.x, "#.00")+","+Format$(Me.y, "#.00")
    End Function   
    
    ' --
    
    Type Vector3D
      Vector2D
      z As Single   
      
      ToString As Function ' -- Overrides the base ToString automagically
    End Type     
    
    Function Vector3D.ToString() As String
      Return Format$(Me.x, "#.00")+","+Format$(Me.y, "#.00")+","+Format$(Me.z, "#.00")
    End Function
    
    Dim v2 As Vector2D
    v2.x = 1
    v2.y = 2
    
    Dim v3 As Vector3D
    v3.x = 1
    v3.y = 2
    v3.z = 3
    
    PrintL "v2 = " + v2.ToString()
    PrintL "v3 = " + v3.ToString()
    
    WaitKey
    
    Here I simply redefine the ToString in ancestor and no error is raised.

    Question for further discussion - is it good or bad ?
    Maybe such a hacks should be allowed, but only with OVERRIDE prefix for the members (both variables and functions)?


    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

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    When you type something like
    Type Vector3D
      Vector2D
    ...
    End Type
    
    thinBasic assumes you want just to copy Vector2D elements config (not functions) into Vector3D
    All the functions/methods remains relevant to the type in which they are declared

    It is not inheritance but just saving of keyboard typing
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  3. #3
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    Hehe,

    I remember we called it inheritance in Journal

    And as holy ThinBasic manual says for example in topic for 1.7.0.0 "Due to new TYPE inheritance recently added in previous thinBasic version, it was not anymore possible to add an element whose name was the same as an already defined UDT"

    I know it currently works more or less like macro expansion, but maybe the function handling in such a case could be something to consider for future.


    Petr
    Last edited by Petr Schreiber; 12-02-2014 at 08:17.
    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

Similar Threads

  1. For discussion: simplified UDT / array of UDT initialization.
    By Petr Schreiber in forum thinBasic General
    Replies: 10
    Last Post: 22-08-2011, 04:41
  2. Learning Oxygen and discussion of ideas for projects
    By kryton9 in forum O2 JIT Compiler / Assembler / Oxygen project
    Replies: 187
    Last Post: 31-07-2009, 09:10
  3. Discussion: Time base for TBEM
    By ErosOlmi in forum TBEM module - thinBasic Event Manager module
    Replies: 12
    Last Post: 10-10-2008, 20:22

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

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