FOREACH on objects
Here's a common coding pattern: iterating over the words (and values) of an object:
foreach word words-of my-object [
The suggestion was made to support such a loop directly:
foreach word my-object [
and also allow:
foreach [word value] my-object [
do-something-else-with word value
The benefit is that no intermediate block of words is constructed.
This change has been added to the A49 release.
>> foreach word system/options [?? word]
Note that the word is bound back to the object, so can be used to access the value (with get).
However, it also supports:
>> foreach [word value] system/options [print [word mold value]]
file-types [%.bmp bmp %.gif gif %.png png %.jpg %.jpeg jpeg]
Be careful with hidden (protected) variables. The loop will skip them:
>> foreach word system/state [?? word]
The state object contains more than two fields, but they are not accessible to user code for security reasons.