Unit in Phunkie represents a type with exactly one value, similar to void
in other languages but as a proper type. It’s often used to represent the absence of a meaningful value while maintaining type safety.
There’s only one way to create a Unit value, as it only has one possible value:
// Create a Unit value
$unit = Unit();
// Unit is also an empty tuple
$unit = Tuple(); // same as Unit()
Unit has several special properties:
$unit = Unit();
// String representation
echo $unit->toString(); // "()"
// Type representation
echo $unit->showType(); // "Unit"
// Cannot access members
$unit->_1; // Throws Error: _1 is not a member of Unit
// Cannot modify
$unit->_1 = 42; // Throws Error: _1 is not a member of Unit
// Cannot copy
$unit->copy(['1' => 42]); // Throws Error: copy is not a member of Unit
Unit is useful in several scenarios:
null
or void
when you need to represent the absence of a value