V, also known as vlang, is a
statically typed
In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a ''type'' (for example, integer, floating point, string) to every '' term'' (a word, phrase, or other set of symbols). Usu ...
,
compiled programming language
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
created by Alexander Medvednikov in early 2019. It was inspired by the language
Go, and other influences including
Oberon
Oberon () is a king of the fairy, fairies in Middle Ages, medieval and Renaissance literature. He is best known as a character in William Shakespeare's play ''A Midsummer Night's Dream'', in which he is King of the Fairies and spouse of Titania ...
,
Swift
Swift or SWIFT most commonly refers to:
* SWIFT, an international organization facilitating transactions between banks
** SWIFT code
* Swift (programming language)
* Swift (bird), a family of birds
It may also refer to:
Organizations
* SWIF ...
, and
Rust
Rust is an iron oxide, a usually reddish-brown oxide formed by the reaction of iron and oxygen in the catalytic presence of water or air moisture. Rust consists of hydrous iron(III) oxides (Fe2O3·nH2O) and iron(III) oxide-hydroxide (FeO(OH) ...
.
It is
free and open-source software
Free and open-source software (FOSS) is software available under a license that grants users the right to use, modify, and distribute the software modified or not to everyone free of charge. FOSS is an inclusive umbrella term encompassing free ...
released under the
MIT License
The MIT License is a permissive software license originating at the Massachusetts Institute of Technology (MIT) in the late 1980s. As a permissive license, it puts very few restrictions on reuse and therefore has high license compatibility.
Unl ...
, and currently in
beta
Beta (, ; uppercase , lowercase , or cursive ; or ) is the second letter of the Greek alphabet. In the system of Greek numerals, it has a value of 2. In Ancient Greek, beta represented the voiced bilabial plosive . In Modern Greek, it represe ...
.
The goals of V include ease of use,
readability, and
maintainability
Maintainability is the ease of maintaining or providing maintenance for a functioning product or service. Depending on the field, it can have slightly different meanings.
Usage in different fields Engineering
In engineering, maintainability ...
.
History
According to various sources, the new language was created as a result of frustration with existing languages being used for personal projects.
The language was originally intended for personal use, but after it was mentioned publicly and gained interest, it was decided to make it public. V was initially created to develop a desktop messaging client named Volt.
On public release, the compiler was written in V, and could
compile itself. Key design goals in creating V were being easy to learn and use, higher readability, fast compiling, increased safety, efficient development,
cross-platform
Within computing, cross-platform software (also called multi-platform software, platform-agnostic software, or platform-independent software) is computer software that is designed to work in several Computing platform, computing platforms. Some ...
usability, improved
C interoperability
Interoperability is a characteristic of a product or system to work with other products or systems. While the term was initially defined for information technology or systems engineering services to allow for information exchange, a broader de ...
, better
error handling, modern features, and more maintainable software.
V is released and developed through
GitHub
GitHub () is a Proprietary software, proprietary developer platform that allows developers to create, store, manage, and share their code. It uses Git to provide distributed version control and GitHub itself provides access control, bug trackin ...
,
and maintained by developers and contributors internationally. It is among the languages that have been listed on the
TIOBE index.
Features
Safety
V has policies to facilitate memory-safety, speed, and secure code,
including various default features for greater program safety.
It employs
bounds checking
In computer programming, bounds checking is any method of detecting whether a variable is within some bounds before it is used. It is usually used to ensure that a number fits into a given type (range checking), or that a variable being used as ...
, to guard against out of bounds use of
variables. Option/result
types
Type may refer to:
Science and technology Computing
* Typing, producing text via a keyboard, typewriter, etc.
* Data type, collection of values used for computations.
* File type
* TYPE (DOS command), a command to display contents of a file.
* Ty ...
are used, where the option
data type
In computer science and computer programming, a data type (or simply type) is a collection or grouping of data values, usually specified by a set of possible values, a set of allowed operations on these values, and/or a representation of these ...
() can be represented by (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
An argument is a series of sentences, statements, or propositions some of which are called premises and one is the conclusion. The purpose of an argument is to give reasons for one's conclusion via justification, explanation, and/or persua ...
. 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
In computer programming, variable shadowing occurs when a variable declared within a certain scope (decision block, method, or inner class) has the same name as a variable declared in an outer scope. At the level of identifiers (names, rather th ...
,
null pointer
In computing, a null pointer (sometimes shortened to nullptr or null) or null reference is a value saved for indicating that the Pointer (computer programming), pointer or reference (computer science), reference does not refer to a valid Object (c ...
s (unless marked as unsafe), or
global variables (unless enabled via flag).
Performance
V uses
value types and string buffers to reduce memory allocations.
The language can be compiled to human-readable C, and in terms of execution and compilation, it's considered to be as performant.
Memory management
V supports 4 memory management options:
# Use of an optional
garbage collection (GC), that can be disabled, for handling allocations, and is the default.
#
Manual memory management via disabling the GC (
-gc none
).
# Autofree, which handles most
objects via free call insertion, and then the remaining percentage is freed by GC (
-autofree
).
#
Arena allocation (
-prealloc
).
Source code translators
V supports a
source-to-source compiler
A source-to-source translator, source-to-source compiler (S2S compiler), transcompiler, or transpiler is a type of translator that takes the source code of a program written in a programming language as its input and produces an equivalent so ...
(transpiler) and can translate C code into V.
Working translators are also being developed for Go,
JavaScript
JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have ...
, and
WebAssembly
WebAssembly (Wasm) defines a portable binary-code format and a corresponding text format for executable programs as well as software interfaces for facilitating communication between such programs and their host environment.
The main goal of ...
.
Syntax
Hello world
The
"Hello, World!" program
A "Hello, World!" program is usually a simple computer program that emits (or displays) to the screen (often the Console application, console) a message similar to "Hello, World!". A small piece of code in most general-purpose programming languag ...
in V:
fn main()
Variables
Variables are immutable by default and are defined using and a value. Use the
reserved word
In a programming language, a reserved word (sometimes known as a reserved identifier) is a word that cannot be used by a programmer as an identifier, such as the name of a variable, function, or label – it is "reserved from use". In brief, an '' ...
(keyword) to make them mutable. Mutable variables can be assigned to using :
a := 1
mut b := 2
b = 3
Redeclaring a variable, whether in an inner scope or in the same scope, is not allowed:
a := 1
a := 2 // error: redefinition of a
Structs
Struct example:
struct Point
mut p := Point
println(p.x) // Struct fields are accessed using a dot
// Alternative literal syntax for structs with 3 fields or fewer
p = Point
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:
struct Point
p := &Point
// References have the same syntax for accessing fields
println(p.x)
Methods
Methods in V are functions defined with a receiver
argument
An argument is a series of sentences, statements, or propositions some of which are called premises and one is the conclusion. The purpose of an argument is to give reasons for one's conclusion via justification, explanation, and/or persu ...
. 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:
struct User
fn (u User) is_registered() bool
user := User
println(user.is_registered()) // "false"
user2 := User
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.
fn do_something(s string) !string
a := do_something("foo") or // a will be "foo"
b := do_something("bar") or // b will be "default"
c := do_something("bar") or // exits with error "invalid string" and a traceback
println(a)
println(b)
See also
*
Comparison of programming languages
*
History of programming languages
*
List of programming languages
*
List of programming languages by type
References
Further reading
*
*
*
*
*
*
External links
*
* {{GitHub, vlang, Vlang
DocumentationModules
2019 software
Programming languages created in 2019
Programming languages
Multi-paradigm programming languages
Procedural programming languages
Functional languages
Concurrent programming languages
Cross-platform software
Cross-platform free software
Free and open source compilers
Software using the MIT license
Source-to-source compilers
Statically typed programming languages
Systems programming languages
Articles with example code