Case statements without constants?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Case statements without constants?

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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Case statements without constants?

Free Pascal - General mailing list
Ryan Joseph <[hidden email]> schrieb am Mo., 29. Okt. 2018, 10:41:
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 

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Case statements without constants?

Michael Van Canneyt
In reply to this 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  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Case statements without constants?

R0b0t1
On Mon, Oct 29, 2018 at 11:20 AM Michael Van Canneyt
<[hidden email]> wrote:

>
> > 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  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Case statements without constants?

Martin Friebe
On 29/10/2018 11:24, 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Case statements without constants?

Ryan Joseph
In reply to this post by R0b0t1


> On Oct 29, 2018, at 5:24 PM, R0b0t1 <[hidden email]> 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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Case statements without constants?

Marco van de Voort
In our previous episode, Ryan Joseph said:
> >
> > 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  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal