mirror of
https://github.com/leafo/moonscript.git
synced 2024-11-22 02:44:23 +00:00
add inherited callback to classes
This commit is contained in:
parent
1ac76d147a
commit
bc13b1b8cc
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user