Vectormike's Blog

What is Vlang? An introduction

If you’ve managed to end up at this article, it’s probably because you either have no idea what Vlang is or want to know more. Either way, it’s simple: Vlang is a programming language. Does that clear it up? (Just kidding.)

To be more specific, Vlang is a statically typed programming language created by Alexander Medvednikov that became open source in July 2019. According to the creator, the name was shortened to V so as to avoid messing up Git history. It is similar to Go, and inspired by Rust, Swift, and Oberon.

In the documentation, V boasts about C-like compilation time (the main backend code compiles to C), less memory allocation, easy learning curve, built-in serialization(transformation of objects to a streams of bytes) and no dependencies upon compilation. V’s safety is also top notch with no null, no global variables, no undefined, and support for bounds checking. By default, it also supports pure functions, immutable variables, and immutable structs.

With the basics covered, we’ll spend the rest of the article talking a little bit more about these claims and where V could be headed in the future.

Getting started with V

To follow along, you must have git, a C compiler like gcc, and make installed already; if you do not, go ahead and set those up before we proceed.

To begin with Vlang, it is best to install directly from the source (Github).

If on MacOS, Linux, FreeBSD, etc., run:

git clone https://github.com/vlang/v && cd v && make

On Windows:

git clone https://github.com/vlang/v
cd v
make

This will self compile and build V on your machine.

Next, to avoid retyping the complete execution path repeatedly, add V to your path as follows.

On MacOS:

sudo ./v symlink

On Windows:

.\v.exe symlink

Writing our first Vlang program

For our first program, we will use the familiar hello world:

// hello_world.v
fn main() {
  println('Hello World!')
}

Save this file as helloworld.v and run v run helloworld.v to get your output.

Remember that V is statically typed, so the types come after the argument name, like so:

fn add(x int, y int) int {
  return x + y
}

Using functions in V

Like C++, V does not allow function overloading, which means two functions cannot have same name with different parameters. Limiting function overloading is intended to make code easier to maintain.

In V, functions are also private by default, so other modules cannot use them unless we prepend pub to them, like so:

// a public function
pub fn add() {
}
// a private function
fn sum() {
}

Using variables in V

Variables in V are assigned with :=, as in the snippet below:

name := 'Victor'
age := 23
large_number := i64(9999999999)
println(name)
println(age)
println(large_number)

They are also immutable; to be able to redeclare/reassign a variable, you will have to prepend mut to it, like so:

mut age := 23
println(age)
age = 24
println(age)

Using types in V

As we now know very well, V is statically typed. Below are its primitive types:

bool

string

i8    i16  int  i64      i128 (soon)
byte  u16  u32  u64      u128 (soon)

rune // represents a Unicode code point

f32 f64

any_int, any_float // internal intermediate types of number literals

byteptr, voidptr, charptr, size_t // these are mostly used for C interoperability

any // similar to C's void* and Go's interface{}

Other data types are:

Strings, Numbers, Arrays V supports string interpolation as well:

name := 'Victor'
println('Hello, $name!') // Hello, Victor!

HTTP support and JSON decoding

Vlang has inbuilt HTTP support for data fetching and JSON decoding. To use them, you will need to import their respective modules:

import http
import json

fn fetchUsers() {
  users := http.get('http://jsonplaceholder.typicode.com/users') or {
    println('failed to fetch users from server')
    return
  }
}

If you plan on working with JSON API like in the example above, you will need to decode the response from the URL (change the data from a string to an object) before you can handle it.

To do this, you will first need to define a struct for your kind of response, as seen in the example below:

struct User {
  name string
  email int
}


import http
import json

fn fetchUsers() []User {
  users := http.get('http://jsonplaceholder.typicode.com/users')
  users := json.decode([]User, users) or {
    return []User{}
  }
}

Package manager

V has a built-in package manager called vpm. To use vpm, run:

v install [package]

Memory management in V

Similar to Rust, Vlang uses value types and string buffers for memory management, but does not do garbage collection. Below is an official demo from V:

V Demo

Autofree can also be disabled so as to perform manual memory freeing -noautofree.

Why use Vlang?

V has a simpler, more readable syntax than many other frameworks (like Rust), making it clean and easy to use. Its main philosophy is to do each thing in one way with very few rules.

In my own opinion, V is a cleaner version of Go, with cool features like hot reloading, option/result and mandatory error checks, no null, no global variables and no undefined behavior. V also has the upside of being able to be used in most fields such as web development, game development, systems programming, GUI, embedded, tooling, mobile (WIP), and others.

While there is a lot more work to be done for V to surpass some of the other frameworks out there, now is a great time to get to understand the language and contribute to its development.

Conclusion

With so many other programming languages out there, it is important to remember that V is still in alpha stage, which means most parts are still in development. Nonetheless, it seems like a pretty good language with its “one way of doing things” approach. This makes code predictable and very maintainable.

The V documentation also adds to its simplicity and does a good job helping users get a grasp of what everything is and how it works.

For more about Vlang, check out some of the resources below. In the meantime, I hope this article has answered some of your questions about the V language and its role in modern programming.

More about Vlang