Skip to content

fix FixedArray::get and corresponding doc test #2146

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions builtin/builtin.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,7 @@ fn FixedArray::blit_from_bytes(Self[Byte], Int, Bytes, Int, Int) -> Unit
fn FixedArray::blit_from_string(Self[Byte], Int, String, Int, Int) -> Unit
fn[A] FixedArray::blit_to(Self[A], Self[A], len~ : Int, src_offset~ : Int = .., dst_offset~ : Int = ..) -> Unit
fn[T] FixedArray::fill(Self[T], T) -> Unit
#deprecated
fn[T] FixedArray::get(Self[T], Int) -> T
fn[T] FixedArray::get(Self[T], Int) -> T?
fn[T] FixedArray::is_empty(Self[T]) -> Bool
fn[T] FixedArray::iter(Self[T]) -> Iter[T]
fn[T] FixedArray::iter2(Self[T]) -> Iter2[Int, T]
Expand Down
30 changes: 30 additions & 0 deletions builtin/fixedarray.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,36 @@
// See the License for the specific language governing permissions and
// limitations under the License.

///|
/// Retrieves an element at the specified index from a fixed-size array.
///
/// Parameters:
///
/// * `array` : The fixed-size array to access.
/// * `index` : The position in the array from which to retrieve the element.
///
/// Returns `Some(element)` if the index is within bounds, or `None` if the index
/// is out of bounds.
///
/// Example:
///
/// ```moonbit
/// test "FixedArray::get" {
/// let arr : FixedArray[Int] = [1, 2, 3]
/// inspect!(arr.get(1), content="Some(2)")
/// }
///
/// test "FixedArray::get/out_of_bounds" {
/// let arr : FixedArray[Int] = [1, 2, 3]
/// inspect!(arr.get(3), content="None")
/// }
/// ```
pub fn[T] FixedArray::get(self : FixedArray[T], idx : Int) -> T? {
let len = self.length()
guard idx >= 0 && idx < len else { None }
Some(self.unsafe_get(idx))
}

///|
///
/// Creates an iterator over the elements of a fixed-size array, providing
Expand Down
30 changes: 0 additions & 30 deletions builtin/intrinsics.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -1554,36 +1554,6 @@ pub fn[T] FixedArray::unsafe_set(
val : T
) -> Unit = "%fixedarray.unsafe_set"

///|
/// Retrieves an element at the specified index from a fixed-size array. This
/// function is similar to `op_get` but provides explicit bounds checking.
///
/// Parameters:
///
/// * `array` : The fixed-size array to access.
/// * `index` : The position in the array from which to retrieve the element.
///
/// Returns the element at the specified index.
///
/// Throws a runtime error if the index is out of bounds (less than 0 or greater
/// than or equal to the length of the array).
///
/// Example:
///
/// ```moonbit
/// test "FixedArray::get" {
/// let arr = [1, 2, 3]
/// inspect(arr.get(1), content="Some(2)")
/// }
///
/// test "FixedArray::get/out_of_bounds" {
/// let arr = [1, 2, 3]
/// inspect(arr.get(3), content="None")
/// }
/// ```
#deprecated("use `arr.op_get(i)`(arr[i]) instead")
pub fn[T] FixedArray::get(self : FixedArray[T], idx : Int) -> T = "%fixedarray.get"

///|
/// Sets a value at the specified index in a fixed-size array. The original value
/// at that index is overwritten.
Expand Down
Loading