Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My_Class:catch won't let me set any properties when constructing object if class.properties is used #101

Open
andrewstarks opened this issue Oct 15, 2013 · 2 comments

Comments

@andrewstarks
Copy link
Contributor

I want to use the catch feature of the pl library. I also use the class.properties feature.

Expectation #1: That catch works without class.properties. This is not clear from the documentation, but it appears to be the case. If it is, it should be documented. I always use class.properties, so this isn't an issue that I would need changed, however.

Expectation #2: Within the initializer, I'm free to set up the class in whichever way that I need to. Then after the object is made, the catch function will catch any attempted access to uninitialized variables.

local mc = class.mc(class.properties)
mc:catch(
    function(self,name)
        --print('hello.')
        return function()

            error("no such method: " .. name, 2) 
        end
    end
)
function mc:_init(args)
    self.foo = "bar"
    return self
end
local c = mc()
c.baz = "I shouldn't be allowed."

As it is, I get:

/Users/andrew/Dropbox/src/sandbox/testpl.lua: no such method: set_foo

I wish that there was a way to initialize public (non getter/setter) properties inside of a class, while using catch.

Approaches that my rebel forces may take, in order to create the changes we seek in the ruling class library

Attempt #1: I may attempt to 'turn off' the catch (or make that possible) during the init phase.

Attempt #2: I may make a method that is defined when catch is used called public. Everything inside of public is set as such:

mc:_public(args)
    self.foo = args.foo
    self.bar = args.bar
end

Attempt #3: I may make a class.properties.public method in which I can do something like:

mc:_init(args)
    self:public("var_name", value) --tedious and un-Lua ish
end

I'll have to see which one I'm smart enough to implement... :)

-Andrew

@andrewstarks
Copy link
Contributor Author

Actually, I think I can say that catch and class.properties don't play nice together.

Is this correct?

@alerque
Copy link
Member

alerque commented Sep 29, 2020

Having taken a couple stabs at the related code I can confirm: class:catch() and class.properties will not play nice together. They are both trying to take over similar function using different Lua mechanisms. One or the other –not both– will ever work. Which one gets dibs is a mystery to me and may be different depending on which of the various alternative class instantiation syntax options you go with and/or what Lua VM you are using.

There may or may not be a way to resolve this. At this point I'm guessing they are mutually exclusive and should be documented as such.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants