1
0
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:
yuyi 2023-06-08 16:42:29 +08:00 committed by GitHub
parent bcd5c91bdc
commit 01b20485c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 1 deletions

View File

@ -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 {

View 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
}

View File

@ -0,0 +1,11 @@
module ecs
pub struct Position {
x int
y int
}
pub struct Velocity {
x f64
y f64
}