Path: utzoo!attcan!uunet!mcvax!enea!sommar From: sommar@enea.se (Erland Sommarskog) Newsgroups: comp.lang.eiffel Subject: Re: Multi-branch instructions and unique values (Version 2.2 preview) Message-ID: <4377@enea.se> Date: 19 Mar 89 11:36:04 GMT Organization: ENEA DATA AB, Sweden Lines: 68 Bertrand Meyer (bertrand@eiffel.UUCP) writes: > It is important to note that the test instruction has >no ``else'' clause or equivalent, and that its effect if none of >the test values matches that of the expression is NOT to >execute an empty operation but to trigger an exception. The >test instruction (as its Pascal and Ada counterparts) is >potentially dangerous because it reflects the programmer's >expectation that the value of e will be one of the stated >test values. The least we can expect from the programmer is >that he precisely specify what these expected values are. >If at run-time the expression does not evaluate to any of >these, then this is a serious abnormal case that should be >detected immediately, not quietly ignored. I fully share the view that if the case selector is not matched, this should be considered as an error and an exception should be raised. However, I object to the omission of an OTHERWISE clause and to to the comparison to Ada and Pascal. Ada and Pascal first. I don't have any Pascal standard reference at hand, but the behaviour I expect is to abandon execution if the CASE selector is out of range, unless an OTHERWISE clause have been given. (OTHERWISE is not standard Pascal.) A problem may be if the compiler allows you to turn these checks off. (VAX- Pascal does.) Ada have a different approach. The fragment below is illegal Ada: ch : character; ... Case ch of when 'A'..'Z' => Text_io.Put_line('Capital letter'); when 'a'..'z' => Text_io.Put_line('Common letter'); End case; Ada requires you to either mention all elements of the selector's subtype or give an OTHERS clause. The only possibility to get into a CASE statement in Ada without a match is if the selector is outside its subtype due to defaulting from initiating it. This certainly is a problem, but hardly not one with CASE statements as such. Why have an OTHERWISE clause? Say we have a simple interactive program where we read one character from the user and then chose a command. Without OTHERWISE I would have to write: (Pascal syntax, since I'm lazy.) CASE 'ch' OF 'A', 'a' : Do_a_command; 'F', 'f' : Do_f_command; 'R', 'r' : Do_r_command; chr(0)..'@', 'B'..'E', 'G'..'Q', 'S'..'`', 'b'..'e', 'g'..'q', 's'..chr(255) : writeln('Illgeal command'); END; With an OTHERWISE it would be very cleaner. Not talking on how much easier it would be to add one more command. There are other situations where an OTHERWISE nicely exhibits the problem behind the logic. At least in Pascal and Ada programs. I understand that Eiffel will take away some of these situations, but I doubt that it will remove all of them. My suggestion is that the TEST statement is given an optoinal OTHERWISE clause. If the clause is not present, an exception is raised if there is no choice matching the selector. -- Erland Sommarskog ENEA Data, Stockholm This signature is not to be quoted. sommar@enea.se