Early return in method?


#1

Is it possible to early return in method, like:


[
// inside a method

!result: do_some_test_here
if result.not [return false]
...
result:= do_another_test_here
if result.not [return false]
...
result
]

It’s not necessary, but make code easier to read IMHO.


#2

Currently - no, there is no mechanism to do an early return.

This was an intentional language design - however I have been reconsidering it over the last couple years.

I agree the readability can be better without lots of nesting.

So I’ll add a return and some related features at some point in the future.

loop does have an early exit using the exit statement. Though it just exits the loop and not the entire routine.


As a side note - not is actually a prefix operator so you can use it before some method call. Also it is good code practice to name identifiers (and routines) that are Boolean objects with a question mark ?. Here is your example code rearranged a bit.

[
// inside a method

!result?: do_some_test_here
if not result? [return false]
...
result? := do_another_test_here
if not result? [return false]
...
result?
]

#3

Thanks for the not and ? tips!

Since you mentioned exit in loop, I wonder if there’re also continue in loop, which is just like early return: not absolutely necessary, but helps a lot on code readability.


#4

Yes - we have a loop skip command planned which has a similar use to continue.

You can see some of these features in the planned syntax PDF - though reading an EBNF grammar is not the most easy or interesting thing to do. [Though I do it all the time. :sunglasses:]


#5

Did “Skip” ever get implemented? (I don’t see it in the docs).


#6

Nope, not yet. We have been focusing on the revamped IDE.

Though we will be turning to the language again - including skip - soon afterwards.


#7

As we start coding our AI we are finding a need for this even more than before.
Also something akin to "Break " would be great.
Can these be added to the hot list?


#8

return is on our hotlist. :madsci: