lua-users home
lua-l archive

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


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
 
(Another thing that I would expect is the IN operator to work like in Python when used with strings.  But that’s another discussion...)
 
From: Thiago L.
Sent: Friday, November 21, 2014 9:04 PM
Subject: Re: ## operator (error)
 
 
On 21/11/14 04:57 PM, tonyp@acm.org wrote:
I didn’t know you could do ##, but then again I didn’t think about it until you mentioned it.  I actually use the length of a number (as string) quite often.
 
So, being shorter than what I currently do, I tried ## and I have to ask (not you necessarily, whoever knows), why an error?
 
x = '1234567890'
print(#x)
print(#(#x..''))    -- this works (prints 2, the length of 10 as string)
print(##x)          -- this fails "attempt to get length of a number value"
If numbers and strings can be mixed in Lua (as in the example that works above), why is ##a an error if x is a number, and Lua does not implicitly convert the number to a string to have its length taken, rather than give an error?  The length of a number is a meaningful thing.  Of course one could say it should return the length as the number of bytes required to stored the number but this is the less likely interpretation one would expect.
 
TIA
 
Sent: Friday, November 21, 2014 5:37 PM
Subject: Re: [Proposal] ## operator
 
Probably a scenario which is totally unlikely and broken on so many other aspects.. but this code

t = { 1, 2, 3 };
setmetatable(t, { __len = function(tbl) return tostring(rawlen(tbl)); end });
x = ##t;
-- x = 1
 
actually works and would break if ## would be implemented.

As I said, totally unlikely and super-dirty to starts of, but at the moment the parser treats (correctly) ## as two executions of the # operator and if a __len meta returns a string or table, it's actually "working" code (for some definition of working).

-- Marco
 
Because # is not a string method.
-- 
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.