Skip to content

Commit

Permalink
Improve the API of 'Optional'
Browse files Browse the repository at this point in the history
  • Loading branch information
kyouko-taiga committed Aug 23, 2024
1 parent b03fc42 commit a4fe311
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
35 changes: 35 additions & 0 deletions StandardLibrary/Sources/Core/Optional.hylo
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,41 @@ public extension Optional {
None() as Optional
}

/// Returns `true` iff `self` is equal to `.none()`.
public fun is_empty() -> Bool {
if let _: T = self { false } else { true }
}

/// Returns an optional containing the result of calling `transform` on the value wrapped in
/// `self` or `.none()` is `self` is empty.
public fun map<E, R>(_ transform: inout [E](T) -> R) -> Optional<R> {
if let w? = self { transform(w) as _ } else { .none() }
}

}

public extension Optional where T: Movable {

/// Returns the value wrapped in `self` or stops execution if `self` is empty.
///
/// - Requires: `self` is not empty.
public fun postfix!() sink -> T {
if sink let r? = self {
return r
} else {
fatal_error("optional is empty")
}
}

/// Returns the value wrapped in `self` and assigns it to `.none()`.
///
/// - Requires: `self` is not empty.
public fun release() inout -> T {
sink let wrapped = self!
&self = .none()
return wrapped
}

}

// Note: We can't declare confitional conformance of `Optional` to "umbrella traits" yet without
Expand Down
15 changes: 15 additions & 0 deletions Tests/LibraryTests/TestCases/OptionalTests.hylo
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,19 @@ public fun main() {
&x = .none()
let z = if let i: Int = x { i.copy() } else { 0 }
precondition(z == 0)

&x = 42 as _
precondition(x! == 42)

&x = 42 as _
precondition(!x.is_empty())

&x = 42 as _
precondition(x.release() == 42)
precondition(x.is_empty())

&x = 42 as _
&x = x.map(fun(_ n) { n + 1 })
precondition(x.release() == 43)
precondition((x.map(fun(_ n) { n + 1 })).is_empty())
}

0 comments on commit a4fe311

Please sign in to comment.