V (programming language): Difference between revisions

Content deleted Content added
Small code snippets are not protected by copyright (especially common and simplistic small examples). Undid revision 1300654243 by Jan200101 (talk)
Tags: Undo Reverted
Added a Criticism Section
 
(14 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 using [[TiDB]] }}{{Dead link|date=July 2025 |bot=InternetArchiveBot |fix-attempted=yes }}</ref><ref name="hackaday"/> and maintained by developers and contributors internationally.{{Sfn|Rao|2021}} It is among the languages that have been listed on the [[TIOBE index]].<ref>{{cite web |title=TIOBE Index |url=https://www.tiobe.com/tiobe-index |publisher=[[TIOBE index|TIOBE]]|website=tiobe |archive-url=https://web.archive.org/web/20250411043213/https://www.tiobe.com/tiobe-index/|archive-date=11 April 2025|access-date=11 April 2025}}</ref>
 
[[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 53:
 
Working translators are also being developed for Go, [[JavaScript]], and [[WebAssembly]].<ref>{{cite web |title=Convert Go to V with go2v|url=https://zenn.dev/tkm/articles/go2v-with-go-lsd|website=Zenn |date=26 January 2023 |access-date=26 January 2023}}</ref><ref>{{cite web |title=The V WebAssembly Compiler Backend|url=https://l-m.dev/cs/the_v_webassembly_compiler_backend/|archive-url=https://web.archive.org/web/20240708075458/https://l-m.dev/cs/the_v_webassembly_compiler_backend/ |website=l-m |date=26 February 2023 |archive-date=8 July 2024}}</ref>{{Sfn|Rao|2021}}
 
== Criticism ==
Criticisms directed at V highlight a general pattern of advertising features/libraries before they are suitable for use. Developers have criticized compiler bugs, lack of [[memory safety]], out-of-date documentation and [[Memory leak|memory leaks]] in trivial software.<ref name=":1">{{Cite web |last=skvortsov |title=V Language Review (2023) |url=https://n-skvortsov-1997.github.io/reviews/ |access-date=2025-08-30 |website=skvortsov |language=en}}</ref><ref>{{Cite web |last=mawfig.github.io |date=2022-06-18 |title=V Language Review (2022) |url=https://mawfig.github.io/2022/06/18/v-lang-in-2022.html |access-date=2025-08-30 |website=mawfig.github.io |language=en}}</ref><ref>{{Cite web |date=2020-06-17 |title=V Update - June 2020 |url=https://xeiaso.net/blog/vlang-update-2020-06-17/ |access-date=2025-08-30 |website=xeiaso.net |language=en}}</ref> These drawbacks may leave software written in V [[Vulnerability (computer security)|open to software vulnerabilities]]. Though, as a [[counterargument]], it may be stated that V is currently in [[Beta software|beta]] as of August, 2025.<ref name="VlangBeta" />
 
Beyond the programming language itself, developers have also criticized moderation within the V community, with individuals being kicked out of [[Chat room|chat-rooms]] for criticizing the language.<ref name=":1" />
 
== 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 ⟶ 69:
 
=== 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 ⟶ 77:
</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 ⟶ 88:
 
=== Structs ===
Struct example:<ref name="Knott"/><ref name="section"/><ref name=":0" />
<syntaxhighlight lang="Vv">
struct PlaceFoo {
a number int
name string
b int
score f32
}
 
// Struct fields can be initialized by name
mut p := Place {
var1 := Foo {
a: 15
number: 21
b: 25
name: "baz"
score: 2.5
}
 
println(p.a) // A dot is used to access struct fields
// or by position
// Alternative literal syntax can be used
pvar2 := PlaceFoo{1550, 25"taz", 3.14}
assert p.a == 15
</syntaxhighlight>
 
=== 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}}
By default, structs are allocated on the [[Stack_memory|stack]]. When structs are referenced by using the prefix {{code|1=&}} or have the {{code|1=[heap]}} attribute, they are allocated on the [[Heap-based_memory_allocation|heap]] instead:{{sfn|Rao|2021}}<ref name=":0" />
<syntaxhighlight lang="Vv">
struct PlaceFoo {
a int
b number int
}
 
@[heap]
p := &Place{30, 30}
struct UserBaz {
// References use the same syntax to access fields
number f32
println(p.a)
 
// 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 is_registeredenrolled_status method (below) has a receiver of type User{{code|1=Client}} 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"/><ref name=":0" /><syntaxhighlight lang="v">
struct Client {
 
enrolled bool
<syntaxhighlight lang="V">
struct User {
age int
}
 
fn (ux UserClient) is_registeredenrolled_status() bool {
return x.enrolled
return u.age > 16
}
 
println(Client{enrolled: true}.enrolled_status()) // true
user := User{
println(Client{enrolled: false}.enrolled_status()) // false
age: 10
println(user.is_registered()) // "false"
 
user2 := User{
age: 20
}
println(user2.is_registered()) // "true"
</syntaxhighlight>
 
=== Error handling ===
OptionalResult types aremay forrepresent typesan whicherror mayreturned representfrom nonea function. Result types mayare representdeclared anby errorprepending returned{{code|!}}: from a function.{{code|!Type}}
 
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)