From e7787fca522edb15614ae21596188f58dc0f5676 Mon Sep 17 00:00:00 2001 From: Vinicius Silva Date: Fri, 29 Dec 2023 10:53:21 -0300 Subject: [PATCH] 11_structs directory created --- 11_structs/default_structs.v | 11 +++++++++++ 11_structs/heap_structs.v | 11 +++++++++++ 11_structs/heap_structs2.v | 23 +++++++++++++++++++++++ 11_structs/literal_struct.v | 17 +++++++++++++++++ 11_structs/required_structs.v | 12 ++++++++++++ 11_structs/structs.v | 15 +++++++++++++++ 6 files changed, 89 insertions(+) create mode 100644 11_structs/default_structs.v create mode 100644 11_structs/heap_structs.v create mode 100644 11_structs/heap_structs2.v create mode 100644 11_structs/literal_struct.v create mode 100644 11_structs/required_structs.v create mode 100644 11_structs/structs.v diff --git a/11_structs/default_structs.v b/11_structs/default_structs.v new file mode 100644 index 0000000..61d2ae0 --- /dev/null +++ b/11_structs/default_structs.v @@ -0,0 +1,11 @@ +struct Foo{ + n int // Default is 0 + s string // Default is '' + a []int // Default is []int{} + pos int = -1 // Default is -1 +} + +pub fn main(){ + mut f := Foo{1,'Test', [12,3,5,3,6], -2} + println(f) +} \ No newline at end of file diff --git a/11_structs/heap_structs.v b/11_structs/heap_structs.v new file mode 100644 index 0000000..fb8a74e --- /dev/null +++ b/11_structs/heap_structs.v @@ -0,0 +1,11 @@ +struct Point{ + x int + y int +} + + + +pub fn main(){ + p := &Point{10,10} + println(p.x) +} \ No newline at end of file diff --git a/11_structs/heap_structs2.v b/11_structs/heap_structs2.v new file mode 100644 index 0000000..130c1dc --- /dev/null +++ b/11_structs/heap_structs2.v @@ -0,0 +1,23 @@ +struct Foo{ + mut: + x int +} + +pub fn main(){ + fa := Foo{1} + mut a := fa + a.x = 2 + assert fa.x == 1 + assert a.x == 2 + + mut fc := Foo{1} + mut c := &fc + + c.x = 2 + assert fc.x == 2 + assert c.x == 2 + + println(fc) + println(c) + +} \ No newline at end of file diff --git a/11_structs/literal_struct.v b/11_structs/literal_struct.v new file mode 100644 index 0000000..e8dcf35 --- /dev/null +++ b/11_structs/literal_struct.v @@ -0,0 +1,17 @@ + +struct Point{ + x int + y int +} + +pub fn main(){ + mut p := Point{x: 10, y: 20} + + assert p.x == 10 + + mut p1 := Point{x: 30, y: 4} + + assert p1.y == 4 + point := [Point{x: 10, y: 20},Point{x: 20, y: 30},Point{x: 30, y: 40}] + println(point) +} \ No newline at end of file diff --git a/11_structs/required_structs.v b/11_structs/required_structs.v new file mode 100644 index 0000000..0bdc485 --- /dev/null +++ b/11_structs/required_structs.v @@ -0,0 +1,12 @@ +struct Foo{ + m int @[required] + n string +} + +pub fn main(){ + mut a := Foo{1, ''} + assert a.m == 1 + + _ := Foo{1,'Test'} + +} \ No newline at end of file diff --git a/11_structs/structs.v b/11_structs/structs.v new file mode 100644 index 0000000..85583e3 --- /dev/null +++ b/11_structs/structs.v @@ -0,0 +1,15 @@ + +struct Point{ + x int + y int +} + +pub fn main(){ + mut p := Point{x: 10, y: 20} + println('${p.x} | ${p.y}') + + + p1 := Point{10, 20} + assert p1.x == 10 +} +