Search found 650 matches

by airstruck
Tue Apr 25, 2017 6:53 pm
Forum: Libraries and Tools
Topic: clasp - tiny class library
Replies: 29
Views: 20781

Re: clasp - 8 lines of class

This won't work if proto isn't passed in, because objectmeta's __index will be nil. local objectmeta = {__index = proto} local proto = setmetatable(proto or {}, -- ... This is why tests are important. You can use these if you want, it's very similar, just change 'constructor' to 'init' and add tests...
by airstruck
Tue Apr 25, 2017 2:07 pm
Forum: Libraries and Tools
Topic: clasp - tiny class library
Replies: 29
Views: 20781

Re: clasp - 8 lines of class

I tried moving metamethod copying outside of instance creation anyway, but I haven't figured it how to do it decently yet. If by "decently" you mean "without cycles," why not move back to closed-over functions? You didn't seem to mind it before, this time you can have it use an ...
by airstruck
Mon Apr 24, 2017 5:48 am
Forum: Libraries and Tools
Topic: clasp - tiny class library
Replies: 29
Views: 20781

Re: clasp - 8 lines of class

Reading metamethods directly from class will break their inheritance on longer chains, because pairs() doesn't read the __index tree, which is mandatory for them to be inherited correctly That's why I'm suggesting copying them from the parent class to the subclass in "extend." The proto._...
by airstruck
Sun Apr 23, 2017 10:26 pm
Forum: Libraries and Tools
Topic: clasp - tiny class library
Replies: 29
Views: 20781

Re: clasp - 8 lines of class

Yeah, I see what you mean about overriding individual metamethods. I guess you'll need to copy these no matter what if they're supposed to be inherited. Here's an idea for moving it from instantiation to declaration: local function new (class, ...) local object = setmetatable({}, class) return objec...
by airstruck
Sun Apr 23, 2017 8:02 pm
Forum: Libraries and Tools
Topic: clasp - tiny class library
Replies: 29
Views: 20781

Re: clasp - 13 lines of class

it's the same as my code just spread out That was the point, still 13 lines but without redefining functions each time, also easier to follow I think. it still makes fractals when removing self.__meta = nil on line 2 It gives an almost identical result to the "pairs" code when you don't r...
by airstruck
Sun Apr 23, 2017 7:48 pm
Forum: Libraries and Tools
Topic: clasp - tiny class library
Replies: 29
Views: 20781

Re: clasp - 13 lines of class

Here's what I came up with, I've got to run but take a look: local function new (self, ...) local meta = self.__meta or {}; meta.__index = self; self.__meta = nil local object = setmetatable({}, meta) return object.init and object:init(...) and object or object end local function extend (base, membe...
by airstruck
Sun Apr 23, 2017 7:31 pm
Forum: Libraries and Tools
Topic: clasp - tiny class library
Replies: 29
Views: 20781

Re: clasp - 13 lines of class

You're right, I just tried it. Also noticed __meta still appears in the dump even after nilling it (with all three methods); something's off.
by airstruck
Sun Apr 23, 2017 7:22 pm
Forum: Libraries and Tools
Topic: clasp - tiny class library
Replies: 29
Views: 20781

Re: clasp - 13 lines of class

Ahh, I see what you mean. Try meta = rawget(self, '__meta') or {}
by airstruck
Sun Apr 23, 2017 7:14 pm
Forum: Libraries and Tools
Topic: clasp - tiny class library
Replies: 29
Views: 20781

Re: clasp - 13 lines of class

I get the exact same structure... https://hastebin.com/movahoyisi.lua
by airstruck
Sun Apr 23, 2017 7:07 pm
Forum: Libraries and Tools
Topic: clasp - tiny class library
Replies: 29
Views: 20781

Re: clasp - 13 lines of class

Not sure how that happened, I just tried this and it seems to work ok: return function (members) local cls = {} function cls.new (self,...) local meta = self.__meta or {}; meta.__index = self; self.__meta = nil -- this changed local object = setmetatable({}, meta) return object.init and object:init(...