for-must-match

Thomas Leonard <tal-v5nx5w6akNyLE8xUarVfuPLx9OUvmyODWmv/[email protected]> Fri, 21 Oct 2011 15:43:46 +0100
Newsgroups gmane.comp.lang.e.general
Message-ID <[email protected]>
Continuing with our experimental syntax changes to improve the 
reliability of E, here's a patch that adds a "for-must-match" feature.

http://gitorious.org/~tal-itinnov/repo-roscidus/it-innovation/commit/3ad4f5c3072447647c617fdb56738bf929cb8a1a

(see also: http://wiki.erights.org/wiki/Surprise_list)


Normally in E, this silently skips the third item:

   for a:int in [1, 2, "hi"] {
     println(a)
   }


Now, it throws an exception:

   pragma.enable("for-must-match")

   for a:int in [1, 2, "hi"] {
     println(a)
   }

To make a loop that skips non-matching items as before, use the "match" 
keyword. e.g.

   pragma.enable("for-must-match")

   for match a:int in [1, 2, "hi"] {
     println(a)
   }

In our code-base of > 20,000 lines of E, I only found two places making 
use of the old behaviour (or at least, all the tests passed after I 
updated those two ;-).

In contrast, there have been many occasions where the old behaviour 
caused problems. Most commonly, someone is looping over some tuples:

   for [foo, bar] in tuples { ... }

and someone changes the format to add an extra field. Since this doesn't 
cause any errors, it's hard to detect that anything is wrong.


-- 
Dr Thomas Leonard
IT Innovation Centre
Gamma House, Enterprise Road,
Southampton SO16 7NS, UK


tel: +44 23 8059 8866

mailto:tal-v5nx5w6akNyLE8xUarVfuPLx9OUvmyODWmv/[email protected]
http://www.it-innovation.soton.ac.uk/