Skip to content

Error when calling a procedure using a side effect #9

@PMunch

Description

@PMunch

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions