Skip to content
Playground(another site)

bynk.types.if_without_else_requires_unit

Error An if with no else branch has a non-unit then-branch; the missing else defaults to (), so the branch must be () or Effect[()].

An if with no else has nothing to produce when its condition is false, so Bynk gives that path the value (). The then branch must therefore be () too, or Effect[()] for a branch that only performs effects; a branch that yields a String would leave the other path without one. Add an else that produces a value of the same type, or make the branch a unit effect.

Example

fn sign(n: Int) -> String {
if n > 0 { "positive" } // ✗ no `else`, so the value would be `()`
}
fn sign(n: Int) -> String {
if n > 0 { "positive" } else { "not positive" } // ✓
}

In the Book

See also

  • The grammar rule it governs: if_expr.
  • Every code in its category, Type checking, on the diagnostic index.
  • bynk explain bynk.types.if_without_else_requires_unit shows the same from the installed toolchain, without a network.