Skip to content

Commit 01b2048

Browse files
authored
parser: fix generic function variable (#18373)
1 parent bcd5c91 commit 01b2048

3 files changed

Lines changed: 54 additions & 1 deletion

File tree

vlib/v/parser/parser.v

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2140,7 +2140,13 @@ fn (mut p Parser) is_following_concrete_types() bool {
21402140
} else if cur_tok.kind == .rsbr {
21412141
break
21422142
} else if cur_tok.kind == .name {
2143-
if !(p.is_typename(cur_tok) && !(cur_tok.lit.len == 1 && !cur_tok.lit[0].is_capital())) {
2143+
if p.peek_token(i + 1).kind == .dot {
2144+
if p.is_typename(cur_tok) {
2145+
return false
2146+
}
2147+
i++
2148+
} else if !(p.is_typename(cur_tok) && !(cur_tok.lit.len == 1
2149+
&& !cur_tok.lit[0].is_capital())) {
21442150
return false
21452151
}
21462152
} else if cur_tok.kind != .comma {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import ecs
2+
3+
struct Entity {
4+
components []Component
5+
}
6+
7+
interface Component {}
8+
9+
fn two_components_filter_query[A, B](entity Entity) bool {
10+
return check_if_entity_has_component[A](entity) && check_if_entity_has_component[B](entity)
11+
}
12+
13+
pub fn check_if_entity_has_component[T](entity Entity) bool {
14+
get_entity_component[T](entity) or { return false }
15+
16+
return true
17+
}
18+
19+
pub fn get_entity_component[T](entity Entity) !&T {
20+
for component in entity.components {
21+
if component is T {
22+
return component
23+
}
24+
}
25+
26+
return error('Entity with does not have a component of type ${T.name}')
27+
}
28+
29+
fn component_interface_hack() []Component {
30+
return [ecs.Position{}, ecs.Velocity{}]
31+
}
32+
33+
fn test_generic_fn_variable() {
34+
query := two_components_filter_query[ecs.Position, ecs.Velocity]
35+
assert true
36+
}

vlib/v/tests/modules/ecs/ecs.v

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module ecs
2+
3+
pub struct Position {
4+
x int
5+
y int
6+
}
7+
8+
pub struct Velocity {
9+
x f64
10+
y f64
11+
}

0 commit comments

Comments
 (0)