Is it possible to create a polymorphic hierarchy with Lua?

After taking AP Computer Science, I became quite fond of Java’s ability to create and maintain hierarchies with relative ease. Is it possible to replicate these structures in Corona?

For example, making an abstract class, calling a super() in a subclass’ constructor. Overriding a subclass’ functions with different parameters, etc?

In Java:

public abstract class example { --some private instance variable public example() { } public void getSomething(); } --subclass of example public class subclass { public subclass() {super();} public void getSomething() { System.out.println("I got something!"); } }

Is this feasible with Lua?

What you’re showing is inheritance and that is possible in Lua.

There are many OOP solutions for Lua, even some in the marketplace I believe.  That said, I use 30log

https://github.com/Yonaba/30log

Docs: https://github.com/Yonaba/30log/wiki

You can also have hierarchies of classes, where the children have functions of the same name as the parent class, but the child functions do something different.

shape -\> circle shape -\> square circle:draw() quare:draw()

That kind of thing.

I have some examples of OOP buttons classes laying around somewhere: https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2016/03/oop_img_button.zip

That is an OLD example using an old version of 30log.  I have a newer version somewhere, but I’m sure where right now.

@everyone - Feel free to correct me if I’m misspoken above.  It is late and I’m tired, so there is the possibility I goofed.

I recommended you 30log (more information on wiki) and midclass (more information on wiki). I have used 30log for Berry.

Lua isn’t like java. You can put what ever you want in a value (function, array, string, object).

If you want to have inheritance you can also do like this:

myClass={} myClass.something="something"--a property local function function1(){}--a method myClass.function1=function1 -- add everything of your parent in my class mySubClass={}--my sub class who get property of my super class mySubClass.parent=myClass -- access to all data of my super class mySubClass.parent.function1() -- call a function of my super class

(I think there is a synthax problem)

With lua it’s easy to have very complex inheritance. Everything is possible.

If you are crazy you can have two class who depend of each other :

class1={} class2={} class1.otherClass=class2 class2.otherClass=class1

it’s useful to link object between them.

In other language it’s quite often too hard to do this, to avoid this difficulties they save id of object to link them. 

Yeah, that’s what I needed, inheritance. Thanks!

Is it possible to make multiple constructors for a class?

For example, say I have a module, and I create two.new functions. 

I really like Classy OOP on the marketplace.  I just started playing around with it and I find the information flow and class constructor process really smooth and efficient.  It’s by Chris @develphant and it’s clean and elegant like his other work ( Coronium Core )

@sdktester 15

If I understand your question correctly, yes, I believe you can make a constructor on each new instance of a class.

true polymorphism:  no, Lua does NOT support having multiple methods of same name with different signatures (number and type of arguments).  why?  because Lua isn’t typed - so it’s a non-starter, the concept doesn’t even make sense.

effective polymorphism:  sure, just define a method like this:

function object:method(...) end

now you can pass whatever you want.

though the more-common variant is to pass a table of parameters

--- @param params Table a table of whatever parameters you wish to pass in -- @usage ..and it's up to YOU to decide which params may/not be required/optional function object:method(params) end

once you finally “get” Lua, you’ll like stop trying to turn it into C++/Java.

What you’re showing is inheritance and that is possible in Lua.

There are many OOP solutions for Lua, even some in the marketplace I believe.  That said, I use 30log

https://github.com/Yonaba/30log

Docs: https://github.com/Yonaba/30log/wiki

You can also have hierarchies of classes, where the children have functions of the same name as the parent class, but the child functions do something different.

shape -\> circle shape -\> square circle:draw() quare:draw()

That kind of thing.

I have some examples of OOP buttons classes laying around somewhere: https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2016/03/oop_img_button.zip

That is an OLD example using an old version of 30log.  I have a newer version somewhere, but I’m sure where right now.

@everyone - Feel free to correct me if I’m misspoken above.  It is late and I’m tired, so there is the possibility I goofed.

I recommended you 30log (more information on wiki) and midclass (more information on wiki). I have used 30log for Berry.

Lua isn’t like java. You can put what ever you want in a value (function, array, string, object).

If you want to have inheritance you can also do like this:

myClass={} myClass.something="something"--a property local function function1(){}--a method myClass.function1=function1 -- add everything of your parent in my class mySubClass={}--my sub class who get property of my super class mySubClass.parent=myClass -- access to all data of my super class mySubClass.parent.function1() -- call a function of my super class

(I think there is a synthax problem)

With lua it’s easy to have very complex inheritance. Everything is possible.

If you are crazy you can have two class who depend of each other :

class1={} class2={} class1.otherClass=class2 class2.otherClass=class1

it’s useful to link object between them.

In other language it’s quite often too hard to do this, to avoid this difficulties they save id of object to link them. 

Yeah, that’s what I needed, inheritance. Thanks!

Is it possible to make multiple constructors for a class?

For example, say I have a module, and I create two.new functions. 

I really like Classy OOP on the marketplace.  I just started playing around with it and I find the information flow and class constructor process really smooth and efficient.  It’s by Chris @develphant and it’s clean and elegant like his other work ( Coronium Core )

@sdktester 15

If I understand your question correctly, yes, I believe you can make a constructor on each new instance of a class.

true polymorphism:  no, Lua does NOT support having multiple methods of same name with different signatures (number and type of arguments).  why?  because Lua isn’t typed - so it’s a non-starter, the concept doesn’t even make sense.

effective polymorphism:  sure, just define a method like this:

function object:method(...) end

now you can pass whatever you want.

though the more-common variant is to pass a table of parameters

--- @param params Table a table of whatever parameters you wish to pass in -- @usage ..and it's up to YOU to decide which params may/not be required/optional function object:method(params) end

once you finally “get” Lua, you’ll like stop trying to turn it into C++/Java.