In some programming languages, an option type is a polymorphic type (i.e., a type that holds a generic type) that represents an optional value. Either the containing variable will hold a “nothing” type, or it will hold “some” value.
For example, for an option
i32
type, we could have eitherNone
orSome(10)
.
The point of this is clear. If we work with data where it may not have a value, we can set it as None
instead of storing a ground truth illegal value.
Option types are supported mainly in functional programming languages like Rust, Haskell, Scala, and OCaml. In addition, it’s now supported in starting in C++17.
- In Rust:
Some(<T>)
,None
, as theOption<T>
enum. - In C++:
std::optional<T>
.