-
Notifications
You must be signed in to change notification settings - Fork 3
Closed
Description
This example will create an error on runtime:
import maybe/maybe
var crash = false
proc wontwork(): Maybe[int] =
if crash:
nothing[int]()
else:
crash = true
just(4)
maybeCase wontwork():
just x:
var y = 3
echo $(x+y)
nothing:
echo "no value"
The reason is simple, maybeCase
substitutes all occurences of x
with it's first argument. This means that the above get's turned into something like this:
if wontwork().valid:
var y = 3
echo $(wontwork().value + y)
else:
echo "no value"
As we can see wontwork
is called twice, the second time producing a different result than the first. What should be happening is something like this:
let x = wontwork()
if x.valid:
var y = 3
echo $(x.value + y)
else:
echo "no value"
Metadata
Metadata
Assignees
Labels
No labels