Option
Option makes absence explicit with Some(value) or None. Blorp has no null -- or null pointer errors.
`Some`
Some(value) carries a present value.
value: Option[Int] = Some(42)
`None`
None means no value is present.
missing: Option[String] = None
Pattern Matching
match forces both Some and None paths to be considered.
match maybe_name:
Some(name): print(name)
None: print("no name")
`get_or`
get_or(default) turns an Option[T] into a T.
name: String = maybe_name.get_or("guest")
`map`
map transforms the value only when it is Some.
doubled: Option[Int] = maybe_score.map(pure func(x): x * 2)
`and_then`
and_then chains a function that also returns Option.
first_name: Option[String] = user.and_then(pure func(u): u.name)
Example
name-input.brp
func main(args: List[String]) -> Int:
match input("name> "):
Some(name):
print("Hello, " + name.trim()) -- with stdin "Ada", prints: name> Hello, Ada
0
None:
print("Goodbye")
0
Try it
terminal
blorp run name-input.brp