lua-users home
lua-l archive

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


What if instead of making implied variables, you say:
numeric for:
for i=start,end,interval do

end

ipairs like:
for t as i,v do
  print(i..' = '..v)
end

Non-numeric:
foreach t as k,v do
  print(k..' = '..v)
end

pairs like:
forany t as k,v do
  print(k..' = '..v)
end

This makes it less magical, but I think it is too many concepts. You
would have to add __for, __foreach, and __forany meta-methods, and
that would be overhead, because a table would have to follow those
metamethods.

On Wed, May 26, 2010 at 11:39 AM, George Petsagourakis
<petsagouris@gmail.com> wrote:
> -- this traverses only the numeric indexes
> -- makes use of one invisible implied default local variable '__i'
> for t do
>  print(__i.." = "..t[__i])
> end
> -- should output:
> --   1 = apples
> --   2 = bananas
> --   3 = apricots
>
> -- this traverses only non numeric indexes
> -- makes use of two invisible implied default local variables '__i' and '__v'
> foreach t do
>  print(__i.." = "..__v)
> end
> -- should output:
> --   apples_eaten = 4
> --   bananas_eaten = 5
> --   apricots_eaten = 0
>
> -- this traverses any indexes
> -- makes use of one invisible implied default local variable '__i'
> forany t do
>  print(__i.." = "..t[__i])
> end
> -- should output:
> --   1 = apples
> --   2 = bananas
> --   3 = apricots
> --   apples_eaten = 4
> --   bananas_eaten = 5
> --   apricots_eaten = 0
>
> Maybe it is a stupid concept, but I will just throw it in here...
>
>