You can edit almost every page by Creating an account. Otherwise, see the FAQ.

V (programming language)

From EverybodyWiki Bios & Wiki


Script error: No such module "Draft topics". Script error: No such module "AfC topic".

V
A capitalized letter V colored blue
The official V logo
ParadigmsMulti-paradigm: functional, imperative, structured
Designed byAlexander Medvednikov[1]
First appeared25 June 2019; 4 years ago (2019-06-25)[2]
Stable release
0.4.x[3]
Typing disciplineStatic, strong
Implementation languageV
PlatformCross-platform
LicenseMIT
Filename extensions.v, .vsh
Websitevlang.io
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]

Veasel is the official mascot of the V programming language, which is shown above.

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][17][18][19]
    • 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[20]
  • Minimal amount of allocations[21]
  • Built-in serialization without runtime reflection[17]
  • Compiles to native binaries without any dependencies[5]

Fast build[edit]

V is written in V and compiles in less than a second.[22][23][24]

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).[25][26] For example, translating DOOM from C to V, which can take less than a second.[27]

Working translators, under various stages of development, exist for Go, JavaScript, and WASM.[28][29][30][31]

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.

link

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 sanitised 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[32][33] 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.

References[edit]

  1. "V's creator".
  2. "First public release". Retrieved 25 June 2019.
  3. "Latest releases".
  4. James, Ben. "The V Programming Language: Vain Or Virtuous?". Hackaday. Retrieved 23 July 2019.
  5. 5.0 5.1 Umoren, Samuel. "Building a Web Server using Vlang". Section. Retrieved 5 April 2021.
  6. Knott, Simon. "An introduction to V". Retrieved 27 June 2019.
  7. Cheng, Jeremy. "VLang for Automation Scripting". Level Up Coding. Retrieved 27 December 2020.
  8. "Introducing the V Tutorial!". Replit. Retrieved 4 January 2021.
  9. 9.0 9.1 "A small presentation of V's features at IBM". YouTube. Retrieved 6 March 2023.
  10. "Why "V"?". Vlang FAQ.
  11. "V is written in?". Vlang FAQ.
  12. "Contributors" – via GitHub.
  13. Jonah, Victor. "What is Vlang? An introduction". LogRocket. Retrieved 25 February 2021.
  14. Laboratory, Independent (2020). The V Programming Language basic (in 日本語). ASIN B08BKJDRFR. Search this book on
  15. Nasufi, Erdet. "An introduction to V". DebConf. Retrieved 24 July 2022.
  16. "On the benefits of using C as a language backend". GitHub. Retrieved 4 January 2021.
  17. 17.0 17.1 Shóstak, Vic. "The V programming language". DEV. Retrieved 8 November 2021.
  18. "C is how old now? - Learning the V programming language". l-m. Retrieved 10 April 2022.
  19. "V language: simple like Go, small binary like Rust". TechRacho (in 日本語). Retrieved 3 March 2021.
  20. Pandey, Arpan. "Building a Web Server using Vlang". Hackers Reboot. Retrieved 16 April 2022.
  21. "The V programming language is now open source". Packt Hub. Retrieved 24 June 2019.
  22. Oliveira, Marcos. "V , the programming language that is making a lot of success". Terminal Root. Retrieved 18 January 2022.
  23. "Building V from source in 0.3 seconds". YouTube. Retrieved 28 March 2021.
  24. Rao, Navule Pavan Kumar (2021). Getting Started with V Programming. Packt Publishing. ISBN 1839213434. Search this book on
  25. "C2V". GitHub.
  26. "Translating C to V". GitHub.
  27. "C2V Demo: Translating DOOM from C to V". YouTube. Retrieved 23 June 2022.
  28. "Go2V". GitHub.
  29. "Convert Go to V with go2v". Zenn (in 日本語). Retrieved 26 January 2023.
  30. "JavaScript and WASM backends". Vlang site.
  31. "The V WebAssembly Compiler Backend". l-m. Retrieved 26 February 2023.
  32. "Vinix: a new OS/kernel written in V". YouTube. Retrieved 24 November 2021.
  33. "Vinix OS". GitHub.

Further Reading[edit]

Books & Magazines[edit]
Translated Documentation[edit]

External links[edit]


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.