it seems that 30log does not follow all definitions of OOP, thus not fulfilling my desire. Maybe I lack to know how this is done right in 30log, so I want to ask for advice.
Polymorphic code allows child classes to effectively override functions of the parent classes. The object itself ends up with different/new implementations of the base functions. If the parent class code executes a function that is overridden in the child class, it will jump into the child implementation, since the base implementation is not existent on the object, anymore.
Here is a brief example how it should work, written in C# (because its OOP and its syntax is easy):
Code: Select all
using System;
class A {
public A() {
Console.WriteLine(test());
}
protected virtual string test() {
return "A";
}
}
class B : A {
public B() : base() {
}
protected override string test() {
return "B";
}
}
public class Program
{
public static void Main()
{
new A(); // prints "A"
new B(); // prints "B"
}
}
Here is a similar implementation of the above code using lua and 30log:
Code: Select all
local class = require "30log"
local A = class("A")
function A:init()
print(self.test())
end
function A:test()
return "A"
end
local B = A:extend()
function B:init()
B.super:init()
end
function B:test()
return "B"
end
A() -- prints "A"
B() -- should print "B", but prints "A"
The problem is very obvious. The constructor of B calls the constructor of A. The constructor of A prints the returned value of the test function. Since the test function is overridden in B, it should use the test function of B, even when called by A, since the object is B that inherits from A, not A. 30log, however, does not do this. It uses only the implementation on the class that it was called from.
Since lua has no real OOP and just works with tables, I see the problem. the super table of B has the test implementation of A. Imo, it should somehow know that the test function was overridden in B and call this instead, or do I do something wrong here?