Skip to content

implement copies only functions IF there are not already present ... may I suggest this version ? #27

Description

@LKSLukeStrike

Because I my code I also need to have access to variables and be able to "force" even in case of already present this is my version of "implement"

`function Classic:defaultArg(_arg, _default, ...) -- return _arg if _arg exists in allowed ... else return _default
for _, _allowed in ipairs({...}) do
if _arg == _allowed then return _arg end
end
return _default
end

function Classic:implement(_new, _type, ...) -- implement vars or fcts or both (depending of _type) if they exist or not (depending of _new)
_new = Classic:defaultArg(_new, "all", "new", "all") -- new = only if not existing, all = even existing
_type = Classic:defaultArg(_type, "all", "var", "fct", "all") -- only vars, fcts or all
for _, _classic in ipairs({...}) do
for _key, _val in pairs(_classic) do
if self[_key] ~= nil and _new == "new" then break end
if type(_val) == "function" and _type == "vars" then break end
self[_key] = _val
end
end
end
`
Notes:

  • I have changed "Object" into "Classic" ... after all the lib is called "CLassic" isn't it ?
  • defaultArg is just an helper to assign a default value to an arg if it not allowed

Now I can call implement like this:
`local CCharacter = CEntity:extend() -- characters

CCharacter:implement(nil, nil, CSpriteFG)
`
Hope it helps ?
LKS

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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