{"id":13525984,"url":"https://github.com/megagrump/muun","last_synced_at":"2025-07-13T17:08:41.688Z","repository":{"id":215193095,"uuid":"174710450","full_name":"megagrump/muun","owner":"megagrump","description":"moonscript compatible class implementation for Lua","archived":false,"fork":false,"pushed_at":"2021-06-02T17:33:40.000Z","size":14,"stargazers_count":7,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-24T21:47:47.528Z","etag":null,"topics":["lua","moonscript","oop"],"latest_commit_sha":null,"homepage":"","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/megagrump.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-03-09T15:21:24.000Z","updated_at":"2024-02-11T23:58:37.000Z","dependencies_parsed_at":null,"dependency_job_id":"12f7befe-dae6-4c29-8b93-1a74ecb5b0f2","html_url":"https://github.com/megagrump/muun","commit_stats":null,"previous_names":["megagrump/muun"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/megagrump/muun","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/megagrump%2Fmuun","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/megagrump%2Fmuun/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/megagrump%2Fmuun/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/megagrump%2Fmuun/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/megagrump","download_url":"https://codeload.github.com/megagrump/muun/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/megagrump%2Fmuun/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265175567,"owners_count":23722661,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["lua","moonscript","oop"],"created_at":"2024-08-01T06:01:24.035Z","updated_at":"2025-07-13T17:08:41.671Z","avatar_url":"https://github.com/megagrump.png","language":"Lua","funding_links":[],"categories":["OO"],"sub_categories":[],"readme":"# muun\n\nA minimal Lua class implementation that is compatible with [moonscript](https://github.com/leafo/moonscript) classes.  \n\nmuun can extend classes defined in moonscript from Lua code, and define classes in Lua code that can be extended from moonscript code.\n\n## How to use\n### Extending moonscript classes from Lua code\n```lua\nlocal class = require('muun')\nlocal MoonScriptClass = require('MoonScriptClass') -- the class we want to extend\n\nlocal ExtendedClass = class('ExtendedClass', MoonScriptClass)\n\n -- override constructor\nfunction ExtendedClass:new(x, y)\t\n\tExtendedClass.__super(self) -- calls MoonScriptClass constructor\n\n\tself.x, self.y = x, y\n\tprint(\"Hi! My position is:\", x, y)\nend\n\n-- create an instance\nlocal instance = ExtendedClass(23, 42)\n```\n---\n### Extending classes written in Lua from moonscript code\n```lua\nlocal class = require('muun')\n\nBaseClass = class('BaseClass')\n\nfunction BaseClass:new()\n\tprint(\"Hi from Lua\")\nend\n\nfunction BaseClass:__inherited(cls)\n\tprint(self.__name, \"inherited from\", cls.__name)\nend\n\n```\n```moonscript\nBaseClass = require('BaseClass')\n\nclass Derived extends BaseClass\n\tnew: =\u003e\n\t\tsuper! -- calls the BaseClass constructor\n\t\tprint(\"Hi from moonscript!\")\n\ntest = Derived!\n```\n#### Output\n```\nDerived\tinherited from \tBaseClass\nHi from Lua!\nHi from moonscript!\n```\n---\n### Define a class in Lua\n\nmuun can also be used as a minimal, but fully functional Lua class implementation.\n\n#### Variant 1\n```lua\n-- define class\nlocal MyClass = class('MyClass', {\n\t -- define constructor\n\tnew = function(self, x, y)\n\t\tself.x, self.y = x, y\n\t\tprint(\"Hi! My position is:\", x, y)\n\tend,\n})\n\n-- create instance\nlocal instance = MyClass(23, 42)\n```\n\n#### Variant 2\n```lua\nlocal MyClass = class('MyClass')\n\n -- define constructor\nfunction MyClass:new(x, y)\n\tself.x, self.y = x, y\n\tprint(\"Hi! My position is:\", x, y)\nend\n\n-- create instance\nlocal instance = MyClass(23, 42)\n```\n\n#### Variant 3\n```lua\nlocal MyClass = {\n\t -- define constructor\n\tnew = function(self, x, y)\n\t\tself.x, self.y = x, y\n\t\tprint(\"Hi! My position is:\", x, y)\n\tend,\n}\n\n-- define class\nMyClass = class('MyClass', MyClass)\n\n-- create instance\nlocal instance = MyClass(23, 42)\n```\n---\n### Inheritance\nAll derived classes have a `__super` table that acts as a proxy for the base class. **Note**: `__super` is not an alias for the base class as in MoonScript, it's merely a proxy that allows you to access the base class.\n\nIf a class implements the `__inherited` method, it gets called whenever another class extends this class.\n```lua\nlocal Base = class('Base')\n\nfunction Base:new(...)\n    print(\"Hi from Base!\")\nend\n\nfunction Base:foo()\n    print(\"Foo!\")\nend\n\nfunction Base.__inherited(parent, cls)\n    print(cls.__name .. \" inherited from \" .. parent.__name)\nend\n\nlocal Derived = class('Derived', Base)\n\nfunction Derived:new(...)\n    Derived.__super(self, ...)\n    print(\"Hi from Derived!\")\nend\n\nfunction Derived:foo()\n    Derived.__super.foo(self)\n    print(\"Bar!\")\nend\n\nlocal instance = Derived()\ninstance:foo()\n```\n\n### Output:\n```\nDerived inherited from Base\nHi from Base!\nHi from Derived!\nFoo!\nBar!\n```\n---\n# License\n\nCopyright 2019, 2021 megagrump@pm.me\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmegagrump%2Fmuun","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmegagrump%2Fmuun","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmegagrump%2Fmuun/lists"}