diff --git a/docs/reference.md b/docs/reference.md index 02b2278..6446d27 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -731,6 +731,21 @@ properties and methods from another class. Here we extend our Inventory class, and limit the amount of items it can carry. +Whenever a class inherits from another, it sends a message to the parent class +by calling the method `__inherited` on the parent class if it exists. The +function recieves two arguments, the class that is being inherited and the +child class. + + ```moon + class Shelf + @__inherited: (child) => + print @__name, "was inherited by", child.__name + + -- will print: Shelf was inherited by Cupboard + class Cupboard extends Shelf + nil + ``` + ### Super `super` is a special keyword that can be used in two different ways: It can be diff --git a/moonscript/transform.lua b/moonscript/transform.lua index 075c586..2781ee2 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -950,6 +950,23 @@ Statement = Transformer({ return { } end end)()), + build["if"]({ + cond = { + "exp", + parent_cls_name, + "and", + parent_cls_name:chain("__inherited") + }, + ["then"] = { + parent_cls_name:chain("__inherited", { + "call", + { + parent_cls_name, + cls_name + } + }) + } + }), cls_name } hoist_declarations(out_body) diff --git a/moonscript/transform.moon b/moonscript/transform.moon index f548aa5..a4aa401 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -486,6 +486,18 @@ Statement = Transformer { .group statements } else {} + -- run the inherited callback + build["if"] { + cond: {"exp", + parent_cls_name, "and", parent_cls_name\chain "__inherited" + } + then: { + parent_cls_name\chain "__inherited", {"call", { + parent_cls_name, cls_name + }} + } + } + cls_name } diff --git a/tests/outputs/class.lua b/tests/outputs/class.lua index 5466e1c..4d06d6e 100644 --- a/tests/outputs/class.lua +++ b/tests/outputs/class.lua @@ -37,6 +37,9 @@ Hello = (function() end }) _base_0.__class = _class_0 + if _parent_0 and _parent_0.__inherited then + _parent_0.__inherited(_parent_0, _class_0) + end return _class_0 end)() local x = Hello(1, 2) @@ -79,6 +82,9 @@ Simple = (function() end }) _base_0.__class = _class_0 + if _parent_0 and _parent_0.__inherited then + _parent_0.__inherited(_parent_0, _class_0) + end return _class_0 end)() local Yikes @@ -112,6 +118,9 @@ Yikes = (function() end }) _base_0.__class = _class_0 + if _parent_0 and _parent_0.__inherited then + _parent_0.__inherited(_parent_0, _class_0) + end return _class_0 end)() x = Yikes() @@ -151,6 +160,9 @@ Hi = (function() end }) _base_0.__class = _class_0 + if _parent_0 and _parent_0.__inherited then + _parent_0.__inherited(_parent_0, _class_0) + end return _class_0 end)() Simple = (function() @@ -187,6 +199,9 @@ Simple = (function() end }) _base_0.__class = _class_0 + if _parent_0 and _parent_0.__inherited then + _parent_0.__inherited(_parent_0, _class_0) + end return _class_0 end)() x = Simple() @@ -227,6 +242,9 @@ Okay = (function() end }) _base_0.__class = _class_0 + if _parent_0 and _parent_0.__inherited then + _parent_0.__inherited(_parent_0, _class_0) + end return _class_0 end)() local Biggie @@ -268,6 +286,9 @@ Biggie = (function() end }) _base_0.__class = _class_0 + if _parent_0 and _parent_0.__inherited then + _parent_0.__inherited(_parent_0, _class_0) + end return _class_0 end)() local Yeah @@ -307,6 +328,9 @@ Yeah = (function() end }) _base_0.__class = _class_0 + if _parent_0 and _parent_0.__inherited then + _parent_0.__inherited(_parent_0, _class_0) + end return _class_0 end)() local What @@ -346,6 +370,9 @@ What = (function() end }) _base_0.__class = _class_0 + if _parent_0 and _parent_0.__inherited then + _parent_0.__inherited(_parent_0, _class_0) + end return _class_0 end)() Hello = (function() @@ -391,6 +418,9 @@ Hello = (function() end }) _base_0.__class = _class_0 + if _parent_0 and _parent_0.__inherited then + _parent_0.__inherited(_parent_0, _class_0) + end return _class_0 end)() do @@ -442,6 +472,9 @@ CoolSuper = (function() end }) _base_0.__class = _class_0 + if _parent_0 and _parent_0.__inherited then + _parent_0.__inherited(_parent_0, _class_0) + end return _class_0 end)() x = self.hello @@ -494,6 +527,9 @@ ClassMan = (function() self.hello = 3434 self.world = 23423 self.red = function(self) end + if _parent_0 and _parent_0.__inherited then + _parent_0.__inherited(_parent_0, _class_0) + end return _class_0 end)() x = self @@ -547,6 +583,9 @@ if something then hello = "world" self.another = "day" print("yeah") + if _parent_0 and _parent_0.__inherited then + _parent_0.__inherited(_parent_0, _class_0) + end return _class_0 end)() end diff --git a/tests/outputs/export.lua b/tests/outputs/export.lua index a7de1b2..50e1328 100644 --- a/tests/outputs/export.lua +++ b/tests/outputs/export.lua @@ -34,6 +34,9 @@ Something = (function() end }) _base_0.__class = _class_0 + if _parent_0 and _parent_0.__inherited then + _parent_0.__inherited(_parent_0, _class_0) + end return _class_0 end)() local What