v2 / vlib / v / tests / generics / generics_fn_variable_3_test.v
36 lines · 27 sloc · 783 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1import ecs
2
3struct Entity {
4 components []Component
5}
6
7interface Component {}
8
9fn 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
13pub 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
19pub 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
29fn component_interface_hack() []Component {
30 return [ecs.Position{}, ecs.Velocity{}]
31}
32
33fn test_generic_fn_variable() {
34 query := two_components_filter_query[ecs.Position, ecs.Velocity]
35 assert true
36}
37