lua-users home
lua-l archive

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




On Wednesday, July 17, 2013, Rena wrote:

On 2013-07-17 4:10 AM, "David Demelier" <demelier.david@gmail.com> wrote:
>
> Hi,
>
> As we have already discussed this, some of us on the lists think that
> math.isfloat is great but should also reflect the opposite as
> math.isinteger.
>
> Joseph Manning and I thought this idea great as you only need to
> remember there are math.is* to check the type. So you don't need to
> think which one of the function is available.
>
> This makes the library clear and adds a symmetry to the API.
>
> However Thijs Schreijer also thougth about the functione type() that
> should returns two values like this:
>
> type(10) -> "number", "integer"
> type(10.0) -> "number", "float"
>
> This is not a bad idea, it will not break compatibility but adds a
> second feature to determine the actual type of the integer. With this,
> the discuss of keeping math.isfloat and adding math.isinteger will be
> gone.
>
> I just hope that one of these 2 solutions will be approved and added
> for the final Lua 5.3
>
> Regards,
>
> --
> Demelier David
>

I really like the idea of having both math.isinteger and math.isfloat. To me they seem much more readable and easier to use than the alternatives. Consider:

if math.isinteger(x) then
if math.isfloat(x) then
Both are simple and clear. vs:

if not math.isinteger(x) -- x is not an integer, but what is it? A float? A table? nil? Even if this method throws an error if given a non-numeric value, that's something else you need to know; it's still less readable than the previous example. (Also, I think that behaviour would be quite silly; it should just answer the question and state that no, a table is not an integer.)

local _, tp = type(x)
if tp == 'integer' then
Now this is just ugly.

if math.type(x) == 'integer' then
Better, but still not as clean. (and what should math.type do with non-numeric values?)

In my opinion the first example is the clear winner - it's clear and readable and concise.

What about type member functions for cases like this:

type.integer(x)
Returns true if it's an integer. 
Since I don't think that a "nil" key is legal, perhaps it would need to be:

type.isnil(x)
type.isfloat(y)
type.isnumber(z)


This also leaves fertile territory for monkey patching type. 

- Andrew