mirror of
https://github.com/EiffelSoftware/eiffel-org.git
synced 2025-12-08 07:42:33 +01:00
Author:halw
Date:2010-02-03T05:36:23.000000Z git-svn-id: https://svn.eiffel.com/eiffel-org/trunk@433 abb3cda0-5349-4a8f-a601-0c33ac3a8c38
This commit is contained in:
@@ -87,7 +87,7 @@ So, this usage of the loop construct has been the traditional mechanism for trav
|
||||
across my_list as c loop print (c.item) end
|
||||
</code>
|
||||
|
||||
Here the <code>across</code> indicates an iteration process across the structure <code>my_list</code>. The <code>as c</code> indicates that a cursor object referenced by the name <code>c</code> will be created to effect the iteration. The element of <code>my_list</code> which is currently referenced by the cursor <code>c</code> is <code>c.item</code> as you see in the call to <code>print (c.item)</code> loop body. The loop body does not contain the call to the structure's <code>forth</code> feature, as our more traditional example did. Neither do you see the call to <code>start</code> nor the check of <code>off</code> in the exit condition. The semantics of the iteration abstract these for you and relieve you of their burden ... while eliminating opportunities for error.
|
||||
Here the <code>across</code> indicates an iteration process across the structure <code>my_list</code>. The <code>as c</code> indicates that a cursor object referenced by the name <code>c</code>, and available only for the scope of the iteration, will be created to effect the iteration. The element of <code>my_list</code> which is currently referenced by the cursor <code>c</code> is <code>c.item</code> as you see in the call to <code>print (c.item)</code> loop body. The loop body does not contain the call to the structure's <code>forth</code> feature, as our more traditional example did. Neither do you see the call to <code>start</code> nor the check of <code>off</code> in the exit condition. The semantics of the iteration abstract these for you and relieve you of their burden ... and some opportunities for error.
|
||||
|
||||
Concerning cursors, both ways of using the loop construct to traverse a structure employ a cursor. In the traditional usage, the cursor is internal to the structure object, the instance of <code>LINKED_LIST [STRING]</code> called <code>my_list</code>, in the case of the example. Applying the feature <code>item</code> to <code>my_list</code> retrieves the list element currently referenced by the cursor. In the iteration version of traversal, the variable <code>c</code> holds the iteration cursor, external to the list object. So, you apply <code>c.item</code> to get the current list element. The obvious use is for instructions that should be part of the system but executed only in some circumstances, for example to provide extra debugging information. The advantage to the external cursor is that multiple traversals of the structure can occur simultaneously without interfering with one another. This is possible in the traditional usage, but only by saving and restoring the structure's cursor.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user