Eiffel "Gotchas"

19 May 1997


Oops!

Gotcha #4 - Answer and Explanation

Here's the question again:

Suppose we have:

   a: ARRAY[INTEGER]
   ...
   !!a.make(1, 1) -- make with index from 1 to 1 (i.e. one element)
   a.put(1, 1) -- put '1' into position 1 of the array

Why can we then write this instruction...

   print(a.item(1))

..but not this one...

   print(a@1)

Lexically, infix "@" is a Free_operator, and Free_operators terminate with a space so the lexical analyser considers "@1)" to be the Free_operator.

The solution is to make sure there is a space after "@":

   print(a @ 1)

Even Bertrand Meyer fell for this "Gocha"! In OOSC 2nd edition, he boasts that Eiffel uses fewer keystrokes than C to access an element of an array, yet even the quirky asymmetrical "a@ i" uses the same number of keystrokes as "a[i]", and the cleaner "a @ i" is one keystroke more. (If you always use spaces between symbols and letters, then "a @ i" does use less characters than "a [ i ]".)


Eiffel and NICE are registered trademarks of the Nonprofit International Consortium for Eiffel.

Eiffel "Gotchas"