Author Topic: Polymorphism in Lua  (Read 2908 times)

0 Members and 1 Guest are viewing this topic.

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 20
  • Posts: 1,483
  • They'll eat you next!
  • Eufloria: Yes
Polymorphism in Lua
« on: August 19, 2011, 03:35:45 PM »
How does that work?

How do you set it up properly?

And another question, is local the same as "private"?

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Polymorphism in Lua
« Reply #1 on: August 19, 2011, 03:36:54 PM »
What is Polymorphism?

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 20
  • Posts: 1,483
  • They'll eat you next!
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #2 on: August 19, 2011, 03:51:51 PM »
Polymorphism is an object made of the same class, though they are not the same since you can change values and make each one unique. Though I wondered how it works in lua :)

Edit: heres a nice example for it if you know C++:

kmercy

  • Seedling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 34
Re: Polymorphism in Lua
« Reply #3 on: August 19, 2011, 04:06:55 PM »
Polymorphism makes the "output" 100% different EVERY time it is run

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 20
  • Posts: 1,483
  • They'll eat you next!
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #4 on: August 19, 2011, 04:21:34 PM »
No, it's just variables with variables connected to it, like a.something .

kmercy

  • Seedling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 34
Re: Polymorphism in Lua
« Reply #5 on: August 19, 2011, 04:47:45 PM »
That is not the meaning of polymorphism

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 20
  • Posts: 1,483
  • They'll eat you next!
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #6 on: August 19, 2011, 04:54:53 PM »
In coding it is :)

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 17
  • Posts: 899
  • Eufloria: Yes

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 20
  • Posts: 1,483
  • They'll eat you next!
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #8 on: August 19, 2011, 04:59:06 PM »
Pilchard, you know anything about it?

Orion63

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 73
  • We are the Knights who say... NI!
Re: Polymorphism in Lua
« Reply #9 on: August 19, 2011, 05:13:56 PM »
Same name, different class/function, that is what, polymorphism is.
It doesn't really make sense talking about it in Lua, since only with a couple of ugly hacks(which normally result in poorer performance), you can achieve object oriented programming, and as far as I have ear or tried, you can't have two functions with the same name(well, you can, but it won't do you anything). If someone can prove me wrong, please do ;)

Anyhow, for a pseudocode(in Lua) version of polymorphism:

Code: [Select]
function OpenDoor(location, key) do
   door = getDoor(location)
   insert(key, door)
   turn(key, door)
   push(door)
end

function OpenDoor(location) do
   door = getDoor(location)
   push(door)
end


Two functions with the same name, but do different things. In this case, the second since doesn't have arg key, only tries to push the door(This can be extended much further, since in C++ you have to declare variable types, so it's not only about nÂș of variables, but also they're variables.
Yes, I know, this is a very poor example, the best I could come up with.

Also, if you REALLY want to, yes, you can simulate polymorphism in Lua. You just need to check the arguments types(if it's nil, it wasn't provided, so...)

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 17
  • Posts: 899
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #10 on: August 19, 2011, 05:32:33 PM »
In Java, (FAO Aino!) one example of polymorphism is overloaded methods - that is, methods with the same name but different paramenters. Orion pretty much covered it above. I think there is a way of making a class-like structure in Lua, but I'm not sure how... It's somewhere on this forum.

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 20
  • Posts: 1,483
  • They'll eat you next!
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #11 on: August 19, 2011, 05:35:01 PM »
It seems like everybody else has a different meaning of Polymorphism here(except Pilchard, he sends a wiki page :D), but what I was looking after is something like this:

Code: [Select]
function CreateSomethings(num)

Somethings = {}

for i = 1,num do

Somethings[i] = Something(math.random(-1000,1000),math.random(-1000,1000),math.random(50,100),0)

end

end

function Something(x,y,health,hunger)

local self = {

X = x
Y = y
HP = health
Hunger = hunger

}

local GetX() = function

return self.X

end

local GetY() = function

return self.Y

end

local GetHealth() = function

return self.HP

end

local GetHunger() = function

return self.Hunger

end

end

I don't know if that one work, but it seems legit and is Lua style... But whenever I try using something like this, the responses are crash. I've tried before, and I wanna prepare for the next time :)

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 20
  • Posts: 1,483
  • They'll eat you next!
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #12 on: August 19, 2011, 05:38:59 PM »
No Pilchard, what I'm looking for is a way to use the same fuunction(method) to create several "things" that is the same, but yet distinct from eachother. It's like making an arrays to store info of several "things", but much betterin reasons IDK how to explain :P

So an example would be to have 2 arrays:

Code: [Select]

NumberOfThings = 0 --always required when using arrays...

X = {}
Y = {}


This will give the things you need, but using polymorphism:

Code: [Select]

public class Something(){

public Something(x,y){

//Constructor :)

public int X = x
public int Y = y

}

}

//Now to creating objects

//I'm skipping the class intro now and stuff!

Something() Creature1 = new Something(10,55)
Something() Creature2 = new Something(107,-1)
Something() Creature3 = new Something(-50,17)


Some java like language, hope you understand it :P

With that you can call "Creature1.X" or Y, and you can make tons of them without doing much... So how does this work in Lua?
« Last Edit: August 19, 2011, 05:43:48 PM by Aino »

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 17
  • Posts: 899
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #13 on: August 19, 2011, 05:47:09 PM »
You could create a table of tables/array of arrays with non-numeric indexes - Lua supports that. Code later maybe.

Or this:
http://en.wikipedia.org/wiki/Lua_%28programming_language%29#Object-oriented_programming

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 20
  • Posts: 1,483
  • They'll eat you next!
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #14 on: August 19, 2011, 06:00:41 PM »
I guess I'll keep running with arrays untill I know more about them in Lua :/