nbuilding/N-lang

Supply default value to a `maybe` using the OR operator

SheepTester opened this issue · 0 comments

Allow the OR operator to also supply a default value to a maybe value by adding a new overload for the OR operator.

If it were a function (|), then

assert type (|) : [t] maybe[t] -> t -> t

The second operand should be lazily evaluated. This should not cause an infinite loop:

let firstYes = [[t] maybes:list[maybe[t]]] -> maybe[t] {
  if let <yes item> = maybes[0] {
    return item | firstYes(maybes |> subsection(1, len(maybes)))
  } else {
    return none
  }
}

assert value firstYes([none, none, yes(2), yes(3)]) == yes(2)
assert value firstYes([none, none]) == none

Should work well in conjunction with #263