GithubHelp home page GithubHelp logo

Comments (5)

alerque avatar alerque commented on August 25, 2024

You can use self._base.sayHello(self) to get your expected results, but I strongly recommend not going down that rabbit hole and instead restructuring your classes so you don't need to call anything other than the _init() function from subclasses. You can add hook methods to the base class and then add the additional functionality by defining hooks in the child class instead.

Also note you do not want the self._base:sayHello() syntax here otherwise self will end up being the base class table instead of your current instance.

from penlight.

distantforest1 avatar distantforest1 commented on August 25, 2024

I apologize, I'm still quite new to Lua. How do you mean? Do you mean something like this:

function Person:speak()
  print("Hello world")
  self:sayHello()
end

function Person:sayHello() print("I have no name") end

local George = class(Person)

function George:sayHello()
  print("My name is George")
end

local g = George()
g:speak()

-- result:
-- Hello World
-- My name is George

Or do you mean actually putting something in the _init itself?

from penlight.

alerque avatar alerque commented on August 25, 2024

Yes. That's not exactly what I started to describe, but my use case is much more complex involving a variable number of hook functions. What you've done there with a stub function in the base class that gets overridden with a useful function is perfectly appropriate ... at least for that MWE.

from penlight.

distantforest1 avatar distantforest1 commented on August 25, 2024

Ok thank you

from penlight.

Tieske avatar Tieske commented on August 25, 2024

from your first example;

local Person = class()
function Person:sayHello() print("Hello world") end

local George = class(Person)
function George:sayHello()
  self.super:sayHello()  -- Pseudo-code to demonstate what I mean <<
  print("My name is George") 
end

local g = George()
g:sayHello()

-- result:
-- Hello World
-- My Name is George

the line g:sayHello() is Lua's syntactic sugar for the real call being g.sayHello(g) (the the table you call on, is injected as the first parameter).

The same applies to the method definition; function George:sayHello() is actually being defined as function George.sayHello(self) (a parameter named self is injected as the first parameter.

What does this have to do with your question then? Look at this line; self.super:sayHello(), the syntactic sugar will be translated into self.super.sayHello(self.super) so instead of the instance (which is self), we're passing self.super (which is the base class). And this is not what you want, since it will not work. That's what @alerque mentioned.

The syntactic sugar is unavailable for the call to the base class, so you'll have to use the full syntax.
So instead of self.super:sayHello() you'll have to use self.super.sayHello(self).

hth.

from penlight.

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.