Sorry for the picture in my last post. I'm not sure what happened. It was just a picture of powershell opened in a terminal, and a break command being sent. Also I'm not familiar with this forum software, or formatting etiquette so please bare with me.
lexikos wrote: ↑19 Oct 2023, 21:40
How many and which statements is it supposed to break out of? What if there is both an
If and a
Loop?
I'm a bit confused by this. You cannot use
break in a pure
if statement that is not nested in a
loop, so I don't think that matters. When you do, you get an error when you try to run the script. As for how many loops I want to break out of, it's only supposed to break the
loop I put the function in, whatever "layer" that is. As per my first example, the function inside the
loop runs every "tick", perhaps this is not the most resource efficient. The function itself is waiting for the ESC key to change a variable that will trigger the
if statement inside the function. Ideally, when this
if statement is triggered from inside the function, and is nested inside the
loop, it should break the
loop it's in, and only the
loop it's in. I'm not asking for fancier functionality, though I know from the documentation you can do other fancy stuff with
break.
lexikos wrote: ↑19 Oct 2023, 21:40
If you really need to "break out of" a group of statements that aren't enclosed by a
loop, it may be that extracting that code into a separate function would be a better course of action. Otherwise, this is a valid use of goto.
I am not trying to break out of anything but a single
loop. As of now, you have to do this in order to achieve the same result as putting break in a function, which is not as reusable:
Code: Select all
FunctionExample02(){
ToolTip "ACTIVATED"
;Other things can go here.
;Other things can go here.
;Other things can go here.
}
Test = 1
F6::{
loop 10{
;-------- I have to rewrite this part every time
;I have to make this if statement every time when I could just put the if statement in the function if I could put [c]break[/c] wherever I want. This more Lines of Code.
if (Test = 1){
FunctionExample02
break
}
;-------- I have to rewrite this part every time
}
}
In my first example, the point of my
loop is to run continuously and break when I press ESC. Maybe there is a better way to do this, but I think the example is functional. I can make this a snippet, but I would prefer not to have to. This is also more lines of code.
lexikos wrote: ↑19 Oct 2023, 21:40
A function returns a value, not a control flow instruction. Code would be harder to follow if any function call could perform a break. (Exceptions are similar, but are intended for error/exceptional conditions, and you know that either the thread will exit or execution will jump to try/catch/finally.)
Powershell does this and I can't say it's really "confusing". My Powershell example in my first post works exactly as you think it would. You enter a
loop and then when the function is called, it does everything in the function; it runs all the code, including
break, and then the script continues after the
loop. If you don't know what the function does when looking through your code, you can just go to the function and read it.
lexikos wrote: ↑19 Oct 2023, 21:40
If you want reusable code, you can surely write it in a way that makes conventional use of control flow structures. There is no need for a function to act as as a substitute for break.
This is your opinion. I often use functions as a part of a "control flow structure" as you put it. If this is about code correctness, when written properly, my code does the correct thing, and produces no errors. I am not asking to only use
break in a function, I am asking for the ability for a function to be able to preform a
break, plus other run other code in the function before it.
lexikos wrote: ↑19 Oct 2023, 21:40
For instance, put the
loop into a reusable function. As a bonus, a single call to a function which performs a
loop is more efficient than a
loop which calls a function for every iteration.
This does not solve my problem. I am looking for the
break functionality to be portable,
not the entire loop. I want to do a specific set of things before I
break, and not allowing me to put it inside a function adds extra complexity to my code. I have to look in more than one place when I want to make an edit. I used a finite
loop as an example so that it doesn't actually run forever, but the loops I am looking to break do. The thing I am trying to avoid typing over and over again is:
Code: Select all
if(ExitVar = 1){
ExitVar := 0
FunctionExample()
;MsgBox ExitVar " FROM FUNCTION"
Break
}
I'm not trying to put
break in a function because I think it's cool to call functions. The whole point is to avoid having to write extra code. I have this flexibility in Powershell and think it is very powerful and versatile. I think AHK v2 should have that too.
lexikos wrote: ↑19 Oct 2023, 21:40
Perhaps you can explain what the alternative to "break can't be used in GoTo" would be. Both Break and Goto are statements which transfer control to a specific line. Identifying the line after a
loop with a label and using Goto is exactly equivalent to using Break. Suggesting that one might be used "in" another makes no sense to me.
You are correct. This is my mistake.
lexikos wrote: ↑19 Oct 2023, 21:40
In both cases, the target line is identified at "compile time" (at load time, really).
I think "load time" is the proper statement here. You are correct, my mistake.
lexikos wrote: ↑19 Oct 2023, 21:40
AHK is trying to stop the user from making mistakes when writing code
Break aside, yes, v2 tries to stop the user from making mistakes when writing code. This is
much more effective than relying solely on runtime errors.
I agree to some degree, but I feel that this is particularly egregious when it comes to
break. If
break is not harmful, the user should be able to put break anywhere they want. I'm aware this is not "correct" or "useful" so to speak. I think that performance and "code correctness" matter. I also agree that relying too heavily on functions can be very confusing, and make your code harder to follow, and not the other way around. But I think, in terms of
break's functionality, this is going too far, and reduces the capability of AHK v2. I can understand if this caused blue screens or make your entire script fail, but so far, I haven't really seen evidence of something that catastrophic.
To say it more simply, I don't think that using
break in this way is incorrect.
lexikos wrote: ↑19 Oct 2023, 21:40
The specific conditions for detecting an error at runtime might not be met during testing, or reproducing those conditions might require going through a number of steps, making testing a much slower process. If an error can be detected before the code executes, that is much better.
Editors are able to detect errors even earlier than AutoHotkey itself, but only if the conditions actually indicate an error in the first place. If the language permits Break to be used (meaningfully) anywhere, the use of Break can't be validated by editors any more than by the program itself.
While I don't disagree in theory, AHK can already tell when
break is used outside of a
loop. Can we not use this same functionality to warn people in an editor based on the same criteria? For example this would be a yellow line in VSCode, which is what I'm using. Trying to use variables you haven't assigned produces the effect I'm thinking about. I feel like that would be a much better way to tell the user "hey, you might be using this wrong" than a hard fail at load time.