Zig a New Programming Language | Cons and Pros?

Developing a programming language from scratch is surely a daunting job. Besides, in making the compiler, mentioning its standard library, and supporting tool editors such as build systems and editors, you need to create the language your way. So, what do you think, will it be functional or imperative? Will it contain metaprogramming skills? For what systems will it be useful? Let's dive in to know more about it.

What is Zig?

Zig is a general-purpose programming language and a tool for operating reusable and optimal software. It's easy, strong, and has a portable SIMD. If you as a team will work on Zig, then you need to decide to develop a language that is purposely designed for the requirements but that was not built lightly.

People have this misconception that Zig is a C-like language:

pub fn readAll(self: Self, buffer: []u8) Error!usize {
    var index: usize = 0;
    while (index != buffer.len) {
        const amt = try self.read(buffer[index..]);
        if (amt == 0) return index;
        index += amt;
    }
    return index;
}

After going through the above code, you will surely say that it doesn't seem like C at all. So, the above pub, fn keywords, var in front of variables, it all appears like JavaScript. This implies we need to know exactly what we mean when we say that Zig is a C-like language.

The syntax of Zig is a lot more different from C, but we need to know how the language actually operates. So, the finest way to offer a sense of this thing is to commence by conversing about what Zig doesn't do.

Language Features Zig Doesn’t Have

  • Inheritance and classes: There is no syntax or a class keyword to show its inheritance.

  • Exceptions: Zig employs error codes but more quickly than Go.

  • Protocols and interfaces: No syntax is there to describe something similar to Go or Java interface.

  • Runtime polymorphism.

  • Overloading function: You cannot head further writing with the same name but with various arguments several times just like in C++.

  • Overloading of operator: Operators like +, -, / can never alter the meaning.

  • Lambdas or closures: There is no way to describe a function inline that gathers some external state.

  • Garbage collection: You should allocate and deallocate memory in Zig.

Uncommon Features Zig Does Have

  • Manual memory allocation and deallocation.

  • Pointers and its arithmetic: Nowadays, many languages forbid this and frequently limited some versions like Java references.

  • Get address of arbitrary data structure.

You will get a better idea of why exactly Zig can be called a C-like language with the following code list.

var x: i32 = 4; var ptr: *i32 = &x; ptr.* = 15;

This makes a type i32 variable which is 32-bit integer in Zig language. We start it to 4 and then we make a pointer ptr to items with i32 type. Now, the pointer is started to x and later it is dereferenced to put 15 to x.

Why change the C-syntax?

This brings numerous advantages and disadvantages when you wish your language to support type-inference. C was designed much before when type inference were is use.