Skip to content

Commit

Permalink
Add. class.super function to implement inheritance.
Browse files Browse the repository at this point in the history
```Lua
local Foo = ut.class()
local Boo = ut.class(Foo) do
local super = ut.class.super(Boo)
function Boo:__init() self = super(self, '__init') end
end
```
  • Loading branch information
moteus committed Jul 21, 2017
1 parent cb0fafd commit a080720
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/lua/lluv/luasocket.lua
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,12 @@ end

----------------------------------------------------------------------------
local TcpSock = ut.class(BaseSock) do
local super = ut.class.super(TcpSock)

local MAX_ACCEPT_COUNT = 10

function TcpSock:__init(s)
assert(TcpSock.__base.__init(self))
self = assert(super(self, '__init'))

self._buf = assert(ut.Buffer.new("\r*\n", true))

Expand Down Expand Up @@ -427,9 +428,10 @@ end

----------------------------------------------------------------------------
local UdpSock = ut.class(BaseSock) do
local super = ut.class.super(UdpSock)

function UdpSock:__init(s)
assert(UdpSock.__base.__init(self))
self = assert(super(self, '__init'))

self._buf = assert(ut.Queue.new())

Expand Down
21 changes: 20 additions & 1 deletion src/lua/lluv/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,25 @@ local function class_self_test()
B.new(1, 2, 3)
end

-------------------------------------------------------------------
local Class = {} do

setmetatable(Class, {__call = function(_, ...) return class(...) end})

local function super(class, self, method, ...)
if class.__base and class.__base[method] then
return class.__base[method](self, ...)
end
if method == '__init' then return self end
end

function Class.super(class)
return function(...) return super(class, ...) end
end

end
-------------------------------------------------------------------

-------------------------------------------------------------------
local corun do

Expand Down Expand Up @@ -895,7 +914,7 @@ return {
List = List;
Errors = MakeErrors;
DeferQueue = DeferQueue;
class = class;
class = Class;
split_first = split_first;
split = split;
usplit = usplit;
Expand Down

0 comments on commit a080720

Please # to comment.