[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: destructive iteration patterns
- From: Josh Haberman <jhaberman@...>
- Date: Tue, 12 Apr 2011 07:18:20 +0000 (UTC)
Sam Roberts <vieuxtech <at> gmail.com> writes:
> > It's sad to have to give up the very nice iterator syntax, but this
> > seems like the cleanest way to provide these semantics which
> > can be implemented much more efficiently!
> >
> > Thoughts?
>
> You don't want accesses to an old entry to "crash", but are you ok
> with it returning nils, or erroring?
I would be ok with throwing an error, but what worries me is the
case where I can't detect incorrect usage:
last_entry = nil
for entry in ParseFast(io.stdin) do
if last_entry then
-- whoops, will actually return this entry's IP address!
print(last_entry.ip_address)
end
-- Doesn't do what you intend!
last_entry = entry
end
Maybe the thing to do is to expose the more efficient version, but
also expose an iterator that is by nature less efficient (by doing a
clone of the entry every time). A lot of people probably won't be
as concerned about efficiency as I am, so they can use the nicer
syntax.
> Why not use userdata, so if they keep an entry in a global, they can
> keep it, and if they don't assign it to a global it will get garbage
> collected?
I ultimately want to avoid having to do a per-entry allocation.
malloc() is *expensive* when performed this often.
Thanks,
Josh