Move empty? to the Length trait

If an object implements the `length` method, this allows us to give it
the `empty?` method for free.
parent 4b87da28
......@@ -233,23 +233,6 @@ impl Length for Array!(T) {
}
}
impl Array!(T) {
## Returns `True` if this `Array` contains one or more values.
##
## # Examples
##
## Using an empty `Array`:
##
## [].empty? # => True
##
## Using a non-empty `Array`:
##
## [10].empty? # => False
def empty? -> Boolean {
length.zero?
}
}
impl Index!(Integer, T) for Array!(T) {
## Returns the value at the given index, or Nil if no value was found.
##
......
......@@ -5,6 +5,21 @@
#! `Array`.
trait Length {
## Returns the number of values in the object.
## Returns the number of values in `self`.
def length -> Integer
## Returns `True` if `self` is empty.
##
## # Examples
##
## Using an empty `Array`:
##
## [].empty? # => True
##
## Using a non-empty `Array`:
##
## [10].empty? # => False
def empty? -> Boolean {
length.zero?
}
}
......@@ -34,6 +34,7 @@ import test::std::test_inspect
import test::std::test_integer
import test::std::test_io
import test::std::test_iterator
import test::std::test_length
import test::std::test_mirror
import test::std::test_nil
import test::std::test_object
......
import std::length::Length
import std::test
import std::test::assert
object EmptyList impl Length {
def length -> Integer {
0
}
}
object NonEmptyList impl Length {
def length -> Integer {
1
}
}
test.group('std::length::Length.empty?') do (g) {
g.test('Checking if an empty object is empty') {
assert.true(EmptyList.new.empty?)
}
g.test('Checking if a non-empty object is empty') {
assert.false(NonEmptyList.new.empty?)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment