208 lines
2.2 KiB
V
208 lines
2.2 KiB
V
fn main()
|
|
{
|
|
mut a:= 3 + 3
|
|
a = 10
|
|
a++
|
|
-23
|
|
goto lbl
|
|
b:= 42
|
|
println( 'hello' )
|
|
abc()
|
|
lbl:
|
|
if true {
|
|
a = 10
|
|
a++
|
|
} else {
|
|
println('false')
|
|
}
|
|
}
|
|
|
|
const (
|
|
pi=3.14
|
|
)
|
|
|
|
pub const (
|
|
i_am_pub_const=true
|
|
)
|
|
|
|
pub enum PubEnum{
|
|
foo bar
|
|
}
|
|
|
|
enum PrivateEnum{
|
|
foo bar
|
|
}
|
|
|
|
struct User {
|
|
name string
|
|
very_long_field bool
|
|
|
|
mut:
|
|
age int
|
|
}
|
|
|
|
|
|
fn abc() int {
|
|
mut u := User{name:'Bob'}
|
|
u.age = 20
|
|
|
|
nums := [1,2,3]
|
|
number := nums[0]
|
|
return 0
|
|
}
|
|
|
|
fn new_user()
|
|
User
|
|
{
|
|
return User{
|
|
name: 'Serious Sam'
|
|
age: 19
|
|
}
|
|
}
|
|
|
|
fn voidfn(){
|
|
println('this is a function that does not return anything')
|
|
}
|
|
|
|
fn fn_with_1_arg(arg int) int {
|
|
return 0
|
|
}
|
|
|
|
fn fn_with_2a_args(arg1, arg2 int) int {
|
|
return 0
|
|
}
|
|
|
|
fn fn_with_2_args_to_be_shorten(arg1 int, arg2 int) int {
|
|
return 0
|
|
}
|
|
|
|
fn fn_with_2b_args(arg1 string, arg2 int) int {
|
|
return 0
|
|
}
|
|
|
|
fn fn_with_3_args(arg1 string, arg2 int, arg3 User) int {
|
|
return 0
|
|
}
|
|
|
|
fn (this User) fn_with_receiver() {
|
|
x := if true {1} else {2}
|
|
println('')
|
|
}
|
|
|
|
fn get_user() ? User {
|
|
return none
|
|
}
|
|
|
|
fn get_user_ptr() & User {
|
|
return &User{}
|
|
}
|
|
|
|
fn fn_with_defer() {
|
|
defer { voidfn()
|
|
}
|
|
}
|
|
|
|
struct Foo {
|
|
field1 int
|
|
field2 string
|
|
pub:
|
|
public_field1 int
|
|
public_field2 f64
|
|
mut:
|
|
mut_field string
|
|
pub mut:
|
|
pub_mut_field string
|
|
}
|
|
|
|
struct Bar {
|
|
val int
|
|
}
|
|
|
|
type FooBar = Foo | Bar
|
|
|
|
const (
|
|
reserved_types = {
|
|
'i8': true
|
|
'i16': true
|
|
'int': true
|
|
'i64': true
|
|
'i128': true
|
|
}
|
|
)
|
|
|
|
fn fn_with_assign_stmts() {
|
|
a,b := fn_with_multi_return()
|
|
}
|
|
|
|
fn fn_with_multi_return() (int,string) {
|
|
return 0,'test'
|
|
}
|
|
|
|
fn unsafe_fn() {
|
|
unsafe { malloc(2) }
|
|
}
|
|
|
|
fn fn_with_or() int {
|
|
fn_with_optional() or {
|
|
return 10
|
|
}
|
|
return 20
|
|
}
|
|
|
|
fn fn_with_optional() ?int {
|
|
if true {
|
|
return error('true')
|
|
}
|
|
return 30
|
|
}
|
|
|
|
fn (f Foo) method_with_or() int {
|
|
f.fn_with_optional() or {
|
|
return 10
|
|
}
|
|
return 20
|
|
}
|
|
|
|
fn (f Foo) fn_with_optional() ?int {
|
|
if true {
|
|
return error('true')
|
|
}
|
|
return 40
|
|
}
|
|
|
|
fn fn_with_match_expr() {
|
|
a := 10
|
|
b := match a {
|
|
10 {
|
|
10
|
|
}
|
|
5 {
|
|
5
|
|
}
|
|
else {
|
|
2
|
|
}
|
|
}
|
|
match b {
|
|
10 {
|
|
println('10')
|
|
}
|
|
20 {
|
|
println('20')
|
|
}
|
|
else {}
|
|
}
|
|
}
|
|
|
|
fn fn_variadic(arg int, args... string) {
|
|
println('Do nothing')
|
|
}
|
|
|
|
fn test_as() {
|
|
a := sum_expr() as Bar
|
|
}
|
|
|
|
fn sum_expr() FooBar {
|
|
return Bar{ val: 5 }
|
|
}
|