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

JavaScript | PHP


The bool type in Zig corresponds exactly to bool in PHP. It's used for storing variables that can be either true or false.

pub fn not(value: bool) bool {
    return !value;
}
<?php

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

debug_zval_dump($m->not(true));
debug_zval_dump($m->not(false));
bool(false)
bool(true)

In packed struct

A bool typically takes up 1 byte. Within a packed struct it uses only a single bit. This allows you to store a large number of states very efficiently.

pub const UserPreferences = packed struct {
    option1: bool = false,
    option2: bool = false,
    option3: bool = false,
    option4: bool = false,
    option5: bool = false,
    option6: bool = false,
    option7: bool = false,
    option8: bool = false,
    option9: bool = false,
    option10: bool = false,
    option11: bool = false,
    option12: bool = false,
    option13: bool = false,
    option14: bool = false,
    option15: bool = false,
    option16: bool = false,
};
<?php

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

$pref = new $m->UserPreferences(option8: true);
print_r($pref);
echo "size = " . strlen($pref->__bytes) . "\n";
UserPreferences Object
(
    [option1] => 
    [option2] => 
    [option3] => 
    [option4] => 
    [option5] => 
    [option6] => 
    [option7] => 
    [option8] => 1
    [option9] => 
    [option10] => 
    [option11] => 
    [option12] => 
    [option13] => 
    [option14] => 
    [option15] => 
    [option16] => 
)
size = 2

Types