The goals of V include ease of use, readability, and maintainability.[9][10]
History
According to one of the developers, the new language was created as a result of frustration with existing languages being used for personal projects.[11] 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 in order to develop a desktop messaging client known as Volt.[6] Upon public release, the compiler was written in V, and could compile itself.[4] Key design goals behind the creation of V were being easy to learn and use, higher readability, fast compilation, increased safety, efficient development, cross-platform usability, improved C interoperability, better error handling, modern features, and more maintainable software.[12][13][10][14]
V is released and developed through GitHub[15][6] and maintained by developers and contributors from the community.[4]
V has policies to facilitate memory-safety, speed, and secure code.[7][17] The language has various default features for greater program safety.[7][17][6][9] It employs bounds checking, to guard against out of bounds usage of variables. Option/result types are used, where the option type (?) can be represented by none (among possible choices) and the result type (!) can handle any returned errors. To ensure greater safety, the checking of errors are mandatory in V. By default, among the following are immutable: variables, structs, and function arguments. This includes string values are immutable, so elements can not be mutated. Other protections, which are the default for the language, are: no usage of undefined values, no shadowing of variables, no usage of null (unless code marked as unsafe), and no usage of global variables (unless enabled via flag).
Performance
V uses value types and string buffers to reduce memory allocations.[18][19][17] The language can be compiled to human-readable C [4][20] and is considered to be as performant.[17]
Memory management
The language's 4 supported options for memory management are the following:[21][6][22][20]
Use of an optional GC (that can be disabled) for handling allocations, and is the default.
Variables are immutable by default and are defined using := and a value. Use the mut keyword to make them mutable. Mutable variables can be assigned to using =:[27]
a:=1mutb:=2b=3
Redeclaring a variable, whether in an inner scope or in the same scope, is not allowed:[27]
a:=1{a:=3// error: redefinition of a}a:=2// error: redefinition of a
structPoint{xintyint}mutp:=Point{x:10y:20}println(p.x)// Struct fields are accessed using a dot// Alternative literal syntax for structs with 3 fields or fewerp=Point{10,20}assertp.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:[12]
structPoint{xintyint}p:=&Point{10,10}// References have the same syntax for accessing fieldsprintln(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][12]
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][21]
fndo_something(sstring)!string{ifs=="foo"{return"foo"}returnerror("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 tracebackprintln(a)println(b)