Page is empty.
It's difficult to implement or there are other reasons ?I don't think that inheritance will ever be added to the language.
I use CallSub as all other B4X developers very widely. But there are problems with not checking types compatibilty at compilation phasePolymorphism is supported through the usage of CallSub (duck typing).
It is not difficult at all. A programming language is not a random bag of features. I don't see it as a good fit for B4X programming language.It's difficult to implement or there are other reasons ?
This is not really related. The compiler can easily warn in such cases. It will be improved.
+1I offer to add to B4x Inheritance + Polymorphism.
class GameCharacter
{
public:
virtual void attack() { std::cout << "The opponent attacks."; }
}
class Knight : public GameCharacter
{
public:
void attack() { std::cout << "The knight thrusts his sword."; }
}
class Archer : public GameCharacter
{
public:
void attack() { std::cout << "The archer fires an arrow."; }
}
'Class module: GameCharacter
Sub Process_Globals
...
Dim KNIGHT = 1 As Int
Dim ARCHER = 2 As Int
...
End Sub
Sub Attack(category as Int)
Select category
Case KNIGHT
Log("The knight thrusts his sword.")
Case ARCHER
Log("The archer fires an arrow.")
Case Else
Log("The opponent attacks.")
End Select
End Sub
'GameCharacter class
Sub Class_Globals
Public mName As String
End Sub
Public Sub Initialize (name As String)
mName = name
End Sub
'Archer class
Sub Class_Globals
Private gc As GameCharacter
End Sub
Public Sub Initialize (name As String)
gc.Initialize(name)
End Sub
Public Sub Attack
Log($"${gc.mName} the archer fires an arrow"$)
End Sub
'Knight class
Sub Class_Globals
Private gc As GameCharacter
End Sub
Public Sub Initialize (name As String)
gc.Initialize(name)
End Sub
Public Sub Attack
Log($"${gc.mName} the brave knight thrusts his sword."$)
End Sub
'main module (b4j - non-ui):
Sub Process_Globals
Private characters As List
End Sub
Sub AppStart (Args() As String)
characters.Initialize
For i = 1 To 100
If Rnd(0, 2) = 0 Then
Dim k As Knight
k.Initialize("k" & i)
characters.Add(k)
Else
Dim a As Archer
a.Initialize("a" & i)
characters.Add(a)
End If
Next
For Each c As Object In characters
CallSub(c, "Attack") 'duck typing
Next
End Sub
or repeat implementation of all methods of base class in the Inheritor class.cObject.bObject.aObject.Method
It's difficult to implement or there are other reasons ?
Maybe it can not be "a good fit for B4A" developers (I'm not so sure) but for B4J developers?It is not difficult at all. A programming language is not a random bag of features. I don't see it as a good fit for B4X programming language.
Excellent, composition/containership works for meComposition is supported. Composition means that you have a class that adds features to another class by holding an instance of that class.