From Handwiki The official V logo | |
| Paradigms | Multi-paradigm: functional, imperative, structured |
|---|---|
| Designed by | Alexander Medvednikov |
| First appeared | 22 June 2019[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 |
| Influenced by | |
| |
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.
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.
V is written in V and compiles in less than a second.[17][18][19]
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.
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]
fn main() {
println('hello world')
}
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
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)
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"
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)
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) }
}
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:
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
}
}
Categories: [Programming language design]