Skip to content

Instantly share code, notes, and snippets.

@speedmax
Created August 14, 2008 06:41
Show Gist options
  • Save speedmax/5378 to your computer and use it in GitHub Desktop.
Save speedmax/5378 to your computer and use it in GitHub Desktop.
-- current method and property lookup
function instance_protocol:__index(member)
if type(class_object[member]) == 'function' then
return function(...)
return class_object[member](self, ...)
end
elseif type(member) == 'string' and type(class_object['.'..member]) == 'function' then
return class_object['.'..member](self)
elseif type(instance_protocol.___index) == 'function' then
return instance_protocol.___index(self, member)
else
return self[member]
end
end
array = class {
['.first'] = function(self) -- virtual property
return self[1]
end;
first = function(self) -- generic method
return self[1]
end
}
local list = array{"hello","world","!"}
-- if we change the object_instance.__index to resolve
* [".something"]
* classobj.prototype["something"]
* ["."]=function(self, "something")
* self["something"]
then this will be possible
print(list.first) -- outputs hello
print(array.first(list)) -- outputs hello
print(list.first()) -- error
string = native(string) {
['.upper'] = function(self)
return self:upper()
end
format = function(self, ...) -- generic method
return self:format(...)
end
}
print ("hello world").upper
print ("hello %s").format("world")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment