lua-users home
lua-l archive

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


In my version when a list is passed to pairs it will use ipairs internally. When a table is passed it will use next or __pairs internally. I didn't explain this because I thought it would be obvious, so sorry for being unclear.

On Sat, Jan 20, 2018 at 11:44 AM, Kevin Martin <kev82@khn.org.uk> wrote:

> On 20 Jan 2018, at 00:22, Elias Hogstvedt <eliashogstvedt@gmail.com> wrote:
>
> Hi Kevin, I'm not sure I get what you mean. I agree with what you're saying about Lua but I don't see how this will change any of the behavior you're describing. pairs will still be pairs

Did you not say that calling pairs on a list behaves like ipairs? These functions do not do the same currently.

If we look at the following code in your version

function f(l)
  for _, v in pairs(l) do
    print(v)
  end
end

I can not tell what it is going to do without knowing the underlying type. Consider what it will do if l is an array vs a table with integer keys (think about the order of the output). In current Lua, I know exactly what this code does.

Lets say I want the behaviour of ipairs. Of course in your version (and normal Lua) I can do this, and it is clear what it is doing.

function f(l)
  for i=1,#l do
    print(l[i])
  end
end

But now imagine that l, rather than being a table or array, is a type in which ordered iteration can be done much more efficiently than random access, e.g. a topological sort on a DAG. By writing the loop this way, we have just sacrificed a load of performance because you have got rid of ipairs. I want to be able to write my function so that it is clear what it does regardless of the type of the argument, and the function uses any efficiencies of the type.

> and tables would behave like they always did. Why do you need to know the type of the list? You are saying the type is irellevant but at the same time that it is relevant.
>


Let us think about my topological sort of DAG object further. Can I currently call table.unpack on it in Lua? In your version can I call list.unpack on it?

Thanks,
Kev