diff --git a/builtin/builtin.mbti b/builtin/builtin.mbti index 96fbbaabb..0dc8717d4 100644 --- a/builtin/builtin.mbti +++ b/builtin/builtin.mbti @@ -695,6 +695,7 @@ impl FixedArray { default[X]() -> Self[X] fill[T](Self[T], T) -> Unit get[T](Self[T], Int) -> T + is_empty[T](Self[T]) -> Bool iter[T](Self[T]) -> Iter[T] iter2[T](Self[T]) -> Iter2[Int, T] length[T](Self[T]) -> Int diff --git a/builtin/fixedarray.mbt b/builtin/fixedarray.mbt index 177e4b279..cd8c89d0a 100644 --- a/builtin/fixedarray.mbt +++ b/builtin/fixedarray.mbt @@ -76,3 +76,26 @@ pub fn compare[T : Compare](self : FixedArray[T], other : FixedArray[T]) -> Int } } } + +///| +/// Tests whether the FixedArray contains no elements. +/// +/// Parameters: +/// +/// * `FixedArray` : The FixedArray to check. +/// +/// Returns `true` if the FixedArray has no elements, `false` otherwise. +/// +/// Example: +/// +/// ```moonbit +/// test "FixedArray::is_empty" { +/// let empty : FixedArray[Int] = [] +/// inspect!(empty.is_empty(), content="true") +/// let non_empty = [1, 2, 3] +/// inspect!(non_empty.is_empty(), content="false") +/// } +/// ``` +pub fn FixedArray::is_empty[T](self : FixedArray[T]) -> Bool { + self.length() == 0 +} diff --git a/builtin/fixedarray_test.mbt b/builtin/fixedarray_test.mbt index 13b8e98e3..278244a02 100644 --- a/builtin/fixedarray_test.mbt +++ b/builtin/fixedarray_test.mbt @@ -31,3 +31,8 @@ test "compare" { inspect!(arr3.compare(arr1), content="-1") inspect!(arr1.compare(arr1), content="0") } + +test "is_empty" { + let arr : FixedArray[Int] = [] + assert_true!(arr.is_empty()) +}