v2 / vlib / v / tests / aliases / alias_interface_test.v
40 lines · 33 sloc · 590 bytes · 87340bcca2aa7f7cb6b524d6573a08b52ed1401b
Raw
1module main
2
3// Define a simple interface
4pub interface Node {
5 kind() int
6 name() string
7}
8
9// Implement the interface
10pub struct MyNode {
11pub:
12 value int
13 text string
14}
15
16pub fn (n MyNode) kind() int {
17 return 1
18}
19
20pub fn (n MyNode) name() string {
21 return n.text
22}
23
24// Define a type alias to the interface
25pub type Expr = Node
26
27// Function using the type alias
28pub fn process_node(expr Expr) string {
29 return expr.name()
30}
31
32fn test_alias_interface() {
33 node := MyNode{
34 value: 42
35 text: 'test'
36 }
37 result := process_node(node)
38 println('Result: ${result}')
39 assert result == 'test'
40}
41