Discussion:
[fpc-pascal] Case statements without constants?
Ryan Joseph
2018-10-29 09:10:37 UTC
Permalink
I tried today to make a case that used variables instead of constants and was surprised to get an error. I’m sure this been true since the 80’s but why don’t cases allow variables??? I thought a case statement was just a group of if..else blocks in succession.

Regards,
Ryan Joseph

_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pasc
Sven Barth via fpc-pascal
2018-10-29 10:08:26 UTC
Permalink
Post by Ryan Joseph
I tried today to make a case that used variables instead of constants and
was surprised to get an error. I’m sure this been true since the 80’s but
why don’t cases allow variables??? I thought a case statement was just a
group of if..else blocks in succession.
No it's not. At least for ordinal values it's usually implemented as a jump
table or something optimized like that which would not work with variables
as labels.

Regards,
Sven
Michael Van Canneyt
2018-10-29 10:24:13 UTC
Permalink
Post by Ryan Joseph
I tried today to make a case that used variables instead of constants and
was surprised to get an error. I’m sure this been true since the 80’s but
why don’t cases allow variables??? I thought a case statement was just a
group of if..else blocks in succession.
Case has never allowed variables.

and it is also not a group of if..else blocks. The compiler uses jump
tables when possible etc when it encounters a case.

Michael.

_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepasca
R0b0t1
2018-10-29 10:24:59 UTC
Permalink
On Mon, Oct 29, 2018 at 11:20 AM Michael Van Canneyt
Post by Michael Van Canneyt
Post by Ryan Joseph
I tried today to make a case that used variables instead of constants and
was surprised to get an error. I’m sure this been true since the 80’s but
why don’t cases allow variables??? I thought a case statement was just a
group of if..else blocks in succession.
Case has never allowed variables.
and it is also not a group of if..else blocks. The compiler uses jump
tables when possible etc when it encounters a case.
I think it is important to differentiate between a language feature
and its common implementation. Perhaps case statements with variables
make sense? Of course, that doesn't mean anyone is going to add them.

I suggest the OP look up pattern matching. Scala may be more readable
than Haskell, and there is also OCaml.

Cheers,
R0b0t1
_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/l
Martin
2018-10-29 10:28:47 UTC
Permalink
Post by R0b0t1
I think it is important to differentiate between a language feature
and its common implementation. Perhaps case statements with variables
make sense? Of course, that doesn't mean anyone is going to add them.
"case" also includes a compile time check, that case-labels have no
duplicates.

Therefore using variables in place of case labels, would require changes
to the definition of what case does.
_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal
Ryan Joseph
2018-10-29 12:42:32 UTC
Permalink
Post by R0b0t1
I think it is important to differentiate between a language feature
and its common implementation. Perhaps case statements with variables
make sense? Of course, that doesn't mean anyone is going to add them.
I’ve been programming Pascal since about 1995 and I never knew cases were an optimization or had any special meaning. In my mind I thought they were just a nice way to do branching when there were lots of cases to check (hence the “case statement”).

Under the hood I would think cases with variables should just perform as if..else statements but as was pointed out that would require a significant amount of work.

Just wanted to make sure I wasn’t going crazy because you’d think a person like me would know something so basic about Pascal by now.

Regards,
Ryan Joseph

_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/
Marco van de Voort
2018-10-29 13:00:16 UTC
Permalink
Post by R0b0t1
I think it is important to differentiate between a language feature
and its common implementation. Perhaps case statements with variables
make sense? Of course, that doesn't mean anyone is going to add them.
I?ve been programming Pascal since about 1995 and I never knew cases were
an optimization or had any special meaning. In my mind I thought they
were just a nice way to do branching when there were lots of cases to
check (hence the ?case statement?).
It is just like a for loop is not exactly like a naieve while loop because
the bounds are only evaluated once. Some details you only acquire as very
experienced programmer, or implementator.

_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/

Loading...