Author Topic: Polymorphism in Lua  (Read 27651 times)

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Polymorphism in Lua
« on: August 19, 2011, 11: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: 4
  • Posts: 1,809
Re: Polymorphism in Lua
« Reply #1 on: August 19, 2011, 11:36:54 PM »
What is Polymorphism?

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #2 on: August 19, 2011, 11: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 20, 2011, 12:06:55 AM »
Polymorphism makes the "output" 100% different EVERY time it is run

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #4 on: August 20, 2011, 12:21:34 AM »
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 20, 2011, 12:47:45 AM »
That is not the meaning of polymorphism

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #6 on: August 20, 2011, 12:54:53 AM »
In coding it is :)

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #8 on: August 20, 2011, 12:59:06 AM »
Pilchard, you know anything about it?

Orion63

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 73
Re: Polymorphism in Lua
« Reply #9 on: August 20, 2011, 01:13:56 AM »
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: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #10 on: August 20, 2011, 01:32:33 AM »
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: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #11 on: August 20, 2011, 01:35:01 AM »
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: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #12 on: August 20, 2011, 01:38:59 AM »
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 20, 2011, 01:43:48 AM by Aino »

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #13 on: August 20, 2011, 01:47:09 AM »
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: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #14 on: August 20, 2011, 02:00:41 AM »
I guess I'll keep running with arrays untill I know more about them in Lua :/

Orion63

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 73
Re: Polymorphism in Lua
« Reply #15 on: August 20, 2011, 02:17:42 AM »
Pilchard is pretty much right.

Something is a (kind of) class with some stats(x, y, hp, hunger), with their respective retrieving methods(since we're talking about OOP), and CreateSomething(n) creates n instances(objects) of the Something class with mostly random numbers...

Wrapping it up - Someone trying to hack OOP into Lua,  and as far as I can see...succeeding.

Quote
"Creature1.X"

Code: [Select]
Creature1 = {X = 50, Y = 30}
You can now call Creature1.X and Creature1.Y

Want to create a single instance of creature without that much work:

Code: [Select]
function CreateSingleCreature(x, y) do
   return {X = x, Y = y}
end

Want to create several?

Code: [Select]
function CreateSeveralCreature(num) do

   CreaturesArray = {}

   for i = 1, num do
      CreaturesArray[i] = CreateSingleCreature( math.random(0, 50) , math.random(0, 50) )
   end

end

Now you have a array with num creatures.

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #16 on: August 20, 2011, 02:24:59 AM »
I guess arrays is more useful in Lua than elsewhere :)

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #17 on: August 20, 2011, 02:44:58 AM »
What about matrices?

Example:

CreatureColour[ID].R

How do you set that up?

Orion63

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 73
Re: Polymorphism in Lua
« Reply #18 on: August 20, 2011, 03:14:20 AM »
Code: [Select]
function CreateSingleCreature(x, y, r, g, b) do
   return {X = x, Y = y, Color = {R = r, G = g, B = b} }
end

You mean this?

You can get get for example the Red element like this:
Code: [Select]
randomCreature = CreateSingleCreature(fill it all with random values)
randomCreature.Color.R

I guess arrays is more useful in Lua than elsewhere :)

I've only started programming this week, but I personally dislike Lua's array/table system.
While Lua's table is a one big swiss army knife(Nothing wrong with that, just personal opinion), for example Python has everything well separated, and each tool serves for different purpose.
Maybe with time...

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #19 on: August 20, 2011, 03:23:17 AM »
Only a week ago??
You're catching up info fast then, I've been sitting for 7 months soon :)

But Lua's array system is very efficient, it can be infinite, atleast thats what I've been getting... Also, with matrices it's super effective catching up things that lazy people would skip :P

Orion63

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 73
Re: Polymorphism in Lua
« Reply #20 on: August 20, 2011, 03:35:55 AM »
Only a week ago??
You're catching up info fast then, I've been sitting for 7 months soon :)

But Lua's array system is very efficient, it can be infinite, atleast thats what I've been getting... Also, with matrices it's super effective catching up things that lazy people would skip :P

^^ Nothing is infinite, and pretty much every programming language now-a-days has an awesome array system(which in 70% of the cases derive from C). You rarely decide to learn a new programming language, because an universal feature is the best implementation. You either get curious with a new language either, because you need it, or like Alan Perlis said: "A language that doesn't affect the way you think about programming, is not worth knowing".

From the last case, languages that stand out: Haskell/Lisp(Some dialects more than others)/J(or APL if you're feeling particularly hardcore).

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #21 on: August 20, 2011, 07:10:56 AM »
Nothing is infinite
O RLY?

Primes + real integers to name just two.

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #22 on: August 20, 2011, 07:28:39 AM »
One more thing is infinite : Dividing by 0 :D

Orion63

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 73
Re: Polymorphism in Lua
« Reply #23 on: August 20, 2011, 08:06:05 AM »
Not what I meant ^^. I was actually talking about physical things, but I probably could have made it way more explicit. Sry ;)

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #24 on: August 20, 2011, 08:22:15 AM »
The universe is practically infinite, it expands so fast that you can't reach the end...

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #25 on: August 20, 2011, 06:29:54 PM »
One more thing is infinite : Dividing by 0 :D

That's undefined, which is slightly different. Something infinite has no end, but we don't even know that for certain about n/0.

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #26 on: August 24, 2011, 03:14:55 PM »
Pilchard, it must be infinite, else some weirdos guys would've used time to calculate it. You never know what people do...

Orion63

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 73
Re: Polymorphism in Lua
« Reply #27 on: August 24, 2011, 03:52:10 PM »
Pilchard, it must be infinite, else some weirdos guys would've used time to calculate it. You never know what people do...

Something that is undefined, does not mean it hasn't been discovered yet, it means it has no solutions. Like the intersection of parallel lines.(I can't confirm the relation with this comparison. There's a distinct line between impossible/undefined functions and I get distracted way too easily in math class...)

And yes, most of the time, zero divisions can be seen as infinity, but really...they are not worth the trouble...

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #28 on: August 24, 2011, 08:57:43 PM »
Parallel lines do intersect. They intersect at an infinite distance from any given point. [/mathnerd]

However, n/0 is undefined, but can be treated as infinite in many cases. Lua being Lua, it has various cases where it isn't[/] infinite but undefined and bleh...

http://lua-users.org/wiki/MathLibraryTutorial  The section on math.huge tells you what Lua treats as infinite and what it does not.

Orion63

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 73
Re: Polymorphism in Lua
« Reply #29 on: August 24, 2011, 09:37:36 PM »
Actually parallel lines only intersect at infinite distance in Non-Euclidean geometry, not in Euclidean geometry, and even there only parallel geodesics intersect at infinity, since geodesics can also be ultra-parallel(which do not intersect at infinity).
On a additional note, Non-Euclidean geometry, makes your head hurt.


Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #30 on: August 24, 2011, 09:41:40 PM »
I think I'll flee fromt his chat, it's too advanced for my current level of knowledge :S