wybe icon indicating copy to clipboard operation
wybe copied to clipboard

Allow for an early return in a proc

Open jimbxb opened this issue 2 years ago • 1 comments

Sometimes, the use of an early return is useful.

Perhaps we can add a return statement that acts like fail but instead of failing the procedure, terminates the procedure without failing

e.g.

def product(matrix:list(list(int)), ?p:int) {
     ?p = 1
     for ?row in matrix {
         for ?i in row {
              if { i = 0 :: ?p = 0; return }
              !p *= i
         }
     }
}

jimbxb avatar May 11 '22 00:05 jimbxb

I suppose this example could be solved with the use of a generalised break, taking an optional integer constant, n argument that breaks n loops

e.g.

def product(matrix:list(list(int)), ?p:int) {
     ?p = 1
     for ?row in matrix {
         for ?i in row {
              if { i = 0 :: 
                  ?p = 0 
                  break 2
              } 
              !p *= i
         }
     }
}

Generalised next makes sense too, then

jimbxb avatar May 11 '22 00:05 jimbxb