Structs
Examples in
V
// V doesn't have classes. But you can define methods on types.
// A method is a function with a special receiver argument.
// The receiver appears in its own argument list between the fn keyword and the method name.
// In this example, the can_register method has a receiver of type User named u.
// The convention is not to use receiver names like self or this, but a short, preferably one letter long, name.
struct User {
age int
}
fn (u User) can_register() bool {
return u.age > 16
}
user := User{age: 10}
println(user.can_register()) // "false"
user2 := User{age: 20}
println(user2.can_register()) // "true"
Last Run
:
false
true
// Struct fields are private and immutable by default (making structs immutable as well).
// Their access modifiers can be changed with pub and mut.
// In total, there are 5 possible options:
struct Foo {
a int // private immutable (default)
mut:
b int // private mutable
c int // (you can list multiple fields with the same access modifier)
pub:
d int // public immmutable (readonly)
pub mut:
e int // public, but mutable only in parent module
__global:
f int // public and mutable both inside and outside parent module
} // (not recommended to use, that's why the 'global' keyword
// starts with __)
module main
/* STRUCT EXAMPLE 1 */
struct Point {
x int
y int
}
fn example1() {
p := Point{
x: 10
y: 20
}
println(p.x) // Struct fields are accessed using a dot
}
/* STRUCT EXAMPLE 2 */
fn example2() {
// Structs are allocated on the stack. To allocate a struct on the heap and get a reference to it, use the & prefix:
// Alternative initialization syntax for structs with 3 fields or fewer
p := &Point{330, 10}
// References have the same syntax for accessing fields
println(p.x)
// The type of p is &Point. It's a reference to Point. References are similar to Go pointers and C++ references.
}
/* STRUCT EXAMPLE 4 */
// Structs can have default values:
struct Foo {
a int
b int = 10
}
fn example4() {
foo := Foo{}
assert foo.a == 0
assert foo.b == 10
println(foo)
}
fn main() {
example1()
example2()
example4()
}