[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua 5.3: wrong coercion?
- From: Andrew Starks <andrew.starks@...>
- Date: Sat, 17 Jan 2015 07:55:10 -0600
On Saturday, January 17, 2015, Lorenzo Donati <lorenzodonatibz@tiscali.it> wrote:
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
I go back and forth between caring and suspecting that I just want to be one of the kewl kids that hates coercion.
However, if I may proffer a guess as to how it would change:
1: A big section of us compiles Lua with the flags turned on.
2: When modules fail, submit pull requests (it will take 2 seconds to patch them).
3: Report back that the world didn't end.
Alternately we can leave N2S off, but if we're going rouge, we may as well do a proper job of it. Making the modules we use explicit would be nice and it will mean that we can report back the level of pain for both flags, which will be useful, for science.
I'll go first. You're welcome.
-Andrew