Jump to content




A nice, small class implementation.


  • You cannot reply to this topic
16 replies to this topic

#1 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 02 May 2013 - 10:12 AM

So in the process of making my [REDACTED], I thought I'd go ahead and share my nice, simple little class system/function with you guys.

http://pastebin.com/VMgD3GZf
Spoiler

Classes are created by calling the class method.
MyClass = class{
  some = 'vars';
  a = 1;
  b = 2;
  c = 3;
}

-- Class variables are optional
MyClass = class()
MyClass.some = 'vars'
MyClass.a = 1
MyClass.b = 2
MyClass.c = 3

To create an instance, just call the class table.
obj = MyClass()
print(obj.a, obj.b, obj.c) --> 123

In the class, the :init() method is called when you create a class. You can use this to set variables at instance creation.
MyClass = class()

function MyClass:init(name)
  self.name = name or 'NoName'
end

obj = MyClass('LUAAA')
print(obj.name) --> LUAAA

Inheritance is done by using the :extend() method on classes.

Parent = class{
  foo = 'bar';
}

Child = Parent:extend{
  bar = 'baz';
}
print(Child.foo) --> bar
print(Child.bar) --> baz

Finally, instances store their parent class in the __class variable, so you can check the types of certain instances.
Object = class()

obj = Object()
print(obj.__class == Object) --> true

As a usage example, here are some silly classes.
Thing = class()

function Thing:speak()
  print 'I am a thing.'
end

Organism = Thing:extend{
  kind = '';
}

function Organism:init(kind)
  self.kind = kind or self.kind
end

function Organism:speak()
  Thing.speak(self)
  print 'A living thing, in fact.'
  print('I am a '..self.kind)
end

Person = Organism:extend{
  name = '';
  kind = 'human';
}

function Person:init(name)
  self.name = name or self.name
end

function Person:speak()
  Organism.speak(self)
  print('My name is '..self.name)
end

unknown = Thing()
unknown:speak() --> I am a thing.

frog = Organism('frog')
frog:speak()
--> I am a thing.
--> A living thing, in fact.
--> I am a frog

bob = Person('Bob')
bob:speak()
--> I am a thing.
--> A living thing, in fact.
--> I am a human
--> My name is Bob

Hopefully this'll teach you a thing or two about good ol' OO. :)/>/>/>/>

#2 Jarle212

  • Members
  • 198 posts
  • LocationNorway

Posted 02 May 2013 - 10:41 AM

Wery nice OOP, I might start using this methode instead of setting metatables every singel time. :)

#3 GravityScore

  • Members
  • 796 posts
  • LocationLand of Meh

Posted 02 May 2013 - 11:54 AM

Very nice!

Now add static class methods, and the ability to call the superclass init method (like Java's super function) so you don't have to go kind or self.kind for every variable :P

#4 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 02 May 2013 - 12:53 PM

 GravityScore, on 02 May 2013 - 11:54 AM, said:

Very nice!

Now add static class methods, and the ability to call the superclass init method (like Java's super function) so you don't have to go kind or self.kind for every variable :P
I'd like to avoid being like Java for the time being :lol:

Seriously though, you can already call superclass methods, just by using another class's method with your self.

Object = class()
function Object:init(x, y)
  self.x = x
  self.y = y
end

ChildObject = Object:extend()
function ChildObject:init(...)
  Object.init(self, ...)
end

test = ChildObject(5, 5)

I'm not entirely sure what you mean by static class methods though.

#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 02 May 2013 - 01:41 PM

 Kingdaro, on 02 May 2013 - 12:53 PM, said:

I'd like to avoid being like Java for the time being :lol:
Seriously though, you can already call superclass methods, just by using another class's method with your self.
he wants the keyword super though. Also super is in more than Java.

 Kingdaro, on 02 May 2013 - 12:53 PM, said:

I'm not entirely sure what you mean by static class methods though.
static are variables or methods that belong to the actual type (or in this case api/file) as opposed to the object. Link

Edited by theoriginalbit, 02 May 2013 - 01:59 PM.


#6 Sammich Lord

    IRC Addict

  • Members
  • 1,212 posts
  • LocationThe Sammich Kingdom

Posted 02 May 2013 - 01:52 PM

 theoriginalbit, on 02 May 2013 - 01:41 PM, said:

static are variables or methods that belong to the actual type (or in this case api/file) as opposed to the object. Link
Your link is broken but I think I found the link you were looking for http://msdn.microsof...v=vs.80%29.aspx

#7 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 02 May 2013 - 01:59 PM

 Sammich Lord, on 02 May 2013 - 01:52 PM, said:

Your link is broken but I think I found the link you were looking for http://msdn.microsof...v=vs.80%29.aspx
Indeed that is the one that I attempted to link. :) thanks.

#8 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 02 May 2013 - 02:35 PM

Ah, so a static variable would be like a class variable in ruby, then? If I understand you correctly, you could just have the variable in your class and change it accordingly. If you wanted to go the extra mile, you could just declare the count variable nil when the instance is created.

Window = class{
  count = 0
}

function Window:init()
  Window.count = Window.count + 1
  self.count = nil
end

a = Window()
b = Window()
c = Window()
print(a.count, b.count, c.count) --> ...nothing
print(Window.count) --> 3

That or just have the variable separate from the class itself, that would probably be the best approach.

 theoriginalbit, on 02 May 2013 - 01:41 PM, said:

he wants the keyword super though.

Not necessary, because you can just use a class method with an alternate self, as I've demonstrated. Not trying to make a full-fledged class system here, just some convenient OO.

 theoriginalbit, on 02 May 2013 - 01:41 PM, said:

Also super is in more than Java.

I'm aware. The super keyword is in moonscript too. :D

EDIT: Ahaha, I made a goof. Even if you set the count variable to nil in the example I've made, it still prints the count because the instance is indexing the base class, which has a count variable. So yeah, just make it a separate variable.

#9 Jarle212

  • Members
  • 198 posts
  • LocationNorway

Posted 02 May 2013 - 03:42 PM

 theoriginalbit, on 02 May 2013 - 01:41 PM, said:

That or just have the variable separate from the class itself, that would probably be the best approach.

Metatables could solve that problem :) if used in a different way than instantiation.

#10 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 02 May 2013 - 04:04 PM

Could, yes.

Should? Nah.

#11 Jarle212

  • Members
  • 198 posts
  • LocationNorway

Posted 02 May 2013 - 04:20 PM

 Kingdaro, on 02 May 2013 - 04:04 PM, said:

Could, yes.

Should? Nah.

:PPPP

#12 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 02 May 2013 - 07:51 PM

 Jarle212, on 02 May 2013 - 03:42 PM, said:

 theoriginalbit, on 02 May 2013 - 01:41 PM, said:

That or just have the variable separate from the class itself, that would probably be the best approach.
Metatables could solve that problem :) if used in a different way than instantiation.
I didn't say that, you quoted the wrong person.

#13 Jarle212

  • Members
  • 198 posts
  • LocationNorway

Posted 03 May 2013 - 07:47 AM

 theoriginalbit, on 02 May 2013 - 07:51 PM, said:

 Jarle212, on 02 May 2013 - 03:42 PM, said:

 theoriginalbit, on 02 May 2013 - 01:41 PM, said:

That or just have the variable separate from the class itself, that would probably be the best approach.
Metatables could solve that problem :) if used in a different way than instantiation.
I didn't say that, you quoted the wrong person.


Sorry, I deleted the wrong quote tags :P. Fixed now

#14 Eric

  • Members
  • 92 posts
  • LocationUK

Posted 04 May 2013 - 03:44 AM

Nice and concise.

Here's my more complex version, inspired by how python handles classes and descriptors: https://github.com/eric-wieser/pylua

There are some examples there at the bottom of the page.

#15 Mads

  • Members
  • 604 posts
  • LocationCopenhagen, Denmark

Posted 04 May 2013 - 10:27 AM

Object = class()

obj = Object()
print(obj.__class = Object) --> true

Of course it would print true. You are making a statement, which is always true.

#16 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 04 May 2013 - 11:59 AM

 Mads, on 04 May 2013 - 10:27 AM, said:

Object = class()

obj = Object()
print(obj.__class = Object) --> true

Of course it would print true. You are making a statement, which is always true.

Thanks for pointing out the typo instead of being a smartass about it.

For the record, it would actually error instead of printing anything.

#17 Mads

  • Members
  • 604 posts
  • LocationCopenhagen, Denmark

Posted 04 May 2013 - 12:06 PM

Oh. Silly Lua and it's restrictions!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users