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

JavaScript | PHP


A type in the Zig language is a special data type that refers to other data types. It's a comptime-only type.

As function arguments

Functions that accept type as arguments cannot be exported by Zigar:

const std = @import("std");

pub const Uint32 = u32;

pub fn printTypeName(comptime T: type) void {
    std.debug.print("{s}\n", .{@typeName(T)});
}
<?php

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

$m->printTypeName($m->Uint32);
PHP Fatal error:  Uncaught Error: Call to undefined method type-example-1::printTypeName() in /zigar/examples/type-example-1.php:6
Stack trace:
#0 {main}
  thrown in /zigar/examples/type-example-1.php on line 6

Used in constant declarations

Constants of the type type can be exported:

pub const Int32 = i32;
pub const FatalError = error{ ate_expired_doritos, thought_cat_was_pillow };
pub const PizzaTopping = enum { pine_apple, anchovy, baked_beans, pierogi };
pub const Point = struct { x: f32, y: f32 };
<?php

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

print_r(new $m->Int32(1234));
print_r(new $m->Point(x: 0.5, y: 0.7));
foreach ($m->PizzaTopping as $item) {
    echo "$item\n";
}
// FIXME!!!
// foreach ($m->FatalError as $error) {
//     echo "$error\n";
// }
i32 Object
(
)
Point Object
(
    [x] => 0.5
    [y] => 0.69999998807907
)
pine_apple
anchovy
baked_beans
pierogi
[FIXME!!!]

In anonymous structs

Types stored in anonymous structs can also be exported:

pub const SmallIntegers = .{
    .u1 = u1,
    .u2 = u2,
    .u3 = u3,
    .u4 = u4,
    .u5 = u5,
    .u6 = u6,
    .u7 = u7,
};
<?php

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

echo new $m->SmallIntegers->u2(3), "\n";
3

As comptime fields

Comptime fields can be of the type type:

fn Point(comptime T: type) {
    return struct {
        x: T,
        y: T,
        comptime Type: type = T,
    };
};
pub const PointI32 = Point(i32);
pub const PointI64 = Point(i64);
pub const PointF32 = Point(f32);
pub const PointF64 = Point(f64);
<?php

$m = zigar_use(__DIR__ . '/type-example-4.zig');

$p1 = new $m->PointI32(x: 45, y: -12);
print_r($p1);
$p2 = new $m->PointI64(x: 45, y: -12);
print_r($p2);
S0 Object
(
    [x] => 45
    [y] => -12
    [Type] => i32 Object
        (
        )

)
S1 Object
(
    [x] => 45
    [y] => -12
    [Type] => i64 Object
        (
        )

)

Types