Summary
In verse, the compiler complains about the following code:
DoStuff():void = {
Result := AnyFailableExpression[] or return # Error: Precedence doesn't allow "return" following "or"
Print("Result is: {Result}")
}
But, I believe this error is a mistake and not true, since it can be easily bypassed by writting the same logic in other ways, such as the following examples:
DoStuff():void = {
Result := AnyFailableExpression[] or (return) # No error, compiles and work fine.
Print("Result is: {Result}")
}
DoStuff():void = {
Result := AnyFailableExpression[] or block{return} # No error, compiles and work fine.
Print("Result is: {Result}")
}
DoStuff():void = {
Result := AnyFailableExpression[] or (Print("Failed :("); return) # No error, compiles and work fine.
Print("Result is: {Result}")
}
DoStuff():int = { # <- Function that returns a value
Result := AnyFailableExpression[] or (return 76) # No error, compiles and work fine.
Print("Result is: {Result}")
Result
}
DoStuff():string = {
Result := AnyFailableExpression[] or block{
Print("Failed :(")
DoSomething()
return "Oh, no!"
} # No error, compiles and work fine.
Print("Result is: {Result}")
"Yayyy!"
}
All the examples above works as intended: If the failable expression succeeds, it continue and prints the result. If it fails, it does an early return (executing all the right side of the “or” statement).
Due to this, I am confident that the original Error (Precedence doesn’t allow “return” following “or”) is a mistake.
Additionally, when doing any of the workaround examples I mentioned above, while it does not throw any error, it still shows a warning that is also incorrect: “Unreachable code - previous expression is guaranteed to exit early”.
This warning does not prevent compiling or usage of the code, but, the statement said on the warning is wrong, because the code still works fine as intended (the code IS reachable).
Both the Error and Warnings should be removed.
Please select what you are reporting on:
Verse
What Type of Bug are you experiencing?
Verse
Steps to Reproduce
Try to use any expression with a return on the right side of a “or” statement. (Does not matter if the function returns void or a specific value)
Expected Result
No errors and no warnings about functionality and/or usage should appear, since the code is already properly working fine as expected.
Observed Result
- If using return directly, an Error will be thrown, preventing from compiling and using the code;
- If the return is enclosed (on a tuple or a code block for example), it will not show the Error, but now it will show a Warning about functionality.
Both the Error and Warnings messages are incorrect, and should be removed since the code behavior works correctly without problems, as described on the examples provided above…
Platform(s)
All (Verse VM)
Upload an image
Additional Notes
This syntax is currently being used to provide “early returns” like guard statements, such as discussed here: Verse lacks `guard` statement