Class Class

pkgxx/class.moon

A homemade class system, designed to use metatables extensively and to be extremely dynamic.

Differences with moonscript’s builtin class:

  • __init instead of new
  • getmetatable(instance) instead of instance.__class
  • Instances can be called if __call is defined in their class.
  • No built-in inheritance mechanism, but __index can be set.
  • class variables are defined through the __class array, and not through @variable: definitions.
MyClass = Class
    __init: (arg) =>
        self.value = arg.value

    print: =>
        print self.value

myInstance = MyClass value: 3.14

myInstance\print!
-- 3.14

-- Static class attributes.
Static = Class
    __class: {
        Foo: 42
    }

print Static.Foo
-- 42

Constructors

Class (name, def) object

Arguments
ArgumentTypeDescription
name
def

Instance

__tostring ! object