Type expressions (ref) - vilinski/nemerle GitHub Wiki

<< Back to Reference Manual

Table of Contents

Intro

Type expressions relate to type declarations much like function calls relate to function and value definitions. Type declarations define ways of constructing types and type expressions define actual types.

Types are both static and dynamic characterization of values. The static type of expression depends on its building blocks and is defined in the paragraph describing given expression. The dynamic (runtime) type is bound to the value at the moment it is created, and remains there until the value is garbage collected.

The type system is modeled after .NET Generics design, except for tuple and function types, which are new, but can be easily simulated using generics.

Type constructor application

primary_type =
Expressions_(ref):qualified_identifier [ '[' type { ',' type } ']' ]

A type constructor (defined with type declaration) can be applied to zero or more arguments forming a type expression. The number of type arguments in type application must match the number of type arguments in definition. Moreover, actual type arguments must solve where constraints imposed on formal type arguments.

Type variable reference

primary_type =
Lexical_structure_(ref):IDENTIFIER

Refer to the type substituted to a given type variable. The type variable has to be defined (bound, quantified) before it is used. A type variable can be defined in type arguments or method header (of a global or local function).

Grouping types

primary_type =
'(' type ')'

This construct has no semantic meaning -- it exists only to enforce particular syntax decomposition.

Void type

primary_type =
'void'

This is mostly an alias for System.Void -- a type with exactly one inhibiting value. It is, however, a first class value -- it can be passed as a function parameter as well as returned from functions.

The name comes from System.Void, but should be in fact unit.

Ref and Out types

primary_type =
'ref' primary_type
| 'out' primary_type

These are for parameters passed by reference.

Array types

primary_type =
'array' '[' [ Lexical_structure_(ref):NUMBER_LITERAL ',' ] type ']'

Define array type. The number is the rank. It defaults to one.

Tuple type

type =
primary_type '*' primary_type { '*' primary_type }

Construct product (tuple) type. This operator is not associative, which means that each two of following types are different:

int * int * int
(int * int) * int
int * (int * int)

Function type

type =
type '->' type

Construct function type with a specified argument and return types respectively. The -> operator is right associative, which means that the following types are equivalent:

int -> int -> int
int -> (int -> int)

Multi-argument function types are written using tuple notation, for example after local declaration:

def some_function (a : int, b : string) : float { ... }

the expression some_function has type int * string -> float.

⚠️ **GitHub.com Fallback** ⚠️