Skip to content

Commit

Permalink
move Option::unwrap to builtin (#594)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yu-zh authored Jun 24, 2024
1 parent 5a3b305 commit 1e4fda1
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 17 deletions.
1 change: 1 addition & 0 deletions builtin/builtin.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ impl Option {
debug_write[X : Debug](X?, Buffer) -> Unit
op_equal[X : Eq](X?, X?) -> Bool
to_string[X : Show](X?) -> String
unwrap[X](X?) -> X
}
impl Result {
debug_write[T : Debug, E : Debug](Self[T, E], Buffer) -> Unit
Expand Down
7 changes: 0 additions & 7 deletions builtin/linked_hash_map.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,6 @@ struct Map[K, V] {

// Implementations

fn unwrap[X](self : X?) -> X {
match self {
None => panic()
Some(x) => x
}
}

fn power_2_above(x : Int, n : Int) -> Int {
for i = x {
if i >= n {
Expand Down
14 changes: 14 additions & 0 deletions builtin/option.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,17 @@ pub fn to_string[X : Show](self : X?) -> String {
Some(x) => "Some(" + x.to_string() + ")"
}
}

/// Extract the value in `Some`.
/// Panic if input is `None`.
pub fn unwrap[X](self : X?) -> X {
match self {
None => panic()
Some(x) => x
}
}

test "panic" {
let _ : Int = Option::None.unwrap()

}
9 changes: 0 additions & 9 deletions option/option.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,6 @@ test "compare" {
@assertion.assert_eq(1, some2.compare(none))?
}

/// Extract the value in `Some`.
/// Panic if input is `None`.
pub fn unwrap[X](self : X?) -> X {
match self {
None => abort("called `Option::unwrap()` on a `None` value")
Some(x) => x
}
}

/// `None`
pub fn Option::default[X]() -> X? {
None
Expand Down
1 change: 0 additions & 1 deletion option/option.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ impl Option {
or[T](T?, T) -> T
or_default[T : Default](T?) -> T
or_else[T](T?, () -> T) -> T
unwrap[X](X?) -> X
}

// Traits
Expand Down

0 comments on commit 1e4fda1

Please sign in to comment.