Hi,

PROC main()
LOCAL aCountry, aI
   aCountry := {"LTU"=>"Lithuania", "ZWE"=>"Zimbabwe"}
   FOR EACH aI in aCountry
     QOUT(aI:__enumKey)              // OK
     EVAL({|| QOUT(aI:__enumKey)})   // RTE
   NEXT
RETURN

It's expected and correct behavior. Variables stored in codeblocks
are detached. During detaching enumerators becomes simple references
to iterated items which is not later changed.

I'm just trying to made my understand about an answer to two questions:

1) is it good that enumerators are detached to references to iterated items (or is it better to remain it enumerators in the codeblocks)? 2) can it remain an enumerator in codeblocks? Can we implement it technically? Or this is not possible because of current enumerators implementation or some other side effects.


It allows to create code like:

   proc main()
      local aCountry, aBlocks, aI, aB
      aCountry := {"LTU"=>"Lithuania", "ZWE"=>"Zimbabwe"}
      aBlocks := array( len( aCountry ) )
      FOR EACH aI, aB in aCountry, aBlocks
         aB := {|x| iif( pcount()==0, aI, aI := x ) }
      NEXT
      ? eval( aBlocks[ 1 ] )
      ? eval( aBlocks[ 1 ], "XXXXX" )
      ? eval( aBlocks[ 1 ] )
      ? eval( aBlocks[ 2 ] )
      ? eval( aBlocks[ 2 ], "YYYYY" )
      ? eval( aBlocks[ 2 ] )
   return

As you can see this code does not fail when codeblocks are
evaluated autside FOR EACH loop and in each codeblocks aI
referes to separated aCountry item which were valid when
codeblock was created.

PROC main()
LOCAL aCountry, aI, aJ, nI

   aCountry := {"LTU", "ZWE"}
   aJ := {}
   FOR nI := 1 TO LEN(aCountry)
     aI := aCountry[nI]
     AADD(aJ, {|| aI})
   NEXT
   FOR nI := 1 TO LEN(aCountry)
     ? EVAL(aJ[nI])
   NEXT
RETURN

But in this sample aI is not detached to current aI value, and sample prints:
ZWE
ZWE

How can I easy understand/remember the difference between this behavior and enumerators behavior?


Best regards,
Mindaugas


_______________________________________________
Harbour mailing list
Harbour@harbour-project.org
http://lists.harbour-project.org/mailman/listinfo/harbour

Reply via email to