mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
33 lines
421 B
V
33 lines
421 B
V
interface Iter<T> {
|
|
next() ?T
|
|
}
|
|
|
|
struct ArrayIter<T> {
|
|
data []T
|
|
mut:
|
|
index int
|
|
}
|
|
|
|
fn (mut i ArrayIter<T>) next<T>() ?T {
|
|
if i.data.len == 0 {
|
|
return none
|
|
}
|
|
i.index += 1
|
|
return i.data[i.index]
|
|
}
|
|
|
|
fn iter<T>(arr []T) Iter<T> {
|
|
return ArrayIter<T>{
|
|
data: arr
|
|
index: 0
|
|
}
|
|
}
|
|
|
|
fn test_generics_fn_return_generic_interface() {
|
|
x := iter([1, 2, 3])
|
|
println(x)
|
|
y := x.next() or { 0 }
|
|
println(y)
|
|
assert y == 2
|
|
}
|