Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Difference between middleclass, classic and oo #15

Open
winterwolf opened this issue Nov 29, 2020 · 3 comments
Open

Difference between middleclass, classic and oo #15

winterwolf opened this issue Nov 29, 2020 · 3 comments

Comments

@winterwolf
Copy link

winterwolf commented Nov 29, 2020

-- https://github.com/kikito/middleclass
local middleclass = require "middleclass"
local CreatureMC = middleclass("CreatureMC")
function CreatureMC:__call() print("CreatureMC was called!") end
local AnimalMC = CreatureMC:subclass("AnimalMC")
local CatMC = AnimalMC:subclass("CatMC")
local tomMC = CatMC()
print(tomMC) -- instance of class CatMC
print(pcall(tomMC)) -- called, no errors


-- https://github.com/rxi/classic
local classic = require "classic"
local CreatureCL = classic:extend()
function CreatureCL:__call() print("CreatureCL was called!") end
local AnimalCL = CreatureCL:extend()
local CatCL = AnimalCL:extend()
local tomCL = CatCL()
print(tomCL) -- nil !?
print(pcall(tomCL)) -- called, error (attempt to call a nil value)


-- https://github.com/limadm/lua-oo
local oo = require "oo"
local CreatureOO = oo()
function CreatureOO:__call() print("CreatureOO was called!") end
local AnimalOO = CreatureOO:extend()
local CatOO = AnimalOO:extend()
local tomOO = CatOO()
print(tomOO) -- table
print(pcall(tomOO)) -- error (attempt to call a table value)
@winterwolf
Copy link
Author

return self after call solved the issue.

@winterwolf
Copy link
Author

... No, wait. In this case CreatureCL called twice 🤦‍♂️

@winterwolf winterwolf reopened this Nov 29, 2020
@winterwolf
Copy link
Author

We can add return setmetatable({}, self) into Object:new() and call it manually to create instances. This solution seems would work well BUT it will not solve the same problem #13 with __index and __newindex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant