GithubHelp home page GithubHelp logo

Comments (7)

stevedonovan avatar stevedonovan commented on May 26, 2024

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.

andrewstarks avatar andrewstarks commented on May 26, 2024

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.

andrewstarks avatar andrewstarks commented on May 26, 2024

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.

stevedonovan avatar stevedonovan commented on May 26, 2024

Nope, didn't arrive here - you used the gmail address?

from penlight.

andrewstarks avatar andrewstarks commented on May 26, 2024

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.

stevedonovan avatar stevedonovan commented on May 26, 2024

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.

alerque avatar alerque commented on May 26, 2024

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)

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.