Callable Types
Edit pageLast modified: 02 February 2025Callable types describe an arbitrary type that describes a function.
Each callable MAY have a list of parameters and/or a return type definition.
tip
Callable type without parameters and return type.
foo()
tip
Callable type with 1 parameter with return type.
foo(T): void
tip
Complex example (see details below).
a(int<0, max>, c(?C): mixed): void
Named Parameters
The name after the type of the parameter defines the parameter that allows passing by name.
The name must appear after the parameter type and begin with a " $
" sign. Just like in the PHP language.
tip
Callable type with one
$name
named parameter.foo(T $name)
tip
Callable type with
$name
named and anonymous parameters.foo(A $a, B, C)
warning
Callable type without parameter's type.
foo($name)
An error similar to the one below should occur
ParseException: Syntax error, unexpected ")"
Output Parameters
Passing a parameter by reference means that the function can change the passed variable while it is running.
To indicate that a parameter is passed by reference, an " &
" sign is used after the type and before the name.
tip
Callable type with one output (referenced) parameter.
foo(T&)
tip
Callable type with one output (referenced) named parameter.
foo(T &$name)
warning
The ampersand (
&
) must be placed after the parameter's type.foo(&T)
An error similar to the one below should occur
ParseException: Syntax error, unexpected "&"
Optional Parameters
An optional parameter means that the argument may not be passed when such a function is called.
An optional parameter is indicated by the " =
" sign at the end of the parameter description.
tip
Callable type with one optional parameter.
foo(T=)
tip
Callable type with one optional named parameter.
foo(T $name=)
tip
Callable type with one optional output parameter.
foo(T&=)
tip
Callable type with one optional output named parameter.
foo(T &$name=)
warning
The optionality char (
=
) must be placed at the end.foo(T= $name)
An error similar to the one below should occur
ParseException: Syntax error, unexpected "$name"
Variadic Parameters
Variadic parameters are indicated by the " ...
" and can be placed either before the type or before the parameter name.
warning
Variadic parameter cannot be optional since they are already optional.
tip
Callable type with one variadic parameter.
foo(...T)
tip
Callable type with one variadic named parameter.
foo(...T $name)
tip
Callable type with one variadic output named parameter.
foo(...T &$name)
tip
Callable type with one variadic parameter.
foo(T...)
tip
Callable type with one variadic named parameter.
foo(T ...$name)
tip
Callable type with one variadic output named parameter.
foo(T &...$name)
warning
The ellipses (
...
) must come before or after the type.foo(...T...)
An error similar to the one below should occur
ParseException: Syntax error, unexpected "..."
warning
Variadic parameter cannot be optional.
foo(T ...$name=)
An error similar to the one below should occur
ParseException: Cannot have variadic param with a default
Attributes
Each callable parameter allows you to define list of additional attributes. An attribute is additional metadata for a parameter.
tip
Simple attribute with one argument for each callable parameter.
Example\Functor(#[type<int8>] int $a): void
tip
Multiple attributes in one group.
Example\Functor(#[type<int8>, const] int&): void
tip
Multiple attribute groups.
Example\OnCreate( #[deprecated] #[inline] (callback(T): void) $callback=, ): void
warning
Only valid identifiers are allowed.
Example\Functor(#[42] int $a): void
An error similar to the one below should occur
ParseException: Syntax error, unexpected "42"