Content deleted Content added
Undid revision 1306725083 by 178.69.159.68 (talk), please discuss this on the talk page or Wikipedia:Copyright problems/2025 July 15 before reverting this |
Wiki-link words and removed erroneous dead link |
||
(12 intermediate revisions by 5 users not shown) | |||
Line 28:
The new language was created as a result of frustration with existing languages being used for personal projects.{{sfn|Chakraborty|Haldar|2023}} It was originally intended for personal use, but after being mentioned publicly and increasing interest, it was decided to make it public. V was initially created to develop a desktop messaging client named Volt.<ref name="hackaday"/> On public release, the compiler was written in V, and could [[Self-hosting (compilers)|compile itself]].{{Sfn|Rao|2021}}{{sfn|Chakraborty|Haldar|2023}} Key design goals in creating V were being easy to learn and use, higher readability, fast compiling, increased safety, efficient development, [[Cross-platform software|cross-platform]] usability, improved [[C (programming language)|C]] [[interoperability]], better [[Error detection and correction|error handling]], modern features, and more maintainable software.<ref name="MUO"/><ref name="analyticsindiamag"/><ref name="nasufi"/><ref>{{cite web |title=V language: simple like Go, small binary like Rust|url=https://techracho.bpsinc.jp/hachi8833/2021_03_09/89457/ |website=TechRacho |access-date=3 March 2021}}</ref>
V is released and developed through [[GitHub]],<ref>{{cite web |url=https://ossinsight.io/analyze/vlang/v#overview |title=GitHub Programming Languages (repository details) |via=[[Open-source software|OSS]] Insight
[[File:Veasel.svg|thumb|Veasel is the official mascot of the V programming language<ref>{{cite web |url=https://github.com/vlang/v-mascot/ |title=V's official mascot |website=GitHub |access-date=8 November 2023}}</ref>]]
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>
<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" />▼
▲Struct example:<ref name="Knott"/><ref name="section"/>
▲<syntaxhighlight lang="V">
number int
name string
score f32
}
// Struct fields can be initialized by name
var1 := Foo {
number: 21
name: "baz"
score: 2.5
}
// or by position
</syntaxhighlight>
=== Heap structs ===
<syntaxhighlight lang="
struct
}
@[heap]
number f32
}▼
// Structs that are referenced are heap allocated
var1 := &Foo{2}
// Baz is always heap allocated because of its [heap] attribute
var2 := Baz{4.5}
</syntaxhighlight>
=== Methods ===
[[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 {{code|1=fn}} keyword and the method name. Methods must be in the same [[Modular programming|module]] as the receiver type.
The
struct Client {
enrolled bool
▲struct User {
}
fn (
return x.enrolled
}
println(Client{enrolled: true}.enrolled_status()) // true
println(Client{enrolled: false}.enrolled_status()) // false
▲}
</syntaxhighlight>
=== Error handling ===
Result types may represent an error returned from a function. Result types are declared by prepending {{code|!}}: {{code|!Type}}
<syntaxhighlight lang="V">
fn something(t string) !string {
if t == "foo" { return "foo" }
return error("invalid
}
x := something("foo") or { "default" } // x will be "foo"
y := something("
z := something("
println(x)
Line 163 ⟶ 158:
</syntaxhighlight>
==See also==
|