[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: length operator [was: table insertion]
- From: Augusto Cesar Radtke <acradtke@...>
- Date: Fri, 5 Nov 2004 10:01:02 -0300
This is interesting because remembers the norm symbol of vectors but
doesn´t look clean for the other types.
Why not introduce a new unary operator called 'sizeof' ?
If Lua is supposed to be a intuitive language what is a better
operator to return the size of the type than 'sizeof' ?
We have a lot of written operators like "and", "or" and "not" to avoid
the use of cryptic symbols. I think typing one character or six isn´t
the problem because the programmer is supposed to be thinking most of
time than typing the code.
I think a metatable event like __sizeof will be useful too.
On Fri, 05 Nov 2004 07:19:23 -0500 (EST), leiradella@bigfoot.com
<leiradella@bigfoot.com> wrote:
> My opinion is that all proposed operators to get the length of a table are too cryptic to be used with a language with an intuitive syntax as Lua. A more intuitive length operator, at least for me, is |t|. Of course |s| could also be used to get the length of a string.
>
> With this operator, appending a value to a table could be written as t[|t|]=value. And t[]=value could be used as a syntatic sugar for appending values, but I also like the t=t..value idea.
>
> Just to make it clear, by length of a table I mean the index of the last non-nil element plus 1, or 0 on an empty table. This value can be stored as part of the table structure and updated as needed on the C side:
>
> t={}
> print(|t|) -- 0
> t[|t|]='a' -- 'a' stored at index 0
> print(|t|) -- 1
> t[5]='b'
> print(|t|) -- 6
>