[M3devel] comparisons vs. subranges
Rodney M. Bates
rodney_bates at lcwb.coop
Sun Mar 14 17:54:58 CET 2010
Jay K wrote:
> Hm. You mean, like I'm not supposed to be able to say:
>
>
> PROCEDURE F1(bool:BOOLEAN;a:[0..1];b:[2..3])=
> BEGIN
> CASE bool OF
> | (a < b) => IO.Put("true\n");
> | (a > b) => IO.Put("false\n");
> END;
>
No, because a and b are not constants, and thus neither is a < b. Use
an IF ladder for this.
>
> but I can say:
>
>
> PROCEDURE F1(bool:BOOLEAN;a:[0..1];b:[2..3])=
> BEGIN
> CASE bool OF
> | (LAST([0..1]) < FIRST([2..3]) => IO.Put("true\n");
> | (FIRST([0..1]) > LAST([2..3])=> IO.Put("false\n");
> END;
Yes. You can apply FIRST to a(non-open) array or ordinal _type_.
>
> (I'd like to be able to say FIRST(a), etc., but I don't think it is
> allowed.)
>
Yes, or rather no. Yes, it's not allowed. Yes, we have no bananas.
You can apply FIRST to a _variable_ of array type and it will be legal
and (if not an open array), constant. You can't apply FIRST to a variable
of ordinal type. While probably not very useful, it has always seemed to
me to be a bit inconsistent that you can't. It might be useful in some
kind of generated, generic, or otherwise parameterized code.
If you intend these examples just to talk about language rules, OK.
If examples of how to write actual code, they seem convoluted to me.
They are better candidates for IF ladders. When the type of the
expression is BOOLEAN, probably a single IF statement, with ELSE,
is in order.
The only situation I can think of where I would code something like this
is if I wanted to force a compile-time error if the two expressions
in the two alternatives ever became equal during maintenance, because
someone changed one of the constants or types in a way that violated some
algorithmic constraint. Or maybe just to emphasize that the case label
expression should be constant and get the compiler to verify that.
More information about the M3devel
mailing list