↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

Why F#, Rust and others use Option type instead of nullable types like C# 8 or TypeScript?

The answers frame Option as a way to represent absence with a regular type that works well with collections, mapping, flattening, and defaulting. They also stress that null handling is often easier to keep safe when the type system and standard library are designed around option values from the start.

Reading notes
#

  • Option is treated as a zero-or-one-element collection, so it fits with common collection operations like foreach, map, flatten, and getOrElse.
  • Several answers say null references are a language problem that option types avoid, especially in languages that can eliminate null altogether.
  • Some answers argue that option types are more composable than nullable unions when values can nest or when a lookup may need to preserve more than one kind of absence.
  • One answer says nullable types need compiler support to track three states safely, including when a value has been checked and is known non-null.
  • Another answer says the choice is tied to language design and history, since C# and TypeScript keep null for compatibility, while Rust, Scala, F#, and Haskell lean toward option-style modeling.
  • One answer states that runtime overhead is not inherently worse for Option, and that in some languages the compiler can optimize optional values down to a null pointer or a packed representation.
  • One answer warns that Option can make later API changes harder when a function that used to accept a plain value is changed to accept an optional one.