Jump to content

V (programming language)

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Wukuendo (talk | contribs) at 15:29, 1 November 2023 (Removed advertisement-like language (see talk as reference), removed incorrect open-source date (refer to first appeared and GitHub release)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

  • Comment: As the previous comment said, this article should use more secondary sources, as primary sources are only suitable for small details that secondary sources do not cover. YouTube videos are not deem reliable as sources and should be removed as well. A portion of the content here seem to be taken from its own website, please make sure to rewrite or remove all contents marked red here. Tutwakhamoe (talk) 14:16, 13 July 2023 (UTC)
  • Comment: There was a page deleted in 2008 with the same title, but referring to a different language, one more "academic". The second deletion discussion was on a page similar in content to what's in this draft, and the third being a much worse version. This version is much better because it includes four books, meaning it's likely suitable for a page. I would suggest though that the primary sources to GitHub and vlang.io be removed in favor of secondary sources. It's a promising draft. SWinxy (talk) 05:32, 31 May 2023 (UTC)

V
A capitalized letter V colored blue
The official V logo
ParadigmsMulti-paradigm: functional, imperative, structured, concurrent
Designed byAlexander Medvednikov[1]
First appeared20 June 2019; 5 years ago (2019-06-20)[2]
Stable release
0.4.2[3] / September 30, 2023; 13 months ago (2023-09-30)
Typing disciplinestatic, strong, infered
Memory managementTracing garbage collection
Implementation languageV
Platformx86-64
OSLinux, macOS, Windows, FreeBSD, OpenBSD, NetBSD, DragonflyBSD, Solaris
LicenseMIT
Filename extensions.v, .vsh
Websitevlang.io
Influenced by
Go, Kotlin, Oberon, Python, Rust, Swift

V (/ˈviː/, as in the letter v), also known as vlang, is a statically typed compiled programming language created by Alexander Medvednikov in early 2019.[4] It was mostly inspired by the Go programming language, but was also influenced by C, Rust, and Oberon-2.[5][6][7] It is free and open-source software, in active development at GitHub[8], and released under the MIT license.

The foremost goal of V is to be easy to use,[9][10][11] and at the same time, to enforce a safe coding style through elimination of ambiguity. For example, variable shadowing is not allowed;[12][13] declaring a variable with a name that is already used in a parent scope will cause a compilation error.

History

The new language was created as a result of frustration with existing languages being used for personal projects.[14] Originally the language was intended for personal use, but after it was mentioned publicly and gained interest, it was decided to make it public. Initially, the name was the same as a product known as Volt.[6] 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".[11] Upon public release, the compiler was written in V, and could compile itself.[15] Along with Alexander Medvednikov, the creator, its community has a large number of contributors from around the world[16][15] 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.[11][17][18][19]

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

Features

Safety

  • Usage of bounds checking
  • Usage of Option/Result
  • Mandatory checking of errors
  • No usage of values that are undefined
  • No shadowing of variables
  • No usage of null (unless in unsafe code)
  • No usage of global variables (unless enabled via flag)
  • Variables are immutable by default
  • Structs are immutable by default
  • Function args are immutable by default
  • Sum types can be used
  • Generics can be used

Performance

  • Fast like C (human-readable C compiled by backend)[20][21][22][23]
    • Added features for safety, if desired, can be disabled or bypassed for greater performance
  • C interop[24][25][26][27]
  • Allocations kept to minimal[28]
  • Serialization with no runtime reflection[20]
  • Native binaries compiled with no dependencies[7][29]

Fast build

V is written in itself and can compile within a second.[30][15][23]

Memory management options

  • Allocations handled by an optional GC, that is the default, which can be disabled
  • Manual memory management (-gc none)
  • Autofree (-autofree), handles most objects via free call insertion
    • Remaining percentage freed by GC
  • Arena allocation with (-prealloc)

Source code translators (code transpilation)

C source code conversion: V (via module) can translate an entire C project into V code.[12][31][19]

Working translators (under various stages of development) exist for Go, JavaScript, and WASM.[32][33]

Syntax

Hello world

fn main() {
	println('hello world')
}

Structs

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

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

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 is_registered method has a receiver of type User named u. The convention is not to use receiver names like self or this, but preferably a short name.

struct User {
	age int
}

fn (u User) is_registered() bool {
	return u.age > 16
}

user := User{
	age: 10
}
println(user.is_registered()) // "false"
user2 := User{
	age: 20
}
println(user2.is_registered()) // "true"

Error handling

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

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

V has a built-in ORM (object-relational mapping) which supports SQLite, MySQL and Postgres. Aims to also support MS SQL and Oracle.

V's ORM provides a number of benefits:

  • One syntax for all SQL dialects
  • Queries are constructed using V's syntax
  • Safety (queries are sanitized to prevent SQL injection)
  • Compile time checks (prevents typos which can only be caught during runtime)
  • Readability and simplicity (need not manually parse results of a query or the 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
	}!
}

References

  1. ^ "Creator of V". GitHub.
  2. ^ "First public release". GitHub. 20 June 2019.
  3. ^ "Latest releases". GitHub.
  4. ^ "Getting Started with V Programming". subscription.packtpub.com. Retrieved 1 November 2023.
  5. ^ Lewkowicz, Jakub (25 June 2019). "SD Times news digest: V language now open sourced". SD Times. Retrieved 25 June 2019.
  6. ^ a b James, Ben (23 July 2019). "The V Programming Language: Vain Or Virtuous?". Hackaday. Retrieved 23 July 2019.
  7. ^ a b Umoren, Samuel. "Building a Web Server using Vlang". Section. Retrieved 5 April 2021.
  8. ^ "GitHub Programming Languages (repository rankings)" – via OSS.
  9. ^ Knott, Simon (27 June 2019). "An introduction to V". Retrieved 27 June 2019.
  10. ^ Cheng, Jeremy. "VLang for Automation Scripting". Level Up Coding. Retrieved 27 December 2020.
  11. ^ a b c Jonah, Victor (25 February 2021). "What is Vlang? An introduction". LogRocket. Retrieved 25 February 2021.
  12. ^ a b "Introducing the V Tutorial!". Replit. Retrieved 4 January 2021.
  13. ^ Galuh, Rosa (8 August 2022). "A Brief Introduction to the V Language". MUO. Retrieved 8 August 2022.
  14. ^ "How To Maintain And Iterate With V - SYNCS 2023 (Sydney Computing Society at the University of Sydney)". YouTube. Retrieved 18 October 2023.
  15. ^ a b c Rao 2021.
  16. ^ "Contributors" – via OSS.
  17. ^ Independent Laboratory 2020.
  18. ^ Lyons 2022.
  19. ^ a b Nasufi, Erdet. "An introduction to V - the vlang". DebConf. Retrieved 24 July 2022.
  20. ^ a b Shóstak, Vic (15 January 2020). "The V programming language". DEV. Retrieved 8 November 2021.
  21. ^ "C is how old now? - Learning the V programming language". l-m. 10 April 2022. Retrieved 10 April 2022.
  22. ^ "V language: simple like Go, small binary like Rust". TechRacho. Retrieved 3 March 2021.
  23. ^ a b "Want to learn and master V?". Exercism. Retrieved 20 January 2023.
  24. ^ Choudhury, Ambika. "Meet V, The New Statically Typed Programming Language Inspired By Go & Rust". Analytics India Magazine (AIM). Retrieved 3 July 2019.
  25. ^ "How to install the V programming Language on Ubuntu 20.04 / Debian 10?". osradar. 24 June 2020. Retrieved 24 June 2020.
  26. ^ Grabowski, Hank. "Fighting Bloat With the V Language". nequalsonelifestyle. Retrieved 25 June 2021.
  27. ^ Walker, Jeremy. "Mechanical March". Exercism. Retrieved 1 March 2023.
  28. ^ "The V programming language is now open source". Packt Hub. 24 June 2019. Retrieved 24 June 2019.
  29. ^ Dr. Rangarajan Krishnamoorthy. "Building V Language DLL". rangakrish. Retrieved 2 April 2023.
  30. ^ Oliveira, Marcos (18 January 2022). "V , the programming language that is making a lot of success". Terminal Root. Retrieved 18 January 2022.
  31. ^ Schlothauer, Sarah. "The trendy five: Blazing hot GitHub repos in June 2019". JAXenter. Archived from the original on 17 February 2020. Retrieved 1 July 2019.
  32. ^ "Convert Go to V with go2v". Zenn. 26 January 2023. Retrieved 26 January 2023.
  33. ^ "The V WebAssembly Compiler Backend". l-m. 26 February 2023. Retrieved 26 February 2023.

Further Reading

Category:Programming languages Category:Cross-platform free software Category:Cross-platform software Category:Free compilers and interpreters Category:Statically typed programming languages Category:Systems programming languages Category:Multi-paradigm programming languages Category:Procedural programming languages Category:Concurrent programming languages Category:Source-to-source compilers Category:Software using the MIT license Category:Programming languages created in 2019 Category:2019 software