Page 1 of 2

One Line If Statements

Posted: 02 Mar 2019, 23:20
by Raccoon
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 }

Re: One Line If Statements

Posted: 02 Mar 2019, 23:24
by TAC109

Code: Select all

v := 1
ifequal v,1, msgbox true ; works fine
Edit: fix example

Re: One Line If Statements

Posted: 03 Mar 2019, 06:16
by nnnik
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.

Re: One Line If Statements

Posted: 03 Mar 2019, 06:45
by Raccoon
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.

Re: One Line If Statements

Posted: 03 Mar 2019, 06:56
by nnnik
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.

Re: One Line If Statements

Posted: 03 Mar 2019, 07:05
by Raccoon
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 ♪ ♫

Re: One Line If Statements

Posted: 03 Mar 2019, 07:16
by nnnik
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.

Re: One Line If Statements

Posted: 03 Mar 2019, 07:24
by Raccoon
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]?

Re: One Line If Statements

Posted: 03 Mar 2019, 08:08
by nnnik
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.

Re: One Line If Statements

Posted: 03 Mar 2019, 14:50
by Raccoon
Can you give me a demonstration of how a person might use commas in the expression of an If? I never knew this worked

Re: One Line If Statements

Posted: 03 Mar 2019, 15:27
by wolf_II
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

Re: One Line If Statements

Posted: 03 Mar 2019, 16:07
by swagfag
this code

Re: One Line If Statements

Posted: 03 Mar 2019, 18:25
by jeeswg
- @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)

Re: One Line If Statements

Posted: 04 Mar 2019, 06:45
by just me
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'?

Re: One Line If Statements

Posted: 04 Mar 2019, 07:14
by guest3456
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"

Re: One Line If Statements

Posted: 04 Mar 2019, 09:14
by jeeswg
- @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.

Re: One Line If Statements

Posted: 04 Mar 2019, 11:51
by nnnik
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.

Re: One Line If Statements

Posted: 04 Mar 2019, 12:21
by Raccoon
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?

Re: One Line If Statements

Posted: 04 Mar 2019, 12:23
by nnnik
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 ()

Re: One Line If Statements

Posted: 04 Mar 2019, 13:23
by Raccoon
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.