V (programming language): Difference between revisions

Content deleted Content added
(1) The common type of struct examples redone (2) Rewording with references
Redo of error handling example
Line 141:
Optional types are for types which may represent none. Result types may represent an error returned from a function.
 
Option types are declared by prepending {{code|?}} to the type name: {{code|?Type}}. Result types use {{code|!}}: {{code|!Type}}.<ref name="Knott"/><ref name="section"/>{{Sfn|Tsoukalos|2022}}
<syntaxhighlight lang="V">
fn do_somethingsomething(st string) !string {
if st == "foo" { return "foo" }
return "foo"
}
return error("invalid string")
}
 
ax := do_somethingsomething("foo") or { "default" } // ax will be "foo"
by := do_somethingsomething("barcar") or { "default" } // by will be "default"
cz := do_somethingsomething("barcar") or { panic("{err}") } // exitswill exit with error "invalid string" and a traceback
 
println(ax)
println(by)
 
</syntaxhighlight>