Skip to content

Core\Reflect::TypeKind

What a value actually is, when its declared type no longer says. Nearly everywhere in Novis the type is written down and nobody has to ask; a mixed is the exception, and asking one what it is holding answers with one of ten kinds.

Core\Reflect::TypeKind is declared in the class Core\Reflect. The method typeOf returns it.

The ten are Null, Bool, Int, Uint, Float, Decimal, Text, Bytes, Array and Object. Every value is in exactly one of them and never in two, so a match covering all ten needs no fallback arm and no answer can be argued with.

Good to know: a whole number that cannot be negative is Uint and not Int, an exact Decimal is never a Float, and a run of octets is Bytes rather than text that happens to hold them. A callable is an object, so it has no kind of its own.

Examples

Asking a mixed what it is holding

<?nvs
// A `mixed` can hold anything, so it is the one place where the type is not
// written down and you may have to ask. The answer is always exactly one of
// ten kinds, which is why the list below needs no "anything else" arm.
class Kind
{
public static function of(mixed $value): string
{
return match (Core\Reflect::typeOf($value)) {
Core\Reflect::TypeKind::Null => 'Null',
Core\Reflect::TypeKind::Bool => 'Bool',
Core\Reflect::TypeKind::Int => 'Int',
Core\Reflect::TypeKind::Uint => 'Uint',
Core\Reflect::TypeKind::Float => 'Float',
Core\Reflect::TypeKind::Decimal => 'Decimal',
Core\Reflect::TypeKind::Text => 'Text',
Core\Reflect::TypeKind::Bytes => 'Bytes',
Core\Reflect::TypeKind::Array => 'Array',
Core\Reflect::TypeKind::Object => 'Object',
};
}
}
class Point
{
public int $x = 1;
}
// The same variable, one assignment after another. The answer follows what is
// in it right now and never what it was declared as.
mixed $slot = null;
echo 'nothing at all: ', Kind::of($slot), "\n";
$slot = true;
echo 'a yes or no: ', Kind::of($slot), "\n";
$slot = 42;
echo 'a whole number: ', Kind::of($slot), "\n";
$slot = 'novis';
echo 'some text: ', Kind::of($slot), "\n";
$slot = [1, 2, 3];
echo 'a list: ', Kind::of($slot), "\n";
$slot = new Point();
echo 'a thing you made: ', Kind::of($slot), "\n";
// An anonymous function is an object, so this prints `Object`.
$slot = fn(int $n): int => $n + 1;
echo 'a function: ', Kind::of($slot), "\n";
Output
nothing at all: Null
a yes or no: Bool
a whole number: Int
some text: Text
a list: Array
a thing you made: Object
a function: Object

Three distinctions a single string type cannot make

<?nvs
// `Core\Reflect::typeOf` returns the kind of a value. This program checks three
// pairs of values that look alike. The two values in each pair have different kinds.
class Kind
{
public static function of(mixed $value): string
{
return match (Core\Reflect::typeOf($value)) {
Core\Reflect::TypeKind::Int => 'Int',
Core\Reflect::TypeKind::Uint => 'Uint',
Core\Reflect::TypeKind::Float => 'Float',
Core\Reflect::TypeKind::Decimal => 'Decimal',
Core\Reflect::TypeKind::Text => 'Text',
Core\Reflect::TypeKind::Bytes => 'Bytes',
default => 'something else',
};
}
}
// An `int` can be negative. A `uint` cannot, so it is the type for a count of
// items.
int $below = -3;
uint $items = 3;
echo '-3 items: ', Kind::of($below), "\n";
echo '3 items: ', Kind::of($items), "\n";
// A `decimal` is exact to the last cent, so it is the type for a price.
// A `float` is approximate, and small errors add up when you use it for money.
decimal $price = 19.90;
float $ratio = 19.90;
echo '19.90 owed: ', Kind::of($price), "\n";
echo '19.90 parts: ', Kind::of($ratio), "\n";
// A `string` is text you can show to a person. `bytes` is raw data from a file
// or a socket, and it may not be valid text.
string $name = 'ab';
bytes $upload = Core\Encoding::fromHex('6162');
echo 'the word ab: ', Kind::of($name), "\n";
echo 'two octets: ', Kind::of($upload), "\n";
Output
-3 items: Int
3 items: Uint
19.90 owed: Decimal
19.90 parts: Float
the word ab: Text
two octets: Bytes

Checking a settings file before you trust it

<?nvs
// Settings from outside your program are stored as `mixed`, because their types
// are not known. This program checks each setting against the kind it should have.
// You can run this check when the program starts.
class Settings
{
public static function name(Core\Reflect::TypeKind $kind): string
{
return match ($kind) {
Core\Reflect::TypeKind::Null => 'nothing',
Core\Reflect::TypeKind::Bool => 'a yes or no',
Core\Reflect::TypeKind::Int => 'a whole number',
Core\Reflect::TypeKind::Uint => 'a count',
Core\Reflect::TypeKind::Float => 'an approximate number',
Core\Reflect::TypeKind::Decimal => 'an exact amount',
Core\Reflect::TypeKind::Text => 'text',
Core\Reflect::TypeKind::Bytes => 'octets',
Core\Reflect::TypeKind::Array => 'a list',
Core\Reflect::TypeKind::Object => 'an object',
};
}
}
decimal $priceAsWritten = 19.90;
array<mixed> $arrived = [
'port' => 8080,
'host' => '0.0.0.0',
'retries' => 3,
'debug' => 'yes',
'unitPrice' => $priceAsWritten,
];
// The kind that the rest of the program expects for each setting.
array<Core\Reflect::TypeKind> $expected = [
'port' => Core\Reflect::TypeKind::Int,
'host' => Core\Reflect::TypeKind::Text,
'retries' => Core\Reflect::TypeKind::Int,
'debug' => Core\Reflect::TypeKind::Bool,
'unitPrice' => Core\Reflect::TypeKind::Decimal,
];
uint $wrong = 0;
foreach ($arrived as tainted string $setting => mixed $value) {
Core\Reflect::TypeKind $found = Core\Reflect::typeOf($value);
if ($found == $expected[$setting]) {
echo $setting, ': ', Settings::name($found), ", as expected\n";
} else {
$wrong = $wrong + 1;
echo $setting, ': ', Settings::name($found), ', but it is read as ',
Settings::name($expected[$setting]), "\n";
}
}
// `debug` is `"yes"`, which is text and not a `Bool`. This program only prints the count.
// A real program can stop at startup when the count is not 0.
echo "\n", $wrong, " setting(s) are not what this program reads\n";
Output
port: a whole number, as expected
host: text, as expected
retries: a whole number, as expected
debug: text, but it is read as a yes or no
unitPrice: an exact amount, as expected
1 setting(s) are not what this program reads