Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* checker: fix generics with anon generics fn argument * cgen: fix typedef of generics anon fn * fix check generics argument types
- Loading branch information
Showing
3 changed files
with
53 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
struct MyStruct<T> { | ||
arr []T | ||
} | ||
|
||
fn (mut s MyStruct<T>) get_data(pos int) T { | ||
return s.arr[pos] | ||
} | ||
|
||
fn (mut s MyStruct<T>) iterate(handler fn (T) int) int { | ||
mut sum := 0 | ||
mut i := 0 | ||
for { | ||
k := s.get_data<T>(i) | ||
sum += handler(k) | ||
i++ | ||
if i > 4 { | ||
break | ||
} | ||
} | ||
return sum | ||
} | ||
|
||
pub fn consume(data int) int { | ||
return data | ||
} | ||
|
||
fn test_generics_with_anon_generics_fn() { | ||
mut s := MyStruct<int>{ | ||
arr: [1, 2, 3, 4, 5] | ||
} | ||
y := s.iterate<int>(consume) | ||
println(y) | ||
assert y == 15 | ||
} |