lua-users home
lua-l archive

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


Rather than introduce a whole raft of predicates implemented in one of the
standard 'C' libraries, why not just extend type as suggested before:

type, subtype = type(x)

Which maintains compatibility except in very exotic cases, and ship an
additional optional library implemented in Lua which provides the
predicates:

isinteger = function(x)
  local t, s = type(x)
  return (t == "number") and (s == "integer")
end

and so on for every desired type predicate ...


> Date: Wed, 24 Jul 2013 13:41:06 +0900
> From: Miles Bader <miles@gnu.org>
> Subject: Re: Lua 5.3 work1 Considering math.isinteger or type()
> To: Thijs Schreijer <thijs@thijsschreijer.nl>
> Cc: "lua-l@lists.lua.org" <lua-l@lists.lua.org>
> Message-ID: <87zjtc1sf1.fsf@catnip.gol.com>
> Content-Type: text/plain
> 
> Thijs Schreijer <thijs@thijsschreijer.nl> writes:
> > Then I would rather have a single type() function and replace its
> > number' return value with the new types. Big incompatibility but the
> > new integer division operator already is. Incompatible but clean.
> 
> Predicates are actually _more_ clean, and yet aren't incompatible
> ("isnumber" and "isinteger"/"isfloat" can exist simultaneously).
> 
> If you are worried about function namespace pollution (I'm not,
> particularly, but ...), it's also possible to use a predicate-style
> solution with a separate type namespace, e.g. "istype(OBJ, TYPE)",
> where TYPE is 'number', 'float', 'table', etc.
> 
> -miles
>