Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2018-10

Case statements without constants?

7 messages · 6 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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.

Regards,
Ryan Joseph

Reply by email

Sven Barth
to Ryan Joseph

Ryan Joseph schrieb am Mo., 29. Okt. 2018,
10:41:

Show quoted text 5 lines
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

Reply by email

Michael Van Canneyt
to Ryan Joseph: I tried today to make a case that used variables instead of constants and was surprised…
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.

Reply by email

R0b0t1
to Michael Van Canneyt
AM Michael Van Canneyt wrote 11 lines
> 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

Reply by email

Martin
to R0b0t1
R0b0t1 wrote
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.

Reply by email

Ryan Joseph
to R0b0t1: On Mon, Oct 29, 2018 at 11:20 AM Michael Van Canneyt I think it is important to…
Show quoted text 5 lines
On Oct 29, 2018, at 5:24 PM, R0b0t1 wrote: 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

Reply by email

Marco van de Voort
to Ryan Joseph

In our previous episode, Ryan Joseph said:

Show quoted text 9 lines
> > 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.

Reply by email