Skip to content

Commit

Permalink
Add Option type & is_some/is_none methods (FuelLabs#31)
Browse files Browse the repository at this point in the history
* Feat:Add Option type & is_some/is_none methods

* Doc:Improve module-level docs

* Fix:Add generic type T to match
  • Loading branch information
nfurfaro authored Jan 28, 2022
1 parent 68fcc90 commit 07feb84
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.sw
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dep chain;
dep contract_id;
dep context;
dep address;
dep option;
dep block;
dep token;
dep result;
Expand Down
51 changes: 51 additions & 0 deletions src/option.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//! Error handling with the `Option` type.
//!
//! [`Option<T>`][`Option`] is the type used for representing the existence or absence of a value. It is an enum with the variants, [`Some(T)`], representing
//! some value, and [`None()`], representing
//! no value.

library option;

/// `Option` is a type that represents either the existence of a value ([`Some`]) or a value's absence
/// ([`None`]).
pub enum Option<T> {
/// Contains the value
Some: T,

/// Signifies the absence of a value
None: (),
}

/////////////////////////////////////////////////////////////////////////////
// Type implementation
/////////////////////////////////////////////////////////////////////////////

impl Option<T> {
/////////////////////////////////////////////////////////////////////////
// Querying the contained values
/////////////////////////////////////////////////////////////////////////

/// Returns `true` if the result is [`Some`].
fn is_some(self) -> bool {
match self {
Option::Some(T) => {
true
},
_ => {
false
},
}
}

/// Returns `true` if the result is [`None`].
fn is_none(self) -> bool {
match self {
Option::Some(T) => {
false
},
_ => {
true
},
}
}
}

0 comments on commit 07feb84

Please sign in to comment.