Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
checker: fix generic array method call with multi-types (#20237)
- Loading branch information
Showing
2 changed files
with
55 additions
and
4 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
44 changes: 44 additions & 0 deletions
44
vlib/v/tests/generics_array_method_call_with_multi_types_test.v
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,44 @@ | ||
pub type EventListener[T] = fn (T) ! | ||
|
||
pub type Check[T] = fn (T) bool | ||
|
||
pub struct EventController[T] { | ||
mut: | ||
id int | ||
listeners map[int]EventListener[T] | ||
} | ||
|
||
fn (mut ec EventController[T]) generate_id() int { | ||
return ec.id++ | ||
} | ||
|
||
@[params] | ||
pub struct EmitOptions { | ||
pub: | ||
error_handler ?fn (int, IError) | ||
} | ||
|
||
pub fn (mut ec EventController[T]) emit(e T, options EmitOptions) { | ||
if ec.listeners.len == 1 { | ||
f := ec.listeners.values()[0] | ||
f(e) or { | ||
if g := options.error_handler { | ||
g(0, err) | ||
} | ||
} | ||
return | ||
} | ||
} | ||
|
||
struct Foo {} | ||
|
||
struct Bar {} | ||
|
||
fn test_generic_array_method_call_with_multi_types() { | ||
foo := EventController[Foo]{} | ||
println(foo) | ||
assert foo.id == 0 | ||
bar := EventController[Bar]{} | ||
println(bar) | ||
assert bar.id == 0 | ||
} |