v2 / vlib / v / tests / comptime / comptime_if_is_generic_interface_test.v
31 lines · 26 sloc · 531 bytes · 8b02d8f406119a4ec25e22dc98b161727a367726
Raw
1struct Context {}
2
3struct App {
4mut:
5 called bool
6}
7
8fn (mut app App) before_request(mut ctx Context) {
9 app.called = true
10}
11
12interface HasBeforeRequest[X] {
13mut:
14 before_request(mut ctx X)
15}
16
17fn check_before_request[A, X](mut app A) bool {
18 mut ctx := Context{}
19 $if A is HasBeforeRequest[X] {
20 app.before_request(mut ctx)
21 return true
22 } $else {
23 return false
24 }
25}
26
27fn test_comptime_if_is_generic_interface_with_generic_type_param() {
28 mut app := App{}
29 assert check_before_request[App, Context](mut app)
30 assert app.called
31}
32