One Line If Statements

Propose new features and changes
User avatar
Raccoon
Posts: 49
Joined: 26 Jul 2014, 16:15

One Line If Statements

02 Mar 2019, 23:20

The limitations of If-condition formatting has bothered me for a very long time.

Currently allowed:

Code: Select all

if (1 = 1)
  msgbox true
  
if (1 = 1) {
  msgbox true
}
Please add support for some-or-all of the following:

Code: Select all

If (1 = 1) msgbox true

If (1 = 1), msgbox true

If (1 = 1) { msgbox true }

If (1 = 1) then msgbox true
(why then?) if (1 == 1) and (2 == 2) then msgbox true
(it's a style that can be explained to newbies)

More style examples to support

Code: Select all

if (hctrl), hwnd := hctrl

if (hctrl), temp:=hctrl, hctrl:=hwnd, hwnd:=temp

if (hctrl)
  , hwnd:=temp
  , hctrl:=hwnd
  , hctrl:=temp
  
if (hctrl),
  hwnd:=temp,
  hctrl:=hwnd,
  hctrl:=temp
  
if (hctrl) {
  hwnd:=temp
  hctrl:=hwnd
  hctrl:=temp }

if (hctrl)
{ hwnd:=temp
  hctrl:=hwnd
  hctrl:=temp }
  
if (hctrl) { hwnd:=temp
  hctrl:=hwnd
  hctrl:=temp }
Last edited by Raccoon on 02 Mar 2019, 23:40, edited 2 times in total.
TAC109
Posts: 427
Joined: 02 Oct 2013, 19:41
Location: New Zealand

Re: One Line If Statements

02 Mar 2019, 23:24

Code: Select all

v := 1
ifequal v,1, msgbox true ; works fine
Edit: fix example
Last edited by TAC109 on 03 Mar 2019, 13:54, edited 1 time in total.
My programs:-
ReClip - a Text Reformatting and Clip Management utility
XRef - Produces Cross Reference lists for scripts
User avatar
nnnik
Posts: 4328
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: One Line If Statements

03 Mar 2019, 06:16

None of that will work with the current v1 parser. The v2 parser is still the same as far as Im aware - however lexikos might change that in the future for v2.
At that point we will be able to change the syntax a lot more freely. Since it possibly is a breaking change only in v2 I guess.
Recommends AHK Studio
User avatar
Raccoon
Posts: 49
Joined: 26 Jul 2014, 16:15

Re: One Line If Statements

03 Mar 2019, 06:45

You say none of that will work with the current v1 parser, but don't go into any detail.

Why wouldn't it?

Code: Select all

if (1 = 1), bar:=foo, baz:=bar, quux:=baz
if (1 = 1) { bar:=foo, baz:=bar, quux:=baz }
At the very least, these two styles should work. We already use the comma as a hard delimiter in classic and expression syntax parsing. Brackets should easily work this way too, there's no confusion about what either of these styles could mean. I don't see why (new-line / line-feed) is the only delimiter that would work in this scenario. Commas are substituted for \n all over the place.
User avatar
nnnik
Posts: 4328
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: One Line If Statements

03 Mar 2019, 06:56

The , is part of the expression here therefore it cannot be used here since we dont know whether it's actually just part of the expression or a 1-line if.
This would be a breaking change because you can already use that code and it is valid but doesn't mean what you say it should mean.
We'd have to reove the old meaning and add a new one to make this work - as removing is a breaking change this would only be possible from v1 to v2.

I think he tried adding the second one before but had issues with object syntax in the expression of the if due to restrictions of the current parser.
Recommends AHK Studio
User avatar
Raccoon
Posts: 49
Joined: 26 Jul 2014, 16:15

Re: One Line If Statements

03 Mar 2019, 07:05

So why would this work? And how is the parer confused by the if statement "for something other than it seems"?

Code: Select all

foo:=5, function(foo), bar:="baz", function(bar)
As far as the help documentation goes, "if (1 = 1)" is known as an "expression if". So it's an expression just like any function or variable assignment and should share in comma communion. ♪ ♫ Comma comma comma communion, if this then else, if this then el-el-else ♪ ♫
User avatar
nnnik
Posts: 4328
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: One Line If Statements

03 Mar 2019, 07:16

As far as the help documentation goes, "if (1 = 1)" is known as an "expression if". So it's an expression just like any function or variable assignment and should share in comma communion. ♪ ♫ Comma comma comma communion, if this then else, if this then el-el-else ♪ ♫
Oh really thats very interesting - can you point me at the exact line in the docs where it states that the if itself is an expression and can be part of an expression?
And I don't mean the part where the if can contain an expression - thats common knowledge I guess.
Recommends AHK Studio
User avatar
Raccoon
Posts: 49
Joined: 26 Jul 2014, 16:15

Re: One Line If Statements

03 Mar 2019, 07:24

No, it doesn't say it can be used like this, it's just confusing to me that it can't be. You're telling me that the parser is confused by how ifs work within proximity to expressions, even though this rendition of IF was written at the advent of expressions for the use with expressions, so it's fully aware of what expressions are and how to interact with them. My suggestion is that when an if statement encounters a comma, it knows to treat that comma as a new-line, and everything beyond it as the commands or expressions to process provided the if-condition(s) are true. It would never encounter a comma for any other reason.

Does an unquoted comma currently mean anything special for any line that begins with [spaces]if[spaces]?
User avatar
nnnik
Posts: 4328
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: One Line If Statements

03 Mar 2019, 08:08

Yes it does. It's part of the expression of the if. That meaning is set and cannot be redefined for your purposes.

The parser has not changed a lot since AHK Basic - it's old and needs changing, it limits what we can do and achieve.
It will be changed in v2.
Recommends AHK Studio
User avatar
Raccoon
Posts: 49
Joined: 26 Jul 2014, 16:15

Re: One Line If Statements

03 Mar 2019, 14:50

Can you give me a demonstration of how a person might use commas in the expression of an If? I never knew this worked
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: One Line If Statements

03 Mar 2019, 15:27

I'm trying if here, but have seen it before in loops (Mandelbrot)

Code: Select all

x := 0

if (True, x++)
    MsgBox, %x% - (if)

Loop % (True, x++)
    MsgBox, %x% - (Loop)

while (True, x++)
    MsgBox, %x% - (while)
Edit: I saw this, but I have no idea any more where on the forum I saw it.

Code: Select all

w:=600,h:=500,m:=50
Gui,+hWndn
Gui,Show,w%w% h%h%
Loop,% (w,x:=-1,d:=DllCall("GetDC",ptr,n)){
Loop,% (h,y:=-1,j:=++x*(3/w)-2.1){
k:=++y*(2.5/h)-1.25,a:=b:=i:=0
while,(a*a+b*b<4&&i<m)
t:=a*a-b*b+j,b:=2*a*b+k,a:=t,i++
DllCall("SetPixel",ptr,d,int,x,int,y,int,i=m?0:i/m*255)
}}MsgBox
swagfag
Posts: 3080
Joined: 11 Jan 2017, 17:59

Re: One Line If Statements

03 Mar 2019, 16:07

this code
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: One Line If Statements

03 Mar 2019, 18:25

- @Raccoon: Great wish-list requests you've been posting, thanks for sharing.

- From here:
Is there any resemblance of syntactic rules??? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=62051&p=263716#p263716
- I've said that this should be added in. The question is what syntax. Perhaps the smart detection of parentheses (but this could be buggy) and/or a 'then' keyword for a simpler safer implementation.
- Using the ternary operator with functions/expressions can achieve a lot, although this can't be used with return/break/continue.
- You could just have an operator added in, equivalent to a line separator (equivalent to carriage return/line feed), to bunch lines up.
- Allowing break/continue/return in one-liners is also important.
- The line separator operator/keyword would immediately solve the break/continue/return problem.

- So you could have:

Code: Select all

;before:
if (a = 2)
	MsgBox(2)
else if (a = 1)
	MsgBox(1)
else
	MsgBox(0)

;after ('then'):
if (a = 2) then MsgBox(2)
else if (a = 1) then MsgBox(1)
else MsgBox(0)

;after ('LINESEP', a better name/symbol might be preferable):
if (a = 2) LINESEP MsgBox(2)
else if (a = 1) LINESEP MsgBox(1)
else MsgBox(0)
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
just me
Posts: 6563
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: One Line If Statements

04 Mar 2019, 06:45

jeeswg wrote:
03 Mar 2019, 18:25
...
I still don't get the advantage of ;after ('then'): and/or ;after ('LINESEP', a better name/symbol might be preferable):.

Do you think it's more than 'convenience'?
guest3456
Posts: 2789
Joined: 09 Oct 2013, 10:31

Re: One Line If Statements

04 Mar 2019, 07:14

Raccoon wrote:
02 Mar 2019, 23:20
The limitations of If-condition formatting has bothered me for a very long time.

Code: Select all

if (hctrl), temp:=hctrl, hctrl:=hwnd, hwnd:=temp
every time this comes up, the usual suggestion is to just use the ternary. surprised no one has mentioned it yet:

Code: Select all

(1=1) ? (temp:hctrl, hctrl:=hwnd, hwnd:=tmp) : "else goes here"

User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: One Line If Statements

04 Mar 2019, 09:14

- @just me:

Code: Select all

if cond action ;ineligible(?): clashes with existing syntax

if cond, action ;disadvantage: ugly, unusual(?)

if cond {action} ;disadvantage: braces required [ineligible(?): {...} seen as an object perhaps]

if cond then action ;advantage: braces not needed

if cond LINSEP action ;advantage: a generally useful feature (for bunching up lines)

;==============================

;LINSEP example 1

if cond
	action1
else
	action2

if cond LINSEP action1 LINSEP else LINSEP action2

if cond _ action1 _ else _ action2

;==============================

;LINSEP example 2

if cond
{
	action1
	break
}
else
{
	action2
	continue
}

if cond LINSEP { LINSEP action1 LINSEP break LINSEP } LINSEP else LINSEP { LINSEP action2 LINSEP continue LINSEP }

if cond _ { _ action1 _ break _ } _ else _ { _ action2 _ continue _ }

;==============================
- I had thought that a line separator could be useful syntax anyway, especially when you have multiple slightly different blocks of code (repeated one after another). I'm not sure what symbol(s) could be used.
- I had wondered whether commas could be used universally to separate statements/lines, although I haven't figured out if there's some obstacle to this.

- @guest3456: I did mention the ternary, although granted I didn't show a concrete example.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
nnnik
Posts: 4328
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: One Line If Statements

04 Mar 2019, 11:51

Correction:

Code: Select all

if cond, action ;ineligible(?): clashes with existing syntax

Code: Select all

if true,false {
	Msgbox Test
}
Is valid code in v1.
Recommends AHK Studio
User avatar
Raccoon
Posts: 49
Joined: 26 Jul 2014, 16:15

Re: One Line If Statements

04 Mar 2019, 12:21

nnnik wrote:
04 Mar 2019, 11:51

Code: Select all

if true,false {
	Msgbox Test
}
Is valid code in v1.
Can you explain what this does?
User avatar
nnnik
Posts: 4328
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: One Line If Statements

04 Mar 2019, 12:23

It evluates true then false and returns the first value - which is true.
It's the same as:

Code: Select all

if (true, false)
But since true, false can only be an expression you dont need the ()
Recommends AHK Studio
User avatar
Raccoon
Posts: 49
Joined: 26 Jul 2014, 16:15

Re: One Line If Statements

04 Mar 2019, 13:23

What happens when we put the comma after the closing parenthesis? As in:

Code: Select all

if (true, false), msgbox true
making the parenthesis mandatory.

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 12 guests