Skip to content

Type declarations (ref)

Alex Zimin edited this page Jul 11, 2011 · 3 revisions

<< Back to Reference Manual.

Table of Contents

Type header

type_header =  
Lexical_structure_(ref):IDENTIFIER [ type_parameters ] 
[ ':' Type_expressions_(ref):type  { ',' type } ] where_constraints  
Type header is similar to .NET. The main difference is the optional type_parameters list, defined below.
type_attributes = Attributes_(ref):attributes

Type parameters

type_parameters =
'[' Lexical_structure_(ref):IDENTIFIER { ',' IDENTIFIER } ']'

where_constraints =
{ 'where' Lexical_structure_(ref):IDENTIFIER ':' Type_expressions_(ref):type { ',' type } } 

When defining polymorphic type one has to specify list of type variables in declaration. It can have following form:

class Foo [a, b]

An optional list of where parameters can be used to add constraints to the type variables.

where a : Nemerle.Collections.IEnumerable, IComparable
where b : Nemerle.Collections.IDictionary

Declarations

type_declaration =
   type_alias
|  interface_declaration
|  class_like_declaration
|  variant_declaration
|  enum_declaration
|  delegate_declaration

Type alias

type_alias =
   type_attributes 'type'  type_header  '='   Type_expressions_(ref):type ';'

Much like typedef in C.

Interface

interface_declaration =
   type_attributes  'interface'  type_header  '{'  {  interface_member  }  '}'

Class like

class_like_declaration =
   type_attributes  'class'  type_header  '{' {  type_member  }  '}'
|  type_attributes  'struct'  type_header  '{' {  type_member  }  '}'
|  type_attributes  'module'  type_header  '{' {  type_member  }  '}'

A module is much like a class, but all module members are static. There is no need to place static attributes on module members. It is also not possible to create instances of module types.

Struct types are like classes, but they are passed by value and cannot inherit from other types.

Variants

variant_declaration =
  type_attributes  'variant'  type_header  '{' {  variant_option  }  '}'

variant_option =
  '|' Lexical_structure_(ref):IDENTIFIER [ '{' { 
  Type_members_(ref):field_definition } '}' ] 
| Type_members_(ref):type_member

A variant declaration consists of a type name and a list of bar-separated variant options enclosed in brackets.

The variant option describes the constructor associated with this variant type. An option may take an argument. Option name must be capitalized.

Variants (unlike variant options) can also have other members (methods and fields). These fields and methods are inherited by all the options.

Enums

enum_declaration =
   type_attributes  'enum'  type_header  '{'  {  
   '|'  Lexical_structure_(ref):IDENTIFIER  [  '='  Lexical_structure_(ref):literal  ]  }  '}'

Enums are like variants, but cannot take arguments. They are passed by value and can be used with bitwise operations.

Delegates

delegate_declaration =
  type_attributes  'delegate'  Type_members_(ref):method_header  
Clone this wiki locally