lua-users home
lua-l archive

Re: NaN as table key [w/ patch for 5.3.3] (was: Has Lua reached perfection?)

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


2018年01月29日 5:31 GMT+02:00 nobody <nobody+lua-list@afra-berlin.de>:
> On 2018年01月29日 03:16, Sean Conner wrote:
>>
>> It was thus said that the Great nobody once stated:
>>>
>>> NaN being unusable as a table key is one of the bigger kinks
>>
>>
>> Just in case you are unaware (or others are unaware), NaN is a special
>> value defined by IEEE-754 (or rather, values) that have a unique property:
>>
>> So it would be difficult to make NaN to be a table [key].
>
>
> ...and it wasn't actually that hard, here's the patch with my notes from
> back then. The code is still rather ugly / proof-of-concept / just make
> it work somehow-style and can almost certainly be improved, I just don't
> have the time to do that right now.
Why must every little feature that is perfectly easy in Lua
be a patch or a new feature? Why can't one just do this:
do local NaN = setmetatable({},{__tostring=function() return "NaN" end})
tbl = setmetatable({},{
 __index = function(tbl,idx) if idx~=idx then return rawget(tbl,"NaN") end end;
 __newindex = function(tbl,idx,val) if idx~=idx then
rawset(tbl,"NaN",val) end end;
})
end
> tbl[0/0] = "not a number"
> tbl[0/0]
not a number
> next(tbl)
NaN not a number

AltStyle によって変換されたページ (->オリジナル) /