lua-users home
lua-l archive

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



On Nov 21, 2014, at 11:37 AM, tonyp@acm.org wrote:

Well, # is the length operator and it’s meant to return the length of whatever follows, provided what follows has a concept of length/size (e.g., table, string), so one -- I being that one, I guess :) -- would expect if a number follows it would return its length (of its common printable representation), rather than give error.  In other words, I would expect to #number to be equivalent to #(#number..’’)
In any case, in a ‘loosely typed language’ giving an error when there is a non-error alternative behavior does not serve any useful purpose, does it?
 
Obviously, it’s not a big issue as it’s easy to overcome, but it seems (to me, at least) not the best possible behavior for #number

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.