lua-users home
lua-l archive

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


You could make `type` return a table.

local type_mt = {__tostring = function(self) return self.type end }
local origtype = type
type = function(obj)
  return setmetatable (
  { type=origtype(obj), subtype = math.type(obj) },
  type_mt )
end

type(1.0) prints as 'number', and type(1.0)==number, where

number = setmetatable ({type="number"}, {__eq = function(a,b) return
a.type == b.type end})





2017-02-06 9:21 GMT+02:00 steve donovan <steve.j.donovan@gmail.com>:
> On Fri, Feb 3, 2017 at 9:25 AM, Xianfu Pan <pxfgod@gmail.com> wrote:
>> 2017-01-27 21:22 GMT+08:00 Kim Alvefur <zash@zash.se>:
>>>     type(1.0) -> "number", "float"
>
> Changing a basic function like `type` to return more than one value
> can cause strange problems.
>
> t = {type(1.0}
>
> So #t becomes 2, not 1.  This also bites when calling type() as the
> last argument of a function. Then to be sure, need to say (type(v)) to
> discard the second value.
>