oden/fine/tests/expression/generic_class.fine
2024-02-11 18:44:41 -08:00

39 lines
No EOL
650 B
Text

class MyIterator {
list: MyList<$0>;
index: i64;
fun next(self) -> $0 or nothing {
if self.index < list.len() {
let result = self.list[self.index];
self.index += 1;
result
} else {
nothing
}
}
}
class MyList {
list: list<$0>;
fun get_iterator(self) -> MyIterator {
new MyIterator {
list: self.list,
index: 0,
}
}
}
fun test() -> f64 {
// Type needs to be inferred as MyList<f64>
let x = MyList { list: [1, 2, 3] };
let sum = 0;
for v in x { // Pick up the iterator methods
sum = sum + v;
}
sum
}
// @ignore undesigned garbage, like all generics
// @no-errors