Recent

Author Topic: [Solved] Weird 'case' block which must not compile  (Read 3425 times)

AlexTP

  • Hero Member
  • *****
  • Posts: 2401
    • UVviewsoft
[Solved] Weird 'case' block which must not compile
« on: April 15, 2021, 08:55:53 pm »
But its compiled.
Lazarus 2.1.0 r64997M FPC 3.2.1 x86_64-linux-gtk2

Code: Pascal  [Select][+][-]
  1. var
  2.   Form1: TForm1;
  3.  
  4. implementation
  5.  
  6. {$R *.lfm}
  7.  
  8. { TForm1 }
  9.  
  10. procedure f;
  11. var
  12.   i: integer;
  13. begin
  14.   i:= 2;
  15.   case i of
  16.     0:
  17.       begin
  18.         sleep(1);
  19.       end;
  20.     1:
  21.       begin
  22.         sleep(3);
  23.       end;
  24.     else
  25.       sleep(4);
  26.  
  27.     sleep(5);
  28.     sleep(6);
  29.   end;
  30. end;
  31.  
  32. procedure TForm1.FormCreate(Sender: TObject);
  33. begin
  34.   f;
  35. end;
  36.  
« Last Edit: April 15, 2021, 09:11:36 pm by Alextp »

WooBean

  • Full Member
  • ***
  • Posts: 230
Re: Weird 'case' block which must not compile
« Reply #1 on: April 15, 2021, 09:10:31 pm »
Hi,

the latest web documentation of FPC allows using list of statements in "else" part of "case of" statement.

"The else clause can contain multiple statements:"
Code: Pascal  [Select][+][-]
  1. Case Number of  
  2.  1..10   : WriteLn (’Small number’);  
  3.  11..100 : WriteLn (’Normal, medium number’);  
  4. else  
  5.   WriteLn (’HUGE number’);  
  6.   Writeln(’How did we get this much ?’);  
  7. end;    
  8.  
(see https://www.freepascal.org/docs-html/ref/refsu55.html#x164-18600013.2.2)
« Last Edit: April 15, 2021, 09:13:19 pm by WooBean »
Platforms: Win7/64, Linux Mint Ulyssa/64

MarkMLl

  • Hero Member
  • *****
  • Posts: 6682
Re: Weird 'case' block which must not compile
« Reply #2 on: April 16, 2021, 12:05:40 am »
the latest web documentation of FPC allows using list of statements in "else" part of "case of" statement.

I believe that's always been the case. But I strongly recommend using "otherwise" rather than "else".

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1119
  • Professional amateur ;-P
Re: Weird 'case' block which must not compile
« Reply #3 on: April 16, 2021, 02:04:46 am »
Hey Mark,

[snip] But I strongly recommend using "otherwise" rather than "else".

I've always used else and just recently have I stumbled into otherwise.

Can you, please, give me a brief history of when both appeared and why is it better to use otherwise?

Many, many thanks in advance for satiating this nagging curiosity of mine :)

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Weird 'case' block which must not compile
« Reply #4 on: April 16, 2021, 02:25:10 am »
Can you, please, give me a brief history of when both appeared and why is it better to use otherwise?
In ISO 7185 Standard Pascal, the case statement doesn't have a clause for non-matching case, i.e. if you want to use a case statement, you have to list ALL of the possibilities or combine it with an if statement, which is kinda ugly. ISO 10206 fixes this by introducing otherwise. The reuse of else keyword, however, is a Borland Pascal extension. Since the latter is more widely used in the form of Turbo Pascal, the use of else for case statement is more prominent than otherwise. Eventually, since both have the same semantics, for widest compatibility both are allowed in Free Pascal, just as in Turbo Pascal.

440bx

  • Hero Member
  • *****
  • Posts: 4014
Re: Weird 'case' block which must not compile
« Reply #5 on: April 16, 2021, 02:36:50 am »
why is it better to use otherwise?
because the "else" in a case statement is ambiguous.  To see an example of the nasty ambiguity it creates look at this post :
https://forum.lazarus.freepascal.org/index.php/topic,49082.msg355332.html#msg355332

The use of "otherwise" eliminates the ambiguity because "otherwise" cannot be used with an "if" statement.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1119
  • Professional amateur ;-P
Re: Weird 'case' block which must not compile
« Reply #6 on: April 16, 2021, 03:23:31 am »
Hey Leledumbo,

In ISO 7185 Standard Pascal, the case statement doesn't have a clause for non-matching case, i.e. if you want to use a case statement, you have to list ALL of the possibilities or combine it with an if statement, which is kinda ugly. ISO 10206 fixes this by introducing otherwise. The reuse of else keyword, however, is a Borland Pascal extension. Since the latter is more widely used in the form of Turbo Pascal, the use of else for case statement is more prominent than otherwise. Eventually, since both have the same semantics, for widest compatibility both are allowed in Free Pascal, just as in Turbo Pascal.

That's very comprehensive. Much obliged for it!!!

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1119
  • Professional amateur ;-P
Re: Weird 'case' block which must not compile
« Reply #7 on: April 16, 2021, 03:26:05 am »
Hey 440bx,

because the "else" in a case statement is ambiguous.  To see an example of the nasty ambiguity it creates look at this post :
https://forum.lazarus.freepascal.org/index.php/topic,49082.msg355332.html#msg355332

The use of "otherwise" eliminates the ambiguity because "otherwise" cannot be used with an "if" statement.

Ouch, I now see the issue. Rather prickly one I must say :)
Well, Imma gonna be using otherwise from now on!!!

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

440bx

  • Hero Member
  • *****
  • Posts: 4014
Re: Weird 'case' block which must not compile
« Reply #8 on: April 16, 2021, 03:54:32 am »
Well, Imma gonna be using otherwise from now on!!!
Just FYI, Delphi 2 and quite likely later versions of Delphi as well, don't support the "otherwise" keyword. 

Because of that, in spite of the "otherwise" being a clearly better choice than using "else", I still use "else".  (I like my programs to compile with both Delphi 2 and the version of FPC I use.)

IOW, there is a price paid in portability when using the "otherwise" keyword.

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1119
  • Professional amateur ;-P
Re: Weird 'case' block which must not compile
« Reply #9 on: April 16, 2021, 06:46:09 am »
Hey 440bx,

Just FYI, Delphi 2 and quite likely later versions of Delphi as well, don't support the "otherwise" keyword. 

You keep mentioning Delphi 2 so much, that you've got me intrigued on why do you need to keep compatibility with such an old version?

I would understand Delphi 7, since, at least to my knowledge, that's when FPC and Delphi kinda parted ways, in a sense, but I'm very intrigued by the specificity of Delphi 2 and not Delphi 1 for that matter.

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

440bx

  • Hero Member
  • *****
  • Posts: 4014
Re: Weird 'case' block which must not compile
« Reply #10 on: April 16, 2021, 07:29:15 am »
You keep mentioning Delphi 2 so much, that you've got me intrigued on why do you need to keep compatibility with such an old version?
Because my 32bit Pascal compiler of choice is Delphi 2.  The great majority of my programs compile with both, Delphi 2 and FPC (without {$MODE DELPHI}) My 64bit compiler of choice is FPC.  I would be extremely surprised if I ever wanted to use the 64bit version of Delphi.  Additionally, I can debug Delphi 2 programs at the source level using SoftICE, not something I will give up anytime soon.

I would understand Delphi 7, since, at least to my knowledge, that's when FPC and Delphi kinda parted ways, in a sense, but I'm very intrigued by the specificity of Delphi 2 and not Delphi 1 for that matter.
Delphi 1 is a 16bit compiler.  That's in the binary museum where it belongs.  I don't use later versions of Delphi because they are bloated and, strictly from a compiler's viewpoint, their additional features aren't worth the bloat.  I have a copy of Delphi 5, which I purchased only because of its support of 64bit integers, I used it for about 3 months before I decided it was more headaches than it was worth.  After Delphi 2, IMO, Delphi went severely and rapidly downhill. 

I guess it's fair to say that I am not the typical user.  My ideal compiler is a "lean, mean, code generating machine" that doesn't offer anything but a good debugger along with tight, fast code.  I dislike run time libraries and, the bigger they are, the more I dislike them.  If I want a button, a listbox or whathaveyou, I can code it myself without bloat. 
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1119
  • Professional amateur ;-P
Re: [Solved] Weird 'case' block which must not compile
« Reply #11 on: April 16, 2021, 07:47:49 am »
Hey 440bx,

I quite like your answer. It paints a perfect picture of what your convictions are :)

Nice to know. And I like it :)

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

MarkMLl

  • Hero Member
  • *****
  • Posts: 6682
Re: Weird 'case' block which must not compile
« Reply #12 on: April 16, 2021, 08:46:22 am »
Can you, please, give me a brief history of when both appeared and why is it better to use otherwise?

I can't put a version on it, but it certainly predates FPC v3. The rationale is to fix this sort of thing:

Code: Pascal  [Select][+][-]
  1.           CASE model OF
  2.             hpm4952: IF NOT sendRsreExpectAcc THEN
  3.                        RAISE Exception.Create('sendRsreExpectAcc() in tryBlockCount()')
  4.                      ELSE BEGIN END
  5.           ELSE
  6.             IF NOT sendTrbrExpectAcc THEN
  7.               RAISE Exception.Create('sendTrbrExpectAcc() in tryBlockCount()')
  8.           END
  9.  

If you didn't have that dummy else begin end then the case statement's else would be misparsed without warning. Use otherwise and there isn't that risk.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

MarkMLl

  • Hero Member
  • *****
  • Posts: 6682
Re: Weird 'case' block which must not compile
« Reply #13 on: April 16, 2021, 08:57:18 am »
Additionally, I can debug Delphi 2 programs at the source level using SoftICE, not something I will give up anytime soon.

Not a Periscope debugger board with a button on the NMI line? Shame on you! :-)

I must say though that having used a Tektronix logic analyser to debug low-level stuff temporarily hosted on a PC I sympathise with your viewpoint.

Quote
Delphi 1 is a 16bit compiler.  That's in the binary museum where it belongs.  I don't use later versions of Delphi because they are bloated and, strictly from a compiler's viewpoint, their additional features aren't worth the bloat.  I have a copy of Delphi 5, which I purchased only because of its support of 64bit integers, I used it for about 3 months before I decided it was more headaches than it was worth.  After Delphi 2, IMO, Delphi went severely and rapidly downhill. 

D1 also had much better helpfiles than several subsequent versions. I remember rumblings to the effect that Borland had had a major mishap with its backups or VCS.

I think it's also worth remembering that at some point Borland required mandatory registration, which is something I'm never happy with.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 5462
  • Compiler Developer
Re: [Solved] Weird 'case' block which must not compile
« Reply #14 on: April 16, 2021, 09:02:25 am »
But its compiled.

And while we're on the topic of ISO Extended Pascal's otherwise-clause this is how case is defined there (section 6.9.3.5):

Code: [Select]
case-statement = `case´ case-index `of´
                 ( case-list-element {`;´ case-list-element }
                 [ [ `;´ ] case-statement-completer ] | case-statement-completer)
                 [ `;´] `end´ .

case-index = expression .

case-list-element = case-constant-list `:´ statement .

case-statement-completer = `otherwise´ statement-sequence .

As you can see each case-label takes a single statement (which can be a block again) while the otherwise-clause (or the else-clause in TP descendants) takes a list of statements already.

 

TinyPortal © 2005-2018