Tuesday, August 9, 2022

A Brief Introduction to the V Language

 V is a new language with speedy compilation times, an easy learning curve, and features to guarantee code safety. Find out what all the fuss is about.


V is a new, open-source, statically-typed programming language created by Alexander Medvednikov. V promises to be simple, fast, and safe to build maintainable software with. Several languages have influenced V’s design including Oberon, Swift, Kotlin, and Python.

Learn about V, its features, and how it compares to other languages.

Things You Can Do With V

V has a lot of power and you can use it in nearly any industry, including system programming, web development, and game development. The language is straightforward, quick, and safe.

V offers a wide range of powerful graphics libraries for game development. These build on top of GDI+/Cocoa Drawing and OpenGL for more sophisticated 2D/3D applications.

V has a UI module that employs native GUI toolkits for mobile apps,WinAPI/GDI+ for Windows, Cocoa for macOS, and custom drawing for Linux.

Reasons to Try V

Although V is a new language, these benefits make it a legitimate option.

Simplicity

To build your project, you just need to run:

v .

You don’t need to set up a build environment, create a makefile, import headers, or set up a virtual environment. If you want to install new libraries via vpm (V package manager), just run

v install <package_name>

Safety

By default, V has:

  • immutable variables
  • immutable structs
  • option/result and mandatory error checks
  • sum types
  • generics
  • immutable function args

and no:

  • null
  • variable shadowing
  • undefined behavior
  • global variables (can be enabled for low-level apps like kernels, via flag)

Compilation Speed

Some programming languages are faster than others, in terms of execution or compilation. V is as performant as C. It boasts compilation times of around 110k loc/s with a C backend and approximately 1 million loc/s with native and tcc backends. V also steers clear of unnecessary allocations to provide a simple-abstraction-free coding approach.

Memory Management

V doesn't use a garbage collector, which makes it different from languages like Go and Java. Instead, V manages memory at compile time, but only works for basic situations. You need to manage it manually for more complex cases.

Formatting for Code Style Consistency

You can use the vfmt formatter to format your V code and improve its readability by running:

v fmt -w <your_filename>.v

Friendly Error Messages

V's error messages are detailed, informative, and helpful. Take a look at this example:

user.v:8:14: error: `update_user` parameter `user` is mutable, you need to provide `mut`: `update_user(mut user)`
7 | mut user := User{}
8 | update_user(user)
| ~~~~
9 | }

With the help of the error message, you can fix this code:

mut user := User{}
update_user(mut user)

C++ vs. Go vs. V: Comparing the Three Languages

In these examples, you can see how C++, Go, and V code differs in specific programming use cases.

Hello World

In C++:

#include <iostream>

int main() {
std::cout << "Hello World!" << std::endl;
}

In Go:

package main
import "fmt"

func main() {
fmt.Println("Hello World!")
}

In V:

println('Hello World!')

In this very basic program printing the text "Hello World!" you can see that V is much simpler than C++ or Go. You don't need to import or include any libraries for such a straightforward program.

Vector Initialization

In C++:

std::vector<int> numbers = {1, 2, 3, 4};

In Go:

numbers := []int{1, 2, 3, 4}

In V:

numbers := [1, 2, 3, 4]

In this use case, you can see that V still produces cleaner code than the two others. Unlike Go and C++ vectors, V does not require its vectors to hold values of a fixed type.

Printing a Vector

In C++:

std::copy(numbers.begin(), numbers.end(),
std::ostream_iterator<int>(std::cout, " "));

std::cout << std::endl;

In Go:

fmt.Println(numbers)

In V:

println(numbers)

V’s println function can print the vector variable natively, just like Go’s can, although the latter uses the fmt package. The C++ iostream header files provide similar functionality. You’ll still need to take care of printing the individual elements of the vector yourself, though.

Reading a File

In C++:

#include <iostream>
#include <fstream>

int main() {
std::string path = "/etc/hosts";
std::ifstream f(path);
std::string text;
text.assign(std::istreambuf_iterator<char>(f), {});

if (!f)
std::cerr << "error reading from file" << std::endl;
}

In Go:

package main

import (
"io/ioutil"
"log"
)

func main() {
path := "/etc/hosts"
b, err := ioutil.ReadFile(path)

if err != nil {
log.Println(err)
return
}

text := string(b)
}

In V:

import os

path := "/etc/hosts"
text := os.read_file(path) or {
eprintln(err)
return
}

For file reading, V uses the os module to provide common OS/platform independent functions. These handle tasks like accessing command line arguments, reading files, and handling processes. In this case, we want to read a file in path and return the contents, so you'll use the read_file(path) function. read_file returns an error if there is no existing content in the given path. If you want to learn more about other modules of V, you might want to check out their documentation.

Ready to Try V?

V is a new programming language that’s still in its early stages of development. Even so, it has a lot of power and is quick, safe, easy, and maintainable.

V has cleaner code than C++ and Go. This suggests that V can assist programmers in adhering to one of the basic programming principles. So, are you ready to try V?


No comments:

Post a Comment