Recent

Author Topic: ways to avoid begin-end’s?  (Read 5912 times)

Weiss

  • Full Member
  • ***
  • Posts: 168
ways to avoid begin-end’s?
« on: May 08, 2024, 10:26:09 pm »
I am thinking, there are quite often times when there are only two statements between begin-end encapsulation. Wouldn’t it be neat if I could say something like
Code: Pascal  [Select][+][-]
  1.  if x=1 then x:=2 AND DoSomething(x)

rather than currently
Code: Pascal  [Select][+][-]
  1. if x=1 then
  2.      begin
  3.        x:=2;
  4.        doSomething(x)
  5.      end;
  6.  

or maybe there is some other neat ways of combining statements
« Last Edit: May 08, 2024, 10:28:41 pm by Weiss »

MarkMLl

  • Hero Member
  • *****
  • Posts: 7533
Re: ways to avoid begin-end’s?
« Reply #1 on: May 08, 2024, 10:58:57 pm »
If there's a single statement then you can omit the begin-end. If there isn't then you can't, and trying to change things will make the language into something that isn't Pascal: isn't going to happen.

Now if an assignment also had a value, which is the situation in C and also in Pascal's predecessor ALGOL-60, then you could do something like

Code: [Select]
if x=1 then
  (x:=2 = 2) AND DoSomething(x);

However (a) that is at least as cumbersome as having an explicit begin-end and (b) that kind of chained assignment becomes incredibly problematic when a language supports any form of automatic casting (e.g. an expression which probably returns an 8-bit value is assigned to a 32-bit variable).

Wirth's newer languages such as Modula-2 required the end in all cases:

Code: [Select]
IF x=1 THEN
  x:=2;
  DoSomething(x)
END;

In that case it would be possible to argue for "syntactic sugar" e.g.

Code: [Select]
IF x=1 THEN:
  x:=2;

But I am really not convinced that that would be a good thing.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11732
  • FPC developer.
Re: ways to avoid begin-end’s?
« Reply #2 on: May 08, 2024, 11:23:42 pm »
No!

cdbc

  • Hero Member
  • *****
  • Posts: 1499
    • http://www.cdbc.dk
Re: ways to avoid begin-end’s?
« Reply #3 on: May 08, 2024, 11:27:10 pm »
Hi
The pascal language entails 'begin .. end', if it doesn't, it's not pascal.
...And I can imagine a debugging nightmare with 'Italian cuisine'-code like this:
Quote
if x=1 then x:=2 AND DoSomething(x)
Not to mention the confusion regarding bit/logical operators...
Nah, I like my begin-ends, they make it easy(ier) to spot mistakes  :D
Right..., /now/ you can call me a 'conservative old fart'  :P
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Weiss

  • Full Member
  • ***
  • Posts: 168
Re: ways to avoid begin-end’s?
« Reply #4 on: May 08, 2024, 11:39:51 pm »
ok. Point taken. I agree, I was wrong. In retrospect, it was probably begin-end’s that made me like Pascal in first place.

Kays

  • Hero Member
  • *****
  • Posts: 604
  • Whasup!?
    • KaiBurghardt.de
Re: ways to avoid begin-end’s?
« Reply #5 on: May 09, 2024, 01:13:18 am »
ways to avoid begin-end’s?
Opening a can of… spaghetti: It is called goTo.
Code: Pascal  [Select][+][-]
  1. label
  2.         9999;
  3. begin
  4.         if x <> 1 then goto 9999;
  5.                 x := 2;
  6.                 doSomething
  7.  
  8. 9999:
  9.         ;
Yeehaw, no (extra) beginend anymore.
Yours Sincerely
Kai Burghardt

Weiss

  • Full Member
  • ***
  • Posts: 168
Re: ways to avoid begin-end’s?
« Reply #6 on: May 09, 2024, 01:58:33 am »
using my example above, begin-end could be avoided by passing argument like so
Code: Pascal  [Select][+][-]
  1. if x=1 then doSomething(2);


Thaddy

  • Hero Member
  • *****
  • Posts: 15557
  • Censorship about opinions does not belong here.
Re: ways to avoid begin-end’s?
« Reply #7 on: May 09, 2024, 05:50:02 am »
I am thinking, there are quite often times when there are only two statements between begin-end encapsulation. Wouldn’t it be neat if I could say something like
Code: Pascal  [Select][+][-]
  1.  if x=1 then x:=2 AND DoSomething(x)
But that is already legal assuming that DoSomething is a function that returns an ordinal value.  :D
« Last Edit: May 09, 2024, 05:51:36 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

dbannon

  • Hero Member
  • *****
  • Posts: 3026
    • tomboy-ng, a rewrite of the classic Tomboy
Re: ways to avoid begin-end’s?
« Reply #8 on: May 09, 2024, 07:13:47 am »
....
But that is already legal assuming that DoSomething is a function that returns an ordinal value.  :D

Yes, to my surprise, it compiles. That means the compiler sees x:=2 as something that returns an ordinal value ? But not (x:=2) ?

Code: Pascal  [Select][+][-]
  1. if x=1 then (x:=2) AND DoSomething(x); // does not compile

Davo

Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Weiss

  • Full Member
  • ***
  • Posts: 168
Re: ways to avoid begin-end’s?
« Reply #9 on: May 09, 2024, 07:37:54 am »
however, something like
Code: Pascal  [Select][+][-]
  1. if X=1 then X:=2 and i

does compile, and, like Thaddy mentioned, if doSomething is a function
Code: Pascal  [Select][+][-]
  1. function doSomething : integer
  2. begin
  3. writeln('What Thaddy says goes');
  4. end;

then this
Code: Pascal  [Select][+][-]
  1. if x=1 then x:=2 and doSomething

compiles and runs fine, prints on screen.

 

Weiss

  • Full Member
  • ***
  • Posts: 168
Re: ways to avoid begin-end’s?
« Reply #10 on: May 09, 2024, 07:39:50 am »
unreal

440bx

  • Hero Member
  • *****
  • Posts: 4491
Re: ways to avoid begin-end’s?
« Reply #11 on: May 09, 2024, 07:54:02 am »
That means the compiler sees x:=2 as something that returns an ordinal value ? But not (x:=2) ?
The compiler sees:
Code: Pascal  [Select][+][-]
  1. if x=1 then x:= (2 AND DoSomething(x));

It doesn't see the assignment as an operation that is separate from the "and".  It sees (as it should) whatever is right of the assignment as a statement.  integer AND integer is a valid statement therefore x := integer and integer (result from the function) is perfectly valid and really, totally run of the mill (though that may not be apparent at first sight.)

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

Khrys

  • Jr. Member
  • **
  • Posts: 82
Re: ways to avoid begin-end’s?
« Reply #12 on: May 09, 2024, 07:56:17 am »
That means the compiler sees x:=2 as something that returns an ordinal value ? But not (x:=2) ?
The compiler sees:
Code: Pascal  [Select][+][-]
  1. if x=1 then x:= (2 AND DoSomething(x));

It doesn't see the assignment as an operation that is separate from the "and".  It sees (as it should) whatever is right of the assignment as a statement.  integer AND integer is a valid statement therefore x := integer and integer (result from the function) is perfectly valid and really, totally run of the mill (though that may not be apparent at first sight.)



Better make sure  DoSomething  doesn't just return some garbage from the stack. Throw in an  Exit(not 0)  and enjoy abusing the syntax, I guess...

440bx

  • Hero Member
  • *****
  • Posts: 4491
Re: ways to avoid begin-end’s?
« Reply #13 on: May 09, 2024, 08:06:19 am »
Better make sure  DoSomething  doesn't just return some garbage from the stack. Throw in an  Exit(not 0)  and enjoy abusing the syntax, I guess...
I believe that for that definition of "DoSomething" the compiler emits a note or warning stating that the function isn't assigned a result (something which is not a good thing regardless of how the function is used.)

Just for the record, it is not syntactic abuse to have a statement such as "x := <some integer> and SomeFunction();" SomeFunction could simply be a function that returns the correct mask to use in that situation.  Quite run of the mill stuff.

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

MarkMLl

  • Hero Member
  • *****
  • Posts: 7533
Re: ways to avoid begin-end’s?
« Reply #14 on: May 09, 2024, 08:52:29 am »
ok. Point taken. I agree, I was wrong. In retrospect, it was probably begin-end’s that made me like Pascal in first place.

It was a fair question, and the discussion has highlighted the ways in which even apparently innocuous "features" of a language can get messy.

Chained assignments, automatic type conversion, confusion between logical and bitwise operations...

Considering this reading of your example

Code: [Select]
    if x=1 then x:= (2 AND DoSomething(x));

the real question is whether DoSomething() returns an integer or a boolean. If it's an integer then that AND might work... but then the compiler could reasonably ask itself whether the sizes are compatible. If it's a Boolean it shouldn't work, but there's a chance of things going wrong if the compiler tries to promote DoSomething()'s result to an integer. So in either case that literal 2, which is clearly numeric but hasn't been explicitly given a type, /should/ at least result in a warning which the user /should/ take note of.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018