Top Qs
Timeline
Chat
Perspective

V (programming language)

General-purpose programming language From Wikipedia, the free encyclopedia

V (programming language)
Remove ads

V, also known as vlang, is a statically typed, compiled programming language created by Alexander Medvednikov in early 2019.[4] It was inspired by Go, and other programming languages including Oberon, Swift, and Rust.[5][6][7] It is free and open-source software released under the MIT License, and currently in beta.[8]

Quick Facts Paradigms, Designed by ...

The goals of V include ease of use, readability, and maintainability.[9][10][11]

Remove ads

History

The new language was created as a result of frustration with existing languages being used for personal projects.[12] 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.[6] On public release, the compiler was written in V, and could compile itself.[4][12] Key design goals in creating V were being easy to learn and use, higher readability, fast compiling, increased safety, efficient development, cross-platform usability, improved C interoperability, better error handling, modern features, and more maintainable software.[13][14][10][15]

V is released and developed through GitHub,[16][6] and maintained by developers and contributors internationally.[4] It is among the languages that have been listed on the TIOBE index.[17]

Thumb
Veasel is the official mascot of the V programming language[18]
Remove ads

Features

Summarize
Perspective

Safety

V has policies to facilitate memory-safety, speed, and secure code,[19][20][6] including various default features for greater program safety.[7][19][12] It employs bounds checking, to guard against out of bounds use of variables. Option/result types are used, where the option data type (?) can be represented by none (among possible choices) and the result type (!) can handle any returned errors. To ensure greater safety, error checking is mandatory. By default, the following are immutable: variables, structs, and function arguments. This includes string values are immutable, so elements cannot be mutated. Other protections, which are the default for the language, are: no use of undefined values, variable shadowing, null pointers (unless marked as unsafe), or global variables (unless enabled via flag).

Performance

V uses value types and string buffers to reduce memory allocations.[21][22][19] The language can be compiled to human-readable C,[4] and in terms of execution and compilation, it's considered to be as performant.[19][12]

Memory management

V supports 4 memory management options:[23][6][12]

  1. Use of an optional garbage collection (GC), that can be disabled, for handling allocations, and is the default.
  2. Manual memory management via disabling the GC (-gc none).
  3. Autofree, which handles most objects via free call insertion, and then the remaining percentage is freed by GC (-autofree).
  4. Arena allocation (-prealloc).

Source code translators

V supports a source-to-source compiler (transpiler) and can translate C code into V.[24][25][10]

Working translators are also being developed for Go, JavaScript, and WebAssembly.[26][27][4]

Remove ads

Syntax

Summarize
Perspective

Hello world

The "Hello, World!" program in V:[19]

fn main() {
	println("Hello, World!")
}

Variables

Variables are immutable by default and are defined using := and a value. Use the mut reserved word (keyword) to make them mutable. Mutable variables can be assigned to using =:[28]

a := 1
mut b := 2
b = 3

Redeclaring a variable, whether in an inner scope or in the same scope, is not allowed:[28]

a := 1
{
    a := 3 // error: redefinition of a
}
a := 2 // error: redefinition of a

Structs

Struct example:[13]

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 by default. To allocate a struct on the heap and get a reference to it, the & prefix can be used:[13]

struct Point {
	x int
	y int
}

p := &Point{10, 10}
// References have the same syntax for accessing fields
println(p.x)

Methods

Methods in V are functions defined with a 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.

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. For example:[9][13]

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.[9][7][23]

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)
Remove ads

See also

References

Further reading

Loading related searches...

Wikiwand - on

Seamless Wikipedia browsing. On steroids.

Remove ads