Comments (7)
Interesting! Let me give it a think.
BTW, the stripped-down version of class() in Microlight (see ml.class near the end of ml.lua) has provision for the constructor to return a new self.
setmetatable(klass,{
__call = function(klass,...)
local obj = setmetatable({},klass)
if rawget(klass,'_init') then
klass.super = base_ctor
local res = klass._init(obj,...) -- call our constructor
if res then -- which can return a new self..
obj = setmetatable(res,klass)
end
elseif base_ctor then -- call base ctor automatically
base_ctor(obj,...)
end
return obj
end
})
(It occurs to me that if that new self was nil, and _init
was allowed to return two values, then we would get the Lua-friendly constructor fail that we started discussing.)
from penlight.
I saw that too, but I got stuck thinking that there were perhaps 3 entry
points for object creation. I can't remember why, but I think it had
something to do with whether or not there was a base constructor, etc. If
there is only this one, then that seems to make things pretty straight
forward....
On Fri, Feb 15, 2013 at 12:15 AM, Steve J Donovan
[email protected]:
Interesting! Let me give it a think.
BTW, the stripped-down version of class() in Microlight (see ml.class near
the end of ml.lua) has provision for the constructor to return a *new self
*.setmetatable(klass,{ __call = function(klass,...) local obj = setmetatable({},klass) if rawget(klass,'_init') then klass.super = base_ctor local res = klass._init(obj,...) -- call our constructor if res then -- which can return a new self.. obj = setmetatable(res,klass) end elseif base_ctor then -- call base ctor automatically base_ctor(obj,...) end return obj end })
(It occurs to me that if that new self was nil, and _init was allowed to
return two values, then we would get the Lua-friendly constructor fail that
we started discussing.)—
Reply to this email directly or view it on GitHubhttps://github.com//issues/48#issuecomment-13594027.
from penlight.
Hey Steve,
Did you get my attachments/emails for the changes I made to PL, so that it
could autoload into a provided table? I'm not sure that github lets you do
attachments.
second,
I applied the necessary patch to PL to make classes fail upon receiving
"nil, err". It needed patching in two spots:
Near the end of call_ctor:
local res, err = c._init(obj,...) --<change starts here.
if not res and err then -- lua for we didn't succeed so fail all nice
like.
return nil, err
end-->Change ends here.
and inside mt.__call
if rawget(c,'_init') then -- explicit constructor
local res, err = call_ctor(c,obj,...) --< change starts here
if not res and err then -- lua for we didn't succeed so fail
all nice like.
return nil, err
end --<change ends here.
This seems to work well for me.
-Andrew
On Fri, Feb 15, 2013 at 7:48 AM, Andrew Starks [email protected]:
I saw that too, but I got stuck thinking that there were perhaps 3 entry
points for object creation. I can't remember why, but I think it had
something to do with whether or not there was a base constructor, etc. If
there is only this one, then that seems to make things pretty straight
forward....On Fri, Feb 15, 2013 at 12:15 AM, Steve J Donovan <
[email protected]> wrote:Interesting! Let me give it a think.
BTW, the stripped-down version of class() in Microlight (see ml.class
near the end of ml.lua) has provision for the constructor to return a new
self.setmetatable(klass,{ __call = function(klass,...) local obj = setmetatable({},klass) if rawget(klass,'_init') then klass.super = base_ctor local res = klass._init(obj,...) -- call our constructor if res then -- which can return a new self.. obj = setmetatable(res,klass) end elseif base_ctor then -- call base ctor automatically base_ctor(obj,...) end return obj end })
(It occurs to me that if that new self was nil, and _init was allowed to
return two values, then we would get the Lua-friendly constructor fail that
we started discussing.)—
Reply to this email directly or view it on GitHubhttps://github.com//issues/48#issuecomment-13594027.
from penlight.
Nope, didn't arrive here - you used the gmail address?
from penlight.
Nope. I don't think I have that... unless it's on the lua list.
On Wed, Feb 20, 2013 at 4:39 AM, Steve J Donovan
[email protected]:
Nope, didn't arrive here - you used the gmail address?
—
Reply to this email directly or view it on GitHubhttps://github.com//issues/48#issuecomment-13825558.
from penlight.
OK, new feature is that a class may define _create
and it can return a new self. I'm using it now for pl.List since I wanted to be able to mark an existing table as a List
.
from penlight.
Hey @andrewstarks do you still use pl.class
? Do you have any idea off the top of your head how relevant this issue thread still is or what the outcome should be now?
from penlight.
Related Issues (20)
- Cleanup: move pl.lapp and pl.config into pl.app HOT 2
- Cleanup: move pl.stringio into the pl.stringx module
- Cleanup: deprecate pl.test HOT 1
- 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
- Running the class's base function HOT 5
- 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
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.