[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Type Metatables for Table and Userdata - Powerpatch
- From: steve donovan <steve.j.donovan@...>
- Date: Thu, 6 Dec 2012 08:05:50 +0200
On Thu, Dec 6, 2012 at 2:36 AM, Claire Lewis <claire_lewis@live.com.au> wrote:
> So hang on, would this mean that, as a host app or module writer, I can no
> longer rely on this to work:
> if some other module or user script has gone and installed a ‘clever’ type
> metatable?
It's already true ;) debug.setmetatable can attach a new metatable to
any type. Now, no _sensible_ library author would do this, but the
temptation to use clever stuff in code gets strong.
Lua people are less tolerant of monkey-patching than the Rubyists.
Sure, you can add your own methods to the string table, Penlight even
has an option to do "require 'pl.stringx'.import()" and bring in
additional Python-style string functions. But increasingly I don't
think this is such a good idea anyway, since these are global
modifications. So restricting to _ENV in some way would be a useful
way to sandbox cleverness on a per-module basis.
On the subject of iterators: I missed 'for k,v in t' when 5.1 finally
removed it, for the sensible reason that the syntax did not make it
clear whether we're doing pairs() or ipairs(). But by now I think
people are clear that this ought to mean pairs() only. So an __iter
metamethod makes sense, and a default table metatable could have that
enabled. But then there's also the common need to iterate over the
values of a collection, without the keys (especially 'arrays')...
steve d.