Comments (5)
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.
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.
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.
Ok thank you
from penlight.
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)
- Cleanup: drop or improve pl.url HOT 4
- Consider adding the built-in table methods to tablex? HOT 3
- Clone a function? HOT 8
- How about adding json decode and encode function HOT 4
- Error in pretty.write: invalid order function for sorting
- Bareword `warn` in compat.lua is a "strict" violation
- pl.Path.isdir Doesn't work correctly for Windows Base directory i.e. D:\\ (Lua 5.4.4) HOT 1
- Test suite failing on Arch Linux HOT 2
- lfs HOT 1
- stringx indent and dedent functions add an extra "\n" at the end of the string HOT 3
- lexer.cpp doesn't recognize #define without trailing newline HOT 2
- Can't use % as escape character in a template HOT 3
- Incorrect integer detection
- Consider implementing `cp` / `mv` via FFI HOT 2
- unexpected behavior for dir.clonetree HOT 1
- The `newline` option in the template module does not behave according to documentation HOT 4
- Fix link on luarocks HOT 3
- penlight 1.14 is rockspec_revision 1, but uses rockspec_revision 3 feature issues_url this breaks on luarocks-5.1 since it does not support rockspec 3 HOT 3
- Why "No error" in `compat.execute` HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from penlight.