[e-lang] Wart: -1 for not found

Kevin Reid kpreid at mac.com
Tue Mar 10 09:47:49 EDT 2009


The EList methods indexOf1, startOf, lastIndexOf1, lastStartOf return  
-1 if nothing is found. This is a wart outside of a static type  
system, and a potential source of bugs if not checked for. I propose  
that they be changed to either return null or a broken reference upon  
failure, or take an ejector/return-value-thunk parameter which is  
invoked upon failure.

The problems with returning -1 are:
   - it is a number, so code can erroneously proceed as if it were an  
index
   - what does -2 mean?

Note this end of a previous discussion thread that indexOf1 should be  
renamed to indexOf: http://www.eros-os.org/pipermail/e-lang/2005-May/010623.html 
  -- doing both of these changes at once would be better than  
separately.

Common Lisp's analogous functions, search and position, return nil  
upon failure.
http://www.lispworks.com/documentation/HyperSpec/Body/f_pos_p.htm
http://www.lispworks.com/documentation/HyperSpec/Body/f_search.htm

Sample code (from evalServerPool.e):

def i := active.indexOf1(es)
if (i != -1) {
     active.removeRun(i, i + 1)
}

If indexOf1 returns null, this can be written as:

if (active.indexOf1(es) =~ i :int) {
     active.removeRun(i, i + 1)
}

(notNull could be used instead of int.)

If indexOf1 takes a failure-handler, this can be written as:

escape fail {
   def i := active.indexOf1(es1, fail)
   active.removeRun(i, i + 1)
}

or, perhaps perversely,

if (es =~ via (active.indexOf1) i) {
   active.removeRun(i, i + 1)
}

-- 
Kevin Reid                            <http://homepage.mac.com/kpreid/>




More information about the e-lang mailing list