[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: string.gsub and table replacements
- From: Matthew Wild <mwild1@...>
- Date: Sat, 28 Jan 2012 12:25:45 +0000
On 28 January 2012 06:29, HyperHacker <hyperhacker@gmail.com> wrote:
> On Fri, Jan 27, 2012 at 06:39, Matthew Wild <mwild1@gmail.com> wrote:
>> On 27 January 2012 11:21, HyperHacker <hyperhacker@gmail.com> wrote:
>>> On Fri, Jan 27, 2012 at 04:20, Luiz Henrique de Figueiredo
>>> <lhf@tecgraf.puc-rio.br> wrote:
>>>>> to make this work, you need to wrap the lookup:
>>>>
>>>> Or do
>>>> params = {["1"]='file.c', ["2"]='file.o'}
>>>>
>>>
>>> Well yes, but that isn't always feasible, e.g. with the {...} example.
>>
>> setmetatable({...}, { __index = function (t, k) return rawget(t,
>> tonumber(k)); end })
>>
>> Regards,
>> Matthew
>>
>
> That's not really much simpler, and still assumes you can modify the table.
Then pass a closure instead that implements the same logic. No need
for a metatable then.
> I feel like the point has been largely missed. :|
It hasn't, but I doubt anyone agreed with it being part of Lua itself.
Converting from a string to a number is a special case, specific to
your current problem. But that's fine, Lua already gives you the
flexibility to do whatever you want. You've already had multiple
solutions to your problem. If this is a common one for you, wrap gsub.
You can pass it a table, or a function. This is *exactly* why it
allows you to do that.
If you want some concrete reasons your logic shouldn't be part of Lua
itself, consider that it's going to be rather surprising behaviour to
people mixing strings and numbers as keys in tables. And which one
should be tried first? Should all numbers be tried? or only whole
integers? Negative numbers? 0? You're suddenly adding a dozen lines of
code to Lua that no-one has asked for before, just for you. It's not
going to happen.
Regards,
Matthew