|
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
From: Marco Mastropaolo
Sent: Friday, November 21, 2014 5:37 PM
To: Lua mailing list
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
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 |