[fpc-devel] "case" with range stupidities
JeLlyFish.software at gmx.net
Thu Feb 15 16:13:48 CET 2007
On Thursday 15 February 2007 14:46, Daniël Mantione wrote:
> Op Thu, 15 Feb 2007, schreef Vinzent Hoefler:
> Because, in a case statement you are already "inside" the brackets:
Yeah, I sort of figured that. ;)
> > Another oddity would be a for loop:
> > for Bla := One to Five do ...
> > would be a range only, yes? (Apart from the fact that - due to the
> > "to" keyword - really isn't).
> It is a range, but one also needs to specify the direction
> (to/downto), so it is logical that for-loops don't use .. syntax.
Oh, right, I forgot that. Damn.
That's why in Ada you can write
for Bla in X'Range loop
for Bla in reverse X'Range loop
for the intended direction of the given range.
> > What about a compiler built-in like Low() and High() called Range()
> > where its return type indicates something that would be compatible
> > to a case and for label and just denote the range of the whole
> > type?
> > Basically a short cut to the "Low (Type) ..|to High (Type)"
> > expression, but without the possible pitfall of writing
> > Low (Type) .. High (A_Similar_And_Unfortunately_Compatible_Type)
> > and thus being less error-prone...?
> It looks okay, but does this pitfall actually exist?
Yes, it does. Especially when copying and pasting. ;)
The reason why I stumbled over that was that I changed a case statement
from a perfect fit:
case Frequency of
1600: Idx = Hz_1600;
where the returned Idx is an index to an array of hardware parameters to
generate that frequency. For some unpublishable reason I changed that
from a perfect match (here: 1600) to a range (say: 1500 .. 1700) and I
started declaring range types for that because putting in the range
into the case label seemed too bad for later changes.
So then I ended up writing:
case Frequency of
Low (Range_1_6_K) .. High (Range_1_6_K) : Idx := ...
Low (Range_1_8_K) .. High (Range_1_8_K) : Idx := ...
for quite some values. Of course you start copy- and pasting things then
and I had to triple check if I really matched the Low(...) with the
according High(...) parts and the correct value for Idx.
> low(colour)..high(wind_direction) would be a type violation.
Yes. But integer ranges are compatible in any case and that's what I am
bound to here (in fact, the code even "kind of" changes those integers
to a type-safe enumeration type for further uses).
> > or
> > |case Bla in
> > |Range (X): ...
> > doesn't actually look unreadable to me...
> Indeed, but do you solve a problem? I.e. I can imagine some kind of
> syntax to allow set constants would help, but this seems mostly a
> typing shortcut.
Well, it is.
The above mentioned lines of Ada code can still be expressed as
for Bla in [reverse] X'Low .. X'High loop
and would still mean the same. And due to the fact that in Pascal such a
Range() built-in would still not be useable in a for loop, it seems,
its use would be too limited to be considered an improvement. Unless
I'd get Reverse_Range(), too. ;)
More information about the fpc-devel