lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 8 April 2014 10:18, Coroutines <coroutines@gmail.com> wrote:
On Mon, Apr 7, 2014 at 7:34 PM, Patrick Donnelly <batrick@batbytes.com> wrote:
On Mon, Apr 7, 2014 at 3:02 PM, Hisham <h@hisham.hm> wrote:
> On this whole discussion, if there are functions to go away, it seems
> the only one that's really a no-brainer for removal is math.pow. Yet,
> by the same logic, another clear candidate for removal which I haven't
> seen mentioned is string.len (curiously, I used it for the first time
> in _years_ last week as an argument for a map-like function — I guess
> that's why I remembered it exists; wouldn't mind using `function(x)
> return #x end` in its place though).

Excellent point, I hope string.len is also on the chopping block.

--
Patrick Donnelly


string.len() asserts its 'self' is a string, # does not -- the only reason to consider *not* removing it -- same for math.pow() it asserts it is operating on numbers -- ^ does not

This is a very good point.
Just as we have rawget and __index, rawset and __newindex;
We have math.pow and __pow, string.len and __len.

I'm not sure if this is a reasonable arrangement;
but I'd at least enjoy consistency across all metamethods.

I know I've ended up doing all sorts of things inside of metamethods to get the original result. e.g:
function mt.__tostring(o)
    local mt = debug.getmetatable(o);
    debug.setmetatable(o, nil)
    local raw = tostring(o);
    debug.setmetatable(o, mt);
    return "prefix " .. raw
end

New idea to possibly kick off discussion: a more generic `debug.raw(o, "tostring")` function?