PDA

View Full Version : Automagical override, something for discussion



Petr Schreiber
12-02-2014, 00:17
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

ErosOlmi
12-02-2014, 01:12
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 :)

Petr Schreiber
12-02-2014, 08:12
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" :p

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