Hasty Briefsbeta

Imagining a language without booleans

a day ago
  • #programming
  • #language-design
  • #conditionals
  • An 'if' statement with an 'else' can produce a value, but without an 'else', it evaluates to 'Option<T>'.
  • The generalized 'if' takes a 'bool' and a 'T', producing 'Option<T>'. If the condition is true, it returns 'Some(e)', otherwise 'None'.
  • The 'else' operator supplies a default for an 'Option<T>', similar to Rust's 'unwrap_or_else()'.
  • 'and' and 'or' can work on options, returning options. 'or' takes the first 'Some' or the second option, while 'and' takes the second option if the first is 'Some'.
  • Booleans can be replaced with 'Option<()>', where 'true' is 'Some(())' and 'false' is 'None'.
  • Further equivalence exists between options and results, leading to a syntax where 'T ? E' means 'Result<T, E>'.
  • Conditionals can be written using 'or if' as an alternative to 'else if', relying on 'or' binding tighter than 'else'.
  • Pattern binding in conditionals can be done with 'is', similar to Rust's 'if let'.
  • Loops can 'break' with a value, producing 'Ok' if they do, and can be composed with 'else'.
  • Functions in this hypothetical language don't require wrapping results in 'Ok' or 'Err', simplifying code.