V (programming language)
Script error: No such module "Draft topics". Script error: No such module "AfC topic".
The official V logo | |
Paradigms | Multi-paradigm: functional, imperative, structured |
---|---|
Designed by | Alexander Medvednikov[1] |
First appeared | 25 June 2019[2] |
Stable release | 0.4.x[3]
|
Typing discipline | Static, strong |
Implementation language | V |
Platform | Cross-platform |
License | MIT |
Filename extensions | .v , .vsh |
Website | vlang |
Influenced by | |
Search V (programming language) on Amazon.
V, or Vlang, is a general-purpose programming language designed by Alexander Medvednikov. It is mostly inspired by the Go programming language but was also influenced by C, Rust, and Oberon-2.[4][5] The foremost goal of V is to be easy to use[6][7], and at the same time, to enforce a safe coding style through elimination of ambiguity. For example, variable shadowing is not allowed[8]; declaring a variable with a name that is already used in a parent scope will cause a compilation error.
History[edit]
The language came about because of frustration with existing programming languages that were being used for personal projects. Originally the new language was intended for personal use, but after it was mentioned publicly and gained interest, it was decided to make the language public.[9] Initially, it had the same name as a product known as Volt. The V language was created in order to develop it, along with other software applications such as Ved (also called Vid). As the extension in use was already ".v", to not mess up the git history, it was decided to name it "V".[10] Upon public release, the compiler was written in V, and could compile itself.[11] Along with Alexander Medvednikov, the creator, its community has a large number of contributors[12] who have helped with continually developing and adding to the compiler, language, and modules. Key design goals behind the creation of V: easier to learn and use, higher readability, fast compilation, increased safety, efficient development, cross-platform usability, improved C interop, better error handling, modern features, and more maintainable software.[13][14][9][15]
Features[edit]
Safety[edit]
- Bounds checking
- No undefined values
- No variable shadowing
- Immutable variables by default
- Immutable structs by default
- Option/Result and mandatory error checks
- Sum types
- Generics
- Immutable function args by default, mutable args have to be marked on call
- No null (allowed in unsafe code)
- No undefined behavior (wip, some overflowing can still result in UB)
- No global variables (can be enabled for low level apps like kernels via a flag)
Performance[edit]
- Fast like C (V's main backend compiles to human readable C), with equivalent code.[16][8][17][18]
- V does introduce some overhead for safety (such as array bounds checking, GC free), but these features can be disabled or bypassed when performance is more important.
- C interop without any costs[19][15]
- Minimal amount of allocations[20]
- Built-in serialization without runtime reflection[8]
- Compiles to native binaries without any dependencies[5]
Fast build[edit]
V is written in V and compiles in less than a second.[21][22][23]
Flexible memory management[edit]
V avoids doing unnecessary allocations by using value types, string buffers, and promoting a simple abstraction-free code style.
Presently, allocations are handled by an optional GC, as the default (which can be disabled). Autofree, when production ready, may become the default.
Autofree can be enabled with -autofree. It takes care of most objects; the compiler inserts necessary free calls automatically during compilation. The remaining percentage of objects are freed via GC.
For developers willing to have more low level control, memory can be managed manually with -gc none.
Arena allocation is available via v -prealloc.
Source code translators (code transpilation)[edit]
V can translate entire C projects (via modules).[24][25] For example, translating DOOM from C to V, which can take less than a second.[26]
Working translators, under various stages of development, exist for Go, JavaScript, and WASM.[27][28][29][30]
Syntax[edit]
Hello world[edit]
fn main() {
println('hello world')
}
Structs[edit]
struct Point {
x int
y int
}
mut p := Point{
x: 10
y: 20
}
println(p.x) // Struct fields are accessed using a dot
// Alternative literal syntax for structs with 3 fields or fewer
p = Point{10, 20}
assert p.x == 10
Heap structs[edit]
Structs are allocated on the stack. To allocate a struct on the heap and get a reference to it, use the & prefix:
struct Point {
x int
y int
}
p := &Point{10, 10}
// References have the same syntax for accessing fields
println(p.x)
Methods[edit]
V doesn't have classes, but you can define methods to types. A method is a function with a special receiver argument. The receiver appears in its own argument list between the fn keyword and the method name. Methods must be in the same module as the receiver type.
In this example, the can_register method has a receiver of type User named u. The convention is not to use receiver names like self or this, but a short, preferably one letter long, name.
struct User {
age int
}
fn (u User) can_register() bool {
return u.age > 16
}
user := User{
age: 10
}
println(user.can_register()) // "false"
user2 := User{
age: 20
}
println(user2.can_register()) // "true"
Error handling[edit]
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 ? to the type name: ?Type. Result types use !: !Type.
fn do_something(s string) !string {
if s == "foo" {
return "foo"
}
return error("invalid string")
}
a := do_something("foo") or { "default" } // a will be "foo"
b := do_something("bar") or { "default" } // b will be "default"
c := do_something("bar") or { panic("{err}") } // exits with error 'invalid string' and a traceback
println(a)
println(b)
Vweb[edit]
import vweb
struct App {
vweb.Context
}
fn main() {
vweb.run(&App{}, 8080)
}
or
import vweb
struct App {
vweb.Context
}
fn main() {
vweb.run_at(new_app(), vweb.RunParams{
host: "localhost"
port: 8099
family: .ip
}) or { panic(err) }
}
ORM[edit]
V has a built-in ORM (object-relational mapping) which supports SQLite, MySQL and Postgres, but soon it will support MS SQL and Oracle.
V's ORM provides a number of benefits:
- One syntax for all SQL dialects. (Migrating between databases becomes much easier.)
- Queries are constructed using V's syntax. (There's no need to learn another syntax.)
- Safety. (All queries are automatically sanitized to prevent SQL injection.)
- Compile time checks. (This prevents typos which can only be caught during runtime.)
- Readability and simplicity. (You don't need to manually parse the results of a query and then manually construct objects from the parsed results.)
import pg
struct Member {
id string [default: "gen_random_uuid()"; primary; sql_type: "uuid"]
name string
created_at string [default: "CURRENT_TIMESTAMP"; sql_type: "TIMESTAMP"]
}
fn main() {
db := pg.connect(pg.Config{
host: "localhost"
port: 5432
user: "user"
password: "password"
dbname: "dbname"
}) or {
println(err)
return
}
defer {
db.close()
}
sql db {
create table Member
}
new_member := Member{
name: "John Doe"
}
sql db {
insert new_member into Member
}
selected_member := sql db {
select from Member where name == "John Doe" limit 1
}
sql db {
update Member set name = "Hitalo" where id == selected_member.id
}
}
Other features[edit]
The Vinix Operating System[31][32] is a demonstration of V's ability to make a usable OS that can run on real hardware, not just emulators or virtual machines. It targets modern 64-bit architectures, CPU features, and multi-core computing. Aims to have source-level compatibility with Linux, allowing the porting over of programs.
Further Reading[edit]
Books & Magazines[edit]
- Laboratory, Independent (June 20, 2020). The V Programming Language basic (in 日本語). ASIN B08BKJDRFR. Search this book on
- Rao, Navule (December 10, 2021). Getting Started with V Programming. Packt Publishing. ISBN 1839213434. Search this book on
- Lyons, Dakota "Kai" (April 13, 2022). Beginning with V Programming. Independently Published. ISBN 8801499963 Check
|isbn=
value: checksum (help). Search this book on - Chakraborty, Soubhik; Haldar, Subhomoy (December 6, 2023). Randomness Revisited using the V Programming Language. Nova Science Publishers. doi:10.52305/CVCN5241. ISBN 8891133280. Search this book on
- Tsoukalos, Mihalis (May 2022). "Discover the V language". Linux Format Magazine (288). ISSN 1470-4234.
Translated Documentation[edit]
- "V language learning notes". GitBook (in 中文).
- "V documentation". Qiita (in 日本語).
External links[edit]
V Language Review (Videos)[edit]
- An introduction to V - the vlang (2022)
- Is V Lang Better Than Go And Rust? Let's Find Out (2023)
- V language - First Impression (2024)
References[edit]
- ↑ "V's creator".
- ↑ "First public release". Retrieved 25 June 2019.
- ↑ "Latest releases".
- ↑ James, Ben. "The V Programming Language: Vain Or Virtuous?". Hackaday. Retrieved 23 July 2019.
- ↑ 5.0 5.1 Umoren, Samuel. "Building a Web Server using Vlang". Section (Wayback Machine). Retrieved 5 April 2021. Cite error: Invalid
<ref>
tag; name "auto2" defined multiple times with different content - ↑ Knott, Simon. "An introduction to V". Retrieved 27 June 2019.
- ↑ Oliveira, Jader. "VLang for Automation Scripting". LinkedIn. Retrieved 4 June 2021.
- ↑ 8.0 8.1 8.2 Shóstak, Vic. "The V programming language". DEV. Retrieved 8 November 2021.
- ↑ 9.0 9.1 "A small presentation of V's features at IBM". YouTube. Retrieved 6 March 2023.
- ↑ "Why "V"?". Vlang FAQ.
- ↑ "V is written in?". Vlang FAQ.
- ↑ "Contributors" – via GitHub.
- ↑ Jonah, Victor. "What is Vlang? An introduction". LogRocket. Retrieved 25 February 2021.
- ↑ Laboratory, Independent (2020). The V Programming Language basic (in 日本語). ASIN B08BKJDRFR. Search this book on
- ↑ 15.0 15.1 Nasufi, Erdet. "An introduction to V". DebConf. Retrieved 24 July 2022.
- ↑ "On the benefits of using C as a language backend". GitHub. Retrieved 4 January 2021.
- ↑ "C is how old now? - Learning the V programming language". l-m. Retrieved 10 April 2022.
- ↑ "V language: simple like Go, small binary like Rust". TechRacho (in 日本語). Retrieved 3 March 2021.
- ↑ Abbas, Hazem. "Get Started with V". MEDevel. Retrieved 4 August 2024.
- ↑ "The V programming language is now open source". Packt Hub. Retrieved 24 June 2019.
- ↑ Oliveira, Marcos. "V , the programming language that is making a lot of success". Terminal Root. Retrieved 18 January 2022.
- ↑ "Building V from source in 0.3 seconds". YouTube. Retrieved 28 March 2021.
- ↑ Rao, Navule Pavan Kumar (2021). Getting Started with V Programming. Packt Publishing. ISBN 1839213434. Search this book on
- ↑ "C2V". GitHub.
- ↑ "Translating C to V". GitHub.
- ↑ "C2V Demo: Translating DOOM from C to V". YouTube. Retrieved 23 June 2022.
- ↑ "Go2V". GitHub.
- ↑ "Convert Go to V with go2v". Zenn (in 日本語). Retrieved 26 January 2023.
- ↑ "JavaScript and WASM backends". Vlang site.
- ↑ "The V WebAssembly Compiler Backend". l-m. Retrieved 26 February 2023.
- ↑ "Vinix: a new OS/kernel written in V". YouTube. Retrieved 24 November 2021.
- ↑ "Vinix OS". GitHub.
This article "V (programming language)" is from Wikipedia. The list of its authors can be seen in its historical and/or the page Edithistory:V (programming language). Articles copied from Draft Namespace on Wikipedia could be seen on the Draft Namespace of Wikipedia and not main one.
- CS1 日本語-language sources (ja)
- CS1 中文-language sources (zh)
- Programming languages
- Cross-platform free software
- Cross-platform software
- Free compilers and interpreters
- Programming languages created in 2019
- Statically typed programming languages
- Systems programming languages
- Multi-paradigm programming languages
- Procedural programming languages
- Concurrent programming languages
- Functional languages
- Software using the MIT license