V (programming language): Difference between revisions

Content deleted Content added
Rewrite of method example and to be consistent with tag removal of struct examples
adding for the sake of attribution
 
(7 intermediate revisions by 4 users not shown)
Line 56:
== Syntax ==
=== Hello world ===
The [["Hello, World!" program]] in V:<ref name="MUO"/><ref name=":0">{{Cite web |title=V Documentation |url=https://docs.vlang.io/ |access-date=2025-08-25 |website=docs.vlang.io}} {{Free-content attribution|title = V Documentation| license statement URL =https://github.com/vlang/v?tab=MIT-1-ov-file| license=The MIT License|this = yes}}</ref>
The [["Hello, World!" program]] in V:<ref name="MUO"/>
<syntaxhighlight lang="v">
fn main() {
Line 64:
 
=== Variables ===
Variables are immutable by default and are defined using {{code|1=:=}} and a value. Use the {{code|mut}} [[reserved word]] (keyword) to make them mutable. Mutable variables can be assigned to using {{code|1==}}:{{sfn|Rao|2021|pp=28-40}}<ref name=":0" />
 
<syntaxhighlight lang="V">
Line 72:
</syntaxhighlight>
 
Redeclaring a variable, whether in an inner scope or in the same scope, is not allowed:{{sfn|Rao|2021|pp=28-40}}<ref name=":0" />
 
<syntaxhighlight lang="V">
Line 83:
 
=== Structs ===
Struct example:<ref name="Knott"/><ref name="section"/><ref name=":0" />
<syntaxhighlight lang="v">
struct FoobarFoo {
number int
name string
Line 92:
 
// Struct fields can be initialized by name
var1 := FoobarFoo {
number: 21
name: "baz"
Line 99:
 
// or by position
var2 := FoobarFoo{50, "quxtaz", 3.14}
</syntaxhighlight>
 
=== Heap structs ===
 
StructsBy thatdefault, structs are allocated on the stack. When structs are referenced by using the prefix {{code|1=&}} or have the heap attribute, willthey beare allocated toon the heap instead of the stack:{{sfn|Rao|2021}}<ref name=":0" />
<syntaxhighlight lang="v">
struct Foo {
Line 111:
 
@[heap]
struct BarBaz {
number f32
}
Line 118:
var1 := &Foo{2}
 
// BarBaz is always heap allocated because of its [heap] attribute
var2 := BarBaz{4.5}
 
</syntaxhighlight>
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 enrolledenrolled_status method (below) has a receiver of type {{code|1=Client}} named {{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"/><ref name=":0" /><syntaxhighlight lang="v">
 
<syntaxhighlight lang="V">
struct Client {
enrolled bool
age int
}
 
fn (x Client) enrolledenrolled_status() bool {
return x.age > 12enrolled
}
 
println(client_2.Client{enrolled: true}.enrolled_status()) // "true"
client_1 := Client{
println(client_1.Client{enrolled: false}.enrolled_status()) // "false"
age: 8
}
 
client_2 := Client{
age: 30
}
println(client_1.enrolled()) // "false"
println(client_2.enrolled()) // "true"
</syntaxhighlight>
</div>
 
=== Error handling ===
Result types may represent an error returned from a function. Result types are declared by prepending {{code|!}}: {{code|!Type}}
{{Copyvio|timestamp=20250715164557 |url=https://docs.vlang.io/type-declarations.html#optionresult-types-and-error-handling}}
Optional types are for types which may represent none. Result types may represent an error returned from a function.
 
OptionOptional types are declared bymay prependingrepresent {{code|?none}}. toOption thetypes type name:prepend {{code|?Type}}. Resultto typesthe usetype {{code|!}}name: {{code|!?Type}}.<ref name="Knott"/><ref name="section"/>{{Sfn|Tsoukalos|2022}}<ref name=":0" />
<syntaxhighlight lang="V">
fn something(t string) !string {
if t == "foo" { return "foo" }
return error("invalid string")
}
 
x := something("foo") or { "default" } // x will be "foo"
y := something("carbaz") or { "default" } // y will be "default"
z := something("carbaz") or { panic("{err}") } // z will exit with an error "invalid string" and a traceback
 
println(x)
Line 168 ⟶ 158:
 
</syntaxhighlight>
</div>
 
==See also==