GithubHelp home page GithubHelp logo

Metamethods doesn't works! ;( about classic HOT 3 OPEN

rxi avatar rxi commented on August 18, 2024
Metamethods doesn't works! ;(

from classic.

Comments (3)

rxi avatar rxi commented on August 18, 2024

Replace:

  else rawget(self, key)

with:

  else return rawget(C, key) or C.super[key]

from classic.

winterwolf avatar winterwolf commented on August 18, 2024

@rxi Thank you so much sir!

from classic.

winterwolf avatar winterwolf commented on August 18, 2024

@rxi I'm sorry, but this solution doesn't work well:

local MiddleClass = require "middleclass"
local M1 = MiddleClass("M1")


function M1:initialize()
  self.pos = {2, 4}
  self.size = {6, 8}
end


function M1:__index(key)
  if key == "x" then return self.pos[1]
    elseif key == "y" then return self.pos[2]
    elseif key == "w" then return self.size[1]
    elseif key == "h" then return self.size[2]
    else return rawget(self, key)
  end
end


function M1:__newindex(key, value)
  if key == "x" then self.pos[1] = value
    elseif key == "y" then self.pos[2] = value
    elseif key == "w" then self.size[1] = value
    elseif key == "h" then self.size[2] = value
    else rawset(self, key, value)
  end
end


local M2 = M1:subclass("M2")
local M3 = M2:subclass("M3")
local M4 = M3:subclass("M4")


local m = M4()

assert(m.x == 2)
m.x = 222
assert(m.pos[1] == 222)

print("No errors in MiddleClass.")


local Classic = require "classic"
local C1 = Classic:extend()


function C1:new()
  self.pos = {2, 4}
  self.size = {6, 8}
end


function C1:__index(key)
  if key == "x" then return self.pos[1]
    elseif key == "y" then return self.pos[2]
    elseif key == "w" then return self.size[1]
    elseif key == "h" then return self.size[2]
    else return rawget(C1, key) or C1.super[key]
  end
end


function C1:__newindex(key, value)
  if key == "x" then self.pos[1] = value
    elseif key == "y" then self.pos[2] = value
    elseif key == "w" then self.size[1] = value
    elseif key == "h" then self.size[2] = value
    else rawset(self, key, value)
  end
end

local C2 = C1:extend()
local C3 = C2:extend()
local C4 = C3:extend()

local c = C4()

assert(c.x == 2)
c.x = 222
assert(c.pos[1] == 222)

print("No errors in Classic.")

No errors in MiddleClass.
luajit: metatables-oop.lua:59: attempt to index field 'pos' (a nil value)

from classic.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.