GithubHelp home page GithubHelp logo

`extends` doesn't work anymore? about 30log HOT 5 CLOSED

yonaba avatar yonaba commented on August 25, 2024
`extends` doesn't work anymore?

from 30log.

Comments (5)

Yonaba avatar Yonaba commented on August 25, 2024

Hi, Martin,

Actually, that works fine if you do this:

function Actor:__init()
    Actor.super.__init(self)
    self.test2 = "2"
    for k, v in pairs( self ) do
        print( k .. ": " .. v .. "\n" )
    end
end

Take a look at this section in the Readme: Chained initialisation

Best regards
Roland.

from 30log.

martin-damien avatar martin-damien commented on August 25, 2024

I modified the code but I still have the same error:

class = require "vendors.30log.30log"

Card = class { __name = "Card" }

function Card:__init()
    self.test1 = "1"
end

Actor = Card:extends { __name = "Actor" }

function Actor:__init()
    Actor.super.__init()
    self.test2 = "2"
    for k, v in pairs( self ) do
        print( k .. ": " .. v .. "\n" )
    end
end

test = Actor()

And result is:

lua: test.lua:6: attempt to index local 'self' (a nil value)
stack traceback:
    test.lua:6: in function '__init'
    test.lua:12: in function '__init'
    ./vendors/30log/30log.lua:35: in function <./vendors/30log/30log.lua:32>
    (tail call): ?
    test.lua:19: in main chunk
    [C]: ?

For information I'm running Lua 5.1.4

from 30log.

Yonaba avatar Yonaba commented on August 25, 2024

Sorry Martin, my bad, I didn't give the correct code.
See the previous sample I gave, I just updated it.
As a matter of fact, you when you can to write a class constructor and you need to call its super class constructor, you will have to call explicitely the superclass itself, passing it the object to be instantiated.

function Subclass:__init(...)
  Superclass.super.__init(self)
  -- ... rest of the code
end

Sorry for the inconvenience.

Best regards,
Roland.

from 30log.

martin-damien avatar martin-damien commented on August 25, 2024

OK, thank you very much. It works now :)

from 30log.

Yonaba avatar Yonaba commented on August 25, 2024

Awesome. Thanks.

from 30log.

Related Issues (20)

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.