Type ‣ Void ᴾᴴᴾ - chung-leong/zigar GitHub Wiki

JavaScript | PHP


In the Zig language, a function returns void when it has no useful information to return. void is a 0-byte type (akin to an empty struct). It's represented as null in PHP.

const std = @import("std");

pub fn hello() void {
    std.debug.print("Hello world!\n", .{});
}
<?php

$m = zigar_use(__DIR__ . '/void-example-1.zig');

debug_zval_dump($m->hello());

Hello world!
NULL

It's possible to have an array of void:

pub var array_of_nothing: [4]void = .{ {}, {}, {}, {} };
<?php

$m = zigar_use(__DIR__ . '/void-example-2.zig');

print_r($m->array_of_nothing);
$m->array_of_nothing[3] = null;
try {
    $m->array_of_nothing[3] = 1;
} catch (Exception $e) {
    echo $e->getMessage(), "\n";
}
[4]void Object
(
    [0] => 
    [1] => 
    [2] => 
    [3] => 
)
not null (zig)

void can also appear in structs:

const Sound = struct {
    loud: void = {},
    thunderous: void = {},
    deafening: void = {},
};
const Fury = struct {
    angry: void,
    frenzied: void,
    tempestuous: void,
};
const Tale = struct {
    sound: Sound = .{},
    fury: Fury = .{},
};

pub const Idiot = struct {
    pub fn tell() Tale {
        return .{};
    }
};
<?php

$m = zigar_use(__DIR__ . '/void-example-3.zig');

$tale = $m->Idiot->tell();
print_r($tale);
echo strlen($tale->__bytes), "\n";
Tale Object
(
    [sound] => Sound Object
        (
            [loud] => 
            [thunderous] => 
            [deafening] => 
        )

    [fury] => Fury Object
        (
            [angry] => 
            [frenzied] => 
            [tempestuous] => 
        )

)
0

Types | Undefined