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

JavaScript | PHP


An enum literal is a comptime token that you would typically cast into an enums. It can also be explicitly exported. It's represented as string in PHP.

const Pet = enum{ dog, cat, dragon };

pub const value: Pet = .dragon;
pub const literal = .dragon;
<?php

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

echo "{$m->value}\n";
echo "{$m->literal}\n";
Pet Object
(
)
dragon

Because enum literals are automatically converted to strings by Zigar, whereas constants of the type []const u8 are treated as arrays, they can be useful in situations where you want to attach static text to a struct.

pub const DataSection = struct {
    comptime type: @TypeOf(.enum_literal) = .data,
    offset: i64,
    len: i64,
};
<?php

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

$section = new $m->DataSection(offset: 16, len: 256);
print_r($section);
DataSection Object
(
    [type] => data
    [offset] => 16
    [len] => 256
)

You can use the @"" syntax to create literals containing whitespace and other illegal characters.

pub const version = .@"11.2.2";
<?php

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

echo $m->version, "\n";

11.2.2

Types | Comptime fields