Class Creation Module

2021 me reading this post right now
Yeah, don’t use this

Hello!

I have created a module which allows for easy creation of Lua classes. I tried to mimic a Roblox Class as best as i can.

  • Every class is a Userdata object
  • Creating classes is very easy

Example
local Class = require(game.ReplicatedStorage.Class)

local MyClass = Class {
    new = function(self)
        self._ = {
              name = "MyClass"
        }
        self.Property = "Foo"

        function self:Function()
            return self.Property .. "Bar"
        end
    end
}

local Object = MyClass.new()
print(typeof(Object)) -- Prints: userdata
print(Object.Property) -- Prints: Foo
print(Object:Function()) -- Prints: FooBar
print(Object.NonExisting) -- Error: NonExisting is not a valid member of MyClass
Documentation

Before using this Module you have to Require it of course

local Class = require(game.ReplicatedStorage.Class)

This module will return a table with a __call metamethod.
The argument is a table with a key new, The value of new is a function. This function is the constructor. As of right now the module only supports the .new() constructor. I will maybe create version 2 with more features. A list of features i want to add is in the Version 2 area.

The function is called with 1 or more arguments. The first argument is a table. This table will hold the properties and functions for this Class. You do NOT need to return this table. The other arguments are the arguments passed into the function call. Whatever its returns is returned to the function call. The return values are

  • The constructed object
  • The values returned by the constructor

Example Table

{
   new = function(self)
      self._ = {
         name = "ExampleClass"
      },

      self.Property = "This is a Example"
      function self:Function()
         print("Hello World!")
      end
   end
}

The table passed in as the first argument also can have a special key. _ this is another table which is not accesible in the real Object. right now this holds the meta table. This is used to overwrite the default metatable for the Object. Extra metamethods can be added in the future

The _ table can also have the key name this is the name of the Class

Version 2

These are the features may come in Version 2 If of course i ever make Version 2

  • Module.Inherits for inheritance
  • The __readonly metamethod for marking read-only values
  • The __writeonly metamethod for marking write-only values, Usefull for Callbacks
  • Property Type checking
  • Classes with Multiple constructors

Feel free to suggest your own feature(s) you want in Version 2!


The module is available here

If you experience any bugs. Please let me know.

13 Likes

Looks cool, but your code examples look like they have syntax errors?

        self. _ = {
              name = "MyClass"
        },
        self.Property = "Foo"

        self:Function()
            return self.Property .. "Bar"
        end

Here you have a space between self. and your _ =; self. _=. This should be self._ = .

You are also attempting to do self:Function() end which will not work. You need to have the function keyword, and you can not define named functions in a table the way you’re trying to. Try somehting like this.

self.Function = function(self)
    return self.Property
end

You are also missing a , after Property = "Foo"

1 Like

Oh thanks, I wrote the example code too fast without first testing it

Edit: Also the setting of the properties is not done in a table. It is done in the new function