Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Rename Option::value to Option::_value #2127

Merged
merged 2 commits into from
Aug 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions noir_stdlib/src/option.nr
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
struct Option<T> {
_is_some: bool,
value: T,
_value: T,
}

impl<T> Option<T> {
/// Constructs a None value
fn none() -> Self {
Self { _is_some: false, value: crate::unsafe::zeroed() }
Self { _is_some: false, _value: crate::unsafe::zeroed() }
}

/// Constructs a Some wrapper around the given value
fn some(value: T) -> Self {
Self { _is_some: true, value }
fn some(_value: T) -> Self {
Self { _is_some: true, _value }
}

/// True if this Option is None
Expand All @@ -27,13 +27,20 @@ impl<T> Option<T> {
/// Asserts `self.is_some()` and returns the wrapped value.
fn unwrap(self) -> T {
assert(self._is_some);
self.value
self._value
}

/// Returns the inner value without asserting `self.is_some()`
/// Note that if `self` is `None`, there is no guarantee what value will be returned,
/// only that it will be of type `T`.
fn unwrap_unchecked(self) -> T {
self._value
}

/// Returns the wrapped value if `self.is_some()`. Otherwise, returns the given default value.
fn unwrap_or(self, default: T) -> T {
if self._is_some {
self.value
self._value
} else {
default
}
Expand All @@ -43,7 +50,7 @@ impl<T> Option<T> {
/// a default value.
fn unwrap_or_else(self, default: fn() -> T) -> T {
if self._is_some {
self.value
self._value
} else {
default()
}
Expand All @@ -52,7 +59,7 @@ impl<T> Option<T> {
/// If self is `Some(x)`, this returns `Some(f(x))`. Otherwise, this returns `None`.
fn map<U>(self, f: fn(T) -> U) -> Option<U> {
if self._is_some {
Option::some(f(self.value))
Option::some(f(self._value))
} else {
Option::none()
}
Expand All @@ -61,7 +68,7 @@ impl<T> Option<T> {
/// If self is `Some(x)`, this returns `f(x)`. Otherwise, this returns the given default value.
fn map_or<U>(self, default: U, f: fn(T) -> U) -> U {
if self._is_some {
f(self.value)
f(self._value)
} else {
default
}
Expand All @@ -70,7 +77,7 @@ impl<T> Option<T> {
/// If self is `Some(x)`, this returns `f(x)`. Otherwise, this returns `default()`.
fn map_or_else<U>(self, default: fn() -> U, f: fn(T) -> U) -> U {
if self._is_some {
f(self.value)
f(self._value)
} else {
default()
}
Expand All @@ -91,7 +98,7 @@ impl<T> Option<T> {
/// In some languages this function is called `flat_map` or `bind`.
fn and_then<U>(self, f: fn(T) -> Option<U>) -> Option<U> {
if self._is_some {
f(self.value)
f(self._value)
} else {
Option::none()
}
Expand Down Expand Up @@ -135,7 +142,7 @@ impl<T> Option<T> {
/// Otherwise, this returns `None`
fn filter(self, predicate: fn(T) -> bool) -> Self {
if self._is_some {
if predicate(self.value) {
if predicate(self._value) {
self
} else {
Option::none()
Expand All @@ -149,7 +156,7 @@ impl<T> Option<T> {
/// This returns None if the outer Option is None. Otherwise, this returns the inner Option.
fn flatten(option: Option<Option<T>>) -> Option<T> {
if option._is_some {
option.value
option._value
} else {
Option::none()
}
Expand Down