Skip to content

Syntax

Generics

Each type can take arguments containing other type declarations. Validation of arguments and their number lies on the implementing side; the parser does not limit their number or nesting and does not check their semantic correctness in any way.

Each argument is enclosed in triangle brackets < and > and separated by a comma (,).

Reference to an existing class, interface or enum with template arguments.

Path\To\ExampleClass<T, U>
type
args
type
type

resultTypeLang\Type\NamedTypeNode

Each template argument allows you to define an additional hint, which can be used, for example, in static analyzers to indicate the call-site variance.

The hint is one of the words covariant, contravariant or invariant written in lower case before the type of the argument.

The covariant hint before the template argument`s type.

HashMap<array-key, covariant Request>
type
args
type
arg · covariant
type

resultTypeLang\Type\NamedTypeNode

Hints of different arguments are independent of each other.

Handler<contravariant Request, invariant Response>
type
args
arg · contravariant
type
arg · invariant
type

resultTypeLang\Type\NamedTypeNode

The hint applies to the whole type that follows it.

Collection<covariant Foo|Bar>
type
args
arg · covariant
union
type
type

resultTypeLang\Type\NamedTypeNode

A template argument may be left unsaid, written as an asterisk (*) in the place of the type. A wildcard argument does not accept a hint.

Any argument at all.

Collection<*>
type
args
wildcard

resultTypeLang\Type\NamedTypeNode

A wildcard beside a type.

HashMap<array-key, *>
type
args
type
wildcard

resultTypeLang\Type\NamedTypeNode

Several wildcards.

HashMap<*, *>
type
args
wildcard
wildcard

resultTypeLang\Type\NamedTypeNode

In addition to modern list declarations such as list<int> or array<array-key, int>, the legacy int[] syntax is allowed.

List (array) of User objects.

User[]
list
type

resultTypeLang\Type\WrappingTypeNode\TypesListTypeNode

List of list (nested array) of User objects.

User[][]
list
list
type

resultTypeLang\Type\WrappingTypeNode\TypesListTypeNode