Struct Methods

Posted by yhuang
Public (Editable by Users)
// 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"