Recent

Author Topic: Reference counting of temporary interfaces  (Read 1169 times)

BeniBela

  • Hero Member
  • *****
  • Posts: 906
    • homepage
Reference counting of temporary interfaces
« on: October 13, 2015, 10:54:00 pm »
I noticed something strange when using interfaces with a for-in-loop. (I had used it as a way to simulate block scoped variables)

Let IFoo be an interface  that can be used in a for-in statement (assume the enumerator does not keep a reference to the IFoo). Let  makeFoo be a function that returns a new IFoo (actually a new one, not the same as before).

Then e.g.:

Code: Pascal  [Select][+][-]
  1. for x in makeFoo do
  2.     writeln(x)

will enumerate all the x in the IFoo.

Afterwards, after the for-in-statement, the IFoo will be destroyed.

But when we write

Code: Pascal  [Select][+][-]
  1. for x in makeFoo do begin
  2.     makeFoo;
  3.     writeln(x);
  4. end

The second call to makeFoo will destroy the IFoo from the first call.

Isn't this very inconsistent?  Either the first foo should be really temporary and be destroyed after the enumerator is created; or kept till the end the loop.

 

TinyPortal © 2005-2018