In other coding languages, i like to use guard clauses like shown in this lua example.
if (not Something) then
return
end
Something.CallMethod()
In Verse, it would look like:
if(Something := MaybeSomething?):
Something.CallMethod()
Using guard clauses in Verse isn’t really a thing when using option
variables. There are some exceptions to this, but achieving some sort of guard clause isn’t easy to do.
Using option
can easily lead to deeply nested if clauses, while simple guard blocks could easily avoid that in many other languages.
So, what is my desired solution? queries.
if (not Something?):
return
Something!.CallMethod()
What does the !
-posfix do? It basically tries to pull out a value from an option and otherwise Errors. This is how its implementation would look like:
postfix'!'(Option:?t where t:type)<transacts>: t =
Option? or Err("Getting Value from Option failed.")
It is basically an assertion, that the option is not set to false and returns the value without forcing me into a nested block.