github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/core/structs/break_doc.yaml (about)

     1  - DocumentID: return
     2    Title: >+
     3      `return`
     4    CategoryID: commands
     5    Summary: >-
     6      Exits current function scope
     7    Description: |-
     8      `return` will terminate execution of a block at the scope level (eg `function`,
     9      `private`, etc)
    10  
    11      Conceptually it is the same as `break` except it doesn't require the scope name
    12      as a parameter and you can specify the exit number rather than defaulting to 0.
    13    Usage: |-
    14      ```
    15      return [ exit-number ]
    16      ```
    17    Examples: |-
    18      **Setting an exit number:**
    19  
    20      ```
    21      function example {
    22          out foo
    23          return 13
    24          out bar
    25      }
    26      example
    27      exitnum
    28      ```
    29  
    30      Running the above code would output:
    31  
    32      ```
    33      foo
    34      13
    35      ```
    36  
    37      **Returning withing an exit number:**
    38  
    39      If we were to run the same code as above but with `return` written without any
    40      parameters (ie instead of `return 13` it would be just `return`), then you
    41      would see the following output:
    42  
    43      ```
    44      foo
    45      0
    46      ```
    47    Flags:
    48    Detail: |-
    49      Any process that has been initialised within a `return`ed scope will have their
    50      exit number updated to the value specified in `return` (or `0` if no parameter
    51      was passed).
    52    Synonyms:
    53    Related:
    54      - exit
    55      - function
    56      - private
    57      - out
    58      - break
    59      - continue
    60      - exitnum
    61  
    62  - DocumentID: break
    63    Title: >+
    64      `break`
    65    CategoryID: commands
    66    Summary: >-
    67      Terminate execution of a block within your processes scope
    68    Description: |-
    69      `break` will terminate execution of a block (eg `function`, `private`,
    70      `foreach`, `if`, etc).
    71  
    72      `break` requires a parameter and that parameter is the name of the caller
    73      block you wish to break out of. If it is a `function` or `private`, then it
    74      will be the name of that function or private. If it is an `if` or `foreach`
    75      loop, then it will be `if` or `foreach` (respectively).
    76    Usage: |-
    77      ```
    78      break block-name
    79      ```
    80    Examples: |-
    81      **Exiting an iteration block:**
    82  
    83      ```
    84      function foo {
    85          %[1..10] -> foreach i {
    86              out $i
    87              if { $i == 5 } then {
    88                  out "exit running function"
    89                  break foo
    90                  out "ended"
    91              }
    92          }
    93      }
    94      ```
    95  
    96      Running the above code would output:
    97  
    98      ```
    99      » foo
   100      1
   101      2
   102      3
   103      4
   104      5
   105      exit running function
   106      ```
   107  
   108      **Exiting a function:**
   109  
   110      `break` can be considered to exhibit the behavior of _return_ (from other
   111      languages) too
   112  
   113      ```
   114      function example {
   115          if { $USER == "root" } then {
   116              err "Don't run this as root"
   117              break example
   118          }
   119          
   120          # ... do something ...
   121      }
   122      ```
   123  
   124      Though in this particular use case it is recommended that you use `return`
   125      instead, the above code does illustrate how `break` behaves.
   126    Flags:
   127    Detail: |-
   128      `break` cannot escape the bounds of its scope (typically the function it is
   129      running inside). For example, in the following code we are calling `break
   130      bar` (which is a different function) inside of the function `foo`:
   131  
   132      ```
   133      function foo {
   134          %[1..10] -> foreach i {
   135              out $i
   136              if { $i == 5 } then {
   137                  out "exit running function"
   138                  break bar
   139                  out "ended"
   140              }
   141          }
   142      }
   143  
   144      function bar {
   145          foo
   146      }
   147      ```
   148  
   149      Regardless of whether we run `foo` or `bar`, both of those functions will
   150      raise the following error:
   151  
   152      ```
   153      Error in `break` (7,17): no block found named `bar` within the scope of `foo`
   154      ```
   155    Synonyms:
   156    Related:
   157      - exit
   158      - foreach
   159      - formap
   160      - if
   161      - function
   162      - private
   163      - out
   164      - continue
   165      - return
   166  
   167  - DocumentID: continue
   168    Title: >+
   169      `continue`
   170    CategoryID: commands
   171    Summary: >-
   172      Terminate process of a block within a caller function
   173    Description: |-
   174      `continue` will terminate execution of a block (eg `function`, `private`,
   175      `foreach`, `if`, etc) right up until the caller function. In iteration loops
   176      like `foreach` and `formap` this will result in behavior similar to the
   177      `continue` statement in other programming languages.
   178    Usage: |-
   179      ```
   180      continue block-name
   181      ```
   182    Examples: |-
   183      ```
   184      %[1..10] -> foreach i {
   185          if { $i == 5 } then {
   186              out "continue"
   187              continue foreach
   188              out "skip this code"
   189          }
   190          out $i
   191      }
   192      ```
   193  
   194      Running the above code would output:
   195  
   196      ```
   197      » foo
   198      1
   199      2
   200      3
   201      4
   202      continue
   203      6
   204      7
   205      8
   206      9
   207      10
   208      ```
   209    Flags:
   210    Detail: |-
   211      `continue` cannot escape the bounds of its scope (typically the function it is
   212      running inside). For example, in the following code we are calling `continue
   213      bar` (which is a different function) inside of the function `foo`:
   214  
   215      ```
   216      function foo {
   217          %[1..10] -> foreach i {
   218              out $i
   219              if { $i == 5 } then {
   220                  out "exit running function"
   221                  continue bar
   222                  out "ended"
   223              }
   224          }
   225      }
   226  
   227      function bar {
   228          foo
   229      }
   230      ```
   231  
   232      Regardless of whether we run `foo` or `bar`, both of those functions will
   233      raise the following error:
   234  
   235      ```
   236      Error in `continue` (7,17): no block found named `bar` within the scope of `foo`
   237      ```
   238    Synonyms:
   239    Related:
   240      - exit
   241      - foreach
   242      - formap
   243      - if
   244      - function
   245      - private
   246      - out
   247      - break
   248      - return