lua-users home
lua-l archive

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




On Saturday, November 22, 2014, <tonyp@acm.org> wrote:
Thank you for your comments.
 
Regarding automatic coercion between strings and numbers being a bad (‘not good’) thing, I’d certainly be in the minority group then, among those who strongly disagree with this, and for me this is just one of the many reasons to like Lua.
 
My reasoning is this:
 
From a human point of view numbers are (have always been) nothing more than strings of digits, just like words are strings of letters.  Think how you write them on paper, or when you type.
Only for computing convenience (based on today’s technologies) we have made a ‘special arrangement’ for numbers to be treated differently internally.  But, in an ideal world, a computing language should hide whatever machine requirements from the user as much as possible/feasible.  There is nothing wrong with thinking of numbers as strings that just *happen* to contain mostly digits.  So, in my view deprecating this ‘bad thing’ would be an utter mistake.  Lua is already in the right direction to the ideal, why go back?

This topic has a high likelihood of wearing many people out, and since you asked and it's your thread:

I believe that you're saying that when someone reads a string of numbers, they immediately think of that string as a number, not a string with number values. For that matter, any value that is pretty printed is a candidate for that. I get that it's not the same and this does not serve to argue against your point, but "false" might also then evaluate to `false`, and it's certainly why some languages consider "" as such. 

Between me and the fine people that make Lua are many obstacles to perfect understanding. On top of that, computers are pretty stupid and so it is a reasonable approach to make each layer of abstraction simple and the rules that govern them clear and devoid of too many exceptions. 

Altleast that's why I generally prefer that coercion are explicit. I'll take an error over a tricky bug that pops up 2 months later and only on a small number of systems, any day. The later is more likely when the language "helps me out" without me telling it to do so. 

-Andrew

 
From: Tim Hill
It’s pretty much agreed that the automatic coercion between strings and numbers in Lua is not good, and is usually discouraged (and there have been discussions about deprecating it). And one of the reasons it’s not good is the very example you give. The “..” operator is non-polymorphic; it always operates on strings[1], so there is no ambiguity as to the desired type of the operator arguments. The “#” operator is polymorphic, and so type coercion is far more ambiguous (and therefore should be avoided). And if you REALLY need # on numbers then it can be added via the C API by adding a metatable to the number type.
—Tim
[1] Ignoring metatables, that is.