V (programming language): Difference between revisions

Content deleted Content added
Rescuing 0 sources and tagging 1 as dead.) #IABot (v2.0.9.5
Small code snippets are not protected by copyright (especially common and simplistic small examples). Undid revision 1300654243 by Jan200101 (talk)
Tags: Undo Reverted
Line 83:
 
=== Structs ===
{{Copyvio|timestamp=20250715164557 |url=https://docs.vlang.io/structs.html}}
Struct example:<ref name="Knott"/><ref name="section"/>
<syntaxhighlight lang="V">
Line 100 ⟶ 99:
assert p.a == 15
</syntaxhighlight>
</div>
 
=== Heap structs ===
{{Copyvio|timestamp=20250715164557 |url=https://docs.vlang.io/structs.html#heap-structs}}
Structs are allocated on the stack by default. The {{code|&}} prefix can be used, for getting a reference to it and allocating on the heap instead:{{sfn|Rao|2021}}
<syntaxhighlight lang="V">
Line 115 ⟶ 112:
println(p.a)
</syntaxhighlight>
</div>
 
=== Methods ===
Line 122 ⟶ 118:
The is_registered method has a receiver of type User named u. 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 User {
Line 142 ⟶ 137:
println(user2.is_registered()) // "true"
</syntaxhighlight>
</div>
 
=== Error handling ===
{{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.
 
Line 163 ⟶ 156:
 
</syntaxhighlight>
</div>
 
==See also==