1. FOR EACH
Harbour support all xHarbour functionality and it offers also additional
features which are not available in xHarbour.
a) it allows to iterate more then one variable
FOR EACH a, b, c IN aVal, cVal, hVal
? a, b, c
NEXT
b) it allows to set descending order by DESCEND flag, f.e.:
FOR EACH a, v IN aVal, cVal DESCEND
? a, b
NEXT
c) it has native support for hashes:
FOR EACH x IN { "ABC" => 123, "ASD" => 456, "ZXC" => 789 }
? x, "@", x:__enumKey()
NEXT
d) it allows to assign string items, f.e.:
s := "abcdefghijk"
FOR EACH c IN @s
IF c $ "aei"
c := UPPER( c )
ENDIF
NEXT
? s // AbcdEfghIjk
e) it gives OOP interface to control enumerator variables what
is very important when more then one variable is iterated or
when FOR EACH is called recursively, f.e.:
hVal := { "ABC" => 123, "ASD" => 456, "ZXC" => 789 }
FOR EACH x IN hVal
? x:__enumIndex(), ":", x:__enumKey(), "=>", x:__enumValue(), ;
"=>", x:__enumBase()[ x:__enumKey() ]
NEXT
f) it gives very flexible OOP mechanism to overload FOR EACH behavior
for user define objects adding to above enumerator methods also
__enumStart(), __enumStop(), __enumSkip() methods what allows to
implement many different enumeration algorithms depending on used
data
g) it does not have any hardcoded limitations for recursive calls
(it's limited only by available memory and HVM stack size), f.e.:
proc main()
p( 0 )
return
proc p( n )
local s := "a", x
? n
if n < 1000
for each x in s
p( n + 1 )
next
endif
return
In xHarbour there is function HB_ENUMINDEX() which is supported by
Harbour in XHB library.
2. WITH OBJECT / END[WITH]3. SWITCH / [ CASE / [EXIT] / ... ] OTHERWISE / END[SWITCH]4. BEGIN SEQUENCE [ WITH ]
In Harbour it does not have any hardcoded limitations for recursive
calls (it's limited only by available memory and HVM stack size), f.e.:
proc main()
p( 0 )
return
proc p( n )
? n
if n < 1000
with object n
p( n + 1 )
end
endif
return
It also uses OOP interface just like FOR EACH, so it's possible to
use :__withObject() to access / assign current WITH OBJECT value.
In xHarbour there are functions HB_QWITH(), HB_WITHOBJECTCOUNTER()
and HB_RESETWITH() which are supported by Harbour in XHB library.
In Harbour it uses jump table with predefined values what gives
significant speed improvement in comparison to sequential PCODE
evaluation just like in DO CASE statements.
In xHarbour SWITCH does not use such jump table and generated
PCODE is similar to the one used for DO CASE or IF / ELSEIF
and only the main switch value calculation is optimized and
reused for all statements so speed improvement is relatively
small.
In xHarbour instead of OTHERWISE the DEFAULT clause is used.
As SWITCH values Harbour supports integer numbers and strings, f.e.:
switch x
case 1 ; [...]
case 10002 ; [...]
case "data" ; [...]
otherwise ; [...]
endswitch
xHarbour supports only integer numbers and one character length strings
like "A", "!", "x", " ", ...
[ RECOVER [ USING ] ]
[ ALWAYS ]
END SEQUENCE
It's unique to Harbour. In xHarbour limited version of above statement
exists as:
TRY
[ CATCH [] ]
[ FINALLY ]
END
TRY gives exactly the same functionality as:
BEGIN SEQUENCE WITH { |e| break(e) }
With the exception to SWITCH implementation, in all other statements
described above, xHarbour causes performance reduction in PCODE evaluation
even if user does not use them at all. In Harbour they are implemented in
different way which does not cause any overhead and slowness for other code.
More at http://harbour-project.svn.sourceforge.net/viewvc/harbour-project/trunk/harbour/doc/xhb-diff.txt
No comments:
Post a Comment