lua-users home
lua-l archive

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




On 17/01/2015 11:29, Tim Hill wrote:

On Jan 17, 2015, at 12:29 AM, Alexandre (gaumerie) <gaumerie@hotmail.com> wrote:

I was not expecting that behaviour neither. However, it is consistent with what Roberto told during the conversation about the power (x^y) operator: the result type of the operator should depends only on the operandes types and not the operandes values.
If string where converted to integer or float according their values in the addition operation, you would have:
  String + Integer -> Integer (if the string represents an integer) or Float (if the string represents a float)
The current behaviour give:
  String + Integer -> Float
  String + Float -> Float
in all cases (given that the string represents a valid number) and so there is no surprise about the result type.


For me, the “surprise" is that implicit coercion of a string to an integer behaves differently to explicit coercion (via tonumber()). I’m not saying it’s not according to the spec, just that it’s a bit .. well .. surprising.

I agree. That's surprising to me too. As I said, I begin to feel we could get rid of this automatic string->number coercion thing and live happily using tonumber explicitly.

I wonder whether there are use cases where that automatic coercion is so important that not having it could amount to a showstopper. To me it seems just a little convenience which we could do without in exchange for a "cleaner" semantics and more explicit and robust code (not really sure about it, though).

—Tim