Recent

Author Topic: A small inconsistency  (Read 1558 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1791
A small inconsistency
« on: July 17, 2025, 10:18:29 am »
With for...in loop,

Code: Pascal  [Select][+][-]
  1. var
  2.     idx: array[0..10] of integer;
  3.     i: integer;
  4.  
  5.     cfs : array[0..10] of TStringList;
  6.     acfs: TStringList;
  7.  
  8. begin
  9.     for i in idx do i := 0;   // this raises compile error.  resetting "for loop" index
  10.     for acfs in cfs do acfs.free;   // this seems to work fine.
  11. end;

The i in the previous case is not a loop index in practice. But only reading is possible, and writing is not possible. How do you think?
« Last Edit: July 17, 2025, 10:21:18 am by egsuh »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12851
  • FPC developer.
Re: A small inconsistency
« Reply #1 on: July 17, 2025, 10:33:23 am »
Yes, it is weird, but consistent. Note that in the second case you don't change the loop variable (the class reference, which is a pointer), but call a method on it, which is allowed. Secondary effects of that (freeing the class, turning the reference stale) can't be modelled by for ... in.


gues1

  • Guest

Thaddy

  • Hero Member
  • *****
  • Posts: 19150
  • Glad to be alive.
Re: A small inconsistency
« Reply #3 on: July 17, 2025, 07:05:12 pm »
Yes, finally you understood it. (or still not?)
objects are fine constructs. You can even initialize them with constructors.

gues1

  • Guest
Re: A small inconsistency
« Reply #4 on: July 17, 2025, 09:59:06 pm »
Yes, finally you understood it. (or still not?)
It's for me ?

If it is, I understand like all other things, but may be I'm not "inline" with you.
This doesn't mean your ideas or solutions aren't good.
When they're worthwhile, I'm happy to share them.

 

TinyPortal © 2005-2018