V (programming language): Difference between revisions

Content deleted Content added
Japanese asin for book written in Japanese
adding for the sake of attribution
 
(37 intermediate revisions by 13 users not shown)
Line 17:
| license = [[MIT License|MIT]]
| file ext = <code>.v</code>, <code>.vsh</code>
| website = {{URL|https://vlang.io}}
| influenced by = [[Go (programming language)|Go]], [[Kotlin (programming language)|Kotlin]], [[Oberon (programming language)|Oberon]], [[Python (programming language)|Python]], [[Rust (programming language)|Rust]], [[Swift (programming language)|Swift]]
}}
 
'''V''', also known as '''vlang''', is a [[Static type checking|statically typed]], [[Compiled language|compiled]] [[programming language]] created by Alexander Medvednikov in early 2019.{{Sfn|Rao|2021}} It was inspired by the language [[Go (programming language)|Go]], and other influencesprogramming languages including [[Oberon (programming language)|Oberon]], [[Swift (programming language)|Swift]], and [[Rust (programming language)|Rust]].<ref>{{cite web |lastlast1=Lewkowicz |firstfirst1=Jakub |title=SD Times news digest: V language now open sourced |url=https://sdtimes.com/softwaredev/sd-times-news-digest-v-language-now-open-sourced-smartbear-acquires-bdd-provider-cucumber-and-kaggle-integrates-into-bigquery|publisher=[[SD Times]] |website=SD Times |date=25 June 2019 |access-date=25 June 2019}}</ref><ref name="hackaday">{{cite web |lastlast1=James |firstfirst1=Ben |date=23 July 2019 |title=The V Programming Language: Vain Or Virtuous? |url=https://hackaday.com/2019/07/23/the-v-programming-language-vain-or-virtuous/ |access-date=23 July 2019 |publisher=[[Hackaday]]|website=Hackaday}}</ref><ref name="section">{{cite web |lastlast1=Umoren |firstfirst1=Samuel |title=Building a Web Server using Vlang |url=https://www.section.io/engineering-education/building-web-server-with-vlang |website=Section |archive-url=https://web.archive.org/web/20230313021526/https://www.section.io/engineering-education/building-web-server-with-vlang|archive-date=13 March 2023|access-date=5 April 2021}}</ref> It is [[free and open-source software]] released under the [[MIT License]], and currently in [[Software release life cycle#Beta|beta]].<ref name=VlangBeta>{{Cite web |title=The V Programming Language |url=https://vlang.io/ |access-date=2023-11-04 |website=vlang.io}}</ref>
 
The goals of V include ease of use, [[Computer programming#Readability of source code|readability]], and [[Maintainability#Software|maintainability]].<ref name="Knott">{{cite web |lastlast1=Knott |firstfirst1=Simon |date=27 June 2019 |title=An introduction to V |url=https://simonknott.de/articles/vlang/ |access-date=27 June 2019}}</ref><ref name="nasufi">{{cite web |lastlast1=Nasufi |firstfirst1=Erdet |title=An introduction to V - the vlang|url=https://debconf22.debconf.org/talks/69-an-introduction-to-v-the-vlang/ |website=Debian Conference ([[Debian Conference|DebConf]]) |access-date=24 July 2022}}</ref><ref name="techgig">{{cite web |last1=Sharma| first1=Gaurav |title=Exploring the newest programming languages for developers in 2024 |url=https://content.techgig.com/technology/exploring-the-newest-programming-languages-for-developers-in-2024/articleshow/108624842.cms |publisher=[[TechGig.com]] |website=TechGig |date=March 19, 2024}}</ref>
 
==History==
According to various sources, theThe new language was created as a result of frustration with existing languages being used for personal projects.<ref name="SYNCS 2023">{{cite web |title=How To Maintain And Iterate With V - SYNCS 2023 (Sydney Computing Society at the University of Sydney)|url=https://www.youtube.com/watch?v=pchEsYwA74Q |website=YouTube |access-date=18 October 2023}}</ref>{{sfn|Chakraborty|Haldar|2023}}{{Sfn|Trex|2024}} The languageIt was originally intended for personal use, but after it wasbeing mentioned publicly and gainedincreasing interest, it was decided to make it public. V was initially created in order to develop a desktop messaging client known asnamed Volt.<ref name="hackaday"/> UponOn 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 behindin the creation ofcreating V were being easy to learn and use, higher readability, fast compilationcompiling, 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.{{Sfn|Independent<ref Laboratory|2020}}{{Sfn|Lyons|2022}}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 from around the worldinternationally.{{Sfn|Rao|2021}} It is among the languages that have been listed on the [[TIOBE index]].<ref>{{Sfncite web |Trextitle=TIOBE Index |2024url=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>]]
 
== Features ==
<!-- theThe list of "safety features" and memory management options needneeds a rewrite (with sources to suggest why those features are considered "safety" (or simply remove the claim that it is a safety feature) and etc.) for them to be encyclopedic. -->
 
===Safety===
V has policies to facilitate memory-safety, speed, and secure code.,<ref name="MUO">{{cite web |last1=Galuh |first1=Rosa |title=A Brief Introduction to the V Language |url=https://www.makeuseof.com/v-language-brief-introduction |publisher=[[Valnet]]|website=MakeUseOf (MUO) |date=8 August 2022 |access-date=8 August 2022}}</ref><ref name="HA">{{cite web |last1=Abbas |first1=Hazem |title=Introduction to V Language and Desktop App Development |url=https://medevel.com/v-desktop-app-tutorial/ |website=medevel |date=5 August 2024 |access-date=3 January 2025}}</ref>{{Sfn|Trex|2024}}<ref The languagename="hackaday"/> hasincluding various default features for greater program safety.<ref name="section" /><ref name="MUO" />{{Sfnsfn|TrexChakraborty|2024Haldar|2023}} It employs [[bounds checking]], to guard against out of bounds usageuse of [[Variable (computer science)|variables]]. Option/result [[Type system|types]] are used, where the option [[data type]] ({{code|?}}) can be represented by {{code|none}} (among possible choices) and the result type ({{code|!}}) can handle any returned errors. To ensure greater safety, theerror checking of errors areis mandatory in V. By default, the following are [[immutable]]: variables, structs, and [[Function (computer programming)|function]] [[Parameter (computer programming)|arguments]]. This includes string values are immutable, so elements can notcannot be mutated. Other protections, which are the default for the language, are: no usageuse of [[undefined value]]s, no [[variable shadowing|shadowing of variables]], no usage of [[null pointer|null]]s (unless code marked as unsafe), and no usage ofor [[global variable]]s (unless enabled via flag).
 
=== Performance ===
V uses [[value type]]s and string buffers to reduce memory allocations.{{sfn|Rao|2021|p=7}}<ref>{{cite web |title=The V programming language is now open source |url=https://hub.packtpub.com/the-v-programming-language-is-now-open-sourced-is-it-too-good-to-be-true// |publisher=[[Packt Publishing]] |website=Packt Hub |date=24 June 2019 |access-date=24 June 2019}}</ref><ref name="MUO"/> The language can be compiled to human-readable C,<ref name="section"/>{{Sfn|Rao|2021}} and in terms of execution and compilation, it's considered to be as performant.<ref name="MUO"/><ref name="analyticsindiamag">{{Sfncite web |Trexfirst1=Ambika |last1=Choudhury |title=Meet V, The New Statically Typed Programming Language Inspired By Go & Rust|url=https://analyticsindiamag.com/meet-v-the-new-statistically-typed-programming-language-inspired-by-go-rust|website=Analytics India Magazine (AIM)|date=9 February 2022|access-date=7 July 2024}}</ref>{{sfn|Chakraborty|Haldar|2023}}
 
=== Memory management ===
TheV language'ssupports 4 supported options for memory management are the followingoptions:{{Sfn|Tsoukalos|2022}}<ref name="hackaday"/>{{sfn|Chakraborty|Haldar|2023}}
 
# Use of an optional [[Garbage collection (computer science)|GCgarbage collection]] (GC), that can be disabled), for handling allocations, and is the default.
# [[Manual memory management]] via disabling the GC (<code>-gc none</code>).
# Autofree, which handles most [[Object (computer science)|objects]] via free call insertion, and then the remaining percentage is freed by GC (<code>-autofree</code>).
# [[Memory pool|Arena allocation]] (<code>-prealloc</code>).
 
===Source code translators===
V supports a [[source-to-source compiler]] (transpiler) and can translate C code into V.<ref name="analyticsindiamag">{{cite web |first=Ambika |last=Choudhury |title=Meet V, The New Statically Typed Programming Language Inspired By Go & Rust|url=https://analyticsindiamag.com/meet-v-the-new-statistically-typed-programming-language-inspired-by-go-rust|website=Analytics India Magazine (AIM)|date=9 February 2022|access-date=7 July 2024}}</ref><ref>{{cite web |firstfirst1=Sarah |lastlast1=Schlothauer |website=JAXenter |date=|title=The trendy five: Blazing hot GitHub repos in June 2019| url=https://jaxenter.com/github-trending-june-2019-159622.html|archive-url=https://web.archive.org/web/20200217130047/https://jaxenter.com/github-trending-june-2019-159622.html |archive-date=17 February 2020 |access-date=1 July 2019}}</ref><ref name="nasufi"/>
 
Working translators are also underbeing developmentdeveloped 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|TrexRao|20242021}}
 
== 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">
ax := 1
mut by := 2
by = 3
</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">
ax := 1
{
ax := 3 // error: redefinition of ax
}
ax := 2 // error: redefinition of ax
</syntaxhighlight>
 
=== Structs ===
Struct example:<ref name="Knott"/><ref name="section"/><ref name=":0" />
Struct example:{{Sfn|Independent Laboratory|2020}}
<syntaxhighlight lang="Vv">
struct PointFoo {
x number int
name string
y int
score f32
}
 
// Struct fields can be initialized by name
mut p := Point {
var1 := Foo {
x: 10
number: 21
y: 20
name: "baz"
score: 2.5
}
 
println(p.x) // Struct fields are accessed using a dot
// or by position
// Alternative literal syntax for structs with 3 fields or fewer
pvar2 := PointFoo{1050, 20"taz", 3.14}
assert p.x == 10
</syntaxhighlight>
 
=== Heap structs ===
 
Structs are allocated on the stack by default. To allocate a struct on the heap and get a reference to it, the {{code|&}} prefix can be used:{{Sfn|Independent Laboratory|2020}}
By default, structs are allocated on the stack. When structs are referenced by using the prefix {{code|1=&}} or have the heap attribute, they are allocated on the heap instead:{{sfn|Rao|2021}}<ref name=":0" />
<syntaxhighlight lang="Vv">
struct PointFoo {
x int
y number int
}
 
@[heap]
p := &Point{10, 10}
struct UserBaz {
// References have the same syntax for accessing fields
number f32
println(p.x)
 
// 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 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"/>{{Sfn|Independent<ref Laboratory|2020}}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 aremay declaredrepresent by{{code|none}}. Option types prependingprepend {{code|?}} to the type name: ?Type. Result types use {{code|!?Type}}: !Type.<ref name="Knott" /><ref name="section" />{{Sfn|Tsoukalos|2022}}<ref name=":0" />
<syntaxhighlight lang="V">
fn do_somethingsomething(st string) !string {
if st == "foo" { return "foo" }
return error("fooinvalid")
}
return error("invalid string")
}
 
ax := do_somethingsomething("foo") or { "default" } // ax will be "foo"
by := do_somethingsomething("barbaz") or { "default" } // by will be "default"
cz := do_somethingsomething("barbaz") or { panic("{err}") } // exitsz withwill errorexit "invalidwith string"an and a tracebackerror
 
println(ax)
println(by)
 
</syntaxhighlight>
Line 172:
== Further reading ==
* {{Cite book |date=June 20, 2020 |title=The V Programming Language basic |publisher=Independent Laboratory |asin=B08BKJDRFR |asin-tld=jp |language=ja |ref=none}}
* {{Cite book |lastlast1=Rao |firstfirst1=Navule Pavan Kumar |date=December 10, 2021 |title=Getting Started with V Programming |publisher=[[Packt Publishing]] |asin=B09FKK3JL7 |isbn=978-1839213434 |language=en |oclc=1290492862}}
* {{Cite book |lastlast1=Lyons |firstfirst1=Dakota "Kai" |date=April 13, 2022 |title=Beginning with V Programming |publisher=Independently Published |asin=B09XSZKTJ1 |isbn=979-8801499963 |language=en |ref=none}}
* {{cite journal |lastlast1=Tsoukalos |firstfirst1=Mihalis |date=May 2022 |title=Discover the V language |journalpublisher=[[Linux Format]]| journal=Linux Format Magazine |issue=288 |issn=1470-4234}}
* {{Cite book |last1=Chakraborty |first1=Soubhik |last2=Haldar |first2=Subhomoy |date=December 6, 2023 |title=Randomness Revisited using the V Programming Language |publisher=[[Nova Science Publishers]] |isbn=979-8891133280 |language=en |ref=none |doi=10.52305/CVCN5241 |s2cid=265170755}}
* {{Cite book |authorlast1=Trex, |first1=Nova |date=24 December 2024| |title=V Programming: Building Robust and Efficient Software Systems| |publisher=Wang Press| asin=B0DRJMP1HM | isbn=979-8304813778}}
 
== External links ==
Line 183:
* [https://github.com/vlang/v/blob/master/doc/docs.md Documentation]
* [https://modules.vlang.io/ Modules]
* [https://www.youtube.com/watch?v=pchEsYwA74Q Video: How To Maintain And Iterate With V (Sydney Computing Society)]
* [https://www.youtube.com/watch?v=_dIAcNp9bJs Video: A small presentation of V's features at IBM]
 
[[Category:2019 software]]
Line 198 ⟶ 200:
[[Category:Statically typed programming languages]]
[[Category:Systems programming languages]]
<!-- Hidden categories below -->
[[Category:Articles with example code]]