Say I have a function with signature proc foo(): Option[int]
and I set var x: Option[int] = foo()
.
How do I perform a different action depending on whether x
is some
or none
?
For example in Scala I could do:
x match {
case Some(a) => println(s"Your number is $a")
case None => println("You don't have a number")
}
or even:
println(x.map(y => s"Your number is $y").getOrElse("You don't have a number"))
So far I have come up with:
if x.isSome():
echo("Your number is ", x.get())
else:
echo("You don't have a number")
Which doesn't look like good functional style. Is there something better?