V (programming language)

The official V logo

Paradigms| Multi-paradigm: functional, imperative, structured
Designed by| Alexander Medvednikov
First appeared| 22 June 2019; 6 years ago (2019-06-22)[1]
Stable release|

0.3.x[2]

Typing discipline| Static, strong
Implementation language| V
Platform| Cross-platform
License| MIT
Filename extensions| `.v`, `.vsh`
Website| vlang.io
Influenced by

* Go
* C
* Rust
* Oberon-2

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.[3][4] The foremost goal of V is to be easy to use[5][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.

## Contents

* 1 Features
* 1.1 Safety
* 1.2 Performance
* 1.3 Fast build
* 1.4 Flexible memory management
* 1.5 Source code translators (code transpilation)
* 2 Syntax
* 2.1 Hello world
* 2.2 Structs
* 2.3 Heap structs
* 2.4 Methods
* 2.5 Error handling
* 2.6 Vweb
* 2.7 ORM
* 3 References
* 4 Further Reading
* 5 External links

## Features

### Safety

* 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

* Fast like C (V's main backend compiles to human readable C), with equivalent code.[9][10][11][12]

V does introduce some overhead for safety (such as array bounds checking, GC free), but these features can be disabled/bypassed when performance is more important.

* C interop[13]
* Minimal amount of allocations[14]
* Built-in serialization without runtime reflection[15]
* Compiles to native binaries without any dependencies[16]

### Fast build

V is written in V and compiles in less than a second.[17][18][19]

### Flexible memory management

V avoids doing unnecessary allocations by using value types, string buffers, and promoting a simple abstraction-free code style.

Presently, allocations are handled by GC until V's autofree engine is production ready.

Autofree can be enabled with -autofree. It takes care of most objects (~90-100%): the compiler inserts necessary free calls automatically during compilation. Remaining small 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)

C source code conversion: V (via module) can translate an entire C project into V code.[20][21][22]

Working translators, under various stages of development, exist for Go, JavaScript, and WASM.[23][24][25][26]

## 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 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

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)

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, 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

## References

1. ↑ "First release". https://github.com/vlang/v/releases?page=15.
2. ↑ "Latest releases". https://github.com/vlang/v/releases.
3. ↑ James, Ben. "The V Programming Language: Vain Or Virtuous?". https://hackaday.com/2019/07/23/the-v-programming-language-vain-or-virtuous/.
4. ↑ Umoren, Samuel. "Building a Web Server using Vlang". https://www.section.io/engineering-education/building-web-server-with-vlang/.
5. ↑ Knott, Simon. "An introduction to V". https://simonknott.de/articles/vlang/.
6. ↑ Jonah, Victor. "What is Vlang? An introduction". https://blog.logrocket.com/what-is-vlang-an-introduction/.
7. ↑ Cheng, Jeremy. "VLang for Automation Scripting". https://levelup.gitconnected.com/vlang-for-automation-scripting-5d977ee97de/.
8. ↑ "Introducing the V Tutorial!". https://replit.com/talk/learn/Introducing-the-V-Tutorial/112856/.
9. ↑ "On the benefits of using C as a language backend". https://github.com/vlang/v/discussions/7849/.
10. ↑ Shóstak, Vic. "The V programming language". https://dev.to/koddr/good-to-know-the-v-programming-language-k5b/.
11. ↑ "C is how old now? - Learning the V programming language". https://l-m.dev/cs/learning_v/.
12. ↑ "V language: simple like Go, small binary like Rust". https://techracho.bpsinc.jp/hachi8833/2021_03_09/89457/.
13. ↑ Pandey, Arpan. "Building a Web Server using Vlang". https://blog.hackersreboot.tech/hello-v-lang/.
14. ↑ "The V programming language is now open source". https://hub.packtpub.com/the-v-programming-language-is-now-open-sourced-is-it-too-good-to-be-true//.
15. ↑ Shóstak, Vic. "The V programming language". https://dev.to/koddr/good-to-know-the-v-programming-language-k5b/.
16. ↑ Umoren, Samuel. "Building a Web Server using Vlang". https://www.section.io/engineering-education/building-web-server-with-vlang/.
17. ↑ Oliveira, Marcos. "V , the programming language that is making a lot of success". https://terminalroot.com/vlang-the-programming-language-that-is-making-a-lot-of-success/.
18. ↑ "Building V from source in 0.3 seconds". https://www.youtube.com/watch?v=pvP6wmcl_Sc.
19. ↑ Rao, Navule Pavan Kumar (2021). Getting Started with V Programming. Packt Publishing. ISBN 1839213434.
20. ↑ "C2V". https://github.com/vlang/c2v/.
21. ↑ "Translating C to V". https://github.com/vlang/v/blob/master/doc/docs.md#translating-c-to-v.
22. ↑ "C2V Demo: Translating DOOM from C to V". https://www.youtube.com/watch?v=6oXrz3oRoEg/.
23. ↑ "Go2V". https://github.com/vlang/go2v/.
24. ↑ "JavaScript and WASM backends". https://vlang.io/faq/.
25. ↑ "Convert Go to V with go2v". https://zenn.dev/tkm/articles/go2v-with-go-lsd.
26. ↑ "The V WebAssembly Compiler Backend". https://l-m.dev/cs/the_v_webassembly_compiler_backend/.

## Further Reading

* Laboratory, Independent (June 20, 2020) (in ja). The V Programming Language basic.
* Rao, Navule Pavan Kumar (December 10, 2021) (in en). Getting Started with V Programming. Packt Publishing. ISBN 1839213434.
* Lyons, Dakota "Kai" (April 13, 2022) (in en). Beginning with V Programming. Independently Published. ISBN 979-8801499963.
* Tsoukalos, Mihalis (March 29, 2022) (in en). Discover the V language. Linux Format 288.

## External links

* Official website
* Official YouTube channel
* An introduction to V - the vlang

0.00

(0 votes)

|

Retrieved from "https://handwiki.org/wiki/index.php?title=V_(programming_language)&oldid=2601267"