Allow single-line if statements without EndIf

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Quin
Addict
Addict
Posts: 1133
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Allow single-line if statements without EndIf

Post by Quin »

In a lot of other BASIC dialects like BCX or AutoIt, I can do something like:

Code: Select all

If Condition Then Call MySub
Meanwhile, to do the same thing in PB, I have to:

Code: Select all

If Condition : MySub() : EndIf
The ability to declare a single statement next to an if block like this would be incredibly nice.
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Allow single-line if statements without EndIf

Post by Caronte3D »

+1
User avatar
Demivec
Addict
Addict
Posts: 4267
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Allow single-line if statements without EndIf

Post by Demivec »

Quin wrote: Mon Feb 17, 2025 4:26 pm The ability to declare a single statement next to an if block like this would be incredibly nice.
The request if implemented only saves the typing of one additional character (or three if formatted according to my preferences).

Code: Select all

;1234567890123456789012345
 If Condition Then MySub()
;12345678901234567890123456
 If Condition:MySub():EndIf
 
;or the way I format it
;1234567890123456789012345678
 If Condition: MySub(): EndIf
I think it would be a nice as a 'throwback syntax' but see it as totally unnecessary otherwise. In BASIC implementations where nested Else and ElsieIf are also implemented in a single statement things can be quite convoluted. I like PureBasic's consistent implementation as separate lines with the option to make compound statements for compactness with ':'.

-1.
Little John
Addict
Addict
Posts: 4789
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Allow single-line if statements without EndIf

Post by Little John »

Demivec wrote: Mon Feb 17, 2025 9:00 pm I like PureBasic's consistent implementation as separate lines with the option to make compound statements for compactness with ':'.
Me too.
User avatar
skywalk
Addict
Addict
Posts: 4218
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Allow single-line if statements without EndIf

Post by skywalk »

This is what : is for.
Adding another syntax keyword(Then) should do more than this.
IFF is an example that would do more.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: Allow single-line if statements without EndIf

Post by BarryG »

Quin
Addict
Addict
Posts: 1133
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Allow single-line if statements without EndIf

Post by Quin »

Clever, and I never would've found it from searching. Thanks!
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Allow single-line if statements without EndIf

Post by Caronte3D »

I wrote +1 but my idea was more like that (without "then"):

Code: Select all

If Condition : MySub()
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: Allow single-line if statements without EndIf

Post by BarryG »

@Caronte3D: Like this? Brackets are required, though. :(

Code: Select all

Macro Do (code)
  : code : EndIf
EndMacro

Procedure MySub()
  Debug "a=1"
EndProcedure

a=1

If a : Do(MySub())
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Allow single-line if statements without EndIf

Post by Caronte3D »

Not bad :wink:
Quin
Addict
Addict
Posts: 1133
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Allow single-line if statements without EndIf

Post by Quin »

Caronte3D wrote: Tue Feb 18, 2025 9:53 am I wrote +1 but my idea was more like that (without "then"):

Code: Select all

If Condition : MySub()
Likewise, I wasn't suggesting adding the Then keyword to PB, I was simply asking for single-line if statements like that :D
User avatar
Skipper
User
User
Posts: 57
Joined: Thu Dec 19, 2024 1:26 pm
Location: NW-Europe

Re: Allow single-line if statements without EndIf

Post by Skipper »

Would this request for syntactic sugar really bring the PureBasic language forward?
I must admit, I don't really get it, but I'm ready to learn something new. :?
<< Win-11 (x64) / Mint linux (x64) / MacOS Monterey (x64) >>
AZJIO
Addict
Addict
Posts: 2191
Joined: Sun May 14, 2017 1:48 am

Re: Allow single-line if statements without EndIf

Post by AZJIO »

Caronte3D wrote: Tue Feb 18, 2025 9:53 am I wrote +1 but my idea was more like that (without "then"):

Code: Select all

If Condition : MySub()
How do I know where the end of the block is? For the compiler, the code will look like this:

Code: Select all

If Condition
	MySub()
The next line will belong to the "if" block or will it be external commands? If you propose a change, you have to provide a way for humans to recognize it, and only then can it be described by program code.

In GRUB2, "fi" is used as the end of the block. It would be easier to enter the code.

Code: Select all

if Condition
	MySub()
fi
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Allow single-line if statements without EndIf

Post by Caronte3D »

AZJIO wrote: Tue Feb 18, 2025 12:20 pm How do I know where the end of the block is?
Easy, the compiler would add EndIf after the next statement, just before the newline and only if everything is on the same line.
If your question is how YOU know... well you can see the only one line (maybe without the ":" if that would be supported.
AZJIO
Addict
Addict
Posts: 2191
Joined: Sun May 14, 2017 1:48 am

Re: Allow single-line if statements without EndIf

Post by AZJIO »

Autoit3 has a difference for such methods. If a one-line condition, then after the keyword "Then" there is a command, and if multi-line, then after the keyword "Then" there is a new line. Here the interpretation is obvious.
The character ":" and #LF$ is just equivalent divisors and are not a separate key word. Character ":" It is not a keyword for the condition construct.

We need to enter a new keyword, for example:

Code: Select all

If Condition fi MySub()
If (Condition) MySub()
If [Condition] MySub()
If {Condition} MySub()
Post Reply