Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Spec clarification regarding metamethods #6

@airstruck

Description

@airstruck

Can you clarify the meaning of this line in the spec:

It SHOULD NOT set any special functions, like metamethods

I don't know if my implementation violates this, since I use __call to instantiate my classes. Here is my class micro-library, base.lua:

return {
    extend = function (self, subtype)
        return setmetatable(subtype or {}, {
            __index = self, 
            __call = function (self, ...)
                local instance = setmetatable({}, { __index = self }) 
                return instance, instance:constructor(...)
            end 
        })
    end,
    constructor = function () end
}

And here is my class commons implementation:

if _G.common_class == false then return end

local Base = require 'base'

_G.common = {
    class = function (name, class, superclass)
        local c = Base.extend(superclass or Base, class)
        c.constructor = class.init
        return c
    end,
    instance = function (class, ...)
        return (class(...))
    end
}

This passes all the tests, but I worry that the __call metamethods on returned classes violate the spec. Am I misunderstanding the spec, or do I need to wrap my returned classes in an extra table or do something else in order not to violate the spec?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions