Skip to content

Commit

Permalink
tests: Prepare test for collect() method
Browse files Browse the repository at this point in the history
  • Loading branch information
dinfuehr committed Feb 9, 2025
1 parent 9f20167 commit 9dda089
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions tests/trait/trait-default-collect.dora
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//= ignore

trait MyIterator {
type Item;
fn next(): Option[Self::Item];

fn collect[T](): T where T: MyFromIterator[Self::Item] {
T::fromIter(self)
}
}

pub trait MyFromIterator[A] {
fn fromIter[T](iter: T): Self where T: MyIterator[Item=A];
}

class Range {
value: Int
}

impl MyIterator for Range {
type Item = Int;

fn next(): Option[Int] {
if self.value <= 0 {
None[Int]
} else {
let result = self.value;
self.value -= 1;
Some[Int](result)
}
}
}

impl MyFromIterator[Int] for Vec[Int] {
fn fromIter[T](iter: T): Vec[Int] where T: MyIterator[Item=Int] {
let result = Vec[Int]::new();

while iter.next() is Some(value) {
result.push(value);
}

result
}
}

fn main() {
let values: Vec[Int] = Range(2).collect[Vec[Int]]();
assert(values == Vec[Int]::new(1, 0));
}

0 comments on commit 9dda089

Please sign in to comment.