add inherited callback to classes

This commit is contained in:
leaf corcoran 2012-09-08 17:52:26 -07:00
parent 1ac76d147a
commit bc13b1b8cc
5 changed files with 86 additions and 0 deletions

View File

@ -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. 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
`super` is a special keyword that can be used in two different ways: It can be `super` is a special keyword that can be used in two different ways: It can be

View File

@ -950,6 +950,23 @@ Statement = Transformer({
return { } return { }
end end
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 cls_name
} }
hoist_declarations(out_body) hoist_declarations(out_body)

View File

@ -486,6 +486,18 @@ Statement = Transformer {
.group statements .group statements
} else {} } 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 cls_name
} }

View File

@ -37,6 +37,9 @@ Hello = (function()
end end
}) })
_base_0.__class = _class_0 _base_0.__class = _class_0
if _parent_0 and _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
return _class_0 return _class_0
end)() end)()
local x = Hello(1, 2) local x = Hello(1, 2)
@ -79,6 +82,9 @@ Simple = (function()
end end
}) })
_base_0.__class = _class_0 _base_0.__class = _class_0
if _parent_0 and _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
return _class_0 return _class_0
end)() end)()
local Yikes local Yikes
@ -112,6 +118,9 @@ Yikes = (function()
end end
}) })
_base_0.__class = _class_0 _base_0.__class = _class_0
if _parent_0 and _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
return _class_0 return _class_0
end)() end)()
x = Yikes() x = Yikes()
@ -151,6 +160,9 @@ Hi = (function()
end end
}) })
_base_0.__class = _class_0 _base_0.__class = _class_0
if _parent_0 and _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
return _class_0 return _class_0
end)() end)()
Simple = (function() Simple = (function()
@ -187,6 +199,9 @@ Simple = (function()
end end
}) })
_base_0.__class = _class_0 _base_0.__class = _class_0
if _parent_0 and _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
return _class_0 return _class_0
end)() end)()
x = Simple() x = Simple()
@ -227,6 +242,9 @@ Okay = (function()
end end
}) })
_base_0.__class = _class_0 _base_0.__class = _class_0
if _parent_0 and _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
return _class_0 return _class_0
end)() end)()
local Biggie local Biggie
@ -268,6 +286,9 @@ Biggie = (function()
end end
}) })
_base_0.__class = _class_0 _base_0.__class = _class_0
if _parent_0 and _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
return _class_0 return _class_0
end)() end)()
local Yeah local Yeah
@ -307,6 +328,9 @@ Yeah = (function()
end end
}) })
_base_0.__class = _class_0 _base_0.__class = _class_0
if _parent_0 and _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
return _class_0 return _class_0
end)() end)()
local What local What
@ -346,6 +370,9 @@ What = (function()
end end
}) })
_base_0.__class = _class_0 _base_0.__class = _class_0
if _parent_0 and _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
return _class_0 return _class_0
end)() end)()
Hello = (function() Hello = (function()
@ -391,6 +418,9 @@ Hello = (function()
end end
}) })
_base_0.__class = _class_0 _base_0.__class = _class_0
if _parent_0 and _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
return _class_0 return _class_0
end)() end)()
do do
@ -442,6 +472,9 @@ CoolSuper = (function()
end end
}) })
_base_0.__class = _class_0 _base_0.__class = _class_0
if _parent_0 and _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
return _class_0 return _class_0
end)() end)()
x = self.hello x = self.hello
@ -494,6 +527,9 @@ ClassMan = (function()
self.hello = 3434 self.hello = 3434
self.world = 23423 self.world = 23423
self.red = function(self) end self.red = function(self) end
if _parent_0 and _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
return _class_0 return _class_0
end)() end)()
x = self x = self
@ -547,6 +583,9 @@ if something then
hello = "world" hello = "world"
self.another = "day" self.another = "day"
print("yeah") print("yeah")
if _parent_0 and _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
return _class_0 return _class_0
end)() end)()
end end

View File

@ -34,6 +34,9 @@ Something = (function()
end end
}) })
_base_0.__class = _class_0 _base_0.__class = _class_0
if _parent_0 and _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
return _class_0 return _class_0
end)() end)()
local What local What