Page 1 of 2
Allow single-line if statements without EndIf
Posted: Mon Feb 17, 2025 4:26 pm
by Quin
In a lot of other BASIC dialects like BCX or AutoIt, I can do something like:
Meanwhile, to do the same thing in PB, I have to:
The ability to declare a single statement next to an if block like this would be incredibly nice.
Re: Allow single-line if statements without EndIf
Posted: Mon Feb 17, 2025 5:03 pm
by Caronte3D
+1
Re: Allow single-line if statements without EndIf
Posted: Mon Feb 17, 2025 9:00 pm
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.
Re: Allow single-line if statements without EndIf
Posted: Mon Feb 17, 2025 9:50 pm
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.
Re: Allow single-line if statements without EndIf
Posted: Mon Feb 17, 2025 10:08 pm
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.
Re: Allow single-line if statements without EndIf
Posted: Tue Feb 18, 2025 12:26 am
by BarryG
Re: Allow single-line if statements without EndIf
Posted: Tue Feb 18, 2025 12:56 am
by Quin
Clever, and I never would've found it from searching. Thanks!
Re: Allow single-line if statements without EndIf
Posted: Tue Feb 18, 2025 9:53 am
by Caronte3D
I wrote +1 but my idea was more like that (without "then"):
Re: Allow single-line if statements without EndIf
Posted: Tue Feb 18, 2025 9:56 am
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())
Re: Allow single-line if statements without EndIf
Posted: Tue Feb 18, 2025 10:09 am
by Caronte3D
Not bad

Re: Allow single-line if statements without EndIf
Posted: Tue Feb 18, 2025 10:29 am
by Quin
Caronte3D wrote: Tue Feb 18, 2025 9:53 am
I wrote +1 but my idea was more like that (without "then"):
Likewise, I wasn't suggesting adding the Then keyword to PB, I was simply asking for single-line if statements like that

Re: Allow single-line if statements without EndIf
Posted: Tue Feb 18, 2025 11:35 am
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.

Re: Allow single-line if statements without EndIf
Posted: Tue Feb 18, 2025 12:20 pm
by AZJIO
Caronte3D wrote: Tue Feb 18, 2025 9:53 am
I wrote +1 but my idea was more like that (without "then"):
How do I know where the end of the block is? For the compiler, the code will look like this:
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.
Re: Allow single-line if statements without EndIf
Posted: Tue Feb 18, 2025 1:29 pm
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.
Re: Allow single-line if statements without EndIf
Posted: Tue Feb 18, 2025 2:25 pm
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()