lua-users home
lua-l archive

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


Thursday, March 12, 2015, 2:39:00 PM, you wrote:

> On Thursday, March 12, 2015 01:37:10 PM mchalkley@mail.com wrote:
>> When trying to use the first version of the "Dir Tree Iterator" sample
>> code on the lua-user.org Wiki page[1], I get the error:
>> 
>> attempt to call field '?' (a userdata value)
>> 
>> for this line:
>>       local entry = diriters[#diriters]()
>> 
>> The second example, using coroutines, works fine.
>> 
>> What's the issue with the first example?
>> 

> I believe LFS changed the way the lfs.dir function works. When this was
> written it must have only returned a unique closure for each directory. Now it
> uses an iterator function with state in a userdata. If you want to stare the
> iterator in a table you need to wrap it in a closure.

>>   local diriters = {lfs.dir(dir)}

> and

>>             table.insert(diriters, lfs.dir(filename))

> Change to

>     local diriters = {diriterator(lfs.dir(dir))}

>     table.insert(diriters, diriterator(lfs.dir(filename)))

> And add this function

>     local function diriterator(func, state)
>         return function()
>             return func(state)
>         end
>     end

> That will do for lfs.dir which doesn't use the counter variable. There's
> another wrapper that works with any iterator, but that's an exercise for the
> reader.

Awesome - that explains it (and it works, even, with a fix to one
little typo)...

Thanks!

Mark