blob: 536dc04e1a5b43caa9d0b78a05f182f7f8392a67 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
// usage test: v ast path_to_v/cmd/tools/vast/test/demo.v
// will generate demo.json
// comment for module
module main
// import module
import os
import math
import time { Time, now }
// const decl
const (
a = 1
b = 3
c = 'c'
)
// struct decl
struct Point {
x int
mut:
y int
pub:
z int
pub mut:
name string
}
// method of Point
pub fn (p Point) get_x() int {
return p.x
}
// embed struct
struct MyPoint {
Point
title string
}
// enum type
enum Color {
red
green
blue
}
// type alias
type Myint = int
// sum type
type MySumType = bool | int | string
// function type
type Myfn = fn (int) int
// interface type
interface Myinterfacer {
add(int, int) int
sub(int, int) int
}
// main funciton
fn main() {
add(1, 3)
println(add(1, 2))
println('ok') // comment println
arr := [1, 3, 5, 7]
for a in arr {
println(a)
add(1, 3)
}
color := Color.red
println(color)
println(os.args)
m := math.max(1, 3)
println(m)
println(now())
t := Time{}
println(t)
p := Point{
x: 1
y: 2
z: 3
}
println(p)
my_point := MyPoint{
// x: 1
// y: 3
// z: 5
}
println(my_point.get_x())
}
// normal function
fn add(x int, y int) int {
return x + y
}
// function with defer stmt
fn defer_fn() {
mut x := 1
println('start fn')
defer {
println('in defer block')
println(x)
}
println('end fn')
}
// generic function
fn g_fn<T>(p T) T {
return p
}
// generic struct
struct GenericStruct<T> {
point Point
mut:
model T
}
// generic interface
interface Gettable<T> {
get() T
}
// generic sumtype
struct None {}
type MyOption<T> = Error | None | T
|