V (programming language): Difference between revisions

Content deleted Content added
m Spaces removed in struct examples to be consistent with documentation, books on subject, and to prevent confusion
Rewrite of method example and to be consistent with tag removal of struct examples
Line 126:
[[Method (computer programming)|Methods]] in V are functions defined with a receiver [[Parameter (computer programming)|argument]]. The receiver appears in its own argument list between the fn keyword and the method name. Methods must be in the same [[Modular programming|module]] as the receiver type.
 
The is_registeredenrolled method (below) has a receiver of type UserClient named u{{code|1=x}}. The convention is not to use receiver names like self or this, but preferably a short name. For example:<ref name="Knott"/><ref name="nasufi"/>
 
{{Copyvio|timestamp=20250715164557 |url=https://docs.vlang.io/structs.html#methods}}
<syntaxhighlight lang="V">
struct UserClient {
age int
}
 
fn (ux UserClient) is_registeredenrolled() bool {
return ux.age > 1612
}
 
userclient_1 := UserClient{
age: 108
}
println(user.is_registered()) // "false"
 
user2client_2 := UserClient{
age: 2030
}
println(user2client_1.is_registeredenrolled()) // "truefalse"
println(userclient_2.is_registeredenrolled()) // "falsetrue"
</syntaxhighlight>
</div>