From c034175ae1cbe53bc0e7e1ae71b51c64dcc3402a Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 11 Sep 2024 23:17:03 -0400 Subject: Add optional:or_else(fallback) and optional:or_fail(message) --- docs/optionals.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'docs') diff --git a/docs/optionals.md b/docs/optionals.md index 445a9681..1414ca6a 100644 --- a/docs/optionals.md +++ b/docs/optionals.md @@ -28,3 +28,20 @@ func maybe_takes_int(x:Int?): This establishes a common language for talking about optional values without having to use a more generalized form of `enum` which may have different naming conventions and which would generate a lot of unnecessary code. + +In addition to using conditionals to check for null values, you can also use +`:or_else(fallback)` or `:or_fail()`: + +```tomo +maybe_x := 5? +>> maybe_x:or_else(-1) += 5 : Int +>> maybe_x:or_fail() += 5 : Int + +maybe_x = !Int +>> maybe_x:or_else(-1) += -1 : Int +>> maybe_x:or_fail() +# Failure! +``` -- cgit v1.2.3