Zig
Comments
by Andrew Kelley
Open-source language designed for robustness, optimality, and clarity.
  • Small, simple language. Focus on debugging your application rather than debugging your knowledge of your programming language.
  • Ships with a build system that obviates the need for a configure script or a makefile. In fact, existing C and C++ projects may choose to depend on Zig instead of e.g. cmake.
  • A fresh take on error handling which makes writing correct code easier than writing buggy code.
  • Debug mode optimizes for fast compilation time and crashing with a stack trace when undefined behavior would happen.
  • Release mode produces heavily optimized code. What other projects call "Link Time Optimization" Zig does automatically.
  • Compatible with C libraries with no wrapper necessary. Directly include C .h files and get access to the functions and symbols therein.
  • Provides standard library which competes with the C standard library and is always compiled against statically in source form. Compile units do not depend on libc unless explicitly linked.
  • Nullable type instead of null pointers.
  • Tagged union type instead of raw unions.
  • Generics so that one can write efficient data structures that work for any data type.
  • No header files required. Top level declarations are entirely order-independent.
  • Compile-time code execution. Compile-time reflection.
  • Partial compile-time function evaluation with eliminates the need for a preprocessor or macros.
  • The binaries produced by Zig have complete debugging information so you can, for example, use GDB to debug your software.
  • Mark functions as tests and automatically run them with zig test.
  • Friendly toward package maintainers. Reproducible build, bootstrapping process carefully documented. Issues filed by package maintainers are considered especially important.
  • Cross-compiling is a primary use case.
  • In addition to creating executables, creating a C library is a primary use case. You can export an auto-generated .h file.
  • Standard library supports Operating System abstractions for:
    • x86_64 linux
    • Support for all popular operating systems and architectures is planned.
  • For OS development, Zig supports all architectures that LLVM does. All the standard library that does not depend on an OS is available to you in freestanding mode.
const math = @import("math.zig");

pub fn parseUnsigned(comptime T: type, buf: []const u8, radix: u8) -> %T {
    var x: T = 0;

    for (buf) |c| {
        const digit = %return charToDigit(c, radix);
        x = %return math.mul(T, x, radix);
        x = %return math.add(T, x, digit);
    }

    return x;
}

error InvalidChar;
fn charToDigit(c: u8, radix: u8) -> %u8 {
    const value = switch (c) {
        '0' ... '9' => c - '0',
        'A' ... 'Z' => c - 'A' + 10,
        'a' ... 'z' => c - 'a' + 10,
        else => return error.InvalidChar,
    };

    if (value >= radix)
        return error.InvalidChar;

    return value;
}
Information updated 06/05/17
View Comments