mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
parser: fix generic function variable (#18373)
This commit is contained in:
parent
bcd5c91bdc
commit
01b20485c3
@ -2140,7 +2140,13 @@ fn (mut p Parser) is_following_concrete_types() bool {
|
||||
} else if cur_tok.kind == .rsbr {
|
||||
break
|
||||
} else if cur_tok.kind == .name {
|
||||
if !(p.is_typename(cur_tok) && !(cur_tok.lit.len == 1 && !cur_tok.lit[0].is_capital())) {
|
||||
if p.peek_token(i + 1).kind == .dot {
|
||||
if p.is_typename(cur_tok) {
|
||||
return false
|
||||
}
|
||||
i++
|
||||
} else if !(p.is_typename(cur_tok) && !(cur_tok.lit.len == 1
|
||||
&& !cur_tok.lit[0].is_capital())) {
|
||||
return false
|
||||
}
|
||||
} else if cur_tok.kind != .comma {
|
||||
|
36
vlib/v/tests/generics_fn_variable_3_test.v
Normal file
36
vlib/v/tests/generics_fn_variable_3_test.v
Normal file
@ -0,0 +1,36 @@
|
||||
import ecs
|
||||
|
||||
struct Entity {
|
||||
components []Component
|
||||
}
|
||||
|
||||
interface Component {}
|
||||
|
||||
fn two_components_filter_query[A, B](entity Entity) bool {
|
||||
return check_if_entity_has_component[A](entity) && check_if_entity_has_component[B](entity)
|
||||
}
|
||||
|
||||
pub fn check_if_entity_has_component[T](entity Entity) bool {
|
||||
get_entity_component[T](entity) or { return false }
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
pub fn get_entity_component[T](entity Entity) !&T {
|
||||
for component in entity.components {
|
||||
if component is T {
|
||||
return component
|
||||
}
|
||||
}
|
||||
|
||||
return error('Entity with does not have a component of type ${T.name}')
|
||||
}
|
||||
|
||||
fn component_interface_hack() []Component {
|
||||
return [ecs.Position{}, ecs.Velocity{}]
|
||||
}
|
||||
|
||||
fn test_generic_fn_variable() {
|
||||
query := two_components_filter_query[ecs.Position, ecs.Velocity]
|
||||
assert true
|
||||
}
|
11
vlib/v/tests/modules/ecs/ecs.v
Normal file
11
vlib/v/tests/modules/ecs/ecs.v
Normal file
@ -0,0 +1,11 @@
|
||||
module ecs
|
||||
|
||||
pub struct Position {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
pub struct Velocity {
|
||||
x f64
|
||||
y f64
|
||||
}
|
Loading…
Reference in New Issue
Block a user