[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: 5.3.0-work3 and lossless integer conversion
- From: Roberto Ierusalimschy <roberto@...>
- Date: Tue, 24 Jun 2014 12:20:11 -0300
> My use case is a messaging (protobuf/JSON/etc) library. Because this
> is a serialization system, I want to be very strict that you get out
> exactly what you put in. I don't want to do any implicit conversion or
> rounding whatsoever. (See this thread for more:
> http://lua-users.org/lists/lua-l/2014-01/msg00137.html)
To be honest, we never thought much about that. Integers first entered
Lua through the C API (lua_pushinteger, lua_tointeger), and we followed
C in truncating floats when doing the conversion. (It was also cheaper,
which was important because such conversions were quite common.) But
the question is quite pertinent: is this useful at all? If a program
specifices 3.2 as a character index in a string, is it useful to
silently truncate that to 3? Or maybe it would be better to throw an
error in that case? In the rare cases that the programmer really wants
to truncate, that probably should be done explicitly.
> While part of me is ok with making users explicitly convert to integer
> first, it would be an unnecessary (in my view) incompatibility between
> Lua 5.3 and other versions of Lua. Pre-5.3 you wouldn't be able to
> coerce to integer, but >=5.3 would require it. I'd prefer to just
> allow floats as long as their value is integral.
Note that older versions of Lua already truncate floats at the
API. Accepting only integral values there would be a source of
incompatibilities. (Hopefully not serious ones, as Lua never really
specified how that rounding was done anyway.)
-- Roberto