blob: 67bab11cd6a1ccc12738abce534b38455b41e945 [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?>
<?asciidoc-toc maxdepth="5"?>
<?asciidoc-numbered maxdepth="5"?>
<book xmlns="http://docbook.org/ns/docbook" xmlns:xl="http://www.w3.org/1999/xlink" version="5.0" xml:lang="en">
<info>
<title>N4JS Language Specification</title>
<date>2019-08-07</date>
<author>
<personname>
<firstname>2019-08-07 15:02:40 CEST</firstname>
</personname>
</author>
<authorinitials>{</authorinitials>
<style>
.admonitionblock td.icon .icon-todo:before{content:"\f249";color:#f4ee42}
</style>
</info>
<preface>
<title></title>
<simpara role="center"><emphasis role="strong">Last Updated: 2019-08-07</emphasis></simpara>
<simpara role="center"><emphasis role="strong">Authors:</emphasis><?asciidoc-br?>
Jens von Pilgrim, Jakub Siberski, Mark-Oliver Reiser, Torsten Krämer, Ákos Kitta, Sebastian Zarnekow, Lorenzo Bettini, Jörg Reichert, Kristian Duske, Marcus Mews, Minh Quang Tran, Luca Beurer-Kellner</simpara>
<simpara><?asciidoc-pagebreak?></simpara>
<bridgehead xml:id="_abstract" renderas="sect1">Abstract</bridgehead>
<simpara>This document contains the N4JS Specification.</simpara>
<simpara><?asciidoc-pagebreak?></simpara>
</preface>
<chapter xml:id="_introduction">
<title>Introduction</title>
<simpara>This specification defines the N4JS language.</simpara>
<simpara>In general, the N4JS JavaScript dialect used is identical to the standard ECMAScript as defined in the 6th edition of
ECMA-262, also known as ECMAScript 2015, referred to as [<link linkend="ECMA15a">ECMA15a</link>].</simpara>
<section xml:id="_notation" role="language-n4js">
<title>Notation</title>
<section xml:id="_grammar-notation">
<title>Grammar Notation</title>
<simpara>For the specification of the syntax and structure of elements, we use a
slightly augmented similar to the grammar language of Xtext <link xl:href="http://www.eclipse.org/Xtext/documentation/301_grammarlanguage.html">Grammar Language</link>.</simpara>
<simpara>Similar to [<link linkend="ECMA11a">ECMA11a</link>], we define types with properties only for the purpose of explanation and usage within this specification.
We use the Xtext notation style to assign values to meta-properties.
Particularly, we use the Xtext notation for collection (<literal>+=</literal>) and boolean (<literal>?=</literal>) values.
These properties are written in italics. Enumerations are defined similar to Xtext.
In order to allow the specification of default values, which are often defined by omitting the value, we always define the literal explicitly if it can be defined by the user.</simpara>
<simpara>The following lists informally defines the grammar:</simpara>
<variablelist>
<varlistentry>
<term>Terminal</term>
<listitem>
<simpara>Terminals (or terminal strings) are enclosed in single quotes, e.g., <literal>terminal</literal>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>Enumerations</term>
<listitem>
<simpara>Rules which contain only terminals used as values for properties are
marked with <literal>enum</literal> for enumeration.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>Properties</term>
<listitem>
<simpara>Values of non-terminals, e.g., other rules, can be assigned to
properties. The property name and the assignment are not part of the
original syntax and only used for the meta description. E.g., <literal><emphasis>name=</emphasis>Identifier</literal>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>Collection Properties</term>
<listitem>
<simpara>If a property is a collection, values are added to that list via <literal>+=</literal>.
E.g.,<literal><emphasis>property+=</emphasis>Value</literal> .</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>Boolean Properties</term>
<listitem>
<simpara>Boolean properties are set to false by default, if the value (usually
a terminal) is found, the boolean value is set to true. Often, the
name of the property is similar to the terminal. E.g., <literal><emphasis>final?</emphasis>='final'?</literal>.</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>Properties of a non-terminal are sometimes listed again below the
grammar. In that case, often pseudo properties are introduced which are
derived from other properties and which are only used for
simplification.</simpara>
</section>
<section xml:id="_type-judgments-and-rules-and-constraints-notation">
<title>Type Judgments and Rules and Constraints Notation</title>
<section xml:id="_typing-rules-and-judgments">
<title>Typing Rules and Judgments</title>
<definition>
<title>Rule</title>
<simpara>
<anchor xml:id="rule" xreflabel="[rule]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="rule">Rule</link></simpara>
<simpara>
We use the common notation for rules such as type inference rules <footnote><simpara>A brief introduction can be found at <link xl:href="http://www.cs.cornell.edu/~ross/publications/mixedsite/tutorial.html">http://www.cs.cornell.edu/~ross/publications/mixedsite/tutorial.html</link>. In general, we refer the reader to [<link linkend="Pierce02a">Pierce02a</link>]</simpara></footnote>, that is</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>p</mi><mi>r</mi><mi>e</mi><mi>m</mi><mi>i</mi><mi>s</mi><mi>e</mi><mi>s</mi></mrow><mrow><mi>c</mi><mi>o</mi><mi>n</mi><mi>c</mi><mi>l</mi><mi>u</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi></mrow></mfrac><mspace width="5.0mm"/><mstyle mathvariant="normal"><mtext>rule name</mtext></mstyle></math>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>p</mi><mi>r</mi><mi>e</mi><mi>m</mi><mi>i</mi><mi>s</mi><mi>e</mi><mi>s</mi></math> is the rule’s premises (e.g., the expression to be inferred), <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>o</mi><mi>n</mi><mi>c</mi><mi>l</mi><mi>u</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi></math> the result of the rule.
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>r</mi><mi>u</mi><mi>l</mi><mi>e</mi><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math> is an optional condition which may be omitted.</simpara>
<simpara>Both parts of the rule may contain multiple expressions, which are concatenated via 'and'.</simpara>
<simpara>For example, the following</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><msub><mi>P</mi><mn>1</mn></msub><mspace width="5.0mm"/><msub><mi>P</mi><mn>2</mn></msub><mspace width="5.0mm"/><msub><mi>P</mi><mn>3</mn></msub></mrow><mi>C</mi></mfrac></math>
<simpara>can be read as</simpara>
<blockquote>
<simpara>if <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>P</mi><mn>1</mn></msub></math>, <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>P</mi><mn>2</mn></msub></math>, <emphasis>and</emphasis> <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>P</mi><mn>3</mn></msub></math> are all true, then <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi></math> is true as well.</simpara>
</blockquote>
</definition>
<simpara>The following judgments (with relation symbols) are used:</simpara>
<variablelist>
<varlistentry>
<term>subtype <math xmlns="http://www.w3.org/1998/Math/MathML"><mo>&lt;</mo></math> </term>
<listitem>
<simpara>-</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>type <math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle></math> </term>
<listitem>
<simpara>in which the left hand side is a declaration or expression, and the right hand side a type.
We also use <math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>[</mo><mspace width="-0.167em"/><mrow><mo>[</mo></mrow></mrow><mrow><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></mrow><mrow><mrow><mo>]</mo></mrow><mspace width="-0.167em"/><mo>]</mo></mrow></math> as a function returning the (inferred) type of an expression.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>expectedTypeIn <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>:</mi></math> </term>
<listitem>
<simpara>a relation with three arguments:
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo></mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mi>:</mi><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi></math> means, that
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi></math> is expected to be a subtype of
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi></math> inside <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>r</mi></math></simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>The following statement, for example, defines transitivity of subtypes
(in a simplified manner):</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>Γ</mi><mo></mo><mi>B</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>A</mi><mspace width="5.0mm"/><mi>Γ</mi><mo></mo><mi>C</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>B</mi></mrow><mrow><mi>Γ</mi><mo></mo><mi>C</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>A</mi></mrow></mfrac></math>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo></math> is the context containing (bound) type variables etc., <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo></math> can be read as <literal>entails</literal>.
Thus, the rule can be read as follows:</simpara>
<blockquote>
<simpara>if the type B is a subtype of type A in context <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>Γ</mi></math> (i.e. with constraints on type variables specified in <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>Γ</mi></math>), and if type C is a subtype of B, then C is also a subtype of A in context <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>Γ</mi></math>.</simpara>
</blockquote>
<simpara>In rules, we sometimes omit the environment if it is not needed.
New information is sometimes added to the environment, in particular, substitutions (that is binding type variables to a type).
The set of substitutions is written with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>θ</mi></math> (theta).
If new substitutions are explicitly added to that set, we write <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>θ</mi><mfenced close=")" open="("><mrow><mi>V</mi><mo></mo><mi>T</mi></mrow></mfenced></math> (<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>V</mi></math> is substituted with type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math>).
Often, these bindings are computed from a parameterized type reference which declares type arguments which are bound to the type variables of the generic declaration.
In this case we simply write <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>θ</mi><mfenced close=")" open="("><mi>p</mi></mfenced></math>, in which <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>p</mi></math> is the parameterized type declaration.
As these new substitutions must become part of a (newly) created environment, we then usually write <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>Γ</mi><mo></mo><mi>θ</mi><mfenced close=")" open="("><mi>p</mi></mfenced></math>.
These substitutions are usually omitted.</simpara>
</section>
<section xml:id="_types-of-an-element">
<title>Types of an Element</title>
<simpara>A variable or other typed element may be associated with three types:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>Declared type: the type explicitly specified in the code, e.g., <literal>var s: string</literal>.</simpara>
</listitem>
<listitem>
<simpara>Inferred type: the type inferred by the type inferencer, e.g., <literal>var s = "Hello"</literal> infers the type of s to <literal>string</literal>. I.e.
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>Γ</mi><mo></mo><mi>s</mi><mi>:</mi><mrow><mi>s</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>g</mi></mrow></math> will be true, or <math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>[</mo><mspace width="-0.167em"/><mrow><mo>[</mo></mrow></mrow><mi>s</mi><mrow><mrow><mo>]</mo></mrow><mspace width="-0.167em"/><mo>]</mo></mrow><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>s</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>g</mi></math>.
If an element is annotated with a type ,i.e. it has a declared type, the inferred type will always be the declared type.</simpara>
</listitem>
<listitem>
<simpara>Actual type: the actual type of a variable during runtime.
This type information is not available at compile time and ignored in this specification.</simpara>
</listitem>
</orderedlist>
<simpara>These types are not type declarations but type references, in fact, as they may be parameterized.
For the sake of simplicity, we often omit the <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>R</mi><mi>e</mi><mi>f</mi></math> suffix to shorten formulas.
Consequently, we define the following properties and pseudo properties for typed elements such as variables:</simpara>
<variablelist>
<varlistentry>
<term>declaredType<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>R</mi><mi>e</mi><mi>f</mi></math> </term>
<listitem>
<simpara>The explicitly declared type, this is usually a real property of the construct.
Not all elements allow the specification of a declared type, such as expressions.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>inferredType<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>R</mi><mi>e</mi><mi>f</mi></math> or <math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>[</mo><mspace width="-0.167em"/><mrow><mo>[</mo></mrow></mrow><mrow><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></mrow><mrow><mrow><mo>]</mo></mrow><mspace width="-0.167em"/><mo>]</mo></mrow></math></term>
<listitem>
<simpara>This pseudo property is the inferred type computed by the type inferencer.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>type<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>R</mi><mi>e</mi><mi>f</mi></math> </term>
<listitem>
<simpara>A pseudo property for elements with a <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi></math> property.
It is similar to the inferred type, i.e. <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mo>=</mo><mrow><mo>[</mo><mspace width="-0.167em"/><mrow><mo>[</mo></mrow></mrow><mi>e</mi><mrow><mrow><mo>]</mo></mrow><mspace width="-0.167em"/><mo>]</mo></mrow></math></simpara>
</listitem>
</varlistentry>
</variablelist>
</section>
</section>
</section>
<section xml:id="_auxiliary-functions" role="language-n4js">
<title>Auxiliary Functions</title>
<simpara>This section describes some auxiliary functions required for definition of type inference rules later on.</simpara>
<section xml:id="_binding">
<title>Binding</title>
<simpara>Binding an identifier (variable reference) to a variable declaration (or
variable definition) is not part of this specification as this is
standard ECMAScript functionality. However, some valid ECMAScript
bindings are permitted due to visibility constraints.</simpara>
<definition>
<title>Binding Relation</title>
<simpara>
<anchor xml:id="binding_relation" xreflabel="[binding_relation]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="binding_relation">Binding Relation</link></simpara>
<simpara>
We define a pseudo relation</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>b</mi><mi>i</mi><mi>n</mi><mi>d</mi><mi>:</mi><mi>V</mi><mi>a</mi><mi>r</mi><mi>i</mi><mi>a</mi><mi>b</mi><mi>l</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>e</mi><mi>r</mi><mi>e</mi><mi>n</mi><mi>c</mi><mi>e</mi><mo>×</mo><mi>V</mi><mi>a</mi><mi>r</mi><mi>i</mi><mi>a</mi><mi>b</mi><mi>l</mi><mi>e</mi><mi>D</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi></math>
<simpara>which binds a reference, i.e. an identifier, to a declaration (e.g.,variable declaration).</simpara>
<simpara>Binding of variable references to declaration is defined by ECMAScript already.
Type references only occur in type expressions, how these are handled is explained in <xref linkend="_type-expressions"/>.</simpara>
<simpara>We usually omit this binding mechanism in most rules and use the reference similarly to the declaration or definition it is bound to.
If a variable reference <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>r</mi></math>, for example, is bound to a variable declaration <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>D</mi></math>, i.e. <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>b</mi><mi>i</mi><mi>n</mi><mi>d</mi><mfenced close=")" open="("><mi>r</mi><mi>D</mi></mfenced></math>, we simply write <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>r</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi></math> instead of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>b</mi><mi>i</mi><mi>n</mi><mi>d</mi><mfenced close=")" open="("><mi>r</mi><mi>D</mi></mfenced><mo>,</mo><mi>D</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi></math> to refer to the type expression
(of the variable).<footnote><simpara>One can interpret this similar to delegate methods, that is, instead of writing <literal role="language-n4js">r.binding().getType()</literal>, a method <literal role="language-n4js">r.getType()\{return binding().getType();</literal> is defined.</simpara></footnote></simpara>
</definition>
<simpara>A <literal>DeclaredType</literal> references the type declaration by its simple name that has been imported from a module specifier.
We define the method <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>b</mi><mi>i</mi><mi>n</mi><mi>d</mi></math> for declared types as well:</simpara>
<definition>
<title>Binding Relation of Types</title>
<simpara>
<anchor xml:id="binding_relation_of_types" xreflabel="[binding_relation_of_types]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="binding_relation_of_types">Binding Relation of Types</link></simpara>
<simpara>
We define a pseudo relation</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>b</mi><mi>i</mi><mi>n</mi><mi>d</mi><mi>:</mi><mi>D</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mo>×</mo><mi>C</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mo>|</mo><mi>I</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>f</mi><mi>a</mi><mi>c</mi><mi>e</mi><mo>|</mo><mi>E</mi><mi>n</mi><mi>u</mi><mi>m</mi></math>
<simpara>which binds a type reference, i.e. a simple name, to the type declaration.</simpara>
</definition>
</section>
<section xml:id="_merging-types">
<title>Merging Types</title>
<simpara>In some cases we have to merge types, e.g., types of a union type or item types of an array.
For that purpose, we define a method <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>m</mi><mi>e</mi><mi>r</mi><mi>g</mi><mi>e</mi></math> as follows.</simpara>
<definition>
<title>Merge Function</title>
<simpara>
<anchor xml:id="merge_function" xreflabel="[merge_function]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="merge_function">Merge Function</link></simpara>
<simpara>
We define a pseudo function</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>m</mi><mi>e</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>:</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mo>×</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>×</mo><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mo></mo><mi mathvariant="script">P</mi><mfenced close=")" open="("><mrow><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi></mrow></mfenced></math>
<simpara>The idea of this function is to remove duplicates.
For example; if a union type contains two type expressions <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>t</mi><msub><mi>e</mi><mn>1</mn></msub></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>t</mi><msub><mi>e</mi><mi>k</mi></msub></math>, and if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>τ</mi><mfenced close=")" open="("><mrow><mi>t</mi><msub><mi>e</mi><mn>1</mn></msub></mrow></mfenced><mo>=</mo><mi>τ</mi><mfenced close=")" open="("><mrow><mi>t</mi><msub><mi>e</mi><mn>2</mn></msub></mrow></mfenced></math>, then <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>m</mi><mi>e</mi><mi>r</mi><mi>g</mi><mi>e</mi><mfenced close=")" open="("><mrow><mi>τ</mi><mfenced close=")" open="("><mrow><mi>t</mi><msub><mi>e</mi><mn>1</mn></msub></mrow></mfenced></mrow><mrow><mi>τ</mi><mfenced close=")" open="("><mrow><mi>t</mi><msub><mi>e</mi><mn>2</mn></msub></mrow></mfenced></mrow></mfenced></math> contains only one element.
The order of the elements is lost, however.</simpara>
</definition>
<section xml:id="_logic-formulas">
<title>Logic Formulas</title>
<simpara>In general, we use a pragmatic mixture of pseudo code, predicate logic, and OCL.
Within constraints (also within the inference rules), the properties defined in the grammar are used.</simpara>
<simpara>In some rules, it is necessary to type the rule variables.
Instead of explicitly checking the metatype (via <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>X</mi></mfenced><mo>=</mo><mi>:</mi><mrow><mi>M</mi><mi>e</mi><mi>t</mi><mi>a</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi></mrow></math>), we precede the variable with the type, that is: <math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>M</mi><mi>e</mi><mi>t</mi><mi>a</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi></mrow><mi>X</mi></math>.</simpara>
<simpara>Instead of "type casting" elements, often properties are simply accessed.
If an element does not define that element, it is either assumed to be false or null by default.</simpara>
<simpara>If a property <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>p</mi></math> is optional and not set, we write <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>p</mi><mo>=</mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi></math> to test its absence.
Note that <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>p</mi><mo>=</mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi></math> is different from <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>p</mi><mo>=</mo><mi>N</mi><mi>u</mi><mi>l</mi><mi>l</mi></math>, as the latter refers to the null type.
Non-terminals may implicitly be subclasses.
In that case, the concrete non-terminal, or type, of a property may be subject for a test in a constraint.</simpara>
</section>
</section>
<section xml:id="_symbols-and-font-convention">
<title>Symbols and Font Convention</title>
<simpara>Variables and their properties are printed in italic when used in formulas (such as rules).
A dot-notation is used for member access, e.g. <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>v</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math>.
Also defined functions are printed in italic, e.g., <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>c</mi><mi>c</mi><mfenced close=")" open="("><mi>r</mi><mi>D</mi></mfenced></math>.
Properties which define sets are usually ordered and we assume 0-indexed access to elements, the index subscripted, e.g., <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>v</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><msub><mi>s</mi><mi>i</mi></msub></math>.</simpara>
<simpara>We use the following symbols and font conventions:</simpara>
<variablelist>
<varlistentry>
<term><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo></math>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo></math>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo></math>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mo>¬</mo></math></term>
<listitem>
<simpara>Logical and, or, exclusive or (xor), and not.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo></math>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo></math>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="bold"><mtext>if</mtext></mstyle><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></math>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="bold"><mtext>then</mtext></mstyle><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></math>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="bold"><mtext>else</mtext></mstyle><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></math> </term>
<listitem>
<simpara>Logical implication, if and only if, and if-then-else.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>true</mtext></mstyle></math>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>false</mtext></mstyle></math>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>null</mtext></mstyle></math>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi></mi></math></term>
<listitem>
<simpara>Boolean true, boolean false, null (i.e., not specified, e.g.,
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>v</mi><mo>.</mo><mi>s</mi><mi>u</mi><mi>p</mi><mo>=</mo></math> means that there are is no <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>u</mi><mi>p</mi></math>
(super class) specified), empty set.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo></math>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo></math>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo></math>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo></math>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mo>|</mo><mi>x</mi><mo>|</mo></math></term>
<listitem>
<simpara>Element of, not an element of, union set, intersection set,
cardinality of set x.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><math xmlns="http://www.w3.org/1998/Math/MathML"><mi mathvariant="script">P</mi><mfenced close=")" open="("><mi>X</mi></mfenced></math></term>
<listitem>
<simpara>Power set of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math>, i.e.
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi mathvariant="script">P</mi><mfenced close=")" open="("><mi>X</mi></mfenced><mo>=</mo><mfenced close="}" open="{"><mrow><mi>U</mi><mi>:</mi><mi>U</mi><mo></mo><mi>X</mi></mrow></mfenced></math>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo></math>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo></math>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo></math></term>
<listitem>
<simpara>Exists, not exists, for all; we write <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>x</mi><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><mi>z</mi><mi>:</mi><mi>P</mi><mfenced close=")" open="("><mi>x</mi><mrow><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></mrow><mi>z</mi></mfenced></math> and say</simpara>
<blockquote>
<simpara>"there exists <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>x</mi><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><mi>z</mi></math> such that predicate <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi></math> is true".</simpara>
</blockquote>
<simpara>Note that <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>x</mi><mi>:</mi><mi>P</mi><mfenced close=")" open="("><mi>x</mi></mfenced><mo></mo><mo></mo><mi>x</mi><mi>:</mi><mo>¬</mo><mi>P</mi><mfenced close=")" open="("><mi>x</mi></mfenced></math>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mrow><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></mrow></mfenced></math></term>
<listitem>
<simpara>(mu) read "<emphasis role="strong">metatype of</emphasis>"; metatype of a variable or property, e.g.,</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mstyle mathvariant="bold"><mtext>if</mtext></mstyle><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mi>μ</mi><mfenced close=")" open="("><mi>x</mi></mfenced><mo>=</mo><mi>:</mi><mrow><mi>C</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi></mrow><mstyle mathvariant="bold"><mtext>then</mtext></mstyle><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mstyle mathvariant="bold"><mtext>else</mtext></mstyle><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></math>
</listitem>
</varlistentry>
<varlistentry>
<term><math xmlns="http://www.w3.org/1998/Math/MathML"><mover accent="true"><mi>x</mi><mo>¯</mo></mover></math></term>
<listitem>
<simpara>Sequence of elements <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>x</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>x</mi><mi>n</mi></msub></math>. E.g., if we want to
define a constraint that the owner of a members of a class
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi></math> is the class, we simply write</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>C</mi><mo>.</mo><mover accent="true"><mrow><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi></mrow><mo>¯</mo></mover><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo>=</mo><mi>C</mi></math>
<simpara>instead of</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mo></mo><mi>m</mi><mo></mo><mi>C</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi><mi>m</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo>=</mo><mi>C</mi></math>
<simpara>or even more complicated with index variables.</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>Sequences are 1-based, e.g., a sequence <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi></math> with length <math xmlns="http://www.w3.org/1998/Math/MathML"><mo>|</mo><mi>s</mi><mo>|</mo><mo>=</mo><mi>n</mi></math>, has elements <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>s</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>s</mi><mi>n</mi></msub></math>.</simpara>
</section>
</section>
</chapter>
<chapter xml:id="_grammar">
<title>Grammar</title>
<section xml:id="_lexical-conventions" role="language-n4js">
<title>Lexical Conventions</title>
<simpara>As a super language on top of ECMAScript, the same lexical conventions are supported as described in [<link linkend="ECMA11a">ECMA11a(p.S7)</link>] within strict mode.
Some further constraints are defined, however, restricting certain constructs. These constraints are described in the following.</simpara>
<section xml:id="_identifier-names-and-identifiers">
<title>Identifier Names and Identifiers</title>
<simpara>Cf. [<link linkend="ECMA11a">ECMA11a(p.S7.6)</link>], [<link linkend="ECMA11a">ECMA11a(p.S11.1.2, p.p.63)</link>] and [<link linkend="ECMA11a">ECMA11a(p.S01.2, p.p.51ff)</link>].</simpara>
<simpara>As a reminder, identifiers are defined as follows in the ECMAScript specification:</simpara>
<programlisting language="xtext" linenumbering="unnumbered">IdentifierName: IdentifierStart* IdentifierPart;
IdentifierStart : UnicodeLetter | '_';
\ UnicodeEscapeSequence</programlisting>
<simpara>N4JS supports a limited form of computed-names for member declarations:</simpara>
<programlisting language="xtext" linenumbering="unnumbered">N4JSPropertyComputedName:
'[' (SymbolLiteralComputedName | StringLiteralComputedName) ']'
;
SymbolLiteralComputedName: N4JSIdentifier '.' N4JSIdentifier ;
StringLiteralComputedName: STRING ;</programlisting>
<simpara>As can be seen, a computed-name must be either</simpara>
<itemizedlist>
<listitem>
<simpara>a symbol reference, e.g., <literal>Symbol.iterator</literal></simpara>
</listitem>
<listitem>
<simpara>a string literal, i.e., a compile time known constant.
This notation is useful when interoperating with libraries that define members whose names contain special characters (e.g., a field name starting with commercial-at)</simpara>
</listitem>
</itemizedlist>
<simpara>In N4JS, identifiers are further constrained in order to avoid ambiguities and to make code more readable.
Some of these constraints will lead to errors, others only to warnings.
They do not apply for identifiers declared in definitions file (n4jsd) in order to enable declaration of external entities.</simpara>
<requirement xml:id="IDE-1">
<title>N4JS Identifier Restrictions</title>
<simpara>
<anchor xml:id="Req-IDE-1" xreflabel="[Req-IDE-1]"/>
<emphasis role="strong">Requirement: IDE-1:</emphasis>
<link linkend="Req-IDE-1">N4JS Identifier Restrictions</link> (ver. 1)</simpara>
<simpara>
. If the following constraints do not hold, errors are created.
.. Leading <literal>$</literal> (dollar sign) character is prohibited for any variable name such as fields, variables, types functions and methods.
.. Leading <literal>_</literal> (underscore) character is not allowed for identifying any functions or methods.</simpara>
</requirement>
<requirement xml:id="IDE-2">
<title>N4JS identifier recommendations</title>
<simpara>
<anchor xml:id="Req-IDE-2" xreflabel="[Req-IDE-2]"/>
<emphasis role="strong">Requirement: IDE-2:</emphasis>
<link linkend="Req-IDE-2">N4JS identifier recommendations</link> (ver. 1)</simpara>
<simpara>
. If the following constraints do not hold, warnings are created.
. Variable names should, in general, be constructed form the 26 ASCII upper and lower case alphabetic letters (a..z, A..Z), from the 10 decimal digits (0..9) and from the <literal>_</literal> (underscore).
Although the usage of the international characters are allowed (according to the ECMAScript specification)
it is discouraged because these characters may not be read or understood well in every circumstance <footnote><simpara><link xl:href="http://javascript.crockford.com/code.html">http://javascript.crockford.com/code.html</link></simpara></footnote>.</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>Type (and Type Variable) Identifiers</simpara>
<programlisting language="xtext" linenumbering="unnumbered">TypeIdentifier: [_A-Z][_a-zA-Z0-9]*
TypeVariableIdentifier: [_A-Z][_a-zA-Z0-9]*</programlisting>
</listitem>
<listitem>
<simpara>Package Identifiers</simpara>
<programlisting language="xtext" linenumbering="unnumbered">PackageIdentifier: [_a-z][._a-zA-Z0-9]*; // i.e. the folder names, must not end with .</programlisting>
</listitem>
<listitem>
<simpara>Member Identifiers and Enum Literals</simpara>
<programlisting language="xtext" linenumbering="unnumbered">InstanceFieldIdentifier: [_a-z][_a-zA-Z0-9]*
StaticFieldIdentifier: [_A-Z][_A-Z0-9]*([_A-Z0-9]+)*
EnumLiteral: [_A-Z][_A-Z0-9]*([_A-Z0-9]+)*</programlisting>
</listitem>
<listitem>
<simpara>Variable and Parameter Names</simpara>
<programlisting language="xtext" linenumbering="unnumbered">VariableIdentifier: [_a-zA-Z0-9]*
􏰀ParameterIdentifier: [_a-z][_a-zA-Z0-9]*</programlisting>
</listitem>
<listitem>
<simpara>Methods</simpara>
<programlisting language="xtext" linenumbering="unnumbered">MethodIdentifier: [_a-z][_a-zA-Z0-9]*;</programlisting>
</listitem>
<listitem>
<simpara>Annotations</simpara>
<programlisting language="xtext" linenumbering="unnumbered">AnnotationIdentifier: [_A-Z][_a-zA-Z0-9]*</programlisting>
</listitem>
</orderedlist>
<simpara>The following rules describe how fully qualified names of elements are created.
Note that these fully qualified names cannot be used in N4JS directly.
Though they may be shown in error messages etc. to identify elements.</simpara>
<programlisting language="xtext" linenumbering="unnumbered">TypeIdentifier: [A-Z][a-zA-Z0-9]*;
PackageIdentifier: [a-z][a-zA-Z0-9]*;
FQNType: (PackageIdentifier '.')+ TypeIdentifier;</programlisting>
</requirement>
</section>
<section xml:id="_this-keyword">
<title>This Keyword</title>
<simpara>Cf. [<link linkend="ECMA11a">ECMA11a(p.S11.1.1, p.p.63)</link>]</simpara>
</section>
<section xml:id="_regular-expression-literals">
<title>Regular Expression Literals</title>
<simpara>Cf. [<link linkend="ECMA11a">ECMA11a(p.S7.8.5)</link>]</simpara>
</section>
<section xml:id="_automatic-semicolon-insertion">
<title>Automatic Semicolon Insertion</title>
<simpara>Cf. [<link linkend="ECMA11a">ECMA11a(p.S7.9)</link>]</simpara>
<simpara><link linkend="_acronyms">ASI</link> is supported by the parser, however warnings are issued.</simpara>
</section>
<section xml:id="_jsdoc">
<title>JSDoc</title>
<simpara>JSDoc are comments similar to JavaDoc in Java for documenting types, functions and members.
There is no semantic information expressed in JSDoc, that is, the behavior of a program must not change if all the JSDoc is removed.
The JSDoc tags and overall syntax is a mixture of tags defined by the <link xl:href="https://developers.google.com/closure/compiler/docs/js-for-compiler">Google Closure Compiler</link>, Java&#8217;s <link xl:href="http://www.oracle.com/technetwork/java/javase/documentation/index-jsp-135444.html">JavaDoc</link> tool and N4-specific tags.</simpara>
<simpara>JSDoc comments are multiline comments, starting with <literal>/**</literal> (instead of simple multiline comments, starting with <literal>/*</literal>).</simpara>
<programlisting language="xtext" linenumbering="unnumbered">MultiLineComment: '/*' MultiLineCommentChars? '*/' // from ECMAScript specification
JSDoc: '/**' MultiLineCommentChars? '*/'</programlisting>
<simpara>In general, JSDoc comments are placed directly before the annotated language element.
In some cases, this is slightly different, such as for method parameters, for example, where it is then explicitly specified.</simpara>
<simpara>The content of JSDoc comments will be covered in more detail in upcoming chapters.
For documentation purposes, multi- and single-line descriptions are used in several constructs.</simpara>
<programlisting language="xtext" linenumbering="unnumbered">MLVALUE: ([^@]+[^\n]+)+;
SLVALUE: ([^\n]+);</programlisting>
<variablelist>
<varlistentry>
<term><literal>MLVALUE</literal> </term>
<listitem>
<simpara>short for <literal>multi-line value</literal>. This is usually only used for the general description of types or members.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>SLVALUE</literal> </term>
<listitem>
<simpara>short for <literal>single-line value</literal>. This is a description which ends at the end of a line.
It is usually used in combination with other tags, e.g., to further describe a parameter of a method.</simpara>
</listitem>
</varlistentry>
</variablelist>
</section>
</section>
</chapter>
<chapter xml:id="_names">
<title>Names</title>
<simpara><emphasis>Visibility</emphasis> defines the scope in which a declaration is visible, that is in which context references can be bound to the declaration.
<emphasis>Access control</emphasis> defines the extent to which types and members are accessible beyond their immediate context.
Access control may, therefore, restrict the visibility of a declaration by limiting its scope.</simpara>
<simpara><emphasis>Extensibility</emphasis> refers to whether a given type can be subtyped, or in the case of members, whether they can be overridden.
Access control is a prerequisite for extensibility which is further explained in <xref linkend="_n4js-specific-classifiers"/></simpara>
<section xml:id="_access-control" role="language-n4js">
<title>Access Control</title>
<simpara>Types from one project may or may not be made accessible to another project.
Likewise, members from a given type may or may not be made accessible to members existing outside that type.
For example, if a developer writes an application which uses a library, which types within that library can the application see?
Given a type that is set as visible, which members of that type can the application see?</simpara>
<simpara>Accessing a type or member actually means that a reference is bound to a
declaration with the same identifier.</simpara>
<simpara>We distinguish the following contexts from which an element is accessed
as follows:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara><emphasis role="strong">Module or type</emphasis>: access from elements in the same module or type.</simpara>
</listitem>
<listitem>
<simpara><emphasis role="strong">Subtype</emphasis>: access from a subtype.</simpara>
</listitem>
<listitem>
<simpara><emphasis role="strong">Project</emphasis>: access from the same project.</simpara>
</listitem>
<listitem>
<simpara><emphasis role="strong">Vendor</emphasis>: access from different project of the same vendor.</simpara>
</listitem>
<listitem>
<simpara><emphasis role="strong">World</emphasis>: access from anything else.</simpara>
</listitem>
</orderedlist>
<simpara>Accessibility is defined by modifiers on types and members, e.g <literal>public</literal>, <literal>protected</literal>, <literal>project</literal>, <literal>private</literal>, via the <literal>export</literal> statement, and by the <literal>@Internal</literal> annotation.
Extensibility is defined by the <literal>@Final</literal> annotation respectively.</simpara>
</section>
<section xml:id="_accessibility-of-types-top-level-variables-and-function-declarations" role="language-n4js">
<title>Accessibility of Types, Top-Level Variables and Function Declarations</title>
<simpara>We define types (classes, interfaces, enums) whereby each type has members (fields and methods, depending on the kind of type).
When we define a type, we need to define whether it is visible only for the specifying module, project or whether that type should be accessible from outside of that project.</simpara>
<simpara>The same is true for variable declarations and function declarations defined as top-level elements of a module.</simpara>
<simpara>The following type access modifiers are supported by N4JS:</simpara>
<programlisting language="xtext" linenumbering="unnumbered">enum TypeAccessModifier: project
| public;</programlisting>
<simpara>If a type is not exported, its visibility is private.
If a type has declared visibility <literal>public</literal>, it may additionally be marked as internal via the annotation <literal>@Internal</literal>.
Thus, we have the following set of type access modifiers:</simpara>
<simpara>TAM = <literal>private</literal> <literal>project</literal> <literal>public@Internal</literal> <literal>public</literal></simpara>
<simpara>That is, in N4JS, only the type access modifiers and are available.
The redundant <literal>project</literal> modifier serves only documentation purpose and can be synthesized if the <literal>export</literal> modifier is preset.</simpara>
<simpara>All other modifiers used here are synthesized as shown in the next example:</simpara>
<formalpara>
<title>Synthesized Type Access Modifiers in N4JS</title>
<para>
<programlisting language="n4js" linenumbering="unnumbered">class C0 {} // private
export class C1 {} // project
export project class C1 {} // project
export @Internal public class C1 {} // public@Internal
export public class C2 {} // public
var v0; // private
export var v1; // project
export project var v1; // project
export @Internal public var v3; // public@Internal
export public var v2; // public
function f0() {} // private
export function f1() {} // project
export project function f1() {} // project
export @Internal public function f3() {} // public@Internal
export public function f2() {} // public</programlisting>
</para>
</formalpara>
<simpara>The access control levels are defined as listed in <xref linkend="tab:type-access-control"/>.</simpara>
<table xml:id="tab:type-access-control" frame="all" rowsep="1" colsep="1">
<title>Type Access Control</title>
<tgroup cols="5">
<colspec colname="col_1" colwidth="20*"/>
<colspec colname="col_2" colwidth="20*"/>
<colspec colname="col_3" colwidth="20*"/>
<colspec colname="col_4" colwidth="20*"/>
<colspec colname="col_5" colwidth="20*"/>
<thead>
<row>
<entry align="center" valign="top" namest="col_1" nameend="col_5"><emphasis role="strong">Type Access</emphasis></entry>
</row>
</thead>
<tbody>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">Modifier</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis role="strong">Module</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis role="strong">Project</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis role="strong">Vendor</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis role="strong">World</emphasis></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><literal>private</literal></simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><literal>project</literal></simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><literal>public@Internal</literal></simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><literal>public</literal></simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
</row>
</tbody>
</tgroup>
</table>
<simpara><literal>TAM</literal> is a totally ordered set:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mstyle mathvariant="monospace"><mtext>private</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>project</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>public@Internal</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle></math>
<requirement xml:id="IDE-3">
<title>Type Access Modifiers</title>
<simpara>
<anchor xml:id="Req-IDE-3" xreflabel="[Req-IDE-3]"/>
<emphasis role="strong">Requirement: IDE-3:</emphasis>
<link linkend="Req-IDE-3">Type Access Modifiers</link> (ver. 1)</simpara>
<simpara>
The following constraints for type access modifiers for a given type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math> (which may be a classifier declaration, a function or a variable) must hold:</simpara>
<itemizedlist>
<listitem>
<simpara>It is an error if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math> is not exported but defined as <literal>project</literal>, <literal>public</literal> or <literal>public@Internal</literal>.</simpara>
</listitem>
<listitem>
<simpara>It is an error if an annotation <literal>@Internal</literal> is present on a module private or <literal>project</literal> visible type.</simpara>
</listitem>
<listitem>
<simpara>The type modifier for all built-in ECMAScript types is <literal>public</literal>.</simpara>
</listitem>
<listitem>
<simpara>The <emphasis>default modifier</emphasis> for user declared exported declarations is <literal>project</literal>.
That is, this modifier is assumed if no modifier is explicitly specified.</simpara>
</listitem>
</itemizedlist>
</requirement>
<definition>
<title>Type Accessibility T</title>
<simpara>
<anchor xml:id="type_accessibility_t" xreflabel="[type_accessibility_t]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="type_accessibility_t">Type Accessibility T</link></simpara>
<simpara>
The function <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>α</mi><mi>T</mi></msub><mi>:</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>e</mi><mi>r</mi><mi>e</mi><mi>n</mi><mi>c</mi><mi>e</mi><mo>×</mo><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>D</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo></mo><mi>B</mi><mi>o</mi><mi>o</mi><mi>l</mi><mi>e</mi><mi>a</mi><mi>n</mi></math> computes whether a given type, (top-level) variable or function
reference can access the declaration that it references.
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>α</mi><mi>T</mi></msub></math> is defined with <xref linkend="tab:type-access-control"/>.</simpara>
<simpara>Formally, we define <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>α</mi><mi>T</mi></msub></math> for a given reference <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>r</mi></math> and a module top level variable, function or type declaration <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>D</mi></math> as follows <footnote><simpara>See for definitions of metatype properties.</simpara></footnote>:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mi>D</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle></mrow><mrow><msub><mi>α</mi><mi>T</mi></msub><mfenced close=")" open="("><mi>r</mi><mi>D</mi></mfenced></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mi>D</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public@Internal</mtext></mstyle><mspace width="3.0mm"/><mi>r</mi><mo>.</mo><mi>v</mi><mi>e</mi><mi>n</mi><mi>d</mi><mi>o</mi><mi>r</mi><mo>=</mo><mi>D</mi><mo>.</mo><mi>v</mi><mi>e</mi><mi>n</mi><mi>d</mi><mi>o</mi><mi>r</mi></mrow><mrow><msub><mi>α</mi><mi>T</mi></msub><mfenced close=")" open="("><mi>r</mi><mi>D</mi></mfenced></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mi>D</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>project</mtext></mstyle><mspace width="3.0mm"/><mi>r</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>j</mi><mi>e</mi><mi>c</mi><mi>t</mi><mo>=</mo><mi>D</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>j</mi><mi>e</mi><mi>c</mi><mi>t</mi></mrow><mrow><msub><mi>α</mi><mi>T</mi></msub><mfenced close=")" open="("><mi>r</mi><mi>D</mi></mfenced></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mi>D</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>private</mtext></mstyle><mspace width="3.0mm"/><mi>r</mi><mo>.</mo><mi>m</mi><mi>o</mi><mi>d</mi><mi>u</mi><mi>l</mi><mi>e</mi><mo>=</mo><mi>D</mi><mo>.</mo><mi>m</mi><mi>o</mi><mi>d</mi><mi>u</mi><mi>l</mi><mi>e</mi></mrow><mrow><msub><mi>α</mi><mi>T</mi></msub><mfenced close=")" open="("><mi>r</mi><mi>D</mi></mfenced></mrow></mfrac></math></simpara>
<simpara>If the type of the arguments is clear from the context, we simply write <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>α</mi><mfenced close=")" open="("><mi>r</mi><mi>D</mi></mfenced></math> instead of <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>α</mi><mi>T</mi></msub><mfenced close=")" open="("><mi>r</mi><mi>D</mi></mfenced></math>.</simpara>
<simpara>Accessibility for types is only checked for types that manifest themselves in the concrete syntax of the N4JS file.
Types that do not have to be written to concrete syntax may be used even if they are generally not accessible.
This is illustrated by <xref linkend="ex:implicit-type-references"/>:</simpara>
<example xml:id="ex:implicit-type-references">
<title>Implicit, allowed type references in N4JS</title>
<programlisting language="n4js" linenumbering="unnumbered">export public class D {
public takeC(): C { .. }
public acceptC(c: C): void { .. }
}
/* private */ class C {}</programlisting>
<programlisting language="n4js" linenumbering="unnumbered">var d: D = new D()
d.acceptC( d.takeC() )</programlisting>
</example>
</definition>
<section xml:id="_accessibility-of-members">
<title>Accessibility of Members</title>
<simpara>Accessibility at the member level is only applicable when the type itself is accessible.
If you cannot access the type, you cannot access any of its members.
Note that inherited members (from an interface or class) become members of a class.
For example, if <literal>B extends A</literal>, and if <literal>A</literal> is not accessible to some client <literal>C</literal> but <literal>B</literal> is, then the members of <literal>A</literal> are indirectly accessible to <literal>C</literal> in so far as they are accessed via <literal>B</literal>.
This is true in particular for interfaces, as their properties are possibly merged into the consuming class (cf. <xref linkend="_implementation-of-members"/>).</simpara>
<simpara>The following member access modifiers are supported by N4JS:</simpara>
<programlisting language="xtext" linenumbering="unnumbered">enum MemberAccessModifier: private
| project
| protected
| public;</programlisting>
<simpara>The modifiers <literal>protected</literal> and <literal>public</literal> may be annotated with <literal>@Internal</literal>.
Thus, we can define the following set of member access modifiers:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mi>M</mi><mi>A</mi><mi>M</mi><mo>=</mo><mrow><mo>{</mo><mspace width="3.0mm"/></mrow></mtd></mtr><mtr><mtd><mstyle mathvariant="monospace"><mtext>private</mtext></mstyle><mo>,</mo><mstyle mathvariant="monospace"><mtext>protected@Internal</mtext></mstyle><mo>,</mo><mstyle mathvariant="monospace"><mtext>protected</mtext></mstyle><mo>,</mo></mtd></mtr><mtr><mtd><mstyle mathvariant="monospace"><mtext>project</mtext></mstyle><mo>,</mo><mstyle mathvariant="monospace"><mtext>public@Internal</mtext></mstyle><mo>,</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle></mtd></mtr><mtr><mtd><mrow><mo>}</mo></mrow></mtd></mtr></mtable></math>
<simpara><literal>protected@Internal</literal> and <literal>public@Internal</literal> are synthesized tags and were introduced as shorthand notation for the <literal>@Internal</literal> annotation together with <literal>protected</literal> or <literal>public</literal> access modifiers.
The <literal>project</literal> modifier is the default one and it can be omitted.
As with the type access modifiers, not all member access modifiers are available in N4JS.
Instead, they are synthesized from different construct as shown in the next example.</simpara>
<example>
<title>Synthesized Member Access Modifiers in N4JS</title>
<programlisting language="n4js" linenumbering="unnumbered">export @Internal public class C {
private f0; // private
f1; // project
project f2; // project
@Internal protected f3; // protected@Internal
protected f4; // protected
@Internal public f5; // public@Internal
public f6; // public
private m0() {} // private
m1() {} // project
project m2() {} // project
@Internal protected m3() {} // protected@Internal
protected m4() {} // protected
@Internal public m5() {} // public@Internal
public m6() {} // public
}</programlisting>
</example>
<simpara><literal>MAM</literal> does not define a totally ordered set. However, its subset</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>M</mi><mi>A</mi><mi>M</mi><mo>\</mo><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>public@Internal</mtext></mstyle></mfenced></math>
<simpara>is a totally ordered set <footnote><simpara>That is, for application developers not providing a library or a public API available to other vendors, member access modifiers behave almost similar to modifiers known from Java.</simpara></footnote> :</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mstyle mathvariant="monospace"><mtext>private</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>project</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>protected@Internal</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>protected</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle></math>
<simpara><xref linkend="tab:Member-Access-Controls"/> shows which members are accessible from where.</simpara>
<table xml:id="tab:Member-Access-Controls" frame="all" rowsep="1" colsep="1">
<title>Member Access Control</title>
<tgroup cols="7">
<colspec colname="col_1" colwidth="25*"/>
<colspec colname="col_2" colwidth="12.5*"/>
<colspec colname="col_3" colwidth="12.5*"/>
<colspec colname="col_4" colwidth="12.5*"/>
<colspec colname="col_5" colwidth="12.5*"/>
<colspec colname="col_6" colwidth="12.5*"/>
<colspec colname="col_7" colwidth="12.5*"/>
<thead>
<row>
<entry align="left" valign="top">Access Modifier</entry>
<entry align="center" valign="top">Inside Module</entry>
<entry align="center" valign="top">Inside Project</entry>
<entry align="center" valign="top">Vendor</entry>
<entry align="center" valign="top">Vendor Subtypes</entry>
<entry align="center" valign="top">Other Projects</entry>
<entry align="center" valign="top">Everywhere</entry>
</row>
</thead>
<tbody>
<row>
<entry align="left" valign="top"><simpara><literal>private</literal></simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>project</literal></simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>protected@Internal</literal></simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>protected</literal></simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>public@Internal</literal></simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>public</literal></simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
</row>
</tbody>
</tgroup>
</table>
<definition>
<title>Type and Member Accessibility Relation</title>
<simpara>
<anchor xml:id="type_and_member_accessibility_relation" xreflabel="[type_and_member_accessibility_relation]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="type_and_member_accessibility_relation">Type and Member Accessibility Relation</link></simpara>
<simpara>
We define the relation</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mo>=</mo><mspace width="1.0em"/><mi>:</mi><mspace width="1.0em"/><mi>T</mi><mi>A</mi><mi>M</mi><mo>×</mo><mi>M</mi><mi>A</mi><mi>M</mi></math>
<simpara>as follows:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mo>=</mo><mspace width="1.0em"/><mi>:</mi><mi>:</mi><mo>=</mo><mrow><mo>{</mo></mrow></mtd><mtd><mfenced close=")" open="("><mstyle mathvariant="monospace"><mtext>private</mtext></mstyle><mstyle mathvariant="monospace"><mtext>private</mtext></mstyle></mfenced><mo>,</mo><mfenced close=")" open="("><mstyle mathvariant="monospace"><mtext>project</mtext></mstyle><mstyle mathvariant="monospace"><mtext>project</mtext></mstyle></mfenced><mo>,</mo></mtd></mtr><mtr><mtd/><mtd><mrow><mfenced close=")" open="("><mstyle mathvariant="monospace"><mtext>public@Internal</mtext></mstyle><mstyle mathvariant="monospace"><mtext>public@Internal</mtext></mstyle></mfenced><mo>,</mo><mfenced close=")" open="("><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle></mfenced><mo>}</mo></mrow></mtd></mtr></mtable></math>
<simpara>We further define the relation <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>:</mi><mi>T</mi><mi>A</mi><mi>M</mi><mo>×</mo><mi>M</mi><mi>A</mi><mi>M</mi></math> as follows:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mo></mo><mi>m</mi><mi>a</mi><msup><mi>m</mi><mi>'</mi></msup><mo></mo><mi>M</mi><mi>A</mi><mi>M</mi><mi>:</mi><mi>t</mi><mi>a</mi><mi>m</mi><mo>=</mo><mi>m</mi><mi>a</mi><msup><mi>m</mi><mi>'</mi></msup><mo></mo><mi>m</mi><mi>a</mi><msup><mi>m</mi><mi>'</mi></msup><mo></mo><mi>m</mi><mi>a</mi><mi>m</mi></mrow><mrow><mi>t</mi><mi>a</mi><mi>m</mi><mo></mo><mi>m</mi><mi>a</mi><mi>m</mi></mrow></mfrac></math>
<simpara>Less, greater then etc. are defined accordingly.</simpara>
</definition>
<definition>
<title>Member Accessibility</title>
<simpara>
<anchor xml:id="member_accessibility" xreflabel="[member_accessibility]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="member_accessibility">Member Accessibility</link></simpara>
<simpara>
The function</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><msub><mi>α</mi><mi>m</mi></msub><mi>:</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>e</mi><mi>r</mi><mi>e</mi><mi>n</mi><mi>c</mi><mi>e</mi><mo>×</mo><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>D</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo></mo><mi>B</mi><mi>o</mi><mi>o</mi><mi>l</mi><mi>e</mi><mi>a</mi><mi>n</mi></math>
<simpara>computes if a given reference can access the member declaration that it
references.</simpara>
</definition>
<simpara>Note that <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>α</mi><mi>m</mi></msub></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>b</mi><mi>i</mi><mi>n</mi><mi>d</mi></math> are different functions.
A reference can only bind to a declaration if it can access the declaration.
However, bind requires more condition to work (correct metatypes, no shadowing etc).</simpara>
<simpara>Formally, we define <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>α</mi><mi>m</mi></msub></math> for a given reference <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>r</mi></math> and member declaration <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi></math> as follows: <footnote><simpara>See <xref linkend="_n4js-specific-classifiers"/> for definitions of metatype properties. Note that <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>r</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>c</mi><mi>e</mi><mi>i</mi><mi>v</mi><mi>e</mi><mi>r</mi></math> always refers to a type declaration in the context of an expression as the receiver type of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>r</mi></math>. The declaring type of the member declaration is considered to be the receiver type of the member reference rather than the type that originally declares the member declaration.</simpara></footnote> <footnote><simpara>Note the Java-like access restriction for members of visibility <literal role="language-n4js">protected</literal> or <literal role="language-n4js">protected@Internal</literal> to code that is responsible for the implementation of that object. [<link linkend="Gosling15a">Gosling15a(p.S6.6.2, p.p.166)</link>]</simpara></footnote></simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mi>M</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle></mrow><mrow><msub><mi>α</mi><mi>m</mi></msub><mfenced close=")" open="("><mi>r</mi><mi>M</mi></mfenced></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mi>r</mi><mo>.</mo><mi>v</mi><mi>e</mi><mi>n</mi><mi>d</mi><mi>o</mi><mi>r</mi><mo>=</mo><mi>M</mi><mo>.</mo><mi>v</mi><mi>e</mi><mi>n</mi><mi>d</mi><mi>o</mi><mi>r</mi><mspace width="3.0mm"/><mi>M</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public@Internal</mtext></mstyle></mrow><mrow><msub><mi>α</mi><mi>m</mi></msub><mfenced close=")" open="("><mi>r</mi><mi>M</mi></mfenced></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mi>r</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo></mo><mi>r</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>c</mi><mi>e</mi><mi>i</mi><mi>v</mi><mi>e</mi><mi>r</mi><mo>.</mo><mi>s</mi><mi>u</mi><mi>p</mi><mi>e</mi><msup><mi>r</mi><mo>*</mo></msup><mspace width="3.0mm"/><mi>M</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>protected</mtext></mstyle></mrow><mrow><msub><mi>α</mi><mi>m</mi></msub><mfenced close=")" open="("><mi>r</mi><mi>M</mi></mfenced></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mi>r</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo></mo><mi>r</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>c</mi><mi>e</mi><mi>i</mi><mi>v</mi><mi>e</mi><mi>r</mi><mo>.</mo><mi>s</mi><mi>u</mi><mi>p</mi><mi>e</mi><msup><mi>r</mi><mo>*</mo></msup><mspace width="3.0mm"/><mi>r</mi><mo>.</mo><mi>v</mi><mi>e</mi><mi>n</mi><mi>d</mi><mi>o</mi><mi>r</mi><mo>=</mo><mi>M</mi><mo>.</mo><mi>v</mi><mi>e</mi><mi>n</mi><mi>d</mi><mi>o</mi><mi>r</mi><mspace width="3.0mm"/><mi>M</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>protected@Internal</mtext></mstyle></mrow><mrow><msub><mi>α</mi><mi>m</mi></msub><mfenced close=")" open="("><mi>r</mi><mi>M</mi></mfenced></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mi>r</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>j</mi><mi>e</mi><mi>c</mi><mi>t</mi><mo>=</mo><mi>M</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>j</mi><mi>e</mi><mi>c</mi><mi>t</mi><mspace width="3.0mm"/><mi>M</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>project</mtext></mstyle></mrow><mrow><msub><mi>α</mi><mi>m</mi></msub><mfenced close=")" open="("><mi>r</mi><mi>M</mi></mfenced></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mi>r</mi><mo>.</mo><mi>m</mi><mi>o</mi><mi>d</mi><mi>u</mi><mi>l</mi><mi>e</mi><mo>=</mo><mi>r</mi><mo>.</mo><mi>m</mi><mi>o</mi><mi>d</mi><mi>u</mi><mi>l</mi><mi>e</mi><mspace width="3.0mm"/><mi>M</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>private</mtext></mstyle></mrow><mrow><msub><mi>α</mi><mi>m</mi></msub><mfenced close=")" open="("><mi>r</mi><mi>M</mi></mfenced></mrow></mfrac></math></simpara>
<simpara>If the type of the arguments is clear from the context, we simply write <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>α</mi><mfenced close=")" open="("><mi>r</mi><mi>M</mi></mfenced></math> instead of <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>α</mi><mi>m</mi></msub><mfenced close=")" open="("><mi>r</mi><mi>M</mi></mfenced></math>.</simpara>
<simpara>Although private members are accessible inside a module, it is not possible to redefine (override etc.) these members (see <xref linkend="_redefinition-of-members"/>).</simpara>
<requirement xml:id="IDE-4">
<title>Default Member Access Modifiers</title>
<simpara>
<anchor xml:id="Req-IDE-4" xreflabel="[Req-IDE-4]"/>
<emphasis role="strong">Requirement: IDE-4:</emphasis>
<link linkend="Req-IDE-4">Default Member Access Modifiers</link> (ver. 1)</simpara>
<simpara>
The following constraints for member access modifiers must hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>The <emphasis>default modifier</emphasis> for members of user-declared classes is <literal>project</literal>.</simpara>
</listitem>
<listitem>
<simpara>The <emphasis>default modifier</emphasis> for members of interfaces is the same as the visibility of the interface itself, except for private interfaces.
For private interfaces, the default modifier for members is <literal>project</literal>.</simpara>
</listitem>
<listitem>
<simpara>The modifier for enum literals is always <literal>public</literal>.</simpara>
</listitem>
<listitem>
<simpara>Private members of a classifier are visible and accessible within a module, i.e. you can access the private method of a class, for instance,
when the use of the class as receiver is in the same module where the class has been defined.
In case of inheritance, private members are visible if the host (e.g. the class) is in the same module as the provider (the extended class).
This also means that abstract members of a class are allowed to be defined private as they may be overridden within a module.</simpara>
</listitem>
</orderedlist>
</requirement>
<example>
<title>Type and Member Access Modifiers</title>
<programlisting language="n4js" linenumbering="unnumbered">export project interface I {
project foo();
}
// This interface may be used publicly, but since the inherited method foo() is project visible only,
// it is not possible to implement that interface in other projects.
export public interface J extends I {
}
// Since the visibility of foo is set to public here, it is possible to implement this interface in other projects.
export public interface K extends I {
@Override public foo();
}
// Since foo is private, it is not possible to subclass the class in other modules. Still, it
// is possible to use it in other projects.
// XPECT noerrors --&gt;
export public abstract class C {
private abstract foo();
public static C instance() {
// return some default instance
...
}
}</programlisting>
<simpara>As demonstrated in the following snippet, class <literal>C</literal> can be used but not subclassed in other modules:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">import C from "C"
// XPECT errors --&gt; "Cannot extend class C: cannot implement one or more non-accessible abstract members: method C.foo." at "C"
export public abstract class Sub extends C {
}
// XPECT noerrors --&gt;
var c: C = C.instance();</programlisting>
</example>
<simpara>Members of non-visible types are, in general, not visible for a client.
Members may become visible, however, if they are accessed via a visible type which inherits these members.
The following examples demonstrate two different scenarios:</simpara>
<example>
<title>Declaring type vs receiver type</title>
<simpara>It is especially noteworthy that the declaring type of a member is
generally not considered for the accessibility of that member but only
the receiver type is relevant.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class Base {
public m(b: Base): void {}
}
export public class ApiType extends Base {
}</programlisting>
<programlisting language="n4js" linenumbering="unnumbered">import * as N from "Base";
var t = new N.ApiType();
// member can be accessed although type Base is not exported:
t.m(t);</programlisting>
</example>
<simpara>The property access to the member <literal>m</literal> is valid because it fulfills the constraints for accessibility.
The receiver of the property access is <literal>t</literal> of type <literal>ApiType</literal>.
That type is exported and accessible.
Therefore, the inherited member <literal>m</literal> is also considered valid since it is also defined <literal>public</literal>.</simpara>
<simpara>This rule allows for defining a common functionality in module or project visible types that becomes accessible via exported, visible subtypes.</simpara>
<example>
<title>Member Access and Type Access Interplay</title>
<simpara>The following example demonstrates the behavior when
non-visible types are used as return types. In this case, all the
members of the non-visible types are not accessible, even if they have a
public access modifier.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class A {
foo(): void{}
}
export public class C {
public getHidden(): A { return new A() };
}</programlisting>
<programlisting language="n4js" linenumbering="unnumbered">import * as Nfrom "A"
class Client {
f(): void {
var c = new N.C();
// XPECT noerrors --&gt; Getting an instance the hidden type is possible
var hidden = c.getHidden();
// XPECT errors --&gt; "The method foo is not visible." at "foo"
hidden.foo();
}
}</programlisting>
</example>
</section>
<section xml:id="_valid-names">
<title>Valid Names</title>
<simpara>For identifier and property names, the same constraints as in ECMAScript
[<link linkend="ECMA11a">ECMA11a(p.S7.6)</link>]
[<link linkend="ECMA11a">ECMA11a(p.S7.6.1.2)</link>]
[<link linkend="ECMA11a">ECMA11a(p.S11.6)</link>] are applied.</simpara>
<simpara>Identifier names in N4JS are defined similar to [<link linkend="ECMA11a">ECMA11a(p.S11.6)</link>], making it possible to even use reserved words (keywords etc.).
For some element types, errors or warnings are issued in order to prevent problems when using these names.</simpara>
<requirement xml:id="IDE-5">
<title>Forbidden Identifier Names in N4JS</title>
<simpara>
<anchor xml:id="Req-IDE-5" xreflabel="[Req-IDE-5]"/>
<emphasis role="strong">Requirement: IDE-5:</emphasis>
<link linkend="Req-IDE-5">Forbidden Identifier Names in N4JS</link> (ver. 1)</simpara>
<simpara>
</simpara>
<simpara>In N4JS mode, errors are generated in the following cases:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>A name of a type equals</simpara>
<orderedlist numeration="loweralpha">
<listitem>
<simpara>an access modifier</simpara>
</listitem>
<listitem>
<simpara><literal>set</literal> or <literal>get</literal></simpara>
</listitem>
<listitem>
<simpara>an ECMAScript keyword</simpara>
</listitem>
<listitem>
<simpara>a boolean literal</simpara>
</listitem>
<listitem>
<simpara>the name of a base type</simpara>
</listitem>
</orderedlist>
</listitem>
<listitem>
<simpara>The name of a function or function expression equals (but not the method)</simpara>
<orderedlist numeration="loweralpha">
<listitem>
<simpara>an ECMAScript keyword</simpara>
</listitem>
<listitem>
<simpara>a reserved future ECMAScript word</simpara>
</listitem>
</orderedlist>
</listitem>
</orderedlist>
</requirement>
<requirement xml:id="IDE-6">
<title>Undesired Identifier Names in N4JS</title>
<simpara>
<anchor xml:id="Req-IDE-6" xreflabel="[Req-IDE-6]"/>
<emphasis role="strong">Requirement: IDE-6:</emphasis>
<link linkend="Req-IDE-6">Undesired Identifier Names in N4JS</link> (ver. 1)</simpara>
<simpara>
</simpara>
<simpara>In N4JS mode, warnings are generated in the following cases:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>The name of a member (of a non external type)</simpara>
<orderedlist numeration="loweralpha">
<listitem>
<simpara>equals the name of a base type <footnote><simpara><literal role="language-n4js">string, boolean, number, any, null</literal></simpara></footnote> but the type of the variable is different from that type</simpara>
</listitem>
<listitem>
<simpara>is not static nor const but starts with an upper case letter</simpara>
</listitem>
</orderedlist>
</listitem>
<listitem>
<simpara>The name of a non-external N4 types (class, interface, enum) starts with a lower case letter</simpara>
</listitem>
<listitem>
<simpara>The name of a variable (incl. formal parameter or catch variable and fields)</simpara>
<orderedlist numeration="loweralpha">
<listitem>
<simpara>equals an N4JS keyword</simpara>
</listitem>
<listitem>
<simpara>equals the name of a base type but the type of the variable is different from that type</simpara>
</listitem>
<listitem>
<simpara>is not const but starts with an upper case letter</simpara>
</listitem>
</orderedlist>
</listitem>
</orderedlist>
</requirement>
</section>
<section xml:id="_qualified-names">
<title>Qualified Names</title>
<simpara>In N4JS source code, types can only be referenced using their simple name.
There is no such thing as a fully-qualified type name in N4JS or ECMAScript.
Types are uniquely identified by their simple name, maybe together with an import and the module specifier given there.
Clashes between simple names of imported type and locally declared types can be resolved by importing the type under an alias.</simpara>
<simpara>In some cases, however, we need to define references to types or even members.
For example, if we want to reference certain members in JSDoc comments or for unambiguous error messages.
For this reason, we formally define qualified names even if they cannot occur in source code.</simpara>
<simpara><xref linkend="tab:typenames"/> shows the different names of a given type <literal>C</literal>, defined in a module
<literal>M.n4js</literal>, defined in a package <literal>p</literal> of a project <literal>MyProject</literal>.</simpara>
<simpara>Simple type names are used throughout N4JS code in order to refer to types.
The different forms of module specifiers are only used in import declarations in the string following the <literal>from</literal> keyword.</simpara>
<table xml:id="tab:typenames" frame="all" rowsep="1" colsep="1">
<title>Different forms of module and type specifiers.</title>
<tgroup cols="2">
<colspec colname="col_1" colwidth="50*"/>
<colspec colname="col_2" colwidth="50*"/>
<thead>
<row>
<entry align="left" valign="top">Name</entry>
<entry align="center" valign="top">Example</entry>
</row>
</thead>
<tbody>
<row>
<entry align="left" valign="top"><simpara>Simple Type Name</simpara></entry>
<entry align="center" valign="top"><simpara><literal>C</literal></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>(Plain) Module Specifier</simpara></entry>
<entry align="center" valign="top"><simpara><literal>p/M</literal></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>Complete Module Specifier</simpara></entry>
<entry align="center" valign="top"><simpara><literal>MyProject/p/M</literal></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>Complete Type Specifier</simpara></entry>
<entry align="center" valign="top"><simpara><literal>MyProject/p/M.C</literal></simpara></entry>
</row>
</tbody>
</tgroup>
</table>
</section>
<section xml:id="_name-duplicates">
<title>Name Duplicates</title>
<simpara>There might be cases where two (or more) scopes created by different entities with the same (simple) name overlap.
Those situations can be referred to as shadowing, hiding, or obscuring.
While they are not the same, many of those cases are not allowed in N4JS.
For simplicity we refer to them all as shadowing or duplication (see below).
Rule of thumb is that N4JS allows everything that is allowed in JavaScript StrictMode.</simpara>
<section xml:id="_lexical-environment">
<title>Lexical Environment</title>
<simpara>N4JS handles scopes similar to ECMAScript, so that function scope is applied to variables declared with <literal>var</literal> (and parameters), and block scope for variables is declared with <literal>let</literal> or <literal>const</literal>.
In general, ECMAScript defines <emphasis>Lexical Environments</emphasis> as a specification type used to define the association of Identifiers to specific variables and functions based upon the lexical nesting structure of ECMAScript code [<link linkend="ECMA11a">ECMA11a(p.10.2)</link>].</simpara>
<variablelist>
<varlistentry>
<term>Elements that introduce lexical environments: </term>
<listitem>
<simpara><literal>FunctionDefinition</literal>, <literal>VariableDeclaration</literal>, <literal>CatchBlock</literal>, <literal>WithStatement</literal>, <literal>ImportDeclaration</literal></simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>N4JS specific declarations: </term>
<listitem>
<simpara><literal>N4ClassDeclaration</literal>, <literal>N4InterfaceDeclaration</literal>,
<literal>N4EnumDeclaration</literal>, <literal>N4MethodDeclaration</literal>.</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>Additionally, a built-in lexical environment that defines global scope exists for every <literal>Script</literal>.</simpara>
<simpara>Since N4JS is extended (and a bit more strict) JS strict mode, <emphasis>Object environment record</emphasis>s created by <literal>WithStatement</literal> are not taken into account when resolving duplicates.
This applies to both N4JS mode and JS strict mode.
In unrestricted JS the <literal>WithStatement</literal> is allowed but duplicates are not validated.</simpara>
<note>
<simpara>In case of names introduced by <literal>ImportDeclaration</literal>s only <literal>NamedImportSpecifiers</literal>s are taken into account (their import name or its alias if available).
<literal>WildcardImportSpecifiers</literal>s are not taken into account.
Potential optimizations by compiler or user annotation are also not currently taken into account during analysis.</simpara>
</note>
</section>
<section xml:id="_duplicates-and-shadowing">
<title>Duplicates and Shadowing</title>
<definition>
<title>Shadowing Overriding Duplicates</title>
<simpara>
<anchor xml:id="shadowing_overriding_duplicates" xreflabel="[shadowing_overriding_duplicates]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="shadowing_overriding_duplicates">Shadowing Overriding Duplicates</link></simpara>
<simpara>
</simpara>
<simpara>Two elements with the same name declared in the same lexical environment (cf. [<link linkend="ECMA11a">ECMA11a(p.S10.2.2.1)</link>] are called <emphasis>duplicates</emphasis>.
An element defined in an environment <emphasis>shadows</emphasis> all elements with the same name in outer environments.</simpara>
<simpara>In class hierarchies, a member with the same name as a member defined in a supertype is said to override the latter.
Overriding is discussed in <xref linkend="_redefinition-of-members"/>.</simpara>
<simpara>For the following constraints, we make the following assumptions:</simpara>
<itemizedlist>
<listitem>
<simpara>Names of function expressions or declarations are handles similar to locally declared elements in the function.
Function declarations are additionally declaring a name in their outer scope.</simpara>
</listitem>
<listitem>
<simpara>The implicit formal parameter <literal>arguments</literal> is treated similar to declared formal parameters.</simpara>
</listitem>
<listitem>
<simpara>Formal parameters are defined in the lexical environment of a function, that is, they are defined in the same lexical environment as local <literal>var</literal>-variables or other declarations in that function.</simpara>
</listitem>
<listitem>
<simpara>The "global" environment contains objects globally defined by the execution environment.</simpara>
</listitem>
</itemizedlist>
</definition>
<requirement xml:id="IDE-7">
<title>Forbidden Duplicates</title>
<simpara>
<anchor xml:id="Req-IDE-7" xreflabel="[Req-IDE-7]"/>
<emphasis role="strong">Requirement: IDE-7:</emphasis>
<link linkend="Req-IDE-7">Forbidden Duplicates</link> (ver. 1)</simpara>
<simpara>
There must be no two elements defined in the same lexical environment with the same name,
that is, there must be no duplicates.</simpara>
</requirement>
<requirement xml:id="IDE-8">
<title>Forbidden Shadowing</title>
<simpara>
<anchor xml:id="Req-IDE-8" xreflabel="[Req-IDE-8]"/>
<emphasis role="strong">Requirement: IDE-8:</emphasis>
<link linkend="Req-IDE-8">Forbidden Shadowing</link> (ver. 1)</simpara>
<simpara>
In general, shadowing is allowed in N4JS.
But it is not allowed in the following cases:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>No element defined in the standard global scope must be shadowed.</simpara>
</listitem>
<listitem>
<simpara>There must be no function shadowing another function.</simpara>
</listitem>
<listitem>
<simpara>Elements defined in catch blocks must not shadow elements defined all parent non-catch-block environments.</simpara>
</listitem>
</orderedlist>
</requirement>
<requirement xml:id="IDE-9">
<title>Forbidden Names</title>
<simpara>
<anchor xml:id="Req-IDE-9" xreflabel="[Req-IDE-9]"/>
<emphasis role="strong">Requirement: IDE-9:</emphasis>
<link linkend="Req-IDE-9">Forbidden Names</link> (ver. 1)</simpara>
<simpara>
1. In the script environment, it is not allowed to use the name
’arguments’.<footnote><simpara>This conflicts with the implicit parameter arguments introduced by the transpiler when wrapping the script/module into a definition function.</simpara></footnote></simpara>
<simpara>+
<xref linkend="fig-forbidden-shadowing"/> shows nested lexical environments with named elements declared inside (all named <literal>x</literal> here), the forbidden cases are marked with arrows
(the numbers at the left side refer to the numbers in <xref linkend="Req-IDE-8"/>.</simpara>
<figure xml:id="fig-forbidden-shadowing">
<title>Forbidden Shadowing</title>
<mediaobject>
<imageobject>
<imagedata fileref="chapters/03_names/fig/shadowing.png" width="50%" scalefit="1" align="center"/>
</imageobject>
<textobject><phrase>shadowing</phrase></textobject>
</mediaobject>
</figure>
<simpara>Rationale:</simpara>
<itemizedlist>
<listitem>
<simpara>We expect only few named nested functions.
Since this is expected to be a rare case, no shadowing should occur there as this is maybe not expected by the programmer.</simpara>
</listitem>
<listitem>
<simpara>It is typical that nested environments define local variables.
In particular helper variables (such as <literal>i: number i</literal> or <literal>s: string</literal> ) are expected to be used quite often.
Since this is a typical case, we allow shadowing for local variables.</simpara>
</listitem>
<listitem>
<simpara>Function declarations may shadow type declarations.
However, both entities are to be handled completely differently, so that an error will occur if the shadowing is ignored by the programmer anyway.</simpara>
</listitem>
</itemizedlist>
</requirement>
</section>
</section>
</section>
</chapter>
<chapter xml:id="_types">
<title>Types</title>
<section xml:id="_overview" role="language-n4js">
<title>Overview</title>
<simpara>N4JS is essentially ECMAScript with the inclusion of types.
In the following sections we will describe how types are defined and used in N4JS.</simpara>
<simpara>Besides standard JavaScript types, the following metatypes are introduced:</simpara>
<itemizedlist>
<listitem>
<simpara>Classifiers, that is class or interface (see <xref linkend="_classifiers"/>)</simpara>
</listitem>
<listitem>
<simpara>Enum</simpara>
</listitem>
</itemizedlist>
<simpara>Classifiers, methods and functions may be declared generic.</simpara>
<simpara>Types are related to each other by the subtype relation.</simpara>
<definition>
<title>Subtype Relation</title>
<simpara>
<anchor xml:id="subtype_relation" xreflabel="[subtype_relation]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="subtype_relation">Subtype Relation</link></simpara>
<simpara>
We use <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>u</mi><mi>b</mi><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi></math> for the general subtype relation or type conformance.</simpara>
<simpara>In nominal typing, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>S</mi></math> means that <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi></math> is a (transitive) supertype of <emphasis>T</emphasis>.
Generally in structural typing, this means that <emphasis>T</emphasis> <emphasis>conforms</emphasis> to <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi></math>.
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/></math> is defined transitive reflexive by default.</simpara>
<simpara>We write <math xmlns="http://www.w3.org/1998/Math/MathML"><mo>&lt;</mo></math> to refer to the transitive non-reflexive relation, that is <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>&lt;</mo><mi>S</mi><mo></mo><mi>T</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>S</mi><mo></mo><mi>T</mi><mo></mo><mi>S</mi></math></simpara>
</definition>
<simpara>Whether nominal or structural typing is used depends on the declaration of the type or the reference.
This is explained further in <xref linkend="_structural-typing"/>.</simpara>
<simpara>For convenience reasons, we sometimes revert the operator, that is <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>S</mi><mo></mo><mi>S</mi><mi>:</mi><mo>&gt;</mo><mi>T</mi></math>.
We write <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mspace width="1.0mm"/><mo></mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>S</mi></math> if <emphasis>T</emphasis> is not type conforming to <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi></math>. (cf. [<link linkend="Gosling12a">Gosling12a(p.S4.10)</link>])</simpara>
<simpara>Join and meet are defined as follows:</simpara>
<definition>
<title>Join and Meet</title>
<simpara>
<anchor xml:id="join_and_meet" xreflabel="[join_and_meet]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="join_and_meet">Join and Meet</link></simpara>
<simpara>
A type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>J</mi></math> is called a <emphasis>join</emphasis> (or least common supertype, ) of a pair of types <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi></math> and <emphasis>T</emphasis>, written <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mo></mo><mi>T</mi><mo>=</mo><mi>J</mi></math>, if</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>J</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>J</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>L</mi><mi>:</mi><mrow><mo>(</mo><mrow><mi>S</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>L</mi><mo>)</mo></mrow><mo></mo><mrow><mo>(</mo><mrow><mi>T</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>L</mi><mo>)</mo></mrow><mo></mo><mi>J</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>L</mi></mrow></mrow></math></simpara>
<simpara>Similarly, we say that a type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi></math> is a <emphasis>meet</emphasis> (or greatest common subtype, ) of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi></math> and <emphasis>T</emphasis>, written <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mo></mo><mi>T</mi><mo>=</mo><mi>M</mi></math>, if<?asciidoc-br?></simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>S</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>T</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>L</mi><mi>:</mi><mrow><mo>(</mo><mrow><mi>L</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>S</mi><mo>)</mo></mrow><mo></mo><mrow><mo>(</mo><mrow><mi>L</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>T</mi><mo>)</mo></mrow><mo></mo><mi>L</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>M</mi></mrow></mrow></math></simpara>
</definition>
<simpara>Note that this declarative definition needs to be specified in detail for special cases, such as union and intersection types.
Usually, the union type of two types is also the join.</simpara>
<simpara><xref linkend="fig-cd-predefined-type-hierarchy"/> summarizes all predefined types,
that is primitive and built-in ECMAScript and N4JS types.
Specific rules for the subtype relation are defined in the following sections.
This type hierarchy shows <literal>any</literal> and <literal>undefined</literal> as the top and bottom type (cf. [<link linkend="Pierce02a">Pierce02a(p.15.4)</link>]) We define these types here explicitly:</simpara>
<definition>
<title>Top and Bottom Type</title>
<simpara>
<anchor xml:id="top_and_bottom_type" xreflabel="[top_and_bottom_type]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="top_and_bottom_type">Top and Bottom Type</link></simpara>
<simpara>
We call <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mi>o</mi><mi>p</mi></math> the top type, if for all types <emphasis>T</emphasis> the relation <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>T</mi><mi>o</mi><mi>p</mi></math> is true.
We call <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>B</mi><mi>o</mi><mi>t</mi></math> the bottom type, if for all types <emphasis>T</emphasis> the relation <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>B</mi><mi>o</mi><mi>t</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>T</mi></math> is true.
In N4JS, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mi>o</mi><mi>p</mi><mo>=</mo><mi>a</mi><mi>n</mi><mi>y</mi></math>, the bottom type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>B</mi><mi>o</mi><mi>t</mi><mo>=</mo><mi>u</mi><mi>n</mi><mi>d</mi><mi>e</mi><mi>f</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>d</mi></math>.</simpara>
</definition>
<simpara><literal>null</literal> is almost similar to <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>B</mi><mi>o</mi><mi>t</mi></math>, except that it is not a subtype of <literal>undefined</literal>.</simpara>
<figure xml:id="fig-cd-predefined-type-hierarchy">
<title>Predefined Types Hierarchy</title>
<mediaobject>
<imageobject>
<imagedata fileref="chapters/04_types/fig/cdPredefinedTypesHierarchy.svg" width="80%" scalefit="1" align="center"/>
</imageobject>
<textobject><phrase>cdPredefinedTypesHierarchy</phrase></textobject>
</mediaobject>
</figure>
<simpara>For every primitive type there is a corresponding built-in type as defined in [<link linkend="ECMA11a">ECMA11a</link>], e.g. <literal>string</literal> and <literal>String</literal>.
There is no inheritance supported for primitive types and built-in types – these types are final.</simpara>
<simpara>Although the diagram shows inheritance between <literal>void</literal> and <literal>undefined</literal>, this relationship is only semantic: <literal>void</literal> is a refinement of <literal>undefined</literal> from a type system viewpoint.
The same applies to the relation of <literal>Object</literal> as well as the subtypes shown for <literal>string</literal> and <literal>String</literal>.</simpara>
<example xml:id="ex:class-hierarchy">
<title>Type Examples, Class Hierarchy</title>
<simpara>In the following examples, we assume the following classes to be given:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">// C &lt;: B &lt;: A
class A{}
class B extends A{}
class C extends B{}
// independent types X, Y, and Z
class X{} class Y{} class Z{}
// interfaces I, I1 &lt;: I, I2 &lt;: I, I3
interface I
interface I1 extends I {}
interface I2 extends I {}
interface I3 {}
// class implementing the interfaces
class H1 implements I1{}
class H12 implements I1,I2{}
class H23 implements I2,I3{}
// a generic class with getter (contra-variance) and setter (co-variance)
class G&lt;T&gt; {
get(). T;
set(x: T): void;
}</programlisting>
</example>
</section>
<section xml:id="_type-expressions" role="language-n4js">
<title>Type Expressions</title>
<simpara>In contrast to ECMAScript, N4JS defines static types.
Aside from simple type references, type expressions may be used to specify the type of variables.</simpara>
<section xml:id="_syntax">
<title>Syntax</title>
<simpara>The listing <xref linkend="lst:EBNFTypeExpression"/> summarizes the type expression grammar.
Depending on the context, not all constructs are allowed.
For example, the variadic modifier is only allowed for function parameters.</simpara>
<simpara>References to user-declared types are expressed via <literal>ParameterizedTypeRef</literal>.
This is also true for non-generic types, as the type arguments are optional.
See <xref linkend="_parameterized-types"/> for details on that reference.</simpara>
<simpara>For qualified names and type reference names, see <xref linkend="_qualified-names"/></simpara>
<simpara>The type expressions are usually added to parameter, field, or variable declarations as a suffix, separated with colon (<literal>:</literal>).
The same is true for function, method, getter or setter return types.
Exceptions in the cases of object literals or destructuring are explained later on.</simpara>
<example>
<title>Type Annotation Syntax</title>
<simpara>The following two listings show the very same code and type annotations are provided on
the left hand side. For simplicity, <literal>string</literal> is always used as type expression.<footnote><simpara>In the N4JS IDE, type annotations are highlighted differently than ordinary code.</simpara></footnote></simpara>
<informaltable frame="none" rowsep="0" colsep="0">
<tgroup cols="2">
<colspec colname="col_1" colwidth="50*"/>
<colspec colname="col_2" colwidth="50*"/>
<tbody>
<row>
<entry align="left" valign="top"><programlisting language="n4js" linenumbering="unnumbered">var x: string;
var s: string = "Hello";
function f(p: string): string {
return p;
}
class C {
f: string;
s: string = "Hello";
m(p: string): string {
return p;
}
get x(): string {
return this.f;
}
set x(v: string) {
this.f = v;
}
}</programlisting></entry>
<entry align="left" valign="top"><programlisting language="n4js" linenumbering="unnumbered">var x;
var s = "Hello";
function f(p) {
return p;
}
class C {
f;
s = "Hello";
m(p) {
return p;
}
get x() {
return this.f;
}
set x(v) {
this.f = v;
}
}</programlisting></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<simpara>The code on the right hand side is almost all valid ECMAScript 2015, with the exception of field declarations in the class.
These are moved into the constructor by the N4JS transpiler.</simpara>
</example>
</section>
<section xml:id="_properties">
<title>Properties</title>
<simpara>Besides the properties indirectly defined by the grammar, the following pseudo properties are used for type expressions:</simpara>
<simpara>Properties of <literal>TypeExpression</literal>:</simpara>
<variablelist>
<varlistentry>
<term><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>v</mi><mi>a</mi><mi>r</mi></math> </term>
<listitem>
<simpara>If true, variable of that type is variadic. This is only allowed for parameters. Default value: <literal>false</literal>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>o</mi><mi>p</mi><mi>t</mi></math> </term>
<listitem>
<simpara>If true, variable of that type is optional. This is only allowed for parameters and return types.
This actually means that the type <emphasis>T</emphasis> actually is a union type of <literal>Undef|<emphasis>T</emphasis></literal>.
Default value: <literal>false</literal>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>o</mi><mi>p</mi><mi>t</mi><mi>v</mi><mi>a</mi><mi>r</mi></math> </term>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>o</mi><mi>p</mi><mi>t</mi><mi>v</mi><mi>a</mi><mi>r</mi><mo>=</mo><mi>v</mi><mi>a</mi><mi>r</mi><mo></mo><mi>o</mi><mi>p</mi><mi>t</mi></math>, reflect the facts that a variadic parameter is also optional (as its cardinality is <math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mfenced close="]" open="["><mrow><mn>0</mn><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mi>n</mi></mrow></mfenced><mo>)</mo></mrow><mo>.</mo></math></simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><mi>n</mi><mi>t</mi><mi>i</mi><mi>t</mi><mi>y</mi></math> </term>
<listitem>
<simpara>Pseudo property referencing the variable declaration (or expression) which <literal>owns</literal> the type expression.</simpara>
</listitem>
</varlistentry>
</variablelist>
</section>
<section xml:id="_semantics">
<title>Semantics</title>
<simpara>The ECMAScript types <emphasis>undefined</emphasis> and <emphasis>null</emphasis> are also supported.
These types cannot be referenced directly, however.
Note that <literal>void</literal> and <emphasis>undefined</emphasis> are almost similar.
Actually, the inferred type of a types element with declared type of <literal>void</literal> will be <emphasis>undefined</emphasis>.
The difference between void and undefined is that an element of type void can never have another type,
while an element of type undefined may be assigned a value later on and thus become a different type.
<literal>void</literal> is only used for function and method return types.</simpara>
<simpara>Note that not any type reference is allowed in any context.
Variables or formal parameters must not be declared <literal>void</literal> or union types must not be declared dynamic, for example.
These constraints are explained in the following section.</simpara>
<simpara>The types mentioned above are described in detail in the next sections.
They are hierarchically defined and the following list displays all possible types.
Note that all types are actually references to types.
A type variable can only be used in some cases, e.g., the variable has to be visible in the given scope.</simpara>
<bridgehead xml:id="_ecmascript-types" renderas="sect4">ECMAScript Types</bridgehead>
<variablelist>
<varlistentry>
<term>Predefined Type</term>
<listitem>
<simpara>Predefined types, such as String, Number, or Object; and .</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>Array Type</term>
<listitem>
<simpara><xref linkend="_array-object-type"/>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>Function Type</term>
<listitem>
<simpara>Described in <xref linkend="_functions"/>, <xref linkend="_function-type"/>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>Any Type</term>
<listitem>
<simpara><xref linkend="_any-type"/>.</simpara>
</listitem>
</varlistentry>
</variablelist>
<bridgehead xml:id="_n4types" renderas="sect4">N4Types</bridgehead>
<variablelist>
<varlistentry>
<term>Declared Type</term>
<listitem>
<simpara>(Unparameterized) Reference to defined class <xref linkend="_classes"/> or enum <xref linkend="_enums"/>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>Parameterized Type</term>
<listitem>
<simpara>Parameterized reference to defined generic class or interface; <xref linkend="_parameterized-types"/>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>This Type</term>
<listitem>
<simpara><xref linkend="_this-type"/>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>Constructor and Type Type</term>
<listitem>
<simpara>Class type, that is the meta class of a defined class or interface, <xref linkend="_constructor-and-classifier-type"/>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>Union Types</term>
<listitem>
<simpara>Union of types, <xref linkend="_union-type"/>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>Type Variable</term>
<listitem>
<simpara>Type variable, <xref linkend="_type-variables"/>.</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>Type expressions are used to explicitly declare the type of a variable, parameter and return type of a function or method, fields (and object literal properties).</simpara>
</section>
</section>
<section xml:id="_type-inference" role="language-n4js">
<title>Type Inference</title>
<simpara>If no type is explicitly declared, it is inferred based on the given context, as in the expected type of expressions or function parameters, for example.
The type inference rules are described in the remainder of this specification.</simpara>
<definition>
<title>Default Type</title>
<simpara>
<anchor xml:id="default_type" xreflabel="[default_type]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="default_type">Default Type</link></simpara>
<simpara>
In N4JS mode , if no type is explicitly specified and if no type information can be inferred, <literal>any</literal> is assumed as the default type.</simpara>
<simpara>In JS mode, the default type is <literal>any+</literal>.</simpara>
<simpara>Once the type of a variable is either declared or inferred, it is not supposed to be changed.</simpara>
</definition>
<simpara>Given the following example.</simpara>
<formalpara>
<title>Variable type is not changeable</title>
<para>
<programlisting language="n4js" linenumbering="unnumbered">var x: any;
x = 42;
x-5; // error: any is not a subtype of number.</programlisting>
</para>
</formalpara>
<simpara>Type of <literal>x</literal> is declared as <literal>any</literal> in line 1. Although a number is assigned to <literal>x</literal> in line 2, the type of <literal>x</literal> is not changed. Thus an error is issued in line 3 because the type of <literal>x</literal> is still <literal>any</literal>.</simpara>
<simpara role="todo">At the moment, N4JS does not support type guards or, more general, effect system (cf. [<link linkend="Nielson99a">Nielson99a</link>]).</simpara>
</section>
<section xml:id="_generic-and-parameterized-types" role="language-n4js">
<title>Generic and Parameterized Types</title>
<simpara>Some notes on terminology:</simpara>
<variablelist>
<varlistentry>
<term>Type Parameter vs. Type Argument</term>
<listitem>
<simpara>A type parameter is a declaration containing type variables.
A type argument is a binding of a type parameter to a concrete type or to another type parameter.
Binding to another type parameter can further restrict the bounds of the type parameter.</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>This is similar to function declarations (with formal parameters) and function calls (with arguments).</simpara>
<section xml:id="_generic-types">
<title>Generic Types</title>
<simpara>A class declaration or interface declaration with type parameters declares a generic type.
A generic type declares a family of types.
The type parameters have to be bound with type arguments when referencing a generic type.</simpara>
</section>
<section xml:id="_type-variables">
<title>Type Variables</title>
<simpara>A type variable is an identifier used as a type in the context of a generic class definition, generic interface definition or generic method definition.
A type variable is declared in a type parameter as follows.</simpara>
<bridgehead xml:id="_syntax-2" renderas="sect4">Syntax</bridgehead>
<programlisting language="xtext" linenumbering="unnumbered">TypeVariable:
(declaredCovariant?='out' | declaredContravariant?='in')?
name=IdentifierOrThis ('extends' declaredUpperBound=TypeRef)?
;</programlisting>
<example>
<title>Type Variable as Upper Bound</title>
<simpara>Note that type variables are also interpreted as types.
Thus, the upper bound of a type variable may be a type variable as shown in the following snippet:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class G&lt;T&gt; {
&lt;X extends T&gt; foo(x: X): void { }
}</programlisting>
</example>
<bridgehead xml:id="type-variables-properties" renderas="sect4">Properties</bridgehead>
<simpara>A type parameter defines a type variable, which type may be constrained with an upper bound.</simpara>
<simpara>Properties of <literal>TypeVariable</literal>:</simpara>
<variablelist>
<varlistentry>
<term><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math> </term>
<listitem>
<simpara>Type variable, as type variable contains only an identifier, we use type parameter instead of type variable (and vice versa) if the correct element is clear from the context.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>U</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>B</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>d</mi></math> </term>
<listitem>
<simpara>Upper bound of the concrete type being bound to this type variable, i.e. a super class.</simpara>
</listitem>
</varlistentry>
</variablelist>
<bridgehead xml:id="type-variables-semantics" renderas="sect4">Semantics</bridgehead>
<requirement xml:id="IDE-10">
<title>Type Variable Semantics</title>
<simpara>
<anchor xml:id="Req-IDE-10" xreflabel="[Req-IDE-10]"/>
<emphasis role="strong">Requirement: IDE-10:</emphasis>
<link linkend="Req-IDE-10">Type Variable Semantics</link> (ver. 1)</simpara>
<simpara>
1. Enum is not a valid metatype in <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>U</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>B</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>d</mi><mi>s</mi></math>.
2. Wildcards are not valid in <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>U</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>B</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>d</mi><mi>s</mi></math>.
3. Primitives are not valid in <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>U</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>B</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>d</mi><mi>s</mi></math>.
4. Type variables are valid in <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>U</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>B</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>d</mi><mi>s</mi></math>.</simpara>
</requirement>
<simpara>A type variable can be used in any type expression contained in the generic class, generic interface, or generic function / method definition.</simpara>
<example>
<title>F bounded quantification</title>
<simpara>Using a type variable in the upper bound reference may lead to recursive definition.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class Chain&lt;C extends Chain&lt;C, T&gt;, T&gt; {
next() : C { return null; }
m() : T { return null; }
}</programlisting>
</example>
<bridgehead xml:id="type-variables-type-inference" renderas="sect4">Type Inference</bridgehead>
<simpara>In many cases, type variables are not directly used in subtype relations as they are substituted with the concrete types specified by some type arguments.
In these cases, the ordinary subtype rules apply without change.
However, there are other cases in which type variables cannot be substituted:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>Inside a generic declaration.</simpara>
</listitem>
<listitem>
<simpara>If the generic type is used as raw type.</simpara>
</listitem>
<listitem>
<simpara>If a generic function / method is called without type arguments and without the possibility to infer the type from the context.</simpara>
</listitem>
</orderedlist>
<simpara>In these cases, an unbound type variable may appear on one or both sides of a subtype relation and we require subtype rules that take type variables into account.</simpara>
<simpara>It is important to note that while type variables may have a declared upper bound, they cannot be simply replaced with that upper bound and treated like existential types.
The following example illustrates this:</simpara>
<example>
<title>Type variables vs. existential types</title>
<programlisting language="n4js" linenumbering="unnumbered">class A {}
class B extends A {}
class C extends B {}
class G&lt;T&gt; {}
class X&lt;T extends A, S extends B&gt; {
m(): void {
// plain type variables:
var t: T;
var s: S;
t = s; // ERROR: "S is not a subtype of T." at "s" <co xml:id="CO1-1"/>
// existential types:
var ga: G&lt;? extends A&gt;;
var gb: G&lt;? extends B&gt;;
ga = gb; <co xml:id="CO1-2"/>
}
}</programlisting>
<calloutlist>
<callout arearefs="CO1-1">
<para>Even though the upper bound of <literal>S</literal> is a subtype of <literal>T</literal>’s upper bound (since <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>B</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>A</mi></math>), we cannot infer that <literal>S</literal> is a subtype of <literal>T</literal>,
because there are valid concrete bindings for which this would not be true: for example, if <literal>T</literal> were bound to <literal>C</literal> and <literal>S</literal> to <literal>B</literal>.</para>
</callout>
<callout arearefs="CO1-2">
<para>This differs from existential types (see <literal>ga</literal> and <literal>gb</literal> and line 21):<?asciidoc-br?>
<literal>G&lt;? extends B&gt;</literal> <math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/></math> <literal>G&lt;? extends A&gt;</literal> ).</para>
</callout>
</calloutlist>
</example>
<simpara>We thus have to define subtype rules for type variables, taking the declared upper bound into account.
If we have a subtype relation in which a type variable appears on one or both sides, we distinguish the following cases:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>If we have type variables on both sides: the result is true if and only if there is the identical type variable on both sides.</simpara>
</listitem>
<listitem>
<simpara>If we have a type variable on the left side and no type variable on the right side:<?asciidoc-br?>
the result is true if and only if the type variable on the left has one or more declared upper bounds.<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close=")" open="("><mrow><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>U</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>B</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>d</mi><mi>s</mi></mrow></mfenced><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></math><?asciidoc-br?>
This is the case for</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfenced close=")" open="("><mrow><mi>T</mi><mspace width="3.0mm"/><mi>e</mi><mi>x</mi><mi>t</mi><mi>e</mi><mi>n</mi><mi>d</mi><mi>s</mi><mspace width="3.0mm"/><mi>B</mi></mrow></mfenced><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>A</mi></math>
<simpara>in which T is an unbound type variable and A, B two classes with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>B</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>A</mi></math>.</simpara>
</listitem>
<listitem>
<simpara>In all other cases the result is false.<?asciidoc-br?>
This includes cases such as</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>B</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mfenced close=")" open="("><mrow><mi>T</mi><mspace width="3.0mm"/><mi>e</mi><mi>x</mi><mi>t</mi><mi>e</mi><mi>n</mi><mi>d</mi><mi>s</mi><mspace width="3.0mm"/><mi>A</mi></mrow></mfenced></math>
<simpara>which is always false, even if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>B</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>A</mi></math> or</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfenced close=")" open="("><mrow><mi>T</mi><mspace width="3.0mm"/><mi>e</mi><mi>x</mi><mi>t</mi><mi>e</mi><mi>n</mi><mi>d</mi><mi>s</mi><mspace width="3.0mm"/><mi>A</mi></mrow></mfenced><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mfenced close=")" open="("><mrow><mi>S</mi><mspace width="3.0mm"/><mi>e</mi><mi>x</mi><mi>t</mi><mi>e</mi><mi>n</mi><mi>d</mi><mi>s</mi><mspace width="3.0mm"/><mi>B</mi></mrow></mfenced></math>
<simpara>which is always false, even if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>A</mi><mo>=</mo><mi>B</mi></math>.</simpara>
</listitem>
</orderedlist>
<simpara>We thus obtain the following defintion:</simpara>
<definition>
<title>Subtype Relation for Type Variables</title>
<simpara>
<anchor xml:id="subtype_relation_for_type_variables" xreflabel="[subtype_relation_for_type_variables]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="subtype_relation_for_type_variables">Subtype Relation for Type Variables</link></simpara>
<simpara>
For two types <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>,</mo><mi>S</mi></math> of which at least one is a type variable, we define</simpara>
<itemizedlist>
<listitem>
<simpara>if both <emphasis>T</emphasis> and <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi></math> are type variables:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>T</mi><mo>=</mo><mi>S</mi></mrow><mrow><mi>T</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>S</mi></mrow></mfrac></math>
</listitem>
<listitem>
<simpara>if <emphasis>T</emphasis> is a type variable and <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi></math> is not:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mrow><mi>T</mi><mo>.</mo><mstyle mathvariant="italic"><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>U</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>B</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>d</mi><mi>s</mi></mstyle><mo>.</mo><mstyle mathvariant="italic"><mi>s</mi><mi>i</mi><mi>z</mi><mi>e</mi></mstyle><mo>&gt;</mo><mn>0</mn></mrow><mrow><mi> </mi><mo></mo><mi> </mi><mo></mo><mi>t</mi><mo></mo><mi>T</mi><mo>.</mo><mstyle mathvariant="italic"><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>U</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>B</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>d</mi><mi>s</mi></mstyle><mi>:</mi><mi>t</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>S</mi></mrow></mrow><mrow><mi>T</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>S</mi></mrow></mfrac></math>
</listitem>
</itemizedlist>
</definition>
</section>
<section xml:id="_parameterized-types">
<title>Parameterized Types</title>
<simpara>References to generic types (cf. <xref linkend="_classes"/>) can be parameterized with type arguments.
A type reference with type arguments is called parameterized type.</simpara>
<bridgehead xml:id="parameterized-types-syntax" renderas="sect4">Syntax</bridgehead>
<programlisting language="xtext" linenumbering="unnumbered">ParameterizedTypeRef:
ParameterizedTypeRefNominal | ParameterizedTypeRefStructural;
ParameterizedTypeRefNominal:
declaredType=[Type|TypeReferenceName]
(=&gt; '&lt;' typeArgs+=TypeArgument (',' typeArgs+=TypeArgument)* '&gt;')?;
ParameterizedTypeRefStructural:
definedTypingStrategy=TypingStrategyUseSiteOperator
declaredType=[Type|TypeReferenceName]
(=&gt;'&lt;' typeArgs+=TypeArgument (',' typeArgs+=TypeArgument)* '&gt;')?
('with' TStructMemberList)?;
TypeArgument returns TypeArgument:
Wildcard | TypeRef;
Wildcard returns Wildcard:
'?'
(
'extends' declaredUpperBound=TypeRef
| 'super' declaredLowerBound=TypeRef
)?
;</programlisting>
<bridgehead xml:id="parameterized-types-properties" renderas="sect4">Properties</bridgehead>
<simpara>Properties of parameterized type references (nominal or structural):</simpara>
<variablelist>
<varlistentry>
<term><literal>declaredType</literal> </term>
<listitem>
<simpara>Referenced type by type reference name (either the simple name or a qualified name, e.g. in case of namespace imports).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>typeArgs</literal> </term>
<listitem>
<simpara>The type arguments, may be empty.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>definedTypingStrategy</literal> </term>
<listitem>
<simpara>Typing strategy, by default nominal, see <xref linkend="_structural-typing"/> for details</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>structuralMembers</literal> </term>
<listitem>
<simpara>in case of structural typing, reference can add additional members to the structural type, see <xref linkend="_structural-typing"/> for details.</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara><emphasis role="strong">Pseudo Properties:</emphasis></simpara>
<variablelist>
<varlistentry>
<term><literal>importSpec</literal> </term>
<listitem>
<simpara>The <literal>ImportSpecifier</literal>, may be null if this is a local type reference.
Note that this may be a <literal>NamedImportSpecifier</literal>. See <xref linkend="_import-statement"/> for details for details.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>moduleWideName</literal> </term>
<listitem>
<simpara>Returns simple name of type, that is either the simple name as declared, or the alias in case of an imported type with alias in the import statement.</simpara>
</listitem>
</varlistentry>
</variablelist>
<bridgehead xml:id="parameterized-types-semantics" renderas="sect4">Semantics</bridgehead>
<simpara>The main purpose of a parameterized type reference is to simply refer to the declared type.
If the declared type is a generic type, the parameterized type references defines a <emphasis>substitution</emphasis> of the type parameters of a generic type with actual type arguments.
A type argument can either be a concrete type, a wildcard or a type variable declared in the surrounding generic declaration.
The actual type arguments must conform to the type parameters so that code referencing the generic type parameters is still valid.</simpara>
<requirement xml:id="IDE-11">
<title>Parameterized Types</title>
<simpara>
<anchor xml:id="Req-IDE-11" xreflabel="[Req-IDE-11]"/>
<emphasis role="strong">Requirement: IDE-11:</emphasis>
<link linkend="Req-IDE-11">Parameterized Types</link> (ver. 1)</simpara>
<simpara>
For a given parameterized type reference <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>R</mi></math> with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>G</mi><mo>=</mo><mi>R</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi></math>, the following constraints must hold:</simpara>
<itemizedlist>
<listitem>
<simpara>The actual type arguments must conform to the type parameters, that is:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo>|</mo><mi>G</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>P</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>|</mo><mo>=</mo><mo>|</mo><mi>R</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>A</mi><mi>r</mi><mi>g</mi><mi>s</mi><mo>|</mo></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mo></mo><mi> </mi><mi>i</mi><mo>,</mo><mn>0</mn><mo>&lt;</mo><mi>i</mi><mo>&lt;</mo><mo>|</mo><mi>R</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>A</mi><mi>r</mi><mi>g</mi><mi>s</mi><mo>|</mo><mi>:</mi><mrow><mo>[</mo><mspace width="-0.167em"/><mrow><mo>[</mo></mrow></mrow><mrow><mi>R</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>A</mi><mi>r</mi><mi>g</mi><msub><mi>s</mi><mi>i</mi></msub></mrow><mrow><mrow><mo>]</mo></mrow><mspace width="-0.167em"/><mo>]</mo></mrow><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mrow><mo>[</mo><mspace width="-0.167em"/><mrow><mo>[</mo></mrow></mrow><mrow><mi>R</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>P</mi><mi>a</mi><mi>r</mi><msub><mi>s</mi><mi>i</mi></msub></mrow><mrow><mrow><mo>]</mo></mrow><mspace width="-0.167em"/><mo>]</mo></mrow></math></simpara>
</listitem>
</itemizedlist>
</requirement>
<simpara>We define type erasure similar to Java [<link linkend="Gosling12a">Gosling12a(p.S4.6)</link>] as 'mapping from types (possibly including parameterized types and type variables)
to types (that are never parameterized types or type variables)'. We write <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math><superscript>o</superscript>
for the erasure of type <emphasis>T</emphasis>.<footnote><simpara>The notation <math xmlns="http://www.w3.org/1998/Math/MathML"><mo>|</mo><mi>T</mi><mo>|</mo></math> used in [<link linkend="Gosling12a">Gosling12a</link>] conflicts with the notation of cardinality of sets, which we use in case of union or intersection types for types as well. The notation used here is inspired by [<link linkend="Crary02a">Crary02a</link>], in which a mapping is defined between a typed language <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>λ</mi></math> to an untyped language <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>λ</mi></math><superscript>o</superscript>.</simpara></footnote></simpara>
<definition>
<title>Parameterized Type</title>
<simpara>
<anchor xml:id="parameterized_type" xreflabel="[parameterized_type]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="parameterized_type">Parameterized Type</link></simpara>
<simpara>
A parameterized type reference <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>R</mi></math> defines a parameterized type <emphasis>T</emphasis>, in which all type parameters of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>R</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi></math> are substituted with the actual values of the type arguments.
We call the type <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>T</mi><mn>0</mn></msup></math>, in which all type parameters of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>R</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi></math> are ignored, the <emphasis>raw type</emphasis> or <emphasis>erasure</emphasis> of <emphasis>T</emphasis>.</simpara>
<simpara>We define for types in general:</simpara>
<itemizedlist>
<listitem>
<simpara>The erasure <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>G</mi></math><superscript>o</superscript> of a parameterized type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>G</mi><mo>&lt;</mo><msub><mi>T</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub><mo>&gt;</mo></math> is simply <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>G</mi></math>.</simpara>
</listitem>
<listitem>
<simpara>The erasure of a type variable is the erasure of its upper bound.</simpara>
</listitem>
<listitem>
<simpara>The erasure of any other type is the type itself.</simpara>
</listitem>
</itemizedlist>
</definition>
<simpara>This concept of type erasure is purely defined for specification purposes.
It is not to be confused with the <literal>real</literal> type erasure which takes place at runtime, in which almost no types (except primitive types) are available.</simpara>
<simpara>That is, the type reference in <literal>var G&lt;string&gt; gs;</literal> actually defines a type <literal>G&lt;string&gt;</literal>, so that <math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>[</mo><mspace width="-0.167em"/><mrow><mo>[</mo></mrow></mrow><mrow><mi>g</mi><mi>s</mi></mrow><mrow><mrow><mo>]</mo></mrow><mspace width="-0.167em"/><mo>]</mo></mrow><mo>=</mo><mstyle mathvariant="monospace"><mtext>G</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>string</mtext></mstyle><mo>&gt;</mo></math>.
It may reference a type defined by a class declaration <literal>class G&lt;T&gt;</literal>.
It is important that the type <literal>G&lt;string&gt;</literal> is different from <literal>G&lt;T&gt;</literal>.</simpara>
<simpara>If a parameterized type reference <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>R</mi></math> has no type arguments, then it is similar to the declared type.
That is, <math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>[</mo><mspace width="-0.167em"/><mrow><mo>[</mo></mrow></mrow><mi>R</mi><mrow><mrow><mo>]</mo></mrow><mspace width="-0.167em"/><mo>]</mo></mrow><mo>=</mo><mi>T</mi><mo>=</mo><mi>R</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi></math> if (and only if) <math xmlns="http://www.w3.org/1998/Math/MathML"><mo>|</mo><mi>R</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>A</mi><mi>r</mi><mi>g</mi><mi>s</mi><mo>|</mo><mo>=</mo><mn>0</mn></math>.</simpara>
<simpara>In the following, we do not distinguish between parameter type reference and parameter type – they are both two sides of the same coin.</simpara>
<example>
<title>Raw Types</title>
<simpara>In Java, due to backward compatibility (generics were only introduced in Java 1.5), it is possible to use raw types in which we refer to a generic type without specifying any type arguments.
This is not possible in N4JS, as there is no unique interpretation of the type in that case as shown in the following example.
Given the following declarations:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class A{}
class B extends A{}
class G&lt;T extends A&gt; { t: T; }
var g: G;</programlisting>
</example>
<simpara>In this case, variable <literal>g</literal> refers to the <emphasis>raw type</emphasis> <literal>G</literal>.
This is forbidden in N4JS, because two interpretations are possible:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara><literal>g</literal> is of type <literal>G&lt;? extends&gt;</literal></simpara>
</listitem>
<listitem>
<simpara><literal>g</literal> is of type <literal>G&lt;A&gt;</literal></simpara>
</listitem>
</orderedlist>
<simpara>In the first case, an existential type would be created, and <literal>g.t = new A();</literal> must fail.</simpara>
<simpara>In the second case, <literal>g = new G&lt;B&gt;();</literal> must fail.</simpara>
<simpara>In Java, both assignments work with raw types, which is not really safe.
To avoid problems due to different interpretations, usage of raw types
is not allowed in N4JS. <footnote><simpara>Although raw type usage is prohibited, the N4JS validator interprets raw types according to the first case, which may lead to consequential errors.</simpara></footnote></simpara>
<simpara>Calls to generic functions and methods can also be parameterized, this is described in <xref linkend="_function-calls"/>.
Note that invocation of generic functions or methods does not need to be parameterized.</simpara>
<definition>
<title>Type Conformance</title>
<simpara>
<anchor xml:id="type_conformance" xreflabel="[type_conformance]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="type_conformance">Type Conformance</link></simpara>
<simpara>
We define type conformance for non-primitive type references as follows:</simpara>
<itemizedlist>
<listitem>
<simpara>For two non-parameterized types <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>T</mi><mn>0</mn></msup></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>S</mi><mn>0</mn></msup></math>,</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><msup><mi>S</mi><mn>0</mn></msup><mo></mo><msup><mi>T</mi><mn>0</mn></msup><mo>.</mo><mi>s</mi><mi>u</mi><msup><mi>p</mi><mo>*</mo></msup><mo></mo><msup><mi>T</mi><mn>0</mn></msup><mo>.</mo><mi>i</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>f</mi><mi>a</mi><mi>c</mi><mi>e</mi><msup><mi>s</mi><mo>*</mo></msup></mrow><mrow><msup><mi>T</mi><mn>0</mn></msup><mo>&lt;</mo><mi>:</mi><msup><mi>S</mi><mn>0</mn></msup></mrow></mfrac></math>
</listitem>
<listitem>
<simpara>For two parameterized types <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>&lt;</mo><msub><mi>T</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub><mo>&gt;</mo></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mo>&lt;</mo><msub><mi>S</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>S</mi><mi>m</mi></msub><mo>&gt;</mo></math></simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><msup><mi>T</mi><mn>0</mn></msup><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><msup><mi>S</mi><mn>0</mn></msup></mrow><mrow><mspace width="10.0em"/><mi>T</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>S</mi><mspace width="10.0em"/></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>(</mo><mi>n</mi><mo>=</mo><mn>0</mn><mo></mo><mi>m</mi><mo>=</mo><mn>0</mn><mo></mo><mrow><mo>(</mo><mi>n</mi><mo>=</mo><mi>m</mi><mo></mo><mo></mo><mi>i</mi><mi>:</mi></mrow></mrow></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="2.0em"/><mrow><msub><mi>T</mi><mi>i</mi></msub><mo>.</mo><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>B</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>d</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><msub><mi>S</mi><mi>i</mi></msub><mo>.</mo><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>B</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>d</mi></mrow></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mrow><mspace width="1.0em"/><mo></mo><mrow><msub><mi>T</mi><mi>i</mi></msub><mo>.</mo><mi>l</mi><mi>o</mi><mi>w</mi><mi>e</mi><mi>r</mi><mi>B</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>d</mi><mi>:</mi><mo>&gt;</mo><msub><mi>S</mi><mi>i</mi></msub><mo>.</mo><mi>l</mi><mi>o</mi><mi>w</mi><mi>e</mi><mi>r</mi><mi>B</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>d</mi></mrow><mo>)</mo></mrow><mo>)</mo></mrow><mi>}</mi></math></simpara>
</listitem>
</itemizedlist>
</definition>
<example>
<title>Subtyping with parameterized types</title>
<simpara>Let classes A, B, and C are defined as in the chapter beginning (<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>B</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>A</mi></math>).
The following subtype relations are evaluated as indicated:</simpara>
<programlisting language="xtext" linenumbering="unnumbered">G&lt;A&gt; &lt;: G&lt;B&gt; -&gt; false
G&lt;B&gt; &lt;: G&lt;A&gt; -&gt; false
G&lt;A&gt; &lt;: G&lt;A&gt; -&gt; true
G&lt;A&gt; &lt;: G&lt;?&gt; -&gt; true
G&lt;? extends A&gt; &lt;: G&lt;? extends A&gt; -&gt; true
G&lt;? super A&gt; &lt;: G&lt;? super A&gt; -&gt; true
G&lt;? extends A&gt; &lt;: G&lt;? extends B&gt; -&gt; false
G&lt;? extends B&gt; &lt;: G&lt;? extends A&gt; -&gt; true
G&lt;? super A&gt; &lt;: G&lt;? super B&gt; -&gt; true
G&lt;? super B&gt; &lt;: G&lt;? super A&gt; -&gt; false
G&lt;? extends A&gt; &lt;: G&lt;A&gt; -&gt; false
G&lt;A&gt; &lt;: G&lt;? extends A&gt; -&gt; true
G&lt;? super A&gt; &lt;: G&lt;A&gt; -&gt; false
G&lt;A&gt; &lt;: G&lt;? super A&gt; -&gt; true
G&lt;? super A&gt; &lt;: G&lt;? extends A&gt; -&gt; false
G&lt;? extends A&gt; &lt;: G&lt;? super A&gt; -&gt; false
G&lt;?&gt; &lt;: G&lt;? super A&gt; -&gt; false
G&lt;? super A&gt; &lt;: G&lt;?&gt; -&gt; true
G&lt;?&gt; &lt;: G&lt;? extends A&gt; -&gt; false
G&lt;? extends A&gt; &lt;: G&lt;?&gt; -&gt; true</programlisting>
</example>
<simpara>The figure <xref linkend="cdVarianceChart"/> shows the subtype relations of parameterized types (of a single generic type), which can be used as a cheat sheet.</simpara>
<figure xml:id="cdVarianceChart">
<title>Cheat Sheet: Subtype Relation of Parameterized Types</title>
<mediaobject>
<imageobject>
<imagedata fileref="chapters/04_types/fig/cdVarianceChart.svg" align="center"/>
</imageobject>
<textobject><phrase>cdVarianceChart</phrase></textobject>
</mediaobject>
</figure>
<example>
<title>Subtyping between different generic types</title>
<simpara>Let classes <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>G</mi></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>H</mi></math> be two generic classes where:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class G&lt;T&gt; {}
class H&lt;T&gt; extends G&lt;T&gt; {}</programlisting>
<simpara>Given a simple, non-parameterized class <emphasis>A</emphasis>, the following
subtype relations are evaluated as indicated:</simpara>
<programlisting language="xtext" linenumbering="unnumbered">G&lt;A&gt; &lt;: G&lt;A&gt; -&gt; true
H&lt;A&gt; &lt;: G&lt;A&gt; -&gt; true
G&lt;A&gt; &lt;: H&lt;A&gt; -&gt; false</programlisting>
</example>
<bridgehead xml:id="parameterized-types-type-inference" renderas="sect4">Type Inference</bridgehead>
<simpara>Type inference for parameterized types uses the concept of existential types (in Java, a slightly modified version called capture conversion is implemented).</simpara>
<simpara>The general concept for checking type conformance and inferring types for generic and parameterized types is described in [<link linkend="Igarashi01a">Igarashi01a</link>] for <emphasis>Featherweight Java with Generics</emphasis>.</simpara>
<simpara>The concept of existential types with wildcard capture (a special kind of existential type) is published in [<link linkend="Torgersen05a">Torgersen05a</link>], further developed in [<link linkend="Cameron08b">Cameron08b</link>] (further developed in [<link linkend="Cameron09a">Cameron09a</link>] [<link linkend="Summers10a">Summers10a</link>], also see [<link linkend="Wehr08a">Wehr08a</link>] for a similar approach).
The key feature of the Java generic wildcard handling is called capture conversion, described in [<link linkend="Gosling12a">Gosling12a(p.S5.1.10)</link>].
However, there are some slight differences to Java 6 and 7, only with Java 8 similar results can be expected.
All these papers include formal proofs of certain aspects, however even these paper lack proof of other aspect</simpara>
<simpara>The idea is quite simple: All unbound wildcards are replaced with freshly created new types <footnote><simpara>in the Java 8 spec and compiler, they are called type variables, which are types as well</simpara></footnote>,
fulfilling the constraints defined by the wildcard’s upper and lower bound.
These newly created types are then handled similar to real types during type inference and type conformance validation.</simpara>
<example>
<title>Existential Type</title>
<simpara>The inferred type of a variable
declared as</simpara>
<simpara><literal>var x: G&lt;? extends A&gt;;</literal>,</simpara>
<simpara>that is the parameterized type, is an existential type <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>E</mi><mn>1</mn></msub></math>, which is a subtype of A.
If you have another variable declared as</simpara>
<simpara><literal>var y: G&lt;? extends A&gt;;</literal></simpara>
<simpara>another type <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>E</mi><mn>2</mn></msub></math> is created, which is also a subtype of A.
Note that <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>E</mi><mn>1</mn></msub><mo></mo><msub><mi>E</mi><mn>2</mn></msub></math>! Assuming typical setter or getter in G, e.g. <literal>set(T t)</literal> and <literal>T get()</literal>, the following code snippet will produce an error:</simpara>
<simpara><literal>y.set(x.get())</literal></simpara>
<simpara>This is no surprise, as <literal>x.get()</literal> actually returns a type <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>E</mi><mn>1</mn></msub></math>, which is not a subtype of <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>E</mi><mn>2</mn></msub></math>.</simpara>
</example>
<simpara>The upper and lower bound declarations are, of course, still available during type inference for these existential types.
This enables the type inferencer to calculate the join and meet of parameterized types as well.</simpara>
<requirement xml:id="IDE-12">
<title>Join of Parameterized Types</title>
<simpara>
<anchor xml:id="Req-IDE-12" xreflabel="[Req-IDE-12]"/>
<emphasis role="strong">Requirement: IDE-12:</emphasis>
<link linkend="Req-IDE-12">Join of Parameterized Types</link> (ver. 1)</simpara>
<simpara>
The join of two parameterized types <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>G</mi><mo>&lt;</mo><msub><mi>T</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub><mo>&gt;</mo></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>H</mi><mo>&lt;</mo><msub><mi>S</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>S</mi><mi>m</mi></msub><mo>&gt;</mo></math> is the join of the raw types, this join is then parameterized with the join of the
upper bounds of of type arguments and the meet of the lower bounds of the type arguments.</simpara>
<simpara>For all type rules, we assume that the upper and lower bounds of a non-generic type, including type variables,
simply equal the type itself, that is for a given type <emphasis>T</emphasis>, the following constraints hold:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mfenced close=")" open="("><mi>T</mi></mfenced><mo>=</mo><mi>l</mi><mi>o</mi><mi>w</mi><mi>e</mi><mi>r</mi><mfenced close=")" open="("><mi>T</mi></mfenced><mo>=</mo><mi>T</mi></math></simpara>
</requirement>
<example>
<title>Upper and lower bound of parameterized types</title>
<simpara>Assuming the given classes listed above, the following upper and lower bounds are expected:</simpara>
<programlisting language="xtext" linenumbering="unnumbered">G&lt;A&gt; -&gt; upperBound = lowerBound = A
G&lt;? extends A&gt; -&gt; lowerBound = null, upperBound = A
G&lt;? super A&gt; -&gt; lowerBound = A, upperBound = any
G&lt;?&gt; -&gt; lowerBound = null, upperBound = any</programlisting>
<simpara>This leads to the following expected subtype relations:</simpara>
<programlisting language="xtext" linenumbering="unnumbered">(? extends A) &lt;: A -&gt; true
(? super A) &lt;: A -&gt; false
A &lt;: (? extends A) -&gt; false
A &lt;: (? super A) -&gt; true</programlisting>
</example>
<simpara>Note that there is a slight difference to Java: In N4JS it is not possible to use a generic type in a raw fashion, that is to say without specifying any type arguments.
In Java, this is possible due to backwards compatibility with early Java versions in which no generics were supported.</simpara>
<simpara>In case an upper bound of a type variable shall consist only of a few members, it seems convenient to use additional structural members,
like on interface I2 in the example <xref linkend="ex:use-declared-interfaces-for-lower-bounds"/> below.
However, type variables must not be constrained using structural types (see constraint <xref linkend="Req-IDE-76"/>).
Hence, the recommended solution is to use an explicitly declared interface that uses definition site structural typing for these constraints as an upper bound (see interface in <xref linkend="ex:use-declared-interfaces-for-lower-bounds"/>).</simpara>
<example xml:id="ex:use-declared-interfaces-for-lower-bounds">
<title>Use declared interfaces for lower bounds</title>
<programlisting language="n4js" linenumbering="unnumbered">interface I1&lt;T extends any with {prop : int}&gt; { // error
}
interface ~J {
prop : int;
}
interface I2&lt;T extends J&gt; {
}</programlisting>
</example>
</section>
</section>
<section xml:id="_primitive-ecmascript-types" role="language-n4js">
<title>Primitive ECMAScript Types</title>
<simpara>N4JS provides the same basic types as ECMAScript [<link linkend="ECMA11a">ECMA11a(p.p.28)</link>].</simpara>
<note>
<simpara>In ECMAScript, basic types come in two flavors: as primitive types [<link linkend="ECMA11a">ECMA11a(p.S8Types, p.p.28)</link>] and as Objects [<link linkend="ECMA11a">ECMA11a(p.S15, p.p.102)</link>].
In N4JS, primitive types are written with lower cases, object types with first case capitalized.
For example, <literal>String</literal> is the primitive ECMAScript string type, while <literal>String</literal> is an object.</simpara>
</note>
<simpara>The following ECMAScript primitive types are supported, they are written
with lower case letters::</simpara>
<itemizedlist>
<listitem>
<simpara><literal>undefined</literal>: [<link linkend="ECMA11a">ECMA11a(p.S8.3)</link>]; cannot be used in type expression, see void below.</simpara>
</listitem>
<listitem>
<simpara><literal>null</literal> [<link linkend="ECMA11a">ECMA11a(p.S8.3)</link>]; cannot be used in type expression</simpara>
</listitem>
<listitem>
<simpara><literal>boolean</literal> [<link linkend="ECMA11a">ECMA11a(p.S8.3)</link>]</simpara>
</listitem>
<listitem>
<simpara><literal>string</literal> [<link linkend="ECMA11a">ECMA11a(p.S8.4)</link>]</simpara>
</listitem>
<listitem>
<simpara><literal>number</literal> [<link linkend="ECMA11a">ECMA11a(p.S8.5)</link>]</simpara>
</listitem>
</itemizedlist>
<simpara>Although Object is a primitive type in [<link linkend="ECMA11a">ECMA11a(p.S8.5)</link>], it is interpreted here as an object type and described in <xref linkend="_object-type"/>.</simpara>
<simpara>Please note that primitive types are values (= no objects) so they have no properties and you cannot inherit from them.</simpara>
<section xml:id="_undefined-type">
<title>Undefined Type</title>
<simpara>As a built-in type, the type <literal>undefined</literal> cannot be declared explicitly by the user by means of a type expression.
Note in ECMAScript there are three distinct use cases of <literal>undefined</literal>:</simpara>
<itemizedlist>
<listitem>
<simpara><literal>undefined</literal> as type (as used here)</simpara>
</listitem>
<listitem>
<simpara><literal>undefined</literal> as value (the only value of the undefined type)</simpara>
</listitem>
<listitem>
<simpara><literal>undefined</literal> is a property of the global object with undefined (value) as initial value.
Since ECMAScript 5 it is not allowed to reassign this property but this is not enforced by all ECMAScript/JavaScript engines.</simpara>
</listitem>
</itemizedlist>
<simpara>The type <literal>undefined</literal> will be inferred to false in a boolean expression.
It is important to note that something that is not assigned to a value is <literal>undefined</literal> but not <literal>null</literal>.</simpara>
<simpara>The type <literal>undefined</literal> is a subtype of all types. That is,</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>undefined</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>T</mi></mrow></mfrac></math>
<simpara>is an axiom and true for all types <emphasis>T</emphasis>.</simpara>
<simpara>Whenever an expression <emphasis>E</emphasis> has an inferred type of <literal>undefined</literal>, which means it will always evaluate to
value <literal>undefined</literal> at runtime, a warning is shown, unless <emphasis>E</emphasis> is &#8230;&#8203;</simpara>
<itemizedlist>
<listitem>
<simpara>a <literal>void</literal> expression</simpara>
</listitem>
<listitem>
<simpara>the direct child expression of a <literal>void</literal> expression,</simpara>
</listitem>
<listitem>
<simpara>the direct child expression of an expression statement,</simpara>
</listitem>
<listitem>
<simpara>the <literal>undefined</literal> literal (i.e. the literal representing the <literal>undefined</literal> value),</simpara>
</listitem>
<listitem>
<simpara>the <literal>this</literal> literal.</simpara>
</listitem>
</itemizedlist>
</section>
<section xml:id="_null-type">
<title>Null Type</title>
<simpara>The <literal>null</literal> type cannot be declared explicitly by the user. Only the keyword <literal>null</literal> is inferred to type <literal>null</literal>.</simpara>
<bridgehead xml:id="null-type-semantics" renderas="sect4">Semantics</bridgehead>
<simpara>In contrast to <literal>undefined</literal>, it expresses the intentional absence of a value.</simpara>
<simpara>The <literal>null</literal> type can be assigned to any other type.
That is, the type <literal>null</literal> is a subtype of all other types except <literal>undefined</literal>:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi><mo></mo><mstyle mathvariant="monospace"><mtext>undefined</mtext></mstyle></mrow><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>null</mtext></mstyle><mi> </mi><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>Type</mtext></mstyle><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></mfrac></math>
<simpara>Please note that</simpara>
<itemizedlist>
<listitem>
<simpara><literal>null==undefined</literal> evaluates to <literal>true</literal></simpara>
</listitem>
<listitem>
<simpara><literal>null===undefined</literal> evaluates to <literal>false</literal></simpara>
</listitem>
<listitem>
<simpara><literal>typeof null</literal> evaluates to <literal>object</literal></simpara>
</listitem>
</itemizedlist>
<simpara>Only the <literal>null</literal> keyword is inferred to type null. If <literal>null</literal> is assigned to a variable, the type of the variable is not changed.
This is true, in particular, for variable declarations.
For example in</simpara>
<programlisting language="n4js" linenumbering="unnumbered">var x = null;</programlisting>
<simpara>the type of variable <literal>x</literal> is inferred to <literal>any</literal> (cf. <xref linkend="_variable-statement"/>).</simpara>
<simpara>The type <literal>null</literal> will be inferred to false in a boolean expression.</simpara>
<simpara>The call <literal>typeof null</literal> will return ’object’.</simpara>
</section>
<section xml:id="_primitive-boolean-type">
<title>Primitive Boolean Type</title>
<simpara>Represents a logical entity having two values, true and false.</simpara>
<simpara>Please note that a boolean primitive is coerced to a number in a comparison operation so that</simpara>
<informaltable frame="all" rowsep="1" colsep="1">
<tgroup cols="2">
<colspec colname="col_1" colwidth="66.6666*"/>
<colspec colname="col_2" colwidth="33.3334*"/>
<thead>
<row>
<entry align="center" valign="top">Source</entry>
<entry align="center" valign="middle">Result</entry>
</row>
</thead>
<tbody>
<row>
<entry align="left" valign="top"><programlisting language="n4js" linenumbering="unnumbered">var a = true; console.log(a == 1)</programlisting></entry>
<entry align="center" valign="middle"><simpara><emphasis role="strong">prints true</emphasis></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><programlisting language="n4js" linenumbering="unnumbered">var b = false; console.log(b == 0)</programlisting></entry>
<entry align="center" valign="middle"><simpara><emphasis role="strong">prints true</emphasis></simpara></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<bridgehead xml:id="primitive-boolean-type-semantics" renderas="sect4">Semantics</bridgehead>
<simpara>The type <literal>boolean</literal> is subtype of <literal>any</literal>:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow/><mrow><mstyle mathvariant="monospace"><mtext>boolean</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>any</mtext></mstyle></mrow></mfrac><mrow/></math>
<simpara>Variables of type <literal>boolean</literal> can be auto-converted (coerced) to <literal>Boolean</literal>, as described in <xref linkend="_autoboxing-and-coercing"/>.</simpara>
</section>
<section xml:id="_primitive-string-type">
<title>Primitive String Type</title>
<simpara>A finite sequence of zero or more 16-bit unsigned integer values (elements).
Each element is considered to be a single UTF-16 code unit.</simpara>
<simpara>Also string as primitive type has no properties, you can access the properties available on the object String as string will be coerced to String on the fly
but just for that property call, the original variable keeps its type:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">var a = "MyString"
console.log(typeof a) // string
console.log(a.length) // 8
console.log(typeof a) // string</programlisting>
<simpara>You can handle a primitive <literal>string</literal> like an object type <literal>String</literal> but with these exceptions:</simpara>
<itemizedlist>
<listitem>
<simpara><literal>typeof "MyString"</literal> is <literal>'string'</literal> but <literal>typeof new String("MyString")</literal> is <literal>'object'</literal></simpara>
</listitem>
<listitem>
<simpara><literal>"MyString" instanceof String</literal> or <literal>instanceof Object</literal> will return <literal>false</literal>, for <literal>new String("MyString")</literal> both checks evaluate to <literal>true</literal></simpara>
</listitem>
<listitem>
<simpara><literal>console.log(eval("2+2"))</literal> returns <literal>4</literal>, <literal>console.log(eval(new String("2+2")))</literal> returns string <literal>"2+2"</literal></simpara>
</listitem>
</itemizedlist>
<simpara>This marks a difference to Java.
In JavaScript, Unicode escape sequences are never interpreted as a special character.</simpara>
<bridgehead xml:id="primitive-string-type-semantics" renderas="sect4">Semantics</bridgehead>
<simpara>The <literal>string</literal> type is a subtype of <literal>any</literal>:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow/><mrow><mstyle mathvariant="monospace"><mtext>string</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>any</mtext></mstyle></mrow></mfrac><mrow/></math>
<simpara>It is supertype of the N4JS primitive type <literal>pathselector</literal>, <literal>typeName</literal> and <literal>i18nKey</literal>.
<xref linkend="_primitive-pathselector-and-i18nkey"/></simpara>
<simpara>However, variables of type <literal>string</literal> can be auto-converted (coerced) to <literal>string</literal>, as described in <xref linkend="_autoboxing-and-coercing"/>.</simpara>
</section>
<section xml:id="_primitive-number-type">
<title>Primitive Number Type</title>
<simpara>In ECMAScript numbers are usually 64-bit floating point numbers.
For details see [<link linkend="ECMA11a">ECMA11a(p.8.5)</link>].
The prefix <literal>0</literal> indicates that the number is octal-based and the prefix <literal>0x</literal> marks it as hexadecimal-based.</simpara>
<simpara><literal>NaN</literal> can be produced by e.g. &#8216;0 / 0&#8217;' or &#8216;1 - x&#8217;. <literal>typeof NaN</literal> will return <literal>number</literal>.</simpara>
<bridgehead xml:id="primitive-number-type-semantics" renderas="sect4">Semantics</bridgehead>
<simpara>The type <literal>number</literal> is subtype of <literal>any</literal>:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow/><mrow><mstyle mathvariant="monospace"><mtext>number</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>any</mtext></mstyle></mrow></mfrac><mrow/></math>
<simpara>However, variables of type <literal>number</literal> can be auto-converted (coerced) to <literal>Number</literal>, as described in <link linkend="_integer-literals">Integer Literals</link> .</simpara>
</section>
<section xml:id="_primitive-type-int">
<title>Primitive Type int</title>
<simpara>Actually ECMAScript defines an internal type <literal>int32</literal>.
A number of this type is returned by the binary or operation using zero as operand, e.g. ECMAScript’s internal type int32 can be represented in N4JS by a built-in primitive type called <literal>int</literal>.
For details on how numeric literals map to types <literal>number</literal> and <literal>int</literal>, refer to <xref linkend="_integer-literals"/>.</simpara>
<important>
<simpara>for the time being, built-in type <literal>int</literal> is synonymous to type <literal>number</literal>.
This means one can be assigned to the other and a value declared to be of type <literal>int</literal> may actually be a 64-bit floating
point number.<footnote><simpara>The rationale for having this limited implementation of type is that API designers already want to start providing hints where later only 32-bit integers will be used. For the time being, <emphasis role="strong">this is checked neither statically nor at runtime</emphasis>!</simpara></footnote></simpara>
</important>
</section>
<section xml:id="_primitive-symbol-type">
<title>Primitive Symbol Type</title>
<simpara>The primitive type <literal>symbol</literal> is directly as in ECMAScript 6.
Support for symbols is kept to a minimum in N4JS. While this primitive type can be used without any restrictions, the only value of this type available in N4JS is the built-in symbol <literal>Symbol.iterator</literal>.
Other built-in symbols from ECMAScript 6 and the creation of new symbols are not supported.
For more details, see <xref linkend="_symbol"/>.</simpara>
</section>
</section>
<section xml:id="_primitive-n4js-types" role="language-n4js">
<title>Primitive N4JS Types</title>
<simpara>Additionally to the primitive ECMAScript types, the following N4JS-specific primitive types are supported:</simpara>
<variablelist>
<varlistentry>
<term><literal>any</literal></term>
<listitem>
<simpara>enables ECMAScript-like untyped variable declarations</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>void</literal></term>
<listitem>
<simpara>almost similar to undefined, except it can be used as a return type of functions and methods</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>unknown</literal></term>
<listitem>
<simpara>inferred in case of a type inference error</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>pathSelector&lt;T&gt;</literal>, <literal>i18nKey</literal></term>
<listitem>
<simpara>subtypes of string</simpara>
</listitem>
</varlistentry>
</variablelist>
<section xml:id="_any-type">
<title>Any Type</title>
<simpara>Any type is the default type of all variables for without a type declaration.
It has no properties.
A value of any other type can be assigned to a variable of type <literal>any</literal>, but a variable declared <literal>any</literal> can only be assigned to another variable declared with the type <literal>any</literal>.</simpara>
<section xml:id="any-type-semantics">
<title>Semantics</title>
<simpara><literal>any</literal> is supertype of all other types. That is,</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>Type</mtext></mstyle><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mrow><mi>a</mi><mi>n</mi><mi>y</mi></mrow></mrow></mfrac></math>
<simpara>is an axiom and true for all types.</simpara>
</section>
<section xml:id="any-type-type-inference">
<title>Type Inference</title>
<simpara>If a variable is explicitly declared as type <literal>any</literal>, the inferred type of that variable will always be <literal>any</literal>.</simpara>
<section xml:id="_default-type-of-variables">
<title>Default Type of Variables</title>
<simpara>If a type annotation is missing and no initializer is provided, then the type of a variable is implicitly set to <literal>any</literal>.</simpara>
<simpara>In that case, the inferred type of that variable will always be <literal>any</literal> as well.
If an initializer is provided, the declared type of the variable will be set to the inferred type of the initializer.
Therefore in the latter case, the inferred type of the variable will always be the type of the initializer (cf. <xref linkend="_variable-statement"/>).</simpara>
<simpara>If a variable is declared as type , it can be used just as every variable can be used in raw ECMAScript.
Since every property can be get and set, the types of properties is inferred as as well.
This is formally expressed in <xref linkend="_identifier"/>.</simpara>
</section>
</section>
</section>
<section xml:id="_void-type">
<title>Void Type</title>
<simpara>The type <literal>void</literal> is used to denote that there is no value at all, as opposed to type
<literal>undefined</literal> which denotes that there is a value, but it is always undefined.</simpara>
<simpara>The only valid use of type <literal>void</literal> is to declare that a function or method does not
return anything. In particular, this means:</simpara>
<itemizedlist>
<listitem>
<simpara><literal>void</literal> is disallowed as type argument,</simpara>
</listitem>
<listitem>
<simpara><literal>void</literal> is disallowed as upper/lower bound of type parameters and wild cards,</simpara>
</listitem>
<listitem>
<simpara>when used as return type of functions or methods, <literal>void</literal> may not be nested, i.e.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">function foo(): void {} // ok
function bar(): any|void {} // error</programlisting>
</listitem>
</itemizedlist>
<simpara>In all the above disallowed cases, type <literal>undefined</literal> should be used instead of <literal>void</literal>.</simpara>
<section xml:id="void-type-semantics">
<title>Semantics</title>
<requirement xml:id="IDE-13">
<title>Void Type</title>
<simpara>
<anchor xml:id="Req-IDE-13" xreflabel="[Req-IDE-13]"/>
<emphasis role="strong">Requirement: IDE-13:</emphasis>
<link linkend="Req-IDE-13">Void Type</link> (ver. 1)</simpara>
<simpara>
* The type <literal>void</literal> may only be used as the immediate return type of a function or method.</simpara>
<itemizedlist>
<listitem>
<simpara>If a function <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi></math> is declared to return <literal>void</literal>, an error is created if a return statement contains an expression:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>n</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>void</mtext></mstyle><mo></mo></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>r</mi><mo>,</mo><mi>μ</mi><mfenced close=")" open="("><mi>r</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>ReturnStatement</mtext></mstyle><mo>,</mo><mi>r</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>i</mi><mi>n</mi><mi>g</mi><mi>F</mi><mi>u</mi><mi>n</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo>=</mo><mi>f</mi><mi>:</mi><mi>r</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>null</mtext></mstyle></math></simpara>
</listitem>
<listitem>
<simpara>If a function <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi></math> is declared to return <literal>void</literal>, an error is issued if the function is called in any statement or expression but an expression statement directly:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>n</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>void</mtext></mstyle><mo></mo></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>e</mi><mo>,</mo><mi>b</mi><mi>i</mi><mi>n</mi><mi>d</mi><mfenced close=")" open="("><mi>e</mi><mi>f</mi></mfenced><mi>:</mi><mi>μ</mi><mfenced close=")" open="("><mrow><mi>e</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>r</mi></mrow></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>ExpressionStatement</mtext></mstyle></math></simpara>
</listitem>
</itemizedlist>
</requirement>
<simpara>The following type hierarchy is defined: <literal>void</literal> is only a subtype of itself but not of any other type and no other type is a subtype of void.</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow/><mrow><mstyle mathvariant="monospace"><mtext>void</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>void</mtext></mstyle></mrow></mfrac></math>
<simpara>Since <literal>void</literal> cannot be used as the type of variables, fields, formal parameters, etc., a
function or method with a return type of void cannot be used on the right-hand side of
an assignment or in the argument list of a call expression (note the difference to plain
JavaScript).</simpara>
<simpara>The ECMAScript <literal>void</literal> operator (see <link linkend="_unary-expression">Unary Expressions</link>) has a type
of <literal>undefined</literal>, not <literal>void</literal>, because it evaluates to value <literal>undefined</literal> and can be used
on the right-hand side of assignments, etc.</simpara>
</section>
</section>
<section xml:id="_unknown-type">
<title>Unknown Type</title>
<simpara>Internally N4JS defines the type <literal>unknown</literal>.
This type cannot be used by the user.
Instead, it is inferred in case of errors.
<literal>unknown</literal> behaves almost similar to <literal>any+</literal>.
However no error messages once a variable or expression has been inferred to <literal>unknown</literal> in order to avoid consequential errors.</simpara>
</section>
<section xml:id="_primitive-pathselector-and-i18nkey">
<title>Primitive Pathselector and I18nKey</title>
<simpara>N4JS introduces three new types which are subtypes of string.
These types are, in fact, translated to strings and do not add any new functionality.
They are solely defined for enabling additional validation.</simpara>
<itemizedlist>
<listitem>
<simpara><literal>pathSelector&lt;T&gt;</literal> is a generic type for specifying path selector expressions. PathSelectors are used to specify a path to a property in a (JSON-like) model tree.</simpara>
</listitem>
<listitem>
<simpara>The type variable <literal>T</literal> defines the context type (or type of the root of the tree) in which the selector is to be validated.
A path selector is defined as a string literal that has to conform to the path selector grammar.
The context type is then used to perform a semantic</simpara>
</listitem>
<listitem>
<simpara><literal>i18nKey</literal> is a string which refers to an internationalization key.
The <literal>i18nKey</literal> type is used to reference resource keys specified in resource files.
In a project <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>p</mi></math>, the <literal>i18nKey</literal> type defines the transitive set of all resource keys accessible from <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>p</mi></math>.
Since resource keys are specified as strings, this means that the <literal>i18nKey</literal> type defines a subset of all string literals that can be assigned to a variable of type <literal>i18nKey</literal> in the current project.
That means that an assignment of a string literal to a variable of type <literal>i18nKey</literal> is only valid if that string literal is contained in the set defined by <literal>i18nKey</literal>.
Resource keys are declared in the properties files of a project and all resource keys from a project are accessible to any project depending on it.</simpara>
</listitem>
</itemizedlist>
<simpara role="todo">I18nkeys are not yet validated</simpara>
<section xml:id="pathselector-semantics">
<title>Semantics</title>
<simpara>The N4JS primitive types <literal>pathSelector&lt;T&gt;</literal>, <literal>i18nKey</literal> and <literal>pathSelector&lt;T&gt;</literal> are basically only marker types of strings for enabling additional validation.
Thus, they are completely interchangeable with string types:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow/><mrow><mstyle mathvariant="monospace"><mtext>typeName</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mo>&gt;</mo><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>string</mtext></mstyle></mrow></mfrac><mspace width="3.0mm"/><mfrac><mrow/><mrow><mstyle mathvariant="monospace"><mtext>string</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>typeName</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mo>&gt;</mo></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow/><mrow><mstyle mathvariant="monospace"><mtext>i18nKey</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>string</mtext></mstyle></mrow></mfrac><mspace width="3.0mm"/><mfrac><mrow/><mrow><mstyle mathvariant="monospace"><mtext>string</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>i18nKey</mtext></mstyle></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow/><mrow><mstyle mathvariant="monospace"><mtext>pathSelector</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mo>&gt;</mo><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>string</mtext></mstyle></mrow></mfrac><mspace width="3.0mm"/><mfrac><mrow/><mrow><mstyle mathvariant="monospace"><mtext>string</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>pathSelector</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mo>&gt;</mo></mrow></mfrac></math></simpara>
<simpara>As special literals for these N4JS types do not exist, the type has to be explicitly specified in order to enable the additional validation.
Note that this validation cannot be applied for more complicated expressions with parts which cannot be evaluated at compile time.
For example, <literal>"some.path."segment".prop"</literal> cannot be evaluated at compile time.</simpara>
</section>
</section>
</section>
<section xml:id="_built-in-ecmascript-object-types" role="language-n4js">
<title>Built-in ECMAScript Object Types</title>
<simpara>N4JS supports all built-in ECMAScript objects [<link linkend="ECMA11a">ECMA11a(p.S15)</link>], interpreted as classes.
Some of these object types are object versions of primitive types.
The object types have the same name as their corresponding primitive type, but start with an upper case letter.</simpara>
<simpara>The following types, derived from certain ECMAScript predefined objects and constructs, are supported by means of built-in types as they are required by certain expressions.</simpara>
<itemizedlist>
<listitem>
<simpara><literal>Object</literal> [<link linkend="ECMA11a">ECMA11a(p.p.111)</link>];</simpara>
</listitem>
<listitem>
<simpara><literal>Function</literal> [<link linkend="ECMA11a">ECMA11a(p.p.117)</link>]; representing functions and function objects <xref linkend="_function-type"/> but also methods (<xref linkend="_methods"/>)</simpara>
</listitem>
<listitem>
<simpara><literal>Array</literal> [<link linkend="ECMA11a">ECMA11a(p.p.122)</link>], representing array objects, see <xref linkend="_array-object-type"/></simpara>
</listitem>
<listitem>
<simpara><literal>String</literal> [<link linkend="ECMA11a">ECMA11a(p.p.141)</link>]</simpara>
</listitem>
<listitem>
<simpara><literal>Boolean</literal> [<link linkend="ECMA11a">ECMA11a(p.p.141)</link>]</simpara>
</listitem>
<listitem>
<simpara><literal>Number</literal> [<link linkend="ECMA11a">ECMA11a(p.p.141)</link>]</simpara>
</listitem>
<listitem>
<simpara><literal>RegExp</literal> [<link linkend="ECMA11a">ECMA11a(p.p.180)</link>]; they can be constructed by means of special literals (cf. <xref linkend="_literals"/>)</simpara>
</listitem>
<listitem>
<simpara>global object type</simpara>
</listitem>
<listitem>
<simpara><literal>Symbol</literal></simpara>
</listitem>
<listitem>
<simpara><literal>Promise</literal></simpara>
</listitem>
<listitem>
<simpara><literal>Iterator</literal> and <literal>Iterable</literal></simpara>
</listitem>
</itemizedlist>
<simpara>All other ECMAScript types ([<link linkend="ECMA11a">ECMA11a(p.S15)</link>], such as <literal>Math</literal>, <literal>Date</literal>, or <literal>Error</literal> are supported by means of predefined classes.
ECMAScript 2015 types are defined in the ECMAScript 2015 runtime environment.
Since they are defined and used similar to user defined classes, they are not explained in further detail here.
These predefined objects are kind of subtypes of <literal>Object</literal>.</simpara>
<section xml:id="ECMAScript-objects-semantics">
<title>Semantics</title>
<simpara>It is not possible to inherit from any of the built-in ECMAScript object types except for <literal>Object</literal> and <literal>Error</literal>, that is,
to use one of these types as supertype of a class.
From the N4JS language’s point of view, these built-in types are all final.</simpara>
</section>
<section xml:id="_object-type">
<title>Object Type</title>
<simpara><literal>Object</literal> [<link linkend="ECMA11a">ECMA11a(p.S8.6)</link>] is the (implicit) supertype of all declared (i.e., non-primtive) types, including native types.
It models the ECMAScript type <literal>Object</literal>, except that no properties may be dynamically added to it.
In order to declare a variable to which properties can be dynamically added, the type <literal>Object+</literal> has to be declared
(cf. <xref linkend="_type-modifiers"/>).</simpara>
</section>
<section xml:id="_function-object-type">
<title>Function-Object-Type</title>
<simpara>The built-in object type <literal>Function</literal>, a subtype of <literal>Object</literal>, represents all functions, regardless of how they are defined (either via function expression,
function declaration, or method declaration).
They are described in detail in <xref linkend="_function-object-type"/>.</simpara>
<simpara>Since <literal>Function</literal> is the supertype of all functions regardless of number and types of formal parameters, return type, and number and bounds of type parameters,
it would not normally be possible to invoke an instance of <literal>Function</literal>.
For the time being, however, an instance of <literal>Function</literal> can be invoked, any number of arguments may be provided and the invocation may be parameterized with any number of
type arguments (which will be ignored), i.e.  <xref linkend="Req-IDE-101"/> and <xref linkend="Req-IDE-102"/> do not apply.</simpara>
</section>
<section xml:id="_array-object-type">
<title>Array Object Type</title>
<simpara>The <literal>Array</literal> type is generic with one type parameter, which is the item type. An array is accessed with the index operator, the type of the index parameter is <literal>Number</literal>.
The type of the stored values is <emphasis>typeArgs[0]</emphasis> (cf. <xref linkend="_array-literal"/>). Due to type erasure, the item type is not available during runtime,
that is to say there are no reflective methods returning the item type of an array.</simpara>
<requirement xml:id="IDE-14">
<title>Array Type</title>
<simpara>
<anchor xml:id="Req-IDE-14" xreflabel="[Req-IDE-14]"/>
<emphasis role="strong">Requirement: IDE-14:</emphasis>
<link linkend="Req-IDE-14">Array Type</link> (ver. 1)</simpara>
<simpara>
For an array type <emphasis>A</emphasis>, the following conditions must be true:</simpara>
<itemizedlist>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo>|</mo><mi>A</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>A</mi><mi>r</mi><mi>g</mi><mi>s</mi><mo>|</mo><mo>=</mo><mn>1</mn></math></simpara>
</listitem>
</itemizedlist>
</requirement>
</section>
<section xml:id="_string-object-type">
<title>String Object Type</title>
<simpara>Object type version of <literal>string</literal>. It is highly recommend to use the primitive version only.
Note that is is not possible to assign a primitive typed value to an object typed variable.</simpara>
</section>
<section xml:id="_boolean-object-type">
<title>Boolean Object Type</title>
<simpara>Object type version of <literal>boolean</literal>. It is highly recommend to use the primitive version only.
Note that is is not possible to assign a primitive typed value to an object typed variable.</simpara>
</section>
<section xml:id="_number-object-type">
<title>Number Object Type</title>
<simpara>Object type version of <literal>number</literal>. It is highly recommend to use the primitive version only.
Note that is is not possible to assign a primitive typed value to an object typed variable.</simpara>
</section>
<section xml:id="_global-object-type">
<title>Global Object Type</title>
<simpara>This is the globally accessible namespace which contains element such as undefined, and in case of browsers, window. Depending on the runtime environment,
the global object may has different properties defined by means of dynamic polyfills.</simpara>
</section>
<section xml:id="_symbol">
<title>Symbol</title>
<simpara>The symbol constructor function of ECMAScript 2015. Support for symbols
is kept to a minimum in N4JS:</simpara>
<itemizedlist>
<listitem>
<simpara>creating symbols with <literal>var sym = Symbol("description")</literal> is not supported.</simpara>
</listitem>
<listitem>
<simpara>creating shared symbols with <literal>var sym = Symbol.for("key")</literal> is not supported.
Also the inverse <literal>Symbol.keyFor(sym)</literal> is not supported.</simpara>
</listitem>
<listitem>
<simpara>retrieving built-in symbols via properties in <literal>Symbol</literal> is supported, however, the only built-in symbol available in N4JS is the iterator symbol that can be retrieved with <literal>Symbol.iterator</literal>.</simpara>
</listitem>
</itemizedlist>
<simpara>The rationale for this selective support for symbols in N4JS is to allow for the use (and custom definition) of iterators and iterables and their application in the <literal>for&#8230;&#8203;of</literal>
loop with as little support for symbols as possible.</simpara>
</section>
<section xml:id="_promise">
<title>Promise</title>
<simpara><literal>Promise</literal> is provided as a built-in type as in ECMAScript 2015.
Also see <xref linkend="_asynchronous-functions"/> for asynchronous functions.</simpara>
</section>
<section xml:id="_iterator-interface">
<title>Iterator Interface</title>
<simpara>A structurally typed interface for <emphasis>iterators</emphasis> as defined by theECMAScript 6 iterator protocol.</simpara>
<formalpara>
<title>Iterable in N4JS</title>
<para>
<programlisting language="n4js" linenumbering="unnumbered">// providedByRuntime
export public interface ~Iterator&lt;T&gt; {
public next(): IteratorEntry&lt;T&gt;
}
// providedByRuntime
export public interface ~IteratorEntry&lt;T&gt; {
public done: boolean;
public value: T?;
}</programlisting>
</para>
</formalpara>
<simpara role="todo">Interface <literal>IteratorEntry</literal> was introduced mainly as a workaround; this interface could be removed and replaced with a corresponding
structural type reference as return type of method <literal>next()</literal></simpara>
</section>
<section xml:id="_iterable-interface">
<title>Iterable Interface</title>
<simpara>A structurally typed interface for objects that can be iterated over, i.e. <emphasis>iterables</emphasis> as defined by the ECMAScript 6 iterator protocol.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">// providedByRuntime
export public interface ~Iterable&lt;T&gt; {
public [Symbol.iterator](): Iterator&lt;T&gt;
}</programlisting>
<simpara>Note that this interface’s method is special in that a symbol is used as identifier.
You can use the ordinary syntax for computed property names in ECMAScript 6 for overriding / implementing or invoking this method.</simpara>
</section>
</section>
<section xml:id="_built-in-n4js-types" role="language-n4js">
<title>Built-In N4JS Types</title>
<simpara>N4JS additionally provides some built-in classes which are always available with the need to explicitly import them.</simpara>
<section xml:id="_n4object">
<title>N4Object</title>
<simpara>Although <literal>N4Object</literal> is a built-in type, it is not the default supertype.
It is a subtype of <literal>Object</literal>.</simpara>
<section xml:id="N4Object-semantics">
<title>Semantics</title>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow/><mrow><mstyle mathvariant="monospace"><mtext>N4Object</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>Object</mtext></mstyle></mrow></mfrac></math>
</section>
</section>
<section xml:id="_n4class">
<title>N4Class</title>
<simpara>The type <literal>N4Class</literal> is used for extended reflection in N4JS.</simpara>
<simpara role="todo">Add further docs for this type</simpara>
</section>
<section xml:id="IterableN">
<title>IterableN</title>
<simpara role="todo">Work in progress.</simpara>
<simpara>Currently there are built-in types <literal>Iterable2&lt;T1,T2&gt;</literal>&#8230;&#8203;<literal>Iterable9&lt;T1,&#8230;&#8203;,T9&gt;</literal>.
They are mainly intended for type system support of array destructuring literals.</simpara>
<note>
<simpara>This is not documented in detail yet, because we want to gain experience with the current solution first, major refinement may be incoming.</simpara>
</note>
</section>
</section>
<section xml:id="_type-modifiers" role="language-n4js">
<title>Type Modifiers</title>
<simpara>Type expressions can be further described with type modifiers.
The type modifiers add additional constraints to the type expression which are then used to perform a stricter validation of the source code.
Type modifiers can not be used in type arguments.</simpara>
<simpara>The general type modifiers <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi><mi>a</mi><mi>b</mi><mi>l</mi><mi>e</mi></math>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi><mi>o</mi><mi>n</mi><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>d</mi><mi>y</mi><mi>n</mi><mi>a</mi><mi>m</mi><mi>i</mi><mi>c</mi></math> can be used for variables, attributes, method parameters and method types.
Optional and variadic modifiers can only be applied for formal parameters.</simpara>
<section xml:id="Type_Modifiers_Dynamic">
<title>Dynamic</title>
<simpara>The dynamic type modifier marks a type as being dynamic.
A dynamic type behaves like a normal JavaScript object, so you can read/write any property and call any method on it.
The default behavior for a type is to be static, that is no new properties can be added and no unknown properties can be accessed.</simpara>
<simpara><literal>T</literal> <math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/></math> <literal>T+</literal> and <literal>T+</literal> <math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/></math> <literal>T</literal> is always true.
Using dynamically added members of a dynamic type is never type safe.
Using the <literal>delete</literal> operator on a subtype of <literal>N4Object</literal> is not allowed.</simpara>
<requirement xml:id="IDE-15">
<title>Non-Dynamic Primitive Types</title>
<simpara>
<anchor xml:id="Req-IDE-15" xreflabel="[Req-IDE-15]"/>
<emphasis role="strong">Requirement: IDE-15:</emphasis>
<link linkend="Req-IDE-15">Non-Dynamic Primitive Types</link> (ver. 1)</simpara>
<simpara>
1. All primitive types except <literal>any</literal> must not be declared dynamic.
2. Only parameterized type references and this type reference can be declared dynamic.<footnote><simpara>This is a consequence of the syntax definition.</simpara></footnote></simpara>
</requirement>
</section>
<section xml:id="_optional-return-types">
<title>Optional Return Types</title>
<simpara>Only formal parameters and return types can be marked as optional.</simpara>
<simpara>An optional return type indicates that the function / method need not be left via a return statement with an expression; in that case the return value is <literal>undefined</literal>.
For constraints on using the optional modifier, see <xref linkend="_function-object-type"/>.</simpara>
</section>
</section>
<section xml:id="_union-and-intersection-type-composed-types" role="language-n4js">
<title>Union and Intersection Type (Composed Types)</title>
<simpara>Given two or more existing types, it is possible to compose a new type by forming either the union or intersection of the base types.
The following example gives a small overview of common use cases of union and intersection types.</simpara>
<example>
<title>Composed types</title>
<simpara>This example shows how union and intersection types affect the types of their field members in case the fields have different types.
It is for illustration purposes only.
The type of the composed field depends on the access type:
When reading, the field type of an intersection/union also resolves to the intersection/union.
In contrast, when writing a field, the field type of an intersection/union resolves to the union/intersection respectively.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">interface A { f : int = 1; }
interface B { f : string = "h"; }
class CA implements A {}
class CB implements B {}
let aub : A|B; // union type
let aib : A&amp;B; // intersection type
function u() {
aub = (catIsAlive)? new CA() : new CB(); // catIsAlive is boolean
let x = aub.f; // x = {1 | "h"}
aub.f = undefined; // undefined can be assigned to int and string types
}
function i() {
let a = aib as A;
let b = aib as B;
a.f = 23;
b.f = "text";
let x = aib.f; // x = {23 &amp; "text"} which is impossible
}
// type of 'aub.f' --&gt; int|string
let fu = aub.f;
// type of 'aub.f' --&gt; int&amp;string
aub.f = undefined;
// type of 'aib.f' --&gt; int&amp;string
let fi = aib.f;
// type of 'aib.f' --&gt; int|string
aib.f = undefined;</programlisting>
<simpara>Note that no instance <literal>aib</literal> of intersection type <literal>A&amp;B</literal> can be instantiated, since the instance&#8217;s class would have to define a field <literal>f</literal> which would have to comply to both of the interfaces <literal>A</literal> and <literal>B</literal>.
Still the function <literal>i()</literal> shows in general how variables of intersection types can be casted and accessed.</simpara>
</example>
<simpara>The following sections define these <emphasis>union</emphasis> and <emphasis>intersection types</emphasis> in detail.</simpara>
<section xml:id="_union-type">
<title>Union Type</title>
<simpara>Union type reflect the dynamic nature of JavaScript. Union types can be used almost everywhere (e.g., in variable declarations or in formal method parameters).
The type inferencer usually avoids returning union types and prefers single typed joins or meets.
<emphasis>The most common use case for union types is for emulating method overloading</emphasis>, as
we describe later on.<footnote><simpara>For type theory about union types, [<link linkend="Pierce02a">Pierce02a(p.15.7)</link>] and [<link linkend="Igarashi07a">Igarashi07a</link>], other languages that explicitly support the notion of union type include Ceylon [<link linkend="King13a">King13a(p.3.2.4/5)</link>]</simpara></footnote></simpara>
<section xml:id="union-type-syntax">
<title>Syntax</title>
<simpara>For convenience, we repeat the definition of union type expression:</simpara>
<programlisting language="xtext" linenumbering="unnumbered">UnionTypeExpression: 'union' '{' typeRefs+=TypeRefWithoutModifiers (',' typeRefs+=TypeRefWithoutModifiers)* '}';</programlisting>
</section>
<section xml:id="union-type-semantics">
<title>Semantics</title>
<simpara>An union type states that the type of a variable may be one or more types contained in the union type.
In other words, a union type is a kind of type set, and the type of a variable is contained in the type set.
Due to interfaces, a variable may conform to multiple types.</simpara>
<requirement xml:id="IDE-18">
<title>Union Type</title>
<simpara>
<anchor xml:id="Req-IDE-18" xreflabel="[Req-IDE-18]"/>
<emphasis role="strong">Requirement: IDE-18:</emphasis>
<link linkend="Req-IDE-18">Union Type</link> (ver. 1)</simpara>
<simpara>
For a given union type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>U</mi><mo>=</mo><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><msub><mi>T</mi><mn>1</mn></msub><mrow><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></mrow><msub><mi>T</mi><mi>n</mi></msub></mfenced></math>, the following conditions must hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>Non-empty: At least one element has to be specified:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>U</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>s</mi><mo></mo><mi></mi></math> (<math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>n</mi><mo></mo><mn>1</mn><mo>)</mo></mrow></math></simpara>
</listitem>
<listitem>
<simpara>Non-dynamic: The union type itself must not be declared dynamic:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>¬</mo><mi>U</mi><mo>.</mo><mi>d</mi><mi>y</mi><mi>n</mi><mi>a</mi><mi>m</mi><mi>i</mi><mi>c</mi></math></simpara>
</listitem>
<listitem>
<simpara>Non-optional elements:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>T</mi><mo></mo><mi>U</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>s</mi><mo></mo><mo>¬</mo><mi>T</mi><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi></math></simpara>
</listitem>
</orderedlist>
</requirement>
<requirement xml:id="IDE-19">
<title>Union Type Subtyping Rules</title>
<simpara>
<anchor xml:id="Req-IDE-19" xreflabel="[Req-IDE-19]"/>
<emphasis role="strong">Requirement: IDE-19:</emphasis>
<link linkend="Req-IDE-19">Union Type Subtyping Rules</link> (ver. 1)</simpara>
<simpara>
Let <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>U</mi></math> be an union type.</simpara>
<itemizedlist>
<listitem>
<simpara>The union type is a common supertype of all its element types:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>T</mi><mo></mo><mi>U</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>s</mi></mrow><mrow><mi>T</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>U</mi></mrow></mfrac></math>
</listitem>
<listitem>
<simpara>More generally, a type is a subtype of a union type, if it is a
subtype of at least one type contained in the union:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mo></mo><mi>T</mi><mo></mo><mi>U</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>s</mi><mi>:</mi><mi>S</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>T</mi></mrow><mrow><mi>S</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>U</mi></mrow></mfrac></math>
</listitem>
<listitem>
<simpara>A union type is a subtype of a type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi></math>, if all types of the union are subtypes of that type.
This rule is a generalization of the sub typing rules given in [<link linkend="Igarashi07a">Igarashi07a(p.p.40)</link>]</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mo></mo><mi>T</mi><mo></mo><mi>U</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>s</mi><mi>:</mi><mi>T</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>S</mi></mrow><mrow><mi>U</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>S</mi></mrow></mfrac></math>
</listitem>
<listitem>
<simpara>Commutativity: The order of element does not matter:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><mi>A</mi><mo>,</mo><mi>B</mi></mrow></mfenced><mo>=</mo><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><mi>B</mi><mo>,</mo><mi>A</mi></mrow></mfenced></math>
</listitem>
<listitem>
<simpara>Associativity:
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><mi>A</mi><mo>,</mo><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><mi>B</mi><mo>,</mo><mi>C</mi></mrow></mfenced></mrow></mfenced><mo>=</mo><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><mi>A</mi><mo>,</mo><mi>B</mi></mrow></mfenced><mo>,</mo><mi>C</mi></mrow></mfenced></math></simpara>
</listitem>
<listitem>
<simpara>Uniqueness of elements: A union type may not contain duplicates
(similar to sets):</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mn>1</mn><mo></mo><mi>i</mi><mo>&lt;</mo><mi>k</mi><mo></mo><mi>n</mi><mo>,</mo><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msub><mi>T</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub></mrow></mfenced><mi>:</mi><msub><mi>T</mi><mi>i</mi></msub><mo></mo><msub><mi>T</mi><mi>k</mi></msub></math></simpara>
</listitem>
</itemizedlist>
</requirement>
<requirement xml:id="IDE-20">
<title>Implicit simplification of union types</title>
<simpara>
<anchor xml:id="Req-IDE-20" xreflabel="[Req-IDE-20]"/>
<emphasis role="strong">Requirement: IDE-20:</emphasis>
<link linkend="Req-IDE-20">Implicit simplification of union types</link> (ver. 1)</simpara>
<simpara>
Let <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>U</mi></math> be an union type.
The following simplification rules are always automatically applied to union types.</simpara>
<itemizedlist>
<listitem>
<simpara>Simplification of union type with one element:
If a union type contains only one element, it is reduced to the element:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mi>T</mi></mfenced></mrow><mi>T</mi></mfrac></math>
</listitem>
<listitem>
<simpara>Simplification of union types of union types:
A union type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>U</mi></math> containing another union type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>V</mi></math> is reduced to a single union type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>W</mi></math>, with
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>W</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>s</mi><mo>=</mo><mi>U</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>s</mi><mo></mo><mi>V</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>s</mi></math>:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msub><mi>S</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>S</mi><mrow><mi>k</mi><mo>-</mo><mn>1</mn></mrow></msub><mo>,</mo><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msub><mi>T</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>m</mi></msub></mrow></mfenced><mo>,</mo><msub><mi>S</mi><mrow><mi>k</mi><mo>+</mo><mn>1</mn></mrow></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>S</mi><mi>n</mi></msub></mrow></mfenced></mrow><mrow><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msub><mi>S</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>S</mi><mrow><mi>k</mi><mo>-</mo><mn>1</mn></mrow></msub><mo>,</mo><msub><mi>T</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>m</mi></msub><mo>,</mo><msub><mi>S</mi><mrow><mi>k</mi><mo>+</mo><mn>1</mn></mrow></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>S</mi><mi>n</mi></msub></mrow></mfenced></mrow></mfrac></math>
</listitem>
<listitem>
<simpara>Simplification of union type with undefined or null:
Since undefined is the bottom type, and null is kind of a second button type, they are removed from the union:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msub><mi>T</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mrow><mi>k</mi><mo>-</mo><mn>1</mn></mrow></msub><mo>,</mo><msub><mi>T</mi><mi>k</mi></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub></mrow></mfenced></mrow><mrow><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msub><mi>T</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mrow><mi>k</mi><mo>-</mo><mn>1</mn></mrow></msub><mo>,</mo><mi>u</mi><mi>n</mi><mi>d</mi><mi>e</mi><mi>f</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>d</mi></mrow></mfenced><mo>,</mo><msub><mi>T</mi><mi>k</mi></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msub><mi>T</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mrow><mi>k</mi><mo>-</mo><mn>1</mn></mrow></msub><mo>,</mo><msub><mi>T</mi><mi>k</mi></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub></mrow></mfenced></mrow><mrow><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msub><mi>T</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mrow><mi>k</mi><mo>-</mo><mn>1</mn></mrow></msub><mo>,</mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi></mrow></mfenced><mo>,</mo><msub><mi>T</mi><mi>k</mi></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub></mrow></mfrac></math></simpara>
</listitem>
</itemizedlist>
<note>
<simpara>Simplification rules for union types with one element are applied first.</simpara>
</note>
<itemizedlist>
<listitem>
<simpara>The structural typing strategy is propagated to the types of the union:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>~</mi><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msub><mi>T</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub></mrow></mfenced></mrow><mrow><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><mi>~</mi><msub><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mn>1</mn></msub><mo>,</mo><mi></mi><mo>,</mo><mi>~</mi><msub><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mi>n</mi></msub></mrow></mfenced></mrow></mfrac></math>
</listitem>
</itemizedlist>
</requirement>
<simpara>Remarks:</simpara>
<itemizedlist>
<listitem>
<simpara>The simplification rules may be applied recursively.</simpara>
</listitem>
<listitem>
<simpara>For given types <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>B</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>A</mi></math>, and the union type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>U</mi><mo>=</mo><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><mi>A</mi><mo>,</mo><mi>B</mi></mrow></mfenced></math>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>U</mi><mo></mo><mi>B</mi></math>.
The types are equivalent, however: <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>A</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mo>=</mo><mi>U</mi></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>U</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mo>=</mo><mi>A</mi></math>.<footnote><simpara>This is different from Ceylon ( [<link linkend="King13a">King13a(p.3.2.3)</link>]), in which the union is defined to be <literal>the same type as</literal> <emphasis>A</emphasis>. Although the meaning of <literal>same</literal> is not clear, it is possibly used as a synonym for <literal>equivalent</literal>.</simpara></footnote></simpara>
</listitem>
</itemizedlist>
<example>
<title>Subtyping with union type</title>
<simpara>Let A, B, and C be defined as in the chapter beginning (<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>B</mi><mo>&lt;</mo><mi>:</mi><mi>A</mi></math>)</simpara>
<simpara>The following subtyping relations with union types are to be evaluated as follows: <footnote><simpara>See Example <xref linkend="ex:class-hierarchy"/> for class definitions.</simpara></footnote></simpara>
<programlisting language="n4js" linenumbering="unnumbered">A &lt;: union{A} -&gt; true
A &lt;: union{A,B} -&gt; true
B &lt;: union{A,B} -&gt; true
C &lt;: union{A,B} -&gt; true
A &lt;: union{B,C} -&gt; false
B &lt;: union{B,C} -&gt; true
C &lt;: union{B,C} -&gt; true
union{A} &lt;: A -&gt; true
union{B} &lt;: A -&gt; true
union{B,C} &lt;: A -&gt; true
union{A,B} &lt;: B -&gt; false
union{X,Z} &lt;: union{Z,X} -&gt; true
union{X,Y} &lt;: union{X,Y,Z} -&gt; true
union{X,Y,Z} &lt;: union{X,Y} -&gt; false</programlisting>
</example>
<simpara>The simplification constraints are used by the type inferrer.
It may be useful, however, to define union types with superfluous elements, as the next example demonstrates</simpara>
<example>
<title>Superfluous elements in union type</title>
<programlisting language="n4js" linenumbering="unnumbered">class A{}
class B extends A{}
class C extends A{}
function foo(p: union{A,B}) {..}</programlisting>
</example>
<simpara>Although <literal>B</literal> is superfluous, it may indicate that the function handles parameters of type differently than one of type <literal>A</literal> or <literal>C</literal>.</simpara>
<simpara>Although a union type is a <literal><link linkend="_acronyms">LCST</link></literal> of its contained (non-superfluous) types, the type inferrer usually does not create new union types when computing the join of types.
If the join of types including at least one union type is calculated, the union type is preserved if possible.
The same is true for meet.</simpara>
<simpara>For the definition of join and meet for union types, we define how a type is added to a union type:</simpara>
<requirement xml:id="IDE-21">
<title>Union of union type</title>
<simpara>
<anchor xml:id="Req-IDE-21" xreflabel="[Req-IDE-21]"/>
<emphasis role="strong">Requirement: IDE-21:</emphasis>
<link linkend="Req-IDE-21">Union of union type</link> (ver. 1)</simpara>
<simpara>
The union of union types is defined similar to the union of sets.
The union is not simplified, but it contains no duplicates.</simpara>
<simpara>If a type A is contained in a union type, then the union type is a common supertype, and (since it is the union itself) also the <literal><link linkend="_acronyms">LCST</link></literal> of both types.
This finding is the foundation of the definition of join of a (non-union) type with a union type:</simpara>
</requirement>
<requirement xml:id="IDE-22">
<title>Join with Union Type</title>
<simpara>
<anchor xml:id="Req-IDE-22" xreflabel="[Req-IDE-22]"/>
<emphasis role="strong">Requirement: IDE-22:</emphasis>
<link linkend="Req-IDE-22">Join with Union Type</link> (ver. 1)</simpara>
<simpara>
The join <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>J</mi></math> of a union type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>U</mi></math> with a type <emphasis>T</emphasis> is the union of both types:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>J</mi><mo>=</mo><mi>U</mi><mo></mo><mi>T</mi></mrow><mrow><mfenced close=")" open="("><mrow><mi>U</mi><mo></mo><mi>T</mi></mrow></mfenced><mo>=</mo><mi>J</mi></mrow></mfrac></math>
<simpara>Remarks:</simpara>
<itemizedlist>
<listitem>
<simpara>Joining a union type with another type is not similar to joining the elements of the union type directly with another type.
That is</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>A</mi><mspace width="3.0mm"/><mstyle mathvariant="bold"><mi>j</mi><mi>o</mi><mi>i</mi><mi>n</mi></mstyle><mspace width="3.0mm"/><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><mi>B</mi><mo>,</mo><mi>C</mi></mrow></mfenced><mo></mo><mi>A</mi><mspace width="3.0mm"/><mstyle mathvariant="bold"><mi>j</mi><mi>o</mi><mi>i</mi><mi>n</mi></mstyle><mspace width="3.0mm"/><mi>B</mi><mspace width="3.0mm"/><mstyle mathvariant="bold"><mi>j</mi><mi>o</mi><mi>i</mi><mi>n</mi></mstyle><mspace width="3.0mm"/><mi>C</mi></math>
</listitem>
<listitem>
<simpara>The computed join is simplified according to the constraints defined above.</simpara>
</listitem>
</itemizedlist>
</requirement>
<requirement xml:id="IDE-23">
<title>Meet with Union Type</title>
<simpara>
<anchor xml:id="Req-IDE-23" xreflabel="[Req-IDE-23]"/>
<emphasis role="strong">Requirement: IDE-23:</emphasis>
<link linkend="Req-IDE-23">Meet with Union Type</link> (ver. 1)</simpara>
<simpara>
The meet of union types is defined as the meet of the elements.
That is</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mfrac><mrow><msub><mi>T</mi><mn>1</mn></msub><mo></mo><mi>S</mi><mo></mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo></mo><msub><mi>T</mi><mi>n</mi></msub><mo></mo><mi>S</mi></mrow><mrow><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msub><mi>T</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub></mrow></mfenced><mo></mo><mi>S</mi></mrow></mfrac></mtd></mtr><mtr><mtd><mfrac><mrow><msub><mi>T</mi><mn>1</mn></msub><mo></mo><msub><mi>S</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mn>1</mn></msub><mo></mo><msub><mi>S</mi><mi>m</mi></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub><mo></mo><msub><mi>S</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub><mo></mo><msub><mi>S</mi><mi>m</mi></msub></mrow><mrow><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msub><mi>T</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub></mrow></mfenced><mo></mo><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msub><mi>S</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>S</mi><mi>m</mi></msub></mrow></mfenced></mrow></mfrac></mtd></mtr></mtable></math>
<simpara>Remarks:</simpara>
<itemizedlist>
<listitem>
<simpara>The meet of a union type with another type is not a union type itself.
This gets clear when looking at the definition of meet and union type.
While for a given <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>U</mi><mo>=</mo><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><mi>A</mi><mo>,</mo><mi>B</mi></mrow></mfenced></math>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>A</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>U</mi></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>B</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>U</mi></math>, the opposite <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>U</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>A</mi></math> is usually not true (unless <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>U</mi></math> can be simplified to <emphasis>A</emphasis>).
So, for <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>A</mi><mo></mo><mi>U</mi></math>, usually <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>U</mi></math> cannot be the meet.</simpara>
</listitem>
</itemizedlist>
</requirement>
<requirement xml:id="IDE-24">
<title>Upper and Lower Bound of a Union Type</title>
<simpara>
<anchor xml:id="Req-IDE-24" xreflabel="[Req-IDE-24]"/>
<emphasis role="strong">Requirement: IDE-24:</emphasis>
<link linkend="Req-IDE-24">Upper and Lower Bound of a Union Type</link> (ver. 1)</simpara>
<simpara>
</simpara>
<simpara>The upper and lower bound of a union type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>U</mi></math> is a union type <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>U</mi><mi>'</mi></msup></math> containing the upper and lower bound of the elements of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>U</mi></math>:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mfenced close=")" open="("><mrow><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msub><mi>T</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub></mrow></mfenced></mrow></mfenced><mi>:</mi><mo>=</mo><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mfenced close=")" open="("><msub><mi>T</mi><mn>1</mn></msub></mfenced><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mfenced close=")" open="("><msub><mi>T</mi><mn>1</mn></msub></mfenced></mrow></mfenced></mtd></mtr><mtr><mtd><mi>l</mi><mi>o</mi><mi>w</mi><mi>e</mi><mi>r</mi><mfenced close=")" open="("><mrow><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msub><mi>T</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub></mrow></mfenced></mrow></mfenced><mi>:</mi><mo>=</mo><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><mi>l</mi><mi>o</mi><mi>w</mi><mi>e</mi><mi>r</mi><mfenced close=")" open="("><msub><mi>T</mi><mn>1</mn></msub></mfenced><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><mi>l</mi><mi>o</mi><mi>w</mi><mi>e</mi><mi>r</mi><mfenced close=")" open="("><msub><mi>T</mi><mn>1</mn></msub></mfenced></mrow></mfenced></mtd></mtr></mtable></math>
</requirement>
</section>
<section xml:id="_warnings">
<title>Warnings</title>
<simpara>In case the <literal>any</literal> type is used in a union type, all other types in the union type definition become obsolete.
However, defining other typers along with the <literal>any</literal> type might seem reasonable in case those other types are treated specifically and thus are mentioned explicitly in the definition.
Nevertheless the use of the <literal>any</literal> type produces a warning, since its use can indicate a misunderstanding of the union type concept and since documentation can also be done in a comment.</simpara>
<requirement xml:id="IDE-25">
<title>Any type in union types</title>
<simpara>
<anchor xml:id="Req-IDE-25" xreflabel="[Req-IDE-25]"/>
<emphasis role="strong">Requirement: IDE-25:</emphasis>
<link linkend="Req-IDE-25">Any type in union types</link> (ver. 1)</simpara>
<simpara>
No union type shall conatin an type:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mo></mo><mi>a</mi><mi>n</mi><mi>y</mi><mo></mo><mi>U</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>s</mi></math>
<simpara>Similar to the documentary purpose of using specific classes along with the <literal>any</literal> type is the following case.
When two types are used, one of them a subtype of the other, then this subtype is obsolete. Still it can be used for documentary purposes.
However, a warning will be produced to indicate unecessary code.
The warning is only produced when both of the types are either classes or interfaces, since e.g. structural types are supertypes of any classes or interfaces.</simpara>
</requirement>
<requirement xml:id="IDE-26">
<title>Redundant subtypes in union types</title>
<simpara>
<anchor xml:id="Req-IDE-26" xreflabel="[Req-IDE-26]"/>
<emphasis role="strong">Requirement: IDE-26:</emphasis>
<link linkend="Req-IDE-26">Redundant subtypes in union types</link> (ver. 1)</simpara>
<simpara>
Union types shall not
contain class or interface types which are a subtype of another class or interface type that also is contained in the union type.</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mo></mo><mi>T</mi><mi>T</mi><mo></mo><mi>U</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>s</mi><mi>:</mi><mo></mo><mi>T</mi><mo></mo><mi>U</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>s</mi><mi>:</mi></mtd></mtr><mtr><mtd><mrow><mo>(</mo><mrow><mi>T</mi><mi>T</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>T</mi><mo></mo><mi>i</mi><mi>s</mi><mi>C</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mi>O</mi><mi>r</mi><mi>I</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>f</mi><mi>a</mi><mi>c</mi><mi>e</mi><mfenced close=")" open="("><mi>T</mi></mfenced><mo></mo><mi>i</mi><mi>s</mi><mi>C</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mi>O</mi><mi>r</mi><mi>I</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>f</mi><mi>a</mi><mi>c</mi><mi>e</mi><mfenced close=")" open="("><mrow><mi>T</mi><mi>T</mi></mrow></mfenced><mo>)</mo></mrow></mrow></mtd></mtr></mtable></math>
</requirement>
</section>
</section>
<section xml:id="_intersection-type">
<title>Intersection Type</title>
<simpara>Intersection type reflects the dynamic nature of JavaScript, similar to union type.
As in Java, intersection type is used to define the type boundaries of type variables in type parameter definitions.
They are inferred by the type inferencer for type checking (as a result of join or meet).
In contrast to Java, however, intersection type can be declared explicitly by means of intersection type expression.<footnote><simpara>For type theory about intersection types, see [<link linkend="Pierce02a">Pierce02a(p.15.7)</link>] and [<link linkend="Laurent12a">Laurent12a</link>], other languages supporting explicit notion of intersection type include Ceylon [<link linkend="King13a">King13a(p.3.2.4/5)</link>].</simpara></footnote></simpara>
<section xml:id="intersection-type-syntax">
<title>Syntax</title>
<simpara>For convenience, we repeat the definition of intersection type expression and of type variables in which intersection types can be defined as in Java:</simpara>
<programlisting language="xtext" linenumbering="unnumbered">InterSectionTypeExpression: 'intersection' '{' typeRefs+=TypeRefWithoutModifiers (',' typeRefs+=TypeRefWithoutModifiers)* '}';
TypeVariable: name=IDENTIFIER ('extends' declaredUpperBounds+=ParameterizedTypeRefNominal ('&amp;' declaredUpperBounds+=ParameterizedTypeRefNominal)*)?</programlisting>
</section>
<section xml:id="intersection-type-semantics">
<title>Semantics</title>
<simpara>An intersection type may contain several interfaces but only one class.
It virtually declares a subclass of this one class and implements all interfaces declared in the intersection type.
If no class is declared in the intersection type, the intersection type virtually declares a subclass of an N4Object instead.
This virtual subclass also explains why only one single class may be contained in the intersection.</simpara>
<requirement xml:id="IDE-27">
<title>Intersection Type</title>
<simpara>
<anchor xml:id="Req-IDE-27" xreflabel="[Req-IDE-27]"/>
<emphasis role="strong">Requirement: IDE-27:</emphasis>
<link linkend="Req-IDE-27">Intersection Type</link> (ver. 1)</simpara>
<simpara>
For a given intersection type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>I</mi></math>, the following conditions must hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>The intersection must contain at least one type:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>I</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>s</mi><mo></mo><mi></mi></math>
</listitem>
<listitem>
<simpara>Only one class must be contained in the intersection type:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfenced close=")" open="("><mrow><mo></mo><mi>C</mi><mo></mo><mi>I</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>s</mi><mi>:</mi><mi>μ</mi><mfenced close=")" open="("><mi>C</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Class</mtext></mstyle></mrow></mfenced><mo></mo><mo></mo><mi>T</mi><mo></mo><mi>I</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>s</mi><mo></mo><mfenced close="}" open="{"><mi>C</mi></mfenced><mi>:</mi><mi>μ</mi><mfenced close=")" open="("><mi>T</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Class</mtext></mstyle></math>
<simpara>For the time being, only a warning is produced when more than one class is contained in the intersection type .</simpara>
</listitem>
<listitem>
<simpara>Non-optional elements:</simpara>
</listitem>
</orderedlist>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mo></mo><mi>T</mi><mo></mo><mi>I</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>s</mi><mo></mo><mo>¬</mo><mi>T</mi><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi></math>
</requirement>
<requirement xml:id="IDE-175">
<title>Intersection Type Subtyping Rules</title>
<simpara>
<anchor xml:id="Req-IDE-175" xreflabel="[Req-IDE-175]"/>
<emphasis role="strong">Requirement: IDE-175:</emphasis>
<link linkend="Req-IDE-175">Intersection Type Subtyping Rules</link> (ver. 1)</simpara>
<simpara>
Let <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>I</mi></math> be an intersection type.</simpara>
<itemizedlist>
<listitem>
<simpara>An intersection type is a subtype of another type, if at least one of
its contained types is a subtype of that type: <footnote><simpara>This rule is a generalization of the subtyping rules given in [<link linkend="Laurent12a">Laurent12a</link>] Table 2, <math xmlns="http://www.w3.org/1998/Math/MathML"><msubsup><mo></mo><mi>l</mi><mn>1</mn></msubsup></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><msubsup><mo></mo><mi>l</mi><mn>2</mn></msubsup></math></simpara></footnote></simpara>
</listitem>
</itemizedlist>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mo></mo><mi>T</mi><mo></mo><mi>I</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>s</mi><mi>:</mi><mi>T</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>S</mi></mrow><mrow><mi>I</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>S</mi></mrow></mfrac></math>
<itemizedlist>
<listitem>
<simpara>A type is a subtype of an intersection type, if it is a subtype of all
types contained in the intersection type: <footnote><simpara>This rule is a generalization of the subtyping rules given in [<link linkend="Laurent12a">Laurent12a</link>] Table 2, <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mo></mo><mi>r</mi></msub></math></simpara></footnote></simpara>
</listitem>
</itemizedlist>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mo></mo><mi>T</mi><mo></mo><mi>I</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>s</mi><mi>:</mi><mi>S</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>T</mi></mrow><mrow><mi>S</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>I</mi></mrow></mfrac></math>
<itemizedlist>
<listitem>
<simpara>Non-optional elements:
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>T</mi><mo></mo><mi>I</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>s</mi><mo></mo><mo>¬</mo><mi>T</mi><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi></math></simpara>
</listitem>
</itemizedlist>
</requirement>
<requirement xml:id="IDE-28">
<title>Implicit simplification of intersection types</title>
<simpara>
<anchor xml:id="Req-IDE-28" xreflabel="[Req-IDE-28]"/>
<emphasis role="strong">Requirement: IDE-28:</emphasis>
<link linkend="Req-IDE-28">Implicit simplification of intersection types</link> (ver. 1)</simpara>
<simpara>
Let <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>I</mi></math> be an intersection type.
The following simplification rules are always automatically applied to intersection types.</simpara>
<itemizedlist>
<listitem>
<simpara>The structural typing strategy is propagated to the types of the intersection:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>~</mi><mi>i</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msub><mi>T</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub></mrow></mfenced></mrow><mrow><mi>i</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><mi>~</mi><msub><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mn>1</mn></msub><mo>,</mo><mi></mi><mo>,</mo><mi>~</mi><msub><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mi>n</mi></msub></mrow></mfenced></mrow></mfrac></math>
</listitem>
</itemizedlist>
<simpara>These subtyping rules are similar to Ceylon. <footnote><simpara>In Ceylon, for a given union type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>U</mi><mo>=</mo><msub><mi>T</mi><mn>1</mn></msub><mo>|</mo><msub><mi>T</mi><mn>2</mn></msub></math> and intersection type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>I</mi><mo>=</mo><msub><mi>T</mi><mn>1</mn></msub><mi>&amp;</mi><msub><mi>T</mi><mn>2</mn></msub></math> (with ’|’ is union and ’&amp;’ is intersection), <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>T</mi><mn>1</mn></msub><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>U</mi></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>T</mi><mn>2</mn></msub><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>U</mi></math> is true, and <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>T</mi><mn>1</mn></msub><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>I</mi></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>T</mi><mn>2</mn></msub><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>I</mi></math> is true. We should define that as well (if it is not already defined). Cf [<link linkend="King13a">King13a(p.3.2.4/5)</link>]</simpara></footnote></simpara>
<simpara>During validation, intersection types containing union or other intersection types may be inferred.
In this case, the composed types are flattened.
The aforementioned constraints must hold.
We also implicitly use this representation in this specification.</simpara>
<example>
<title>Subtyping with intersection type</title>
<simpara>Let A, B, and C be defined as in the chapter beginning (<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi><mo>&lt;</mo><mi>:</mi><mi>B</mi><mo>&lt;</mo><mi>:</mi><mi>A</mi></math>)</simpara>
<simpara>The following subtyping relations with intersection types are to be
evaluated as follows: <footnote><simpara>See Example <xref linkend="ex:class-hierarchy"/> for class definitions.</simpara></footnote></simpara>
<programlisting language="xtext" linenumbering="unnumbered">A &lt;: intersection{A} -&gt; true
A &lt;: intersection{A,A} -&gt; true
intersection{A,X} &lt;: A -&gt; true
intersection{X,A} &lt;: A -&gt; true
A &lt;: intersection{A,X} -&gt; false
intersection{A,X} &lt;: intersection{X,A} -&gt; true
H12 &lt;: intersection{I1,I2} -&gt; true
intersection{I1,I2} &lt;: H12 -&gt; false
H1 &lt;: intersection{I1,I2} -&gt; false
H23 &lt;: intersection{I1,I2} -&gt; false
B &lt;: intersection{A} -&gt; true
intersection{I1,I2} &lt;: I -&gt; true
H12 &lt;: intersection{I,I2} -&gt; true
A &lt;: intersection{A,Any} -&gt; true
intersection{A,Any} &lt;: A -&gt; true</programlisting>
</example>
</requirement>
<simpara>The join of intersection types is defined as the join of the elements.
That is:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><msub><mi>T</mi><mn>1</mn></msub><mo></mo><mi>S</mi><mo></mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo></mo><msub><mi>T</mi><mi>n</mi></msub><mo></mo><mi>S</mi></mrow><mrow><mi>i</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msub><mi>T</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub></mrow></mfenced><mo></mo><mi>S</mi></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><msub><mi>T</mi><mn>1</mn></msub><mo></mo><msub><mi>S</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mn>1</mn></msub><mo></mo><msub><mi>S</mi><mi>m</mi></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub><mo></mo><msub><mi>S</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub><mo></mo><msub><mi>S</mi><mi>m</mi></msub></mrow><mrow><mi>i</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msub><mi>T</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub></mrow></mfenced><mo></mo><mi>i</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msub><mi>S</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>S</mi><mi>m</mi></msub></mrow></mfenced></mrow></mfrac></math></simpara>
<simpara>The meet of intersection types is defined over their elements.
That is:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mi>i</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msub><mi>T</mi><mn>1</mn></msub><mo></mo><mi>S</mi><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub><mo></mo><mi>S</mi></mrow></mfenced></mrow><mrow><mi>i</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msub><mi>T</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub></mrow></mfenced><mo></mo><mi>S</mi></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mi>i</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msub><mi>T</mi><mn>1</mn></msub><mo></mo><msub><mi>S</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mn>1</mn></msub><mo></mo><msub><mi>S</mi><mi>m</mi></msub><mo>,</mo><mspace width="1em"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><mspace width="1em"/><msub><mi>T</mi><mi>n</mi></msub><mo></mo><msub><mi>S</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub><mo></mo><msub><mi>S</mi><mi>m</mi></msub></mrow></mfenced></mrow><mrow><mi>i</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msub><mi>T</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub></mrow></mfenced><mo></mo><mi>i</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msub><mi>S</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>S</mi><mi>m</mi></msub></mrow></mfenced></mrow></mfrac></math></simpara>
<simpara>The upper and lower bound of an intersection type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>I</mi></math> is a union type <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>I</mi><mi>'</mi></msup></math> containing the upper and lower bound of the elements of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>I</mi></math>:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mfenced close=")" open="("><mrow><mi>i</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msub><mi>T</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub></mrow></mfenced></mrow></mfenced><mi>:</mi><mo>=</mo><mi>i</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mfenced close=")" open="("><msub><mi>T</mi><mn>1</mn></msub></mfenced><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mfenced close=")" open="("><msub><mi>T</mi><mn>1</mn></msub></mfenced></mrow></mfenced></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>l</mi><mi>o</mi><mi>w</mi><mi>e</mi><mi>r</mi><mfenced close=")" open="("><mrow><mi>i</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msub><mi>T</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub></mrow></mfenced></mrow></mfenced><mi>:</mi><mo>=</mo><mi>i</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><mi>l</mi><mi>o</mi><mi>w</mi><mi>e</mi><mi>r</mi><mfenced close=")" open="("><msub><mi>T</mi><mn>1</mn></msub></mfenced><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><mi>l</mi><mi>o</mi><mi>w</mi><mi>e</mi><mi>r</mi><mfenced close=")" open="("><msub><mi>T</mi><mn>1</mn></msub></mfenced></mrow></mfenced></math></simpara>
</section>
<section xml:id="_warnings-2">
<title>Warnings</title>
<simpara>Using <literal>any</literal> types in intersection types is obsolete since they do not change the resulting intersection type.
E.g. the intersection type of A, B and <literal>any</literal> is equivialent to the intersection type of A and B.
However, using the <literal>any</literal> type is no error because it can be seen as a neutral argument to the intersection.
Nevertheless the use of the <literal>any</literal> type produces a warning, since its use can indicate a misunderstanding of the intersection type concept and since it always can be omitted.</simpara>
<requirement xml:id="IDE-32">
<title>Any type in intersection types</title>
<simpara>
<anchor xml:id="Req-IDE-32" xreflabel="[Req-IDE-32]"/>
<emphasis role="strong">Requirement: IDE-32:</emphasis>
<link linkend="Req-IDE-32">Any type in intersection types</link> (ver. 1)</simpara>
<simpara>
No intersection type shall contain an type:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>a</mi><mi>n</mi><mi>y</mi><mo></mo><mi>I</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>s</mi></math></simpara>
</requirement>
<simpara>The use of the <literal>any</literal> type in an intersection type is similar to the following case.
When two types are used, one of them a supertype of the other, then this supertype is obsolete.
Hence, a warning will be produced to indicate unecessary code.
The warning is only produced when both of the types are either classes or interfaces, since e.g. structural types are supertypes of any classes or interfaces.</simpara>
<requirement xml:id="IDE-33">
<title>Redundant supertypes in intersection types</title>
<simpara>
<anchor xml:id="Req-IDE-33" xreflabel="[Req-IDE-33]"/>
<emphasis role="strong">Requirement: IDE-33:</emphasis>
<link linkend="Req-IDE-33">Redundant supertypes in intersection types</link> (ver. 1)</simpara>
<simpara>
Intersection types shall not contain class or interface types which are a supertype of another class or interface type that also is contained in the intersection type.</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mo></mo><mi>T</mi><mo></mo><mi>I</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>s</mi><mi>:</mi><mo></mo><mi>T</mi><mi>T</mi><mo></mo><mi>I</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>s</mi><mi>:</mi><mtext>
</mtext><mrow><mo>(</mo><mrow><mi>T</mi><mi>T</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>T</mi><mo></mo><mi>i</mi><mi>s</mi><mi>C</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mi>O</mi><mi>r</mi><mi>I</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>f</mi><mi>a</mi><mi>c</mi><mi>e</mi><mfenced close=")" open="("><mi>T</mi></mfenced><mo></mo><mi>i</mi><mi>s</mi><mi>C</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mi>O</mi><mi>r</mi><mi>I</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>f</mi><mi>a</mi><mi>c</mi><mi>e</mi><mfenced close=")" open="("><mrow><mi>T</mi><mi>T</mi></mrow></mfenced><mo>)</mo></mrow></mrow></math>
</requirement>
</section>
</section>
<section xml:id="_composed-types-in-wildcards">
<title>Composed Types in Wildcards</title>
<simpara>Composed types may appear as the bound of a wildcard.
The following constraints apply: <footnote><simpara>see "Covariance and contravariance with unions and intersections" at <link xl:href="http://ceylon-lang.org/documentation/1.1/tour/generics/">http://ceylon-lang.org/documentation/1.1/tour/generics/</link></simpara></footnote></simpara>
<requirement xml:id="IDE-34">
<title>Composed Types as Bound of a Wildcard</title>
<simpara>
<anchor xml:id="Req-IDE-34" xreflabel="[Req-IDE-34]"/>
<emphasis role="strong">Requirement: IDE-34:</emphasis>
<link linkend="Req-IDE-34">Composed Types as Bound of a Wildcard</link> (ver. 1)</simpara>
<simpara>
A composed type may appear as the upper or lower bound of a wildcard.
In the covariant case, the following subtype relations apply:</simpara>
<programlisting language="ebnf" linenumbering="unnumbered">union{ G&lt;? extends A&gt;, G&lt;? extends B&gt; } \subtype G&lt;? extends union{A,B}&gt;
G&lt;? extends intersection{A,B}&gt; \subtype intersection{ G&lt;? extends A&gt;, G&lt;? extends B&gt; }</programlisting>
<simpara>In the contra variant case, the following subtype relations apply:</simpara>
<programlisting language="ebnf" linenumbering="unnumbered">union{ G&lt;? super A&gt;, G&lt;? super B&gt; } \subtype G&lt;? super intersection{A,B}&gt;
G&lt;? super union{A,B}&gt; \subtype intersection{ G&lt;? super A&gt;, G&lt;? super B&gt; }</programlisting>
</requirement>
</section>
<section xml:id="_property-access-for-composed-types">
<title>Property Access for Composed Types</title>
<simpara>It is possible to directly access properties of union and intersection types.
The following sections define which properties are accessible.</simpara>
<section xml:id="_properties-of-union-type">
<title>Properties of Union Type</title>
<simpara>As an (oversimplified) rule of thumb, the properties of a union type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>U</mi><mo>=</mo><msub><mi>T</mi><mn>1</mn></msub><mo>|</mo><msub><mi>T</mi><mn>2</mn></msub></math> are simply the intersection of the properties <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>U</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>i</mi><mi>e</mi><mi>s</mi><mo>=</mo><msub><mi>T</mi><mn>1</mn></msub><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>i</mi><mi>e</mi><mi>s</mi><mo></mo><msub><mi>T</mi><mn>2</mn></msub><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>i</mi><mi>e</mi><mi>s</mi></math>.
In other words, a property 'p' in the union type is the least common denominator of all 'p' in T_{1} and T_{2}.
It is not quite that simple, however, as the question of "equality" with regards to properties has to be answered.</simpara>
<simpara><?asciidoc-hr?></simpara>
<simpara>For a given union type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>U</mi><mo>=</mo><msub><mi>T</mi><mn>1</mn></msub><mo>|</mo><msub><mi>T</mi><mn>2</mn></msub></math>, the following constraints for its members must hold:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi> </mi><mi>a</mi><mo></mo><mi>U</mi><mo>.</mo><mi>a</mi><mi>t</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>b</mi><mi>u</mi><mi>t</mi><mi>e</mi><mi>s</mi><mi>:</mi></math></simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd/><mtd><mo></mo><mi> </mi><mi>k</mi><mo></mo><mfenced close="}" open="{"><mn>1</mn><mn>2</mn></mfenced><mi>:</mi><mo></mo><mi> </mi><msub><mi>a</mi><mi>k</mi></msub><mo></mo><msub><mi>T</mi><mi>k</mi></msub><mo>.</mo><mi>a</mi><mi>t</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>b</mi><mi>u</mi><mi>t</mi><mi>e</mi><mi>s</mi><mi>:</mi><msub><mi>a</mi><mi>k</mi></msub><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>&gt;</mo><mi>p</mi><mi>r</mi><mi>i</mi><mi>v</mi><mi>a</mi><mi>t</mi><mi>e</mi></mtd></mtr><mtr><mtd/><mtd><mo></mo><mi>a</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mi>m</mi><mi>i</mi><mi>n</mi><mfenced close=")" open="("><mrow><msub><mi>a</mi><mn>1</mn></msub><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi></mrow><mrow><msub><mi>a</mi><mn>2</mn></msub><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi></mrow></mfenced></mtd></mtr><mtr><mtd/><mtd><mo></mo><mi>a</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><msub><mi>a</mi><mn>1</mn></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><msub><mi>a</mi><mn>2</mn></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mtd></mtr><mtr><mtd/><mtd><mo></mo><mi>a</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mo>=</mo><msub><mi>a</mi><mn>1</mn></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mo>=</mo><msub><mi>a</mi><mn>2</mn></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi></mtd></mtr></mtable></math>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi> </mi><mi>m</mi><mo></mo><mi>U</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><mi>s</mi><mi>:</mi></math></simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi> </mi><msub><mi>m</mi><mn>1</mn></msub><mo></mo><msub><mi>T</mi><mn>1</mn></msub><mo>.</mo><mi>m</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><mi>s</mi><mo>,</mo><msub><mi>m</mi><mn>2</mn></msub><mo></mo><msub><mi>T</mi><mn>2</mn></msub><mo>.</mo><mi>m</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><mi>s</mi><mo>,</mo></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="2.0em"/><mstyle mathvariant="bold"><mi>w</mi><mi>i</mi><mi>t</mi><mi>h</mi></mstyle><mi>p</mi><mo>=</mo><mi>m</mi><mo>.</mo><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo></mo><msup><mi>p</mi><mi>'</mi></msup><mo>=</mo><msub><mi>m</mi><mn>1</mn></msub><mo>.</mo><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo></mo><mi>p</mi><mi>"</mi><mo>=</mo><msub><mi>m</mi><mn>2</mn></msub><mo>.</mo><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>,</mo><mstyle mathvariant="bold"><mi>W</mi><mi>L</mi><mi>O</mi><mi>G</mi></mstyle><mo>|</mo><msup><mi>p</mi><mi>'</mi></msup><mo>|</mo><mo></mo><mo>|</mo><mi>p</mi><mi>"</mi><mo>|</mo><mi>:</mi></math><?asciidoc-br?></simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd/><mtd><mspace width="1.2em"/><mo></mo><mi>k</mi><mo></mo><mfenced close="}" open="{"><mn>1</mn><mn>2</mn></mfenced><mi>:</mi><msub><mi>m</mi><mi>k</mi></msub><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>&gt;</mo><mi>p</mi><mi>r</mi><mi>i</mi><mi>v</mi><mi>a</mi><mi>t</mi><mi>e</mi></mtd></mtr><mtr><mtd/><mtd><mo></mo><mi>m</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mi>m</mi><mi>i</mi><mi>n</mi><mfenced close=")" open="("><mrow><msub><mi>m</mi><mn>1</mn></msub><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi></mrow><mrow><msub><mi>m</mi><mn>2</mn></msub><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi></mrow></mfenced></mtd></mtr><mtr><mtd/><mtd><mo></mo><mi>m</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><msub><mi>m</mi><mn>1</mn></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><msub><mi>m</mi><mn>2</mn></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mtd></mtr><mtr><mtd/><mtd><mo></mo><mi>m</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mo>=</mo><msub><mi>m</mi><mn>1</mn></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mo>|</mo><msub><mi>m</mi><mn>2</mn></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi></mtd></mtr><mtr><mtd/><mtd><mo></mo><mo></mo><mi> </mi><mi>i</mi><mo>&lt;</mo><mo>|</mo><mi>p</mi><mi>"</mi><mo>|</mo><mi>:</mi><msub><mi>p</mi><mi>i</mi></msub><mi> </mi><mstyle mathvariant="bold"><mi>e</mi><mi>x</mi><mi>i</mi><mi>s</mi><mi>t</mi><mi>s</mi></mstyle><mspace width="1.0mm"/><mstyle mathvariant="bold"><mi>w</mi><mi>i</mi><mi>t</mi><mi>h</mi></mstyle></mtd></mtr><mtr><mtd/><mtd><mspace width="2.0em"/><msub><mi>p</mi><mi>i</mi></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mfenced close="" open="{"><mtable><mtr><mtd><msub><mrow><mi>p</mi><mi>"</mi></mrow><mi>i</mi></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mtd><mtd><mi>i</mi><mo></mo><mo>|</mo><msup><mi>p</mi><mi>'</mi></msup><mo>|</mo><mo></mo><msub><msup><mi>p</mi><mi>'</mi></msup><mi>i</mi></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><msub><mrow><mi>p</mi><mi>"</mi></mrow><mi>i</mi></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mtd></mtr><mtr><mtd><msub><msup><mi>p</mi><mi>'</mi></msup><mi>i</mi></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>+</mo><mstyle mathvariant="bold"><mi>"</mi><mi>_</mi><mi>"</mi></mstyle><mo>+</mo><msub><mrow><mi>p</mi><mi>"</mi></mrow><mi>i</mi></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mtd><mtd><mstyle mathvariant="bold"><mi>e</mi><mi>l</mi><mi>s</mi><mi>e</mi></mstyle></mtd></mtr></mtable></mfenced></mtd></mtr><mtr><mtd/><mtd><mspace width="2.0em"/><msub><mi>p</mi><mi>i</mi></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mo>=</mo><mfenced close="" open="{"><mtable><mtr><mtd><msub><msup><mi>p</mi><mi>'</mi></msup><mi>i</mi></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>&amp;</mi><msub><mrow><mi>p</mi><mi>"</mi></mrow><mi>i</mi></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi></mtd><mtd><mi>i</mi><mo>&lt;</mo><mo>|</mo><msup><mi>p</mi><mi>'</mi></msup><mo>|</mo></mtd></mtr><mtr><mtd><msub><msup><mi>p</mi><mi>'</mi></msup><mrow><mo>|</mo><msup><mi>p</mi><mi>'</mi></msup><mo>|</mo><mo>-</mo><mn>1</mn></mrow></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>&amp;</mi><msub><mrow><mi>p</mi><mi>"</mi></mrow><mi>i</mi></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi></mtd><mtd><mi>i</mi><mo></mo><mo>|</mo><msup><mi>p</mi><mi>'</mi></msup><mo>|</mo><mo></mo><msub><msup><mi>p</mi><mi>'</mi></msup><mrow><mo>|</mo><msup><mi>p</mi><mi>'</mi></msup><mo>|</mo><mo>-</mo><mn>1</mn></mrow></msub><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi></mtd></mtr><mtr><mtd><msub><mrow><mi>p</mi><mi>"</mi></mrow><mi>i</mi></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi></mtd><mtd><mstyle mathvariant="bold"><mi>e</mi><mi>l</mi><mi>s</mi><mi>e</mi></mstyle></mtd></mtr></mtable></mfenced></mtd></mtr><mtr><mtd/><mtd><mspace width="2.0em"/><msub><mi>p</mi><mi>i</mi></msub><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi><mo>=</mo><mfenced close="" open="{"><mtable><mtr><mtd><mfenced close=")" open="("><mrow><msub><msup><mi>p</mi><mi>'</mi></msup><mi>i</mi></msub><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi><mo></mo><msub><mrow><mi>p</mi><mi>"</mi></mrow><mi>i</mi></msub><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi></mrow></mfenced></mtd><mtd><mi>i</mi><mo>&lt;</mo><mo>|</mo><msup><mi>p</mi><mi>'</mi></msup><mo>|</mo></mtd></mtr><mtr><mtd><msub><mrow><mi>p</mi><mi>"</mi></mrow><mi>i</mi></msub><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi></mtd><mtd><mstyle mathvariant="bold"><mi>e</mi><mi>l</mi><mi>s</mi><mi>e</mi></mstyle></mtd></mtr></mtable></mfenced></mtd></mtr><mtr><mtd/><mtd><mspace width="2.0em"/><msub><mi>p</mi><mi>i</mi></msub><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi><mo>=</mo><mfenced close="" open="{"><mtable><mtr><mtd><msub><msup><mi>p</mi><mi>'</mi></msup><mi>i</mi></msub><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi><mo></mo><msub><mrow><mi>p</mi><mi>"</mi></mrow><mi>i</mi></msub><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi></mtd><mtd><mi>i</mi><mo>&lt;</mo><mo>|</mo><msup><mi>p</mi><mi>'</mi></msup><mo>|</mo><mo></mo><mi>i</mi><mo>=</mo><mo>|</mo><mi>p</mi><mi>"</mi><mo>|</mo><mo>-</mo><mn>1</mn></mtd></mtr><mtr><mtd><msub><mrow><mi>p</mi><mi>"</mi></mrow><mi>i</mi></msub><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi></mtd><mtd><mi>i</mi><mo></mo><mo>|</mo><msup><mi>p</mi><mi>'</mi></msup><mo>|</mo><mo></mo><mi>i</mi><mo>=</mo><mo>|</mo><mi>p</mi><mi>"</mi><mo>|</mo><mo>-</mo><mn>1</mn></mtd></mtr><mtr><mtd><mi>f</mi><mi>a</mi><mi>l</mi><mi>s</mi><mi>e</mi></mtd><mtd><mstyle mathvariant="bold"><mi>e</mi><mi>l</mi><mi>s</mi><mi>e</mi></mstyle></mtd></mtr></mtable></mfenced></mtd></mtr></mtable></math>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mrow><mo>(</mo><mi>l</mi><mo>=</mo><mo>|</mo><msup><mi>p</mi><mi>'</mi></msup><mo>|</mo><mo>=</mo><mo>|</mo><mi>p</mi><mi>"</mi><mo>|</mo><mo></mo><mo>¬</mo><mfenced close=")" open="("><mrow><msub><msup><mi>p</mi><mi>'</mi></msup><mi>l</mi></msub><mo>-</mo><mn>1</mn><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi><mo></mo><msub><mrow><mi>p</mi><mi>"</mi></mrow><mrow><mi>l</mi><mo>-</mo><mn>1</mn></mrow></msub><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi></mrow></mfenced><mo></mo><mo></mo><mi>v</mi><mo></mo><mfenced close="}" open="{"><mrow><mi>p</mi><msub><mi>'</mi><mi>l</mi></msub><mo>-</mo><mn>1</mn></mrow><mrow><mi>p</mi><msub><mi>"</mi><mi>l</mi></msub><mo>-</mo><mn>1</mn></mrow></mfenced><mi>v</mi><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi><mi>:</mi><msub><mi>p</mi><mi>l</mi></msub><mi> </mi><mstyle mathvariant="bold"><mi>e</mi><mi>x</mi><mi>i</mi><mi>s</mi><mi>t</mi><mi>s</mi></mstyle><mspace width="1.0mm"/><mstyle mathvariant="bold"><mi>w</mi><mi>i</mi><mi>t</mi><mi>h</mi></mstyle></mrow></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="2.0em"/><msub><mi>p</mi><mi>l</mi></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>v</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="2.0em"/><msub><mi>p</mi><mi>i</mi></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mo>=</mo><mi>v</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="2.0em"/><msub><mi>p</mi><mi>i</mi></msub><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi><mo>=</mo><mi>t</mi><mi>r</mi><mi>u</mi><mi>e</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="2.0em"/><msub><mi>p</mi><mi>i</mi></msub><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi><mo>=</mo><mi>t</mi><mi>r</mi><mi>u</mi><mi>e</mi></math></simpara>
<simpara><?asciidoc-hr?></simpara>
<simpara>The following table shows how non-method members of union types are merged.
The resulting member type depends on whether the member is being accessed during a read (R) or write (W) operation.
The type of a field, of a getter or of the parameter of a setter is indicated in brackets.</simpara>
<table frame="all" rowsep="1" colsep="1">
<title>Merged Members of Unions</title>
<tgroup cols="5">
<colspec colname="col_1" colwidth="20*"/>
<colspec colname="col_2" colwidth="20*"/>
<colspec colname="col_3" colwidth="20*"/>
<colspec colname="col_4" colwidth="20*"/>
<colspec colname="col_5" colwidth="20*"/>
<thead>
<row>
<entry align="center" valign="top">Members</entry>
<entry align="center" valign="top" namest="col_2" nameend="col_3">S=T</entry>
<entry align="center" valign="top" namest="col_3" nameend="col_4">S≠T</entry>
</row>
</thead>
<tbody>
<row>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"><simpara><emphasis role="strong">R</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis role="strong">W</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis role="strong">R</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis role="strong">W</emphasis></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">field:S | field:T</emphasis></simpara></entry>
<entry align="center" valign="top" namest="col_2" nameend="col_3"><simpara>field:S</simpara></entry>
<entry align="center" valign="top"><simpara>getter:S|T</simpara></entry>
<entry align="center" valign="top"><simpara>setter:S&amp;T</simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">getter:S | getter:T</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara>getter:S</simpara></entry>
<entry align="center" valign="top"><simpara>-</simpara></entry>
<entry align="center" valign="top"><simpara>getter:S|T</simpara></entry>
<entry align="center" valign="top"><simpara>-</simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">setter:S | setter:T</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara>-</simpara></entry>
<entry align="center" valign="top"><simpara>setter:S</simpara></entry>
<entry align="center" valign="top"><simpara>-</simpara></entry>
<entry align="center" valign="top"><simpara>setter:S&amp;T</simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">field:S | getter:T</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara>getter:S</simpara></entry>
<entry align="center" valign="top"><simpara>-</simpara></entry>
<entry align="center" valign="top"><simpara>getter:S|T</simpara></entry>
<entry align="center" valign="top"><simpara>-</simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">field:S | setter:T</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara>-</simpara></entry>
<entry align="center" valign="top"><simpara>setter:S</simpara></entry>
<entry align="center" valign="top"><simpara>-</simpara></entry>
<entry align="center" valign="top"><simpara>setter:S&amp;T</simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">getter:S | setter:T</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara>-</simpara></entry>
<entry align="center" valign="top"><simpara>-</simpara></entry>
<entry align="center" valign="top"><simpara>-</simpara></entry>
<entry align="center" valign="top"><simpara>-</simpara></entry>
</row>
</tbody>
</tgroup>
</table>
<section xml:id="_remarks-on-union-type-s-members">
<title>Remarks on union type’s members:</title>
<itemizedlist>
<listitem>
<simpara>Fields of the same type are merged to a composed field with the same type.
Fields of different types are merged to a getter and setter.</simpara>
</listitem>
<listitem>
<simpara>The return type of a composed getter is the <emphasis>union</emphasis> type of the return types of the merged getters.</simpara>
</listitem>
<listitem>
<simpara>The type of a composed setter is the <emphasis>intersection</emphasis> type of the types of the merged setters.</simpara>
</listitem>
<listitem>
<simpara>Fields can be combined with getters and/or setters:</simpara>
<itemizedlist>
<listitem>
<simpara>fields combined with getters allow read-access.</simpara>
</listitem>
<listitem>
<simpara>non-const fields combined with setters allow write-access.</simpara>
</listitem>
<listitem>
<simpara>non-const fields combined with getters <emphasis>and</emphasis> setters, i.e. each type has either a non-const field or both a getter and a setter of the given name, allow both read- and write-access.</simpara>
<simpara>Again, types need not be identical; for read-access the <emphasis>union</emphasis> of the fields’ types and the getters’ return types is formed, for write-access the <emphasis>intersection</emphasis> of the fields’ types and the setters’ types is formed.
In the third case above, types are combined independently for read- and write-access if the getters and setters have different types.</simpara>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<simpara>The name of a method’s parameter is only used for error or warning messages and cannot be referenced otherwise.</simpara>
</listitem>
<listitem>
<simpara>The return type of a composed method is the <emphasis>union</emphasis> type of the return types of the merged methods.</simpara>
</listitem>
<listitem>
<simpara>A composed method parameter’s type is the <emphasis>intersection</emphasis> type of the merged parameters types.</simpara>
</listitem>
</itemizedlist>
</section>
</section>
<section xml:id="_properties-of-intersection-type">
<title>Properties of Intersection Type</title>
<simpara>As an (oversimplified) rule of thumb, the properties of an intersection type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>I</mi><mo>=</mo><msub><mi>T</mi><mn>1</mn></msub><mi>&amp;</mi><msub><mi>T</mi><mn>2</mn></msub></math> are the union of properties <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>I</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>i</mi><mi>e</mi><mi>s</mi><mo>=</mo><msub><mi>T</mi><mn>1</mn></msub><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>i</mi><mi>e</mi><mi>s</mi><mo></mo><msub><mi>T</mi><mn>2</mn></msub><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>i</mi><mi>e</mi><mi>s</mi></math>.
In other words, a property 'p' in the union type is the greates common denominator of all 'p' in T_{1} and T_{2}.
It is not quite that simple, however, as the question of "equality” with regards to properties has to be answered.</simpara>
<requirement xml:id="IDE-36">
<title>Members of an Intersection Type</title>
<simpara>
<anchor xml:id="Req-IDE-36" xreflabel="[Req-IDE-36]"/>
<emphasis role="strong">Requirement: IDE-36:</emphasis>
<link linkend="Req-IDE-36">Members of an Intersection Type</link> (ver. 1)</simpara>
<simpara>
For a given intersection type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>I</mi><mo>=</mo><msub><mi>T</mi><mn>1</mn></msub><mi>&amp;</mi><msub><mi>T</mi><mn>2</mn></msub></math>, the following constraints for its members must hold:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>a</mi><mo></mo><mi>I</mi><mo>.</mo><mi>a</mi><mi>t</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>b</mi><mi>u</mi><mi>t</mi><mi>e</mi><mi>s</mi><mi>:</mi></math></simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd/><mtd><mrow><mo>(</mo><mrow><mo></mo><msub><mi>a</mi><mn>1</mn></msub><mo></mo><msub><mi>T</mi><mn>1</mn></msub><mo>.</mo><mi>a</mi><mi>t</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>b</mi><mi>u</mi><mi>t</mi><mi>e</mi><mi>s</mi><mo>,</mo><msub><mi>a</mi><mn>1</mn></msub><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>&gt;</mo><mi>p</mi><mi>r</mi><mi>i</mi><mi>v</mi><mi>a</mi><mi>t</mi><mi>e</mi><mo>)</mo></mrow><mo></mo><mrow><mo>(</mo><mrow><mo></mo><msub><mi>a</mi><mn>2</mn></msub><mo></mo><msub><mi>T</mi><mn>2</mn></msub><mo>.</mo><mi>a</mi><mi>t</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>b</mi><mi>u</mi><mi>t</mi><mi>e</mi><mi>s</mi><mo>,</mo><msub><mi>a</mi><mn>2</mn></msub><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>&gt;</mo><mi>p</mi><mi>r</mi><mi>i</mi><mi>v</mi><mi>a</mi><mi>t</mi><mi>e</mi><mo>)</mo></mrow></mrow></mrow></mtd></mtr><mtr><mtd/><mtd><mo></mo><mi>a</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mfenced close="" open="{"><mtable><mtr><mtd><msub><mi>a</mi><mn>1</mn></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mtd><mtd><msub><mi>a</mi><mn>1</mn></msub><mo></mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi><mo></mo><mfenced close=")" open="("><mrow><msub><mi>a</mi><mn>2</mn></msub><mo>=</mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi><mo></mo><msub><mi>a</mi><mn>2</mn></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><msub><mi>a</mi><mn>1</mn></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mrow></mfenced></mtd></mtr><mtr><mtd><msub><mi>a</mi><mn>2</mn></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mtd><mtd><mstyle mathvariant="bold"><mi>e</mi><mi>l</mi><mi>s</mi><mi>e</mi></mstyle></mtd></mtr></mtable></mfenced></mtd></mtr><mtr><mtd/><mtd><mo></mo><mi>a</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mfenced close="" open="{"><mtable><mtr><mtd><msub><mi>a</mi><mn>1</mn></msub><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi></mtd><mtd><msub><mi>a</mi><mn>1</mn></msub><mo></mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi><mo></mo><mfenced close=")" open="("><mrow><msub><mi>a</mi><mn>2</mn></msub><mo>=</mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi><mo></mo><msub><mi>a</mi><mn>2</mn></msub><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo></mo><msub><mi>a</mi><mn>1</mn></msub><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi></mrow></mfenced></mtd></mtr><mtr><mtd><msub><mi>a</mi><mn>2</mn></msub><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi></mtd><mtd><mstyle mathvariant="bold"><mi>e</mi><mi>l</mi><mi>s</mi><mi>e</mi></mstyle></mtd></mtr></mtable></mfenced></mtd></mtr><mtr><mtd/><mtd><mo></mo><mi>a</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mo>=</mo><mfenced close="" open="{"><mtable><mtr><mtd><msub><mi>a</mi><mn>1</mn></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>&amp;</mi><msub><mi>a</mi><mn>2</mn></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi></mtd><mtd><msub><mi>a</mi><mn>1</mn></msub><mo></mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi><mo></mo><msub><mi>a</mi><mn>2</mn></msub><mo></mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi></mtd></mtr><mtr><mtd><msub><mi>a</mi><mn>1</mn></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi></mtd><mtd><msub><mi>a</mi><mn>1</mn></msub><mo></mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi></mtd></mtr><mtr><mtd><msub><mi>a</mi><mn>2</mn></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi></mtd><mtd><mstyle mathvariant="bold"><mi>e</mi><mi>l</mi><mi>s</mi><mi>e</mi></mstyle><mfenced close=")" open="("><mrow><msub><mi>a</mi><mn>2</mn></msub><mo></mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi></mrow></mfenced></mtd></mtr></mtable></mfenced></mtd></mtr></mtable></math>
<simpara><?asciidoc-pagebreak?></simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>m</mi><mo></mo><mi>I</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><mi>s</mi></math>:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>(</mo><mrow><mo></mo><msub><mi>m</mi><mn>1</mn></msub><mo></mo><msub><mi>T</mi><mn>1</mn></msub><mo>.</mo><mi>m</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><mi>s</mi><mo>,</mo><msub><mi>m</mi><mn>1</mn></msub><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>&gt;</mo><mi>p</mi><mi>r</mi><mi>i</mi><mi>v</mi><mi>a</mi><mi>t</mi><mi>e</mi><mo>)</mo></mrow><mo></mo><mrow><mo>(</mo><mrow><mo></mo><msub><mi>m</mi><mn>2</mn></msub><mo></mo><msub><mi>T</mi><mn>1</mn></msub><mo>.</mo><mi>m</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><mi>s</mi><mo>,</mo><msub><mi>m</mi><mn>2</mn></msub><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>&gt;</mo><mi>p</mi><mi>r</mi><mi>i</mi><mi>v</mi><mi>a</mi><mi>t</mi><mi>e</mi><mo>)</mo></mrow><mi>:</mi></mrow></mrow></math></simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd/><mtd><mspace width="2.0em"/><mstyle mathvariant="bold"><mi>w</mi><mi>i</mi><mi>t</mi><mi>h</mi></mstyle><mi>p</mi><mo>=</mo><mi>m</mi><mo>.</mo><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><mi>s</mi></mtd></mtr><mtr><mtd/><mtd><mspace width="3.0em"/><mo></mo><mstyle mathvariant="bold"><mi>i</mi><mi>f</mi></mstyle><mi> </mi><msub><mi>m</mi><mn>1</mn></msub><mi> </mi><mstyle mathvariant="bold"><mi>e</mi><mi>x</mi><mi>i</mi><mi>s</mi><mi>t</mi><mi>s</mi></mstyle><mi> </mi><msup><mi>p</mi><mi>'</mi></msup><mo>=</mo><msub><mi>m</mi><mn>1</mn></msub><mo>.</mo><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><mi>s</mi><mi> </mi><mstyle mathvariant="bold"><mfenced close=")" open="("><mrow><mi>e</mi><mi>l</mi><mi>s</mi><mi>e</mi><msup><mi>p</mi><mi>'</mi></msup><mo>=</mo><mi></mi></mrow></mfenced></mstyle><mo>,</mo></mtd></mtr><mtr><mtd/><mtd><mspace width="3.0em"/><mo></mo><mstyle mathvariant="bold"><mi>i</mi><mi>f</mi></mstyle><mi> </mi><msub><mi>m</mi><mn>2</mn></msub><mi> </mi><mstyle mathvariant="bold"><mi>e</mi><mi>x</mi><mi>i</mi><mi>s</mi><mi>t</mi><mi>s</mi></mstyle><mi> </mi><mi>p</mi><mi>"</mi><mo>=</mo><msub><mi>m</mi><mn>2</mn></msub><mo>.</mo><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><mi>s</mi><mi> </mi><mstyle mathvariant="bold"><mfenced close=")" open="("><mrow><mi>e</mi><mi>l</mi><mi>s</mi><mi>e</mi><mi>p</mi><mi>"</mi><mo>=</mo><mi></mi></mrow></mfenced></mstyle><mo>,</mo><mstyle mathvariant="bold"><mstyle mathvariant="bold"><mi>W</mi><mi>L</mi><mi>O</mi><mi>G</mi></mstyle></mstyle><mo>|</mo><msup><mi>p</mi><mi>'</mi></msup><mo>|</mo><mo></mo><mo>|</mo><mi>p</mi><mi>"</mi><mo>|</mo><mi>:</mi></mtd></mtr><mtr><mtd/><mtd><mspace width="1.0em"/><mi>m</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mfenced close="" open="{"><mtable><mtr><mtd><msub><mi>m</mi><mn>1</mn></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mtd><mtd><msub><mi>m</mi><mn>1</mn></msub><mo></mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi><mo></mo><mfenced close=")" open="("><mrow><msub><mi>m</mi><mn>2</mn></msub><mo>=</mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi><mo></mo><msub><mi>m</mi><mn>2</mn></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><msub><mi>m</mi><mn>1</mn></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mrow></mfenced></mtd></mtr><mtr><mtd><msub><mi>m</mi><mn>2</mn></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mtd><mtd><mstyle mathvariant="bold"><mi>e</mi><mi>l</mi><mi>s</mi><mi>e</mi></mstyle></mtd></mtr></mtable></mfenced></mtd></mtr><mtr><mtd/><mtd><mo></mo><mi>m</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mfenced close="" open="{"><mtable><mtr><mtd><msub><mi>m</mi><mn>1</mn></msub><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi></mtd><mtd><msub><mi>m</mi><mn>1</mn></msub><mo></mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi><mo></mo><mfenced close=")" open="("><mrow><msub><mi>m</mi><mn>2</mn></msub><mo>=</mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi><mo></mo><msub><mi>m</mi><mn>2</mn></msub><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo></mo><msub><mi>m</mi><mn>1</mn></msub><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi></mrow></mfenced></mtd></mtr><mtr><mtd><msub><mi>m</mi><mn>2</mn></msub><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi></mtd><mtd><mstyle mathvariant="bold"><mi>e</mi><mi>l</mi><mi>s</mi><mi>e</mi></mstyle></mtd></mtr></mtable></mfenced></mtd></mtr><mtr><mtd/><mtd><mo></mo><mi>m</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mo>=</mo><mfenced close="" open="{"><mtable><mtr><mtd><msub><mi>m</mi><mn>1</mn></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>&amp;</mi><msub><mi>m</mi><mn>2</mn></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi></mtd><mtd><msub><mi>m</mi><mn>1</mn></msub><mo></mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi><mo></mo><msub><mi>m</mi><mn>2</mn></msub><mo></mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi></mtd></mtr><mtr><mtd><msub><mi>m</mi><mn>1</mn></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi></mtd><mtd><msub><mi>m</mi><mn>1</mn></msub><mo></mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi></mtd></mtr><mtr><mtd><msub><mi>m</mi><mn>2</mn></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi></mtd><mtd><mstyle mathvariant="bold"><mi>e</mi><mi>l</mi><mi>s</mi><mi>e</mi></mstyle><mfenced close=")" open="("><mrow><msub><mi>m</mi><mn>2</mn></msub><mo></mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi></mrow></mfenced></mtd></mtr></mtable></mfenced></mtd></mtr><mtr><mtd/><mtd><mo></mo><mo></mo><mi> </mi><mi>i</mi><mo>&lt;</mo><mo>|</mo><mi>p</mi><mi>"</mi><mo>|</mo><mi>:</mi><msub><mi>p</mi><mi>i</mi></msub><mi> </mi><mstyle mathvariant="bold"><mi>e</mi><mi>x</mi><mi>i</mi><mi>s</mi><mi>t</mi><mi>s</mi><mi>w</mi><mi>i</mi><mi>t</mi><mi>h</mi></mstyle></mtd></mtr><mtr><mtd/><mtd><mspace width="2.0em"/><msub><mi>p</mi><mi>i</mi></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mfenced close="" open="{"><mtable><mtr><mtd><msub><mrow><mi>p</mi><mi>"</mi></mrow><mi>i</mi></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mtd><mtd><mi>i</mi><mo></mo><mo>|</mo><msup><mi>p</mi><mi>'</mi></msup><mo>|</mo><mo></mo><msub><mrow><mi>p</mi><mi>"</mi></mrow><mi>i</mi></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><msub><msup><mi>p</mi><mi>'</mi></msup><mi>i</mi></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mtd></mtr><mtr><mtd><msub><msup><mi>p</mi><mi>'</mi></msup><mi>i</mi></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>+</mo><mstyle mathvariant="bold"><mi>"</mi><mi>_</mi><mi>"</mi></mstyle><mo>+</mo><msub><mrow><mi>p</mi><mi>"</mi></mrow><mi>i</mi></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mtd><mtd><mstyle mathvariant="bold"><mi>e</mi><mi>l</mi><mi>s</mi><mi>e</mi></mstyle></mtd></mtr></mtable></mfenced></mtd></mtr><mtr><mtd/><mtd><mspace width="2.0em"/><msub><mi>p</mi><mi>i</mi></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mo>=</mo><mfenced close="" open="{"><mtable><mtr><mtd><msub><msup><mi>p</mi><mi>'</mi></msup><mi>i</mi></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mo>|</mo><msub><mrow><mi>p</mi><mi>"</mi></mrow><mi>i</mi></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi></mtd><mtd><mi>i</mi><mo>&lt;</mo><mo>|</mo><msup><mi>p</mi><mi>'</mi></msup><mo>|</mo></mtd></mtr><mtr><mtd><msub><msup><mi>p</mi><mi>'</mi></msup><mrow><mo>|</mo><msup><mi>p</mi><mi>'</mi></msup><mo>|</mo><mo>-</mo><mn>1</mn></mrow></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mo>|</mo><msub><mrow><mi>p</mi><mi>"</mi></mrow><mi>i</mi></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi></mtd><mtd><mi>i</mi><mo></mo><mo>|</mo><msup><mi>p</mi><mi>'</mi></msup><mo>|</mo><mo></mo><msub><msup><mi>p</mi><mi>'</mi></msup><mrow><mo>|</mo><msup><mi>p</mi><mi>'</mi></msup><mo>|</mo><mo>-</mo><mn>1</mn></mrow></msub><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi></mtd></mtr><mtr><mtd><msub><mrow><mi>p</mi><mi>"</mi></mrow><mi>i</mi></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi></mtd><mtd><mstyle mathvariant="bold"><mi>e</mi><mi>l</mi><mi>s</mi><mi>e</mi></mstyle></mtd></mtr></mtable></mfenced></mtd></mtr><mtr><mtd/><mtd><mspace width="2.0em"/><msub><mi>p</mi><mi>i</mi></msub><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi><mo>=</mo><mfenced close=")" open="("><mrow><mo></mo><mi>k</mi><mo></mo><mi>m</mi><mi>i</mi><mi>n</mi><mfenced close=")" open="("><mrow><mo>|</mo><msup><mi>p</mi><mi>'</mi></msup><mo>|</mo><mo>-</mo><mn>1</mn></mrow><mi>i</mi></mfenced><mi>:</mi><mi>p</mi><msub><mi>'</mi><mi>k</mi></msub><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi></mrow></mfenced><mo></mo><mfenced close=")" open="("><mrow><mo></mo><mi>k</mi><mo></mo><mi>i</mi><mi>:</mi><mi>p</mi><msub><mi>"</mi><mi>k</mi></msub><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi></mrow></mfenced></mtd></mtr><mtr><mtd/><mtd><mspace width="2.0em"/><msub><mi>p</mi><mi>i</mi></msub><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi><mo>=</mo><mfenced close="" open="{"><mtable><mtr><mtd><msub><mi>p</mi><mi>i</mi></msub><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi><mo></mo><mfenced close=")" open="("><mrow><msub><msup><mi>p</mi><mi>'</mi></msup><mi>i</mi></msub><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi><mo></mo><msub><mrow><mi>p</mi><mi>"</mi></mrow><mi>i</mi></msub><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi></mrow></mfenced></mtd><mtd><mi>i</mi><mo>&lt;</mo><mo>|</mo><msup><mi>p</mi><mi>'</mi></msup><mo>|</mo><mo></mo><mi>i</mi><mo>=</mo><mo>|</mo><mi>p</mi><mi>"</mi><mo>|</mo><mo>-</mo><mn>1</mn></mtd></mtr><mtr><mtd><msub><mrow><mi>p</mi><mi>"</mi></mrow><mi>i</mi></msub><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi></mtd><mtd><mi>i</mi><mo></mo><mo>|</mo><msup><mi>p</mi><mi>'</mi></msup><mo>|</mo><mo></mo><mi>i</mi><mo>=</mo><mo>|</mo><mi>p</mi><mi>"</mi><mo>|</mo><mo>-</mo><mn>1</mn></mtd></mtr><mtr><mtd><mi>f</mi><mi>a</mi><mi>l</mi><mi>s</mi><mi>e</mi></mtd><mtd><mstyle mathvariant="bold"><mi>e</mi><mi>l</mi><mi>s</mi><mi>e</mi></mstyle></mtd></mtr></mtable></mfenced></mtd></mtr></mtable></math>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mrow><mo>(</mo><mi>l</mi><mo>=</mo><mo>|</mo><msup><mi>p</mi><mi>'</mi></msup><mo>|</mo><mo>=</mo><mo>|</mo><mi>p</mi><mi>"</mi><mo>|</mo><mo></mo><mi>l</mi><mo>&gt;</mo><mn>0</mn><mo></mo><mo>¬</mo><mfenced close=")" open="("><mrow><msub><mi>p</mi><mrow><mi>l</mi><mo>-</mo><mn>1</mn></mrow></msub><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi></mrow></mfenced><mo></mo><mo></mo><mi>v</mi><mo></mo><mfenced close="}" open="{"><mrow><mi>p</mi><msub><mi>'</mi><mrow><mi>l</mi><mo>-</mo><mn>1</mn></mrow></msub></mrow><mrow><mi>p</mi><msub><mi>"</mi><mrow><mi>l</mi><mo>-</mo><mn>1</mn></mrow></msub></mrow></mfenced><mi>v</mi><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi><mi>:</mi><msub><mi>p</mi><mi>l</mi></msub><mi> </mi><mstyle mathvariant="bold"><mi>e</mi><mi>x</mi><mi>i</mi><mi>s</mi><mi>t</mi><mi>s</mi></mstyle><mspace width="1.0mm"/><mstyle mathvariant="bold"><mi>w</mi><mi>i</mi><mi>t</mi><mi>h</mi></mstyle></mrow></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="2.0em"/><msub><mi>p</mi><mi>l</mi></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>v</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="2.0em"/><msub><mi>p</mi><mi>i</mi></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mo>=</mo><mi>v</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="2.0em"/><msub><mi>p</mi><mi>i</mi></msub><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi><mo>=</mo><mi>t</mi><mi>r</mi><mi>u</mi><mi>e</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="2.0em"/><msub><mi>p</mi><mi>i</mi></msub><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi><mo>=</mo><mi>t</mi><mi>r</mi><mi>u</mi><mi>e</mi></math></simpara>
</requirement>
<simpara>The following table shows how non-method members of intersection types are merged.
The resulting member type depends on whether the member is being accessed during a read (R) or write (W) operation.
The type of a field, of a getter or of the parameter of a setter is indicated in brackets.</simpara>
<table frame="all" rowsep="1" colsep="1">
<title>Merged Members of Intersections</title>
<tgroup cols="5">
<colspec colname="col_1" colwidth="20*"/>
<colspec colname="col_2" colwidth="20*"/>
<colspec colname="col_3" colwidth="20*"/>
<colspec colname="col_4" colwidth="20*"/>
<colspec colname="col_5" colwidth="20*"/>
<thead>
<row>
<entry align="center" valign="top">Members</entry>
<entry align="center" valign="top" namest="col_2" nameend="col_3">S=T</entry>
<entry align="center" valign="top" namest="col_3" nameend="col_4">S≠T</entry>
</row>
</thead>
<tbody>
<row>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"><simpara><emphasis role="strong">R</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis role="strong">W</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis role="strong">R</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis role="strong">W</emphasis></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">field:S &amp; field:T</emphasis></simpara></entry>
<entry align="center" valign="top" namest="col_2" nameend="col_3"><simpara>field:S</simpara></entry>
<entry align="center" valign="top"><simpara>getter:S&amp;T</simpara></entry>
<entry align="center" valign="top"><simpara>setter:S|T</simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">getter:S &amp; getter:T</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara>getter:S</simpara></entry>
<entry align="center" valign="top"><simpara>-</simpara></entry>
<entry align="center" valign="top"><simpara>getter:S&amp;T</simpara></entry>
<entry align="center" valign="top"><simpara>-</simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">setter:S &amp; setter:T</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara>-</simpara></entry>
<entry align="center" valign="top"><simpara>setter:S</simpara></entry>
<entry align="center" valign="top"><simpara>-</simpara></entry>
<entry align="center" valign="top"><simpara>setter:S|T</simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">field:S &amp; getter:T</emphasis></simpara></entry>
<entry align="center" valign="top" namest="col_2" nameend="col_3"><simpara>field:S</simpara></entry>
<entry align="center" valign="top"><simpara>getter:S&amp;T</simpara></entry>
<entry align="center" valign="top"><simpara>setter:S</simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">field:S &amp; setter:T</emphasis></simpara></entry>
<entry align="center" valign="top" namest="col_2" nameend="col_3"><simpara>field:S</simpara></entry>
<entry align="center" valign="top"><simpara>getter:S</simpara></entry>
<entry align="center" valign="top"><simpara>setter:S|T</simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">getter:S &amp; setter:T</emphasis></simpara></entry>
<entry align="center" valign="top" namest="col_2" nameend="col_3"><simpara>field:S</simpara></entry>
<entry align="center" valign="top"><simpara>getter:S</simpara></entry>
<entry align="center" valign="top"><simpara>setter:T</simpara></entry>
</row>
</tbody>
</tgroup>
</table>
<section xml:id="_remarks-on-intersection-type-s-methods">
<title>Remarks on intersection type’s methods:</title>
<itemizedlist>
<listitem>
<simpara>The name of a method’s parameter is only used for error or warning messages and cannot be referenced otherwise.</simpara>
</listitem>
<listitem>
<simpara>The return type of a method is the <emphasis>intersection</emphasis> type of the return types of the merged methods.</simpara>
</listitem>
<listitem>
<simpara>A method parameter’s type is the <emphasis>union</emphasis> type of the merged parameters types.</simpara>
</listitem>
</itemizedlist>
</section>
</section>
</section>
</section>
<section xml:id="_constructor-and-classifier-type" role="language-n4js">
<title>Constructor and Classifier Type</title>
<simpara>A class definition as described in <xref linkend="_classes"/> declares types.
Often, it is necessary to access these types directly, for example to access staticmembers or for dynamic construction of instances.
These two use cases are actually slightly different and N4JS provides two different types, one for each use case: constructor and classifier type.<footnote><simpara>The classifier type is, in fact, the <literal>type type</literal> or <literal>metatype</literal> of a type. We use the term classifier type in the specification to avoid the bogus <literal>type type</literal> terminology.</simpara></footnote>
The constructor is basically the classifier type with the additional possibility to call it via <literal>new</literal> in order to create new instances of the declared type.</simpara>
<simpara>Both <literal>meta</literal> types are different from Java’s type <literal>Class&lt;T&gt;</literal>, as the latter has a defined set of members, while the N4JS metatypes will have members according to a class definition.
The concept of constructors as metatypes is similar to ECMAScript 2015 [<link linkend="ECMA15a">ECMA15a(p.14.5)</link>].</simpara>
<section xml:id="_syntax-3">
<title>Syntax</title>
<programlisting language="n4js" linenumbering="unnumbered">ConstructorTypeRef returns ConstructorTypeRef: 'constructor' '{' typeArg = [TypeArgument] '}';
ClassifierTypeRef returns ClassifierTypeRef: 'type' '{' typeArg = [TypeRef] '}';</programlisting>
</section>
<section xml:id="_semantics-2">
<title>Semantics</title>
<orderedlist numeration="arabic">
<listitem>
<simpara>Static members of a type <emphasis>T</emphasis> are actually members of the classifier type <literal>type{T}</literal>.</simpara>
</listitem>
<listitem>
<simpara>The keyword <literal>this</literal> in a static method of a type <emphasis>T</emphasis> actually binds to the classifier type <literal>type{T}</literal>.</simpara>
</listitem>
<listitem>
<simpara>The constructor type <literal>constructor</literal><emphasis>{T}</emphasis> is a subtype of the classifier type <literal>type{T}</literal>:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mo></mo><mi>T</mi><mi>:</mi><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mfenced close="}" open="{"><mi>T</mi></mfenced><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mfenced close="}" open="{"><mi>T</mi></mfenced></math>
</listitem>
<listitem>
<simpara>If a class <emphasis>B</emphasis> is a subtype (subclass) of a class <emphasis>A</emphasis>, then the classifier type <literal>type{B}</literal> also is a subtype of <literal>type{A}</literal>:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>B</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>A</mi></mrow><mrow><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mfenced close="}" open="{"><mi>B</mi></mfenced><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mfenced close="}" open="{"><mi>A</mi></mfenced></mrow></mfrac></math>
</listitem>
<listitem>
<simpara>If a class <emphasis>B</emphasis> is a subtype (subclass) of a class <emphasis>A</emphasis>, and if the constructor function of <emphasis>B</emphasis> is a subtype of the constructor function of <emphasis>A</emphasis>, then the
classifier type <literal>constructor{B}</literal> also is a subtype of <literal>constructor{A}</literal> :</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>B</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>A</mi><mspace width="3.0mm"/><mi>B</mi><mo>.</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>A</mi><mo>.</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></mrow><mrow><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mfenced close="}" open="{"><mi>B</mi></mfenced><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mfenced close="}" open="{"><mi>A</mi></mfenced></mrow></mfrac></math>
<simpara>The subtype relation of the constructor function is defined in <xref linkend="_function-type"/>.
In the case of the default <literal>N4Object</literal> constructor, the type of the object literal argument depends on required attributes.</simpara>
<simpara>This subtype relation for the constructor type is enforced if the constructor of the super class is marked as <literal>final</literal>, see <xref linkend="_constructor-and-classifier-type"/> for details.</simpara>
</listitem>
<listitem>
<simpara>The type of a classifier declaration or classifier expression is the constructor of that class:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>μ</mi><mfenced close=")" open="("><mi>C</mi></mfenced><mo></mo><mfenced close="}" open="{"><mrow><mi>c</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>f</mi><mi>i</mi><mi>e</mi><mi>r</mi><mi>D</mi><mi>e</mi><mi>f</mi><mi>i</mi><mi>n</mi><mi>i</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi></mrow></mfenced></mrow><mrow><mi>Γ</mi><mo></mo><mi>C</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>constructor[C]</mtext></mstyle></mrow></mfrac></math>
</listitem>
<listitem>
<simpara>A class cannot be called as a function in ECMAScript.
Thus, the constructor and type type are only subtype of <literal>Object</literal>:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>T</mi><mi>:</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>constructor</mtext></mstyle><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></mfenced><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>Object</mtext></mstyle></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>type</mtext></mstyle><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></mfenced><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>Object</mtext></mstyle></math></simpara>
</listitem>
<listitem>
<simpara>If the type argument of the constructor is not a declared type (i.e., a wildcard or a type variable with bounds), the constructor cannot be used in a new expression.
Thus, the constructor function signature becomes irrelevant for subtype checking.
In that case, the following rules apply:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mfrac><mrow><mi>S</mi><mo>.</mo><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>T</mi><mo>.</mo><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mspace width="3.0mm"/><mi>T</mi><mo>.</mo><mi>l</mi><mi>o</mi><mi>w</mi><mi>e</mi><mi>r</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>S</mi><mo>.</mo><mi>l</mi><mi>o</mi><mi>w</mi><mi>e</mi><mi>r</mi><mspace width="3.0mm"/><mi>μ</mi><mfenced close=")" open="("><mi>T</mi></mfenced><mo></mo><mstyle mathvariant="monospace"><mtext>DeclaredTypeWithAccessModifier</mtext></mstyle></mrow><mrow><mstyle mathvariant="monospace"><mtext>constructor</mtext><mtext>S</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>constructor</mtext><mtext>T</mtext></mstyle></mrow></mfrac></mtd></mtr></mtable></math>
<simpara>Note that this is only true for the right hand side of the subtyping rule.
A constructor type with a wildcard is never a subtype of a constructor type without a wildcard.</simpara>
</listitem>
</orderedlist>
<simpara>The figure <xref linkend="cdConstructorClassifierType"/> shows the subtype relations defined by the preceding rules.</simpara>
<figure xml:id="cdConstructorClassifierType">
<title>Classifier and Constructor Type Subtype Relations</title>
<mediaobject>
<imageobject>
<imagedata fileref="chapters/04_types/fig/cdConstructorClassifierType.svg" width="60%" scalefit="1" align="center"/>
</imageobject>
<textobject><phrase>cdConstructorClassifierType</phrase></textobject>
</mediaobject>
</figure>
<simpara>Consequences:</simpara>
<itemizedlist>
<listitem>
<simpara>Overriding of static methods is possible and by using the constructor or classifier type, polymorphism for static methods is possible as well.</simpara>
<example xml:id="_polymorphism-and-static-methods">
<title>Static Polymorphism</title>
<programlisting language="n4js" linenumbering="unnumbered">class A {
static foo(): string { return "A"; }
static bar(): string { return this.foo(); }
}
class B extends A {
@Override
static foo(): string { return "B"; }
}
A.bar(); // will return "A"
B.bar(); // will return "B", as foo() is called polymorphical</programlisting>
</example>
</listitem>
<listitem>
<simpara>It is even possible to refer to the constructor of an abstract class.
The abstract class itself cannot provide this constructor (it only provides a type..), that is to say only concrete subclasses can provide constructors compatible to the constructor.</simpara>
<example>
<title>Constructor of Abstract Class</title>
<programlisting language="n4js" linenumbering="unnumbered">abstract class A {}
class B extends A {}
function f(ctor: constructor{A}): A { return new ctor(); }
f(A); // not working: type{A} is not a subtype of constructor{A}.
f(B); // ok</programlisting>
</example>
</listitem>
</itemizedlist>
<simpara>Allowing wildcards on constructor type references has pragmatic reasons.
The usage of constructor references usually indicates very dynamic scenarios.
In some of these scenarios, e.g., in case of dynamic creation of objects in the context of generic testing or injectors, arbitrary constructors may be used.
Of course, it won’t be possible to check the correct new expression call in these cases – and using new expressions is prevented by N4JS if the constructor reference contains a wildcard.
But other constraints, implemented by the client logic, may guarantee correct instantiation via more dynamic constructors, for example via the ECMAScript 2015 reflection API.
In order to simplify these scenarios and preventing the use of <literal>any</literal>, wildcards are supported in constructors. Since a constructor with a wildcard cannot be used
in a new expression anyway, using a classifier type is usually better than using a constructor type with wildcard.</simpara>
<simpara>Using wildcards on classifier types would have the same meaning as using the upper bound directly.
That is, a type reference <literal>type{? extends C}</literal> can simply be replaced with <literal>type{c}</literal>, and <literal>type{?}</literal> with <literal>type{any}</literal>.</simpara>
<simpara>To conclude this chapter, let us compare the different types introduced above depending on whether they are used with wildcards or not:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>having a value of type <literal>constructor{C}</literal>, we know we have</simpara>
<itemizedlist>
<listitem>
<simpara>a constructor function of <literal>{C}</literal> or a subclass of <literal>{C}</literal>,</simpara>
</listitem>
<listitem>
<simpara>that can be used for instantiation (i.e. the represented class is not abstract),</simpara>
</listitem>
<listitem>
<simpara>that has a signature compatible to the owned or inherited constructor of <literal>{C}</literal>.</simpara>
<simpara>This means we have the constructor function of class <literal>{C}</literal> (but only if is non-abstract) or the constructor function of any non-abstract
subclass of <literal>{C}</literal> with an override compatible signature to that of <literal>{C}</literal>'s constructor function.</simpara>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<simpara>having a value of type <literal>constructor{? extends C}</literal>, we know we have</simpara>
<itemizedlist>
<listitem>
<simpara>a constructor function of <literal>{C}</literal> or a subclass of <literal>{C}</literal>,</simpara>
</listitem>
<listitem>
<simpara>that can be used for instantiation (i.e. the represented class is not abstract).</simpara>
<simpara>So, same situation as before except that we know nothing about the constructor function’s signature.
However, if <literal>{C}</literal> has a covariant constructor, cf. <xref linkend="_covariant-constructors"/>, we can still conclude that we have an override compatible
constructor function to that of <literal>{C}</literal>, because classes with covariant constructors enforce all their subclasses to have override compatible constructors.</simpara>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<simpara>have a value of type <literal>type{? extends C}</literal> or <literal>type{C}</literal> (the two types are equivalent), we know we have:</simpara>
<itemizedlist>
<listitem>
<simpara>an object representing a type (often constructor functions are used for this, e.g. in the case of classes, but could also be a plain object, e.g. in the case of interfaces),</simpara>
</listitem>
<listitem>
<simpara>that represents type <literal>{C}</literal> or a subtype thereof,</simpara>
</listitem>
<listitem>
<simpara>that cannot be used for instantiation (e.g. could be the constructor function of an abstract class, the object representing an interface, etc.).</simpara>
</listitem>
</itemizedlist>
</listitem>
</orderedlist>
<simpara>Slightly simplified, we can say that in the first above case we can always use the value for creating an instance with <literal>new</literal>, in the second case only if the
referenced type has a covariant constructor, cf. <xref linkend="_covariant-constructors"/>, and never in the third case.</simpara>
</section>
<section xml:id="_constructors-and-prototypes-in-ecmascript-2015">
<title>Constructors and Prototypes in ECMAScript 2015</title>
<simpara><xref linkend="fig-constructors-and-prototypes"/> for two classes A and B in ECMAScript 2015 shows the constructors, prototypes, and the relations between them for the ECMAScript 2015
code shown in <xref linkend="ex-constructors-and-prototypes"/>.</simpara>
<example xml:id="ex-constructors-and-prototypes">
<title>Constructors and Prototypes</title>
<programlisting language="javascript" linenumbering="unnumbered">class A {}
class B extends A {}
var b = new B();</programlisting>
</example>
<note>
<simpara><xref linkend="fig-constructors-and-prototypes"/> shows plain ECMAScript 2015 only.
Also note that <literal>A</literal> is defined without an <literal>extends</literal> clause, which is what ECMAScript 2015 calls a <emphasis>base class</emphasis> (as opposed to a <emphasis>derived class</emphasis>).
The constructor of a base class always has Function.prototype as its prototype.
If we had defined <literal>A</literal> as <literal>class A extends Object {}</literal> in the listing above, then the constructor of <literal>A</literal> would have Object’s constructor as its prototype
(depicted in as a dashed red arrow), which would make a more consistent overall picture.</simpara>
</note>
<figure xml:id="fig-constructors-and-prototypes">
<title>Constructors and prototypes for two classes A and B in ECMAScript 2015 (not N4JS!)</title>
<mediaobject>
<imageobject>
<imagedata fileref="chapters/04_types/fig/ctorsProtosInES6.svg" align="center"/>
</imageobject>
<textobject><phrase>ctorsProtosInES6</phrase></textobject>
</mediaobject>
</figure>
<simpara>Base classes in the above sense are not available in N4JS.
If an N4JS class does not provide an <literal>extends</literal> clause, it will implicitly inherit from built-in class <literal>N4Object</literal>,
if it provides an <literal>extends</literal> clause stating <literal>Object</literal> as its super type, then it corresponds to what is shown in <xref linkend="fig-constructors-and-prototypes"/> with the red dashed arrow.</simpara>
</section>
</section>
<section xml:id="_this-type" role="language-n4js">
<title>This Type</title>
<simpara>The <literal>this</literal> keyword may represent either a <literal>this</literal> literal (cf. <xref linkend="ex:this-keyword-and-type-in-instance-and-static-context"/>) or may refer to the <literal>this</literal> type.
In this section, we describe the latter case.</simpara>
<simpara>Typical use cases of the <literal>this</literal> type include:</simpara>
<itemizedlist>
<listitem>
<simpara>declaring the return type of instance methods</simpara>
</listitem>
<listitem>
<simpara>declaring the return type of static methods</simpara>
</listitem>
<listitem>
<simpara>as formal parameter type of constructors in conjunction with use-site structural typing</simpara>
</listitem>
<listitem>
<simpara>the parameter type of a function type expression, which appears as type of a method parameter</simpara>
</listitem>
<listitem>
<simpara>the parameter type in a return type expression (<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi></math><literal>{this}</literal>,<literal>constructor{this}</literal>)</simpara>
</listitem>
<listitem>
<simpara>an existential type argument inside a return type expression for methods (e.g.<literal>ArrayList&lt;? extends this&gt; method(){&#8230;&#8203;}</literal>)</simpara>
</listitem>
</itemizedlist>
<simpara>The precise rule where it may appear is given below in <xref linkend="Req-IDE-37"/>.</simpara>
<simpara>The <literal>this</literal> type is similar to a type variable, and it is bound to the declared or inferred type of the receiver.
If it is used as return type, all return statements of the methods must return the <literal>this</literal> keyword or a variable value implicitly inferred to a <literal>this</literal> type (e.g. <literal>var x = this; return x;</literal>).</simpara>
<formalpara>
<title>Simple This Type</title>
<para>
<programlisting language="n4js" linenumbering="unnumbered">class A {
f(): this {
return this;
}
})
class B extends A {}
var a: A; var b: B;
a.f(); // returns something with the type of A
b.f(); // returns something with the type of B</programlisting>
</para>
</formalpara>
<simpara><literal>this</literal> can be thought of as a type variable which is implicitly substituted with the declaring class (i.e. this type used in a class <literal>{A}</literal> actually means <literal>&lt;? extends A&gt;</literal>).</simpara>
<section xml:id="this-type-syntax">
<title>Syntax</title>
<programlisting language="xtext" linenumbering="unnumbered">ThisTypeRef returns ThisTypeRef:
ThisTypeRefNominal | ThisTypeRefStructural;
ThisTypeRefNominal returns ThisTypeRefNominal:
{ThisTypeRefNominal} 'this'
;
ThisTypeRefStructural returns ThisTypeRefStructural:
typingStrategy=TypingStrategyUseSiteOperator
'this'
('with' '{' ownedStructuralMembers+=TStructMember* '}')?
;</programlisting>
<simpara>The keyword <literal>this</literal> and the type expression <literal>this</literal> look similar, however they can refer to different types.
The type always refers to the type of instances of a class.
The <literal>this</literal> keyword refers to the type of instances of the class in case of instance methods, but to the classifier the of the class in case of static methods.
See <xref linkend="_this-keyword"/> for details.</simpara>
<example xml:id="ex:this-keyword-and-type-in-instance-and-static-context">
<title>This keyword and type in instance and static context</title>
<simpara>Note that the following code is not working, because some usages below are
not valid in N4JS. This is only to demonstrate the types.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class C {
instanceMethod() {
var c: this = this;
}
static staticMethod() {
var C: type{this} = this;
}
}</programlisting>
</example>
<simpara>Structural typing and additional members in structural referenced types is described in <xref linkend="_structural-typing"/>.</simpara>
</section>
<section xml:id="this-keyword-semantics">
<title>Semantics</title>
<requirement xml:id="IDE-37">
<title>This Type</title>
<simpara>
<anchor xml:id="Req-IDE-37" xreflabel="[Req-IDE-37]"/>
<emphasis role="strong">Requirement: IDE-37:</emphasis>
<link linkend="Req-IDE-37">This Type</link> (ver. 1)</simpara>
<simpara>
</simpara>
<itemizedlist>
<listitem>
<simpara><literal>this</literal> used in the context of a class is actually inferred to an existential type <literal>? extends A</literal> inside the class itself.</simpara>
</listitem>
<listitem>
<simpara>the <literal>this</literal> type may only be used</simpara>
<itemizedlist>
<listitem>
<simpara>as the type of a formal parameter of a constructor, if and only if combined with use-site structural typing.</simpara>
</listitem>
<listitem>
<simpara>at covariant positions within member declarations, except for static members of interfaces.</simpara>
</listitem>
</itemizedlist>
</listitem>
</itemizedlist>
</requirement>
<simpara>Remarks</simpara>
<itemizedlist>
<listitem>
<simpara>Due to the function subtype relation and constraints on overriding methods (in which the overriding method has to be a subtype of the overridden method),
it is not possible to use the <literal>this</literal> type in formal parameters but only as return type. The following listing demonstrates that problem:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class A {
bar(x: this): void { ... } // error
// virtually defines: bar(x: A): void
}
class B extends A {
// virtually defines: bar(x: B): void
}</programlisting>
<simpara>As the <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi></math> type is replaced similar to a type variable, the virtually defined method <literal>bar</literal> in is not override compatible with <literal>bar</literal> in <literal>A</literal>.</simpara>
<simpara>In case of constructors, this problem does not occur because a subclass constructor does not need to be override compatible with the constructor of the super class.
Using <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi></math> as the type of a constructor’s parameter, however, would mean that you can only create an instance of the class if you already have
an instance (considering that due to the lack of method overloading a class can have only a single constructor), making creation of the first instance impossible.
Therefore, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi></math> is also disallowed as the type of a constructor’s parameter.</simpara>
</listitem>
<listitem>
<simpara>The difference between the type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi></math> and the keyword <literal>this</literal> is when and how theactual type is set:
the actual type of the <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi></math> type is computed at compile(or validation) time and is always the containing type (of the member in which the type expression is used)
or a subtype of that type – this isnot a heuristic, this is so by definition.
In contrast, the actual typeof the keyword <literal>this</literal> is only available at runtime, while the type used at compilation time is only a heuristically-computed type, in other words,a good guess.</simpara>
</listitem>
<listitem>
<simpara>The value of the <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi></math> type is, in fact, not influenced by any <literal>@This</literal> annotations.
Instead of using <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi></math> in these cases, the type expressions in the <literal>@This</literal> annotations can be used.</simpara>
</listitem>
<listitem>
<simpara>The <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi></math> type is always bound to the instance-type regardless of the context it occurs in (non-static or static).
To refer to the this-classifier (static type) the construct <literal>type{this}</literal> is used.</simpara>
</listitem>
</itemizedlist>
<simpara><?asciidoc-pagebreak?></simpara>
<example>
<title>This type in function-type-expression</title>
<programlisting language="n4js" linenumbering="unnumbered">class A {
alive: boolean = true;
methodA(func: {function(this)}): string {
func(this); // applying the passed-in function
return "done";
}
}</programlisting>
</example>
<simpara>The use of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi></math> type is limited to situations where it cannot be referred in mixed co- and contra-variant ways.
In the following example the problem is sketched up. <footnote><simpara>The phenomenon is described in IDEBUG-263</simpara></footnote></simpara>
<example>
<title>Problems with this type and type arguments</title>
<programlisting language="n4js" linenumbering="unnumbered">// Non-working example, see problem in line 15.
class M&lt;V&gt; { public value: V; }
class A {
public store: M&lt;{function(this)}&gt;; // usually not allowed, but let's assume it would be possible----
}
class B extends A { public x=0; } // type of store is M&lt;{function(B)}&gt;
var funcA = function(a: A) {/*...something with a...*/}
var funcB = function(b: B) { console.log(b.x); }
var a: A = new A(); var b: B = new B();
b.store.value = funcA // OK, since {function(A)} &lt;: {function(B)}
b.store.value = funcB // OK.
var a2: A = b; // OK, since B is a subtype of A
a2.store.value( a ) // RUNTIME ERROR, the types are all correct, but remember b.store.value was assigned to funcB, which can only handle subtypes of B!</programlisting>
</example>
</section>
</section>
<section xml:id="_enums" role="language-n4js">
<title>Enums</title>
<simpara>Enums are an ordered set of literals.
Although enums are not true classes, they come with built-in methods for accessing value, name and type name of the enum.</simpara>
<simpara>In N4JS, two flavours of enumerations are distinguished: ordinary enums (N4JS) and string based enums.
Ordinary enums (or in short, enums) are used while programming in N4JS.
String based enums are introduced to access enumerations derived from standards, mainly developed by the W3C, in order to access the closed set of string literals defined in webIDL syntax.</simpara>
<section xml:id="_enums-n4js">
<title>Enums (N4JS)</title>
<simpara>Definition and usage of an enumeration:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">// assume this file to be contained in a package "myPackage"
enum Color {
RED, GREEN, BLUE
}
enum Country {
DE : "276",
US : "840",
TR : "792"
}
var red: Color = Color.RED;
var us: Country = Country.US;
console.log(red.name); // --&gt; RED
console.log(red.value); // --&gt; RED
console.log(red.n4class.fqn); // --&gt; myPackage.Color
console.log(red.toString()); // --&gt; RED
console.log(us.name); // --&gt; US
console.log(us.value); // --&gt; 840
console.log(us.n4classfqn); // --&gt; myPackage.Country
console.log(us.toString()); // --&gt; 840</programlisting>
<section xml:id="enums-syntax">
<title>Syntax</title>
<programlisting language="xtext" linenumbering="unnumbered">N4EnumDeclaration &lt;Yield&gt;:
=&gt;( {N4EnumDeclaration}
(declaredModifiers+=N4Modifier)*
'enum' name=BindingIdentifier&lt;Yield&gt;? )
'{'
(literals+=N4EnumLiteral (',' literals+=N4EnumLiteral)*)?
'}';
N4EnumLiteral: name=IdentifierName (':' value=STRING)?;</programlisting>
</section>
<section xml:id="enums-semantics" role="language-n4js">
<title>Semantics</title>
<simpara>The enum declaration <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>E</mi></math> is of type <literal>type{E}</literal> and every enumeration is implicitly derived from <literal>N4Enum</literal>.
There are similarities to other languages such as Java, for example, where the literals of an enum are treated as final static fields with the type
of the enumeration and the concrete enumeration provides specific static methods including the literals.
This leads to the following typing rules:</simpara>
<requirement xml:id="IDE-38">
<title>Enum Type Rules</title>
<simpara>
<anchor xml:id="Req-IDE-38" xreflabel="[Req-IDE-38]"/>
<emphasis role="strong">Requirement: IDE-38:</emphasis>
<link linkend="Req-IDE-38">Enum Type Rules</link> (ver. 1)</simpara>
<simpara>
For a given enumeration declaration <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>E</mi></math> with literals <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>L</mi></math>, the following type rules are defined:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>Every enumeration <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>E</mi></math> is a subtype of the base type <literal>N4Enum</literal>:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mi>E</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>N</mi><mn>4</mn><mi>E</mi><mi>n</mi><mi>u</mi><mi>m</mi></mrow></mfrac><mrow/></math>
<simpara>which itself is a subtype of <literal>Object</literal>:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow/><mrow><mi>N</mi><mn>4</mn><mi>E</mi><mi>n</mi><mi>u</mi><mi>m</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>O</mi><mi>b</mi><mi>j</mi><mi>e</mi><mi>c</mi><mi>t</mi></mrow></mfrac></math>
</listitem>
<listitem>
<simpara>Every literal <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>L</mi></math> of an enumeration <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>E</mi></math> is
of the type of the enumeration:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>L</mi><mo></mo><mi>E</mi><mo>.</mo><mi>l</mi><mi>i</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>s</mi></mrow><mrow><mi>Γ</mi><mo></mo><mi>L</mi><mi>:</mi><mi>E</mi></mrow></mfrac></math>
</listitem>
</orderedlist>
<simpara>This means that every literal is a subtype of <literal>N4Enum</literal> and <literal>Object</literal>:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>L</mi><mo></mo><mi>E</mi><mo>.</mo><mi>l</mi><mi>i</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>s</mi></mrow><mrow><mi>L</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>N</mi><mn>4</mn><mi>E</mi><mi>n</mi><mi>u</mi><mi>m</mi><mo></mo><mi>L</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>O</mi><mi>b</mi><mi>j</mi><mi>e</mi><mi>c</mi><mi>t</mi></mrow></mfrac></math>
<simpara>The base enumeration type <literal>N4Enum</literal> is defined as follows:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">/**
* Base class for all enumeration, literals are assumed to be static constant fields of concrete subclasses.
*/
public object N4Enum {
/**
* Returns the name of a concrete literal
*/
public get name(): string
/**
* Returns the value of a concrete literal. If no value is
* explicitly set, it is similar to the name.
*/
public get value(): string
/**
* Returns a string representation of a concrete literal, it returns
* the same result as value()
*/
public toString(): string
/**
* Returns the meta class object of this enum literal for reflection.
* The very same meta class object can be retrieved from the enumeration type directly.
*/
public static get n4type(): N4EnumType
//IDE-785 this as return type in static
/**
* Returns array of concrete enum literals
*/
public static get literals(): Array&lt;? extends this&gt;
/**
* Returns concrete enum literal that matches provided name,
* if no match found returns undefined.
*/
public static findLiteralByName(name: string): this
/**
* Returns concrete enum literal that matches provided value,
* if no match found returns undefined.
*/
public static findLiteralByValue (value: string): this
}</programlisting>
</requirement>
<requirement xml:id="IDE-39">
<title>Unique literal names</title>
<simpara>
<anchor xml:id="Req-IDE-39" xreflabel="[Req-IDE-39]"/>
<emphasis role="strong">Requirement: IDE-39:</emphasis>
<link linkend="Req-IDE-39">Unique literal names</link> (ver. 1)</simpara>
<simpara>
* <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>i</mi><mo>,</mo><mi>j</mi><mi>:</mi><mi>l</mi><mi>i</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>s</mi><mfenced close="]" open="["><mi>i</mi></mfenced><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>l</mi><mi>i</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>s</mi><mfenced close="]" open="["><mi>j</mi></mfenced><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo></mo><mi>i</mi><mo>=</mo><mi>j</mi></math></simpara>
<simpara>Literal names have to be unique.</simpara>
</requirement>
<requirement xml:id="IDE-40">
<title>Enum Literals are Singletons</title>
<simpara>
<anchor xml:id="Req-IDE-40" xreflabel="[Req-IDE-40]"/>
<emphasis role="strong">Requirement: IDE-40:</emphasis>
<link linkend="Req-IDE-40">Enum Literals are Singletons</link> (ver. 1)</simpara>
<simpara>
Enum literals are singletons:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mo></mo><msub><mi>e</mi><mn>1</mn></msub><mo>,</mo><msub><mi>e</mi><mn>2</mn></msub><mo>,</mo><mi>μ</mi><mfenced close=")" open="("><msub><mi>e</mi><mn>1</mn></msub></mfenced><mo>=</mo><mi>μ</mi><mfenced close=")" open="("><msub><mi>e</mi><mn>2</mn></msub></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>N4EnumLiteral</mtext></mstyle><mo></mo><mi>Γ</mi><mo></mo><msub><mi>e</mi><mn>1</mn></msub><mo>=</mo><mi>Γ</mi><mo></mo><msub><mi>e</mi><mn>2</mn></msub><mi>:</mi><msub><mi>e</mi><mn>1</mn></msub><mo>=</mo><mo>=</mo><msub><mi>e</mi><mn>2</mn></msub><mo></mo><msub><mi>e</mi><mn>1</mn></msub><mo>=</mo><mo>=</mo><mo>=</mo><msub><mi>e</mi><mn>2</mn></msub></math>
</requirement>
<example>
<title>Enumeration List</title>
<simpara>Due to the common base type <literal>N4Enum</literal> it is possible to define generics accepting only enumeration, as shown in this example:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">enum Color { R, G, B}
class EList&lt;T extends N4Enum&gt; {
add(t: T) {}
get(): T { return null; }
}
var colors: EList&lt;Color&gt;;
colors.add(Color.R);
var c: Color = colors.get();</programlisting>
</example>
</section>
</section>
<section xml:id="_string-based-enums">
<title>String-Based Enums</title>
<simpara>In current web standards [<link linkend="W3C:Steen:14:XL">W3C:Steen:14:XL</link>], definitions of enumerations are often given in webIDL syntax.
While the webIDL-definition assembles a set of unique string literals as a named enum-entity, the language binding to ECMAScript refers to the usage of the members of these enumerations only.
Hence, if an element of an enumeration is stored in a variable or field, passed as a parameter into a method or function or given back as a result, the actual type in JavaScript will be <literal>string</literal>.
To provide the N4JS user with some validations regarding the validity of a statement at compile time, a special kind of subtypes of <literal>string</literal> are introduced: the string-based enum using the <literal>@StringBased</literal> annotation.
(See also other string-based types like <literal>typename&lt;T&gt;</literal> <literal>pathSelector&lt;T&gt;</literal> and <literal>i18nKey</literal> in <xref linkend="_primitive-pathselector-and-i18nkey"/>.)</simpara>
<simpara>String-based enums do not have any kind of runtime representation; instead, the transpiler will replace each reference to a literal of a string-based enum by a corresponding string literal in the output code.
Furthermore, no meta-information is available for string-based enums, i.e. the <literal>n4type</literal> property is not available.
The only exception is the static getter <literal>literals</literal>: it is available also for string-based enums and has the same meaning.
In case of string-based enums, however, there won’t be a getter used at runtime; instead, the transpiler replaces every read access to this getter by an array literal containing a string literal for each of the enum’s literals.</simpara>
<requirement xml:id="IDE-41">
<title>String-Based Enum Type Rules</title>
<simpara>
<anchor xml:id="Req-IDE-41" xreflabel="[Req-IDE-41]"/>
<emphasis role="strong">Requirement: IDE-41:</emphasis>
<link linkend="Req-IDE-41">String-Based Enum Type Rules</link> (ver. 1)</simpara>
<simpara>
</simpara>
<simpara>For a string-based enum declaration <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>E</mi><mi>S</mi></msub></math> with literals <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>L</mi><mi>S</mi></msub></math> the following type rules are defined:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>Every string-based enumeration <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>E</mi><mi>S</mi></msub></math> is a subtype of the base type <literal>N4StringBasedEnum</literal>:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>type</mtext></mstyle><mfenced close="}" open="{"><msub><mi>E</mi><mi>S</mi></msub></mfenced><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>N4StringBasedEnum</mtext></mstyle></mrow></mfrac><mrow/></math>
<simpara>which itself is not related to the standard enumeration type <literal>N4Enum</literal></simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mfrac><mrow/><mrow><mstyle mathvariant="monospace"><mtext>N4StringBasedEnum</mtext></mstyle><mspace width="1.0mm"/><mo></mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>N4Enum</mtext></mstyle></mrow></mfrac></mtd></mtr><mtr><mtd><mfrac><mrow/><mrow><mstyle mathvariant="monospace"><mtext>N4Enum</mtext></mstyle><mspace width="1.0mm"/><mo></mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>N4StringBasedEnum</mtext></mstyle></mrow></mfrac></mtd></mtr></mtable></math>
</listitem>
<listitem>
<simpara><literal>N4StringBasedEnum</literal> is a subtype of <literal>string</literal></simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mfrac><mrow/><mrow><mstyle mathvariant="monospace"><mtext>N4StringBasedEnum</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>string</mtext></mstyle></mrow></mfrac></mtd></mtr></mtable></math>
</listitem>
<listitem>
<simpara>Each literal in <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>L</mi><mi>S</mi></msub></math> of a string-based enumeration
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>E</mi><mi>S</mi></msub></math> is of the type of the string-based enumeration.</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>l</mi><mo></mo><msub><mi>E</mi><mi>S</mi></msub><mo>.</mo><msub><mi>L</mi><mi>S</mi></msub></mrow><mrow><mi>Γ</mi><mo></mo><mi>l</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><msub><mi>E</mi><mi>S</mi></msub></mrow></mfrac></math>
</listitem>
<listitem>
<simpara><xref linkend="Req-IDE-39"/> also applies for <literal>N4StringBasedEnum</literal>.</simpara>
</listitem>
<listitem>
<simpara><xref linkend="Req-IDE-40"/> also applies for <literal>N4StringBasedEnum</literal>.</simpara>
</listitem>
<listitem>
<simpara>References to string-based enums may only be used in the following places:</simpara>
<orderedlist numeration="loweralpha">
<listitem>
<simpara>in type annotations</simpara>
</listitem>
<listitem>
<simpara>in property access expressions to refer to one of the enum’s literals</simpara>
</listitem>
<listitem>
<simpara>in property access expressions to read from the static getter <literal>literals</literal></simpara>
<simpara>In particular, it is invalid to use the type of a string-based enum as a
value, as in</simpara>
<programlisting language="n4js" linenumbering="unnumbered"> @StringBased enum Color { RED, GREEN, BLUE }
var c = Color;</programlisting>
</listitem>
</orderedlist>
</listitem>
</orderedlist>
<example>
<title>WebIDL example</title>
<formalpara>
<title>Gecko-Engine webIDL XMLHttpRequestResponseType as taken from [<link linkend="W3C:Steen:14:XL">W3C:Steen:14:XL</link>]</title>
<para>
<programlisting language="n4js" linenumbering="unnumbered">enum XMLHttpRequestResponseType {
"",
"arraybuffer",
"blob",
"document",
"json",
"text" //, ... and some mozilla-specific additions
}</programlisting>
</para>
</formalpara>
<simpara>Compatible Definition of this Enumeration in N4JS, provided through a
runtime-library definition:</simpara>
<formalpara>
<title>File in source-folder: w3c/dom/XMLHttpRequestResponseType.n4js</title>
<para>
<programlisting language="n4js" linenumbering="unnumbered">@StringBased enum XMLHttpRequestResponseType {
vacant : "",
arrayBuffer : "arraybuffer",
blob : "blob",
document : "document",
json : "json",
text : "text"
}</programlisting>
</para>
</formalpara>
<simpara>Usage of the enumeration in the definition files of the runtime-library.
Note the explicit import of the enumeration.</simpara>
<formalpara>
<title>XMLHttpRequestResponse.n4jsd</title>
<para>
<programlisting language="n4js" linenumbering="unnumbered">@@ProvidedByRuntime
import XMLHttpRequestResponseType from "w3c/dom/XMLHttpRequestResponseType";
@Global
export external public class XMLHttpRequestResponse extends XMLHttpRequestEventTarget {
// ...
// Setter Throws TypeError Exception
public responseType: XMLHttpRequestResponseType;
// ...
}</programlisting>
</para>
</formalpara>
<simpara>Client code importing the runtime-library as defined above can now use the Enumeration in a type-safe way:</simpara>
<formalpara>
<title>String-Based Enumeration Usage</title>
<para>
<programlisting language="n4js" linenumbering="unnumbered">import XMLHttpRequestResponseType from "w3c/dom/XMLHttpRequestResponseType";
public function process(req: XMLHttpRequest) : void {
if( req.responseType == XMLHttpRequestResponseType.text ) {
// do stuff ...
} else {
// signal unrecognized type.
var errMessage: req.responseType + " is not supported"; // concatination of two strings.
show( errMessage );
}
}</programlisting>
</para>
</formalpara>
</example>
</requirement>
</section>
</section>
<section xml:id="_short-hand-syntax" role="language-n4js">
<title>Short-Hand Syntax</title>
<simpara>Short-hand syntax is available for a number of built-in types.</simpara>
<section xml:id="_array-short-hand-syntax">
<title>Array Short-Hand Syntax</title>
<simpara>For the built-in type <literal>Array</literal> a convenience short form is available. Thus, writing</simpara>
<programlisting language="n4js" linenumbering="unnumbered">let arr: string[];</programlisting>
<simpara>is equivalent to</simpara>
<programlisting language="n4js" linenumbering="unnumbered">let arr: Array&lt;string&gt;;</programlisting>
<simpara>Multi-dimensional arrays can be declared as such:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">let arr: string[][][];</programlisting>
<simpara>which is equivalent to</simpara>
<programlisting language="n4js" linenumbering="unnumbered">let arr: Array&lt;Array&lt;Array&lt;string&gt;&gt;&gt;;</programlisting>
</section>
<section xml:id="_iterablen-short-hand-syntax">
<title>IterableN Short-Hand Syntax</title>
<simpara>The built-in IterableN types (i.e. <literal>Iterable2</literal>, <literal>Iterable3</literal>, &#8230;&#8203; <literal>Iterable9</literal>, see <xref linkend="IterableN"/>) are
also provided with a short-hand syntax. For example, writing</simpara>
<programlisting language="n4js" linenumbering="unnumbered">let i3: [string,number,string[]];</programlisting>
<simpara>would be equivalent to</simpara>
<programlisting language="n4js" linenumbering="unnumbered">let i3: Iterable3&lt;string,number,Array&lt;string&gt;&gt;;</programlisting>
<simpara>Note the following special cases:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">let i0: [];
let i1: [string];
let union: string|number[];</programlisting>
<simpara>which is equivalent to</simpara>
<programlisting language="n4js" linenumbering="unnumbered">let i0: Iterable&lt;?&gt;;
let i1: Iterable&lt;string&gt;;
let union: union{string,Array&lt;number&gt;}; // not: Array&lt;union{string,number}&gt;</programlisting>
<simpara>Further note: while this syntax is very similar to TypeScript&#8217;s tuple syntax, the semantics
of tuples and IterableN are very different.</simpara>
</section>
</section>
</chapter>
<chapter xml:id="_classifiers">
<title>Classifiers</title>
<section xml:id="_n4js-specific-classifiers">
<title>N4JS Specific Classifiers</title>
<simpara>N4JS provides three new metatypes: class, interface, and enums.
In this section we describe classes and interfaces.
These metatypes, called <emphasis>classifiers</emphasis>, share some common properties which are described before type specific properties are outlined in the following sections.</simpara>
<simpara>All of these metatypes can be marked with type access modifiers:</simpara>
<programlisting language="xtext" linenumbering="unnumbered">enum N4JSTypeAccessModifier: project | public;</programlisting>
<section xml:id="_properties-2" role="language-n4js">
<title>Properties</title>
<simpara><emphasis role="strong">Properties defined by syntactic elements:</emphasis></simpara>
<variablelist>
<varlistentry>
<term><literal>annotations</literal> </term>
<listitem>
<simpara>Arbitrary annotations, see <xref linkend="_annotations"/> for details.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>accessModifier</literal> </term>
<listitem>
<simpara>N4JS type access modifier: <literal>public</literal>, or <literal>project</literal>; <literal>public</literal> can be combined with <literal>@Internal</literal>; if
<emphasis role="strong">export</emphasis> is <literal>true</literal> the default is else the default is <literal>private</literal>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>name</literal> </term>
<listitem>
<simpara>The simple name of a classifier. If the classifier is defined by an anonymous class expression, an artificial but unique name is created.
The name needs to be a valid identifier, see <xref linkend="_valid-names"/>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>typePars</literal> </term>
<listitem>
<simpara>Collection of type parameters of a generic classifier; empty by default.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>ownedMembers</literal> </term>
<listitem>
<simpara>Collection of owned members, i.e. methods and fields defined directly in the classifier and, if present, the explicitly defined constructor.
Depending on the concrete classifier, additional constraints are defined.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>typingStrategy</literal> </term>
<listitem>
<simpara>The definition-site typing strategy. By default nominal typing is used.
See <xref linkend="_structural-typing"/> for details.</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara><emphasis role="strong">The following pseudo properties are defined via annotations:</emphasis></simpara>
<variablelist>
<varlistentry>
<term><literal>export</literal> </term>
<listitem>
<simpara>Boolean property set to true if the <literal>export</literal> modifier is set.
If value is true, the classifier may be accessible outside the project.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>final</literal> </term>
<listitem>
<simpara>Boolean property which is set to final if annotation <literal>@Final</literal> is set.
Also see <xref linkend="_final-modifier"/></simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>deprecated</literal> </term>
<listitem>
<simpara>Boolean property set to true if annotation <literal>@Deprecated</literal> is set.</simpara>
<note>
<simpara>Version 0.4, not implemented in Version 0.3</simpara>
</note>
</listitem>
</varlistentry>
</variablelist>
<simpara><emphasis role="strong">We additionally define the following pseudo properties:</emphasis></simpara>
<variablelist>
<varlistentry>
<term><literal>acc</literal> </term>
<listitem>
<simpara>Type access modifier as described in <xref linkend="_accessibility-of-types-top-level-variables-and-function-declarations"/>, it is the aggregated value of the
<literal>accessModifier</literal> and the <emphasis role="strong">export</emphasis> property.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>owned{Fields|Methods|Getters|Setters|Accessors}</literal> </term>
<listitem>
<simpara>Filters ownedMembers by metatype, short for<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>x</mi><mo></mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>,</mo><mi>μ</mi><mfenced close=")" open="("><mi>x</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Field</mtext></mstyle></math> etc.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>members</literal> </term>
<listitem>
<simpara>Reflexive transitive closure of all members of a classifier and its super classifiers, see <xref linkend="_common-semantics-of-classifiers"/> on how this is calculated.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>fields|methods|getters|setters|accessors</literal> </term>
<listitem>
<simpara>Filters members by metatype, short for<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>x</mi><mo></mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>,</mo><mi>μ</mi><mfenced close=")" open="("><mi>x</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Field</mtext></mstyle></math> etc.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>superClassifiers</literal> </term>
<listitem>
<simpara>Classes and interface may extend or implement classes or interfaces.
Any class or interface extended or interface implemented is called <emphasis>super classifier</emphasis>.
We distinguish the directly subtyped classifiers and from the transitive closure of supertypes <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>u</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>C</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>f</mi><mi>i</mi><mi>e</mi><mi>r</mi><msup><mi>s</mi><mo>*</mo></msup></math></simpara>
</listitem>
</varlistentry>
</variablelist>
</section>
<section xml:id="_common-semantics-of-classifiers" role="language-n4js">
<title>Common Semantics of Classifiers</title>
<requirement xml:id="IDE-42">
<title>Subtyping of Classifiers</title>
<simpara>
<anchor xml:id="Req-IDE-42" xreflabel="[Req-IDE-42]"/>
<emphasis role="strong">Requirement: IDE-42:</emphasis>
<link linkend="Req-IDE-42">Subtyping of Classifiers</link> (ver. 1)</simpara>
<simpara>
For a given type <literal>C</literal>, and supertypes <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>u</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>C</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>f</mi><mi>i</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>=</mo><mfenced close="}" open="{"><msub><mi>S</mi><mn>1</mn></msub><mrow><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></mrow><msub><mi>S</mi><mi>n</mi></msub></mfenced></math> directly subtyped
<literal>C</literal>, the following constraints must be true:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>The supertypes must be accessible to the subtype:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>S</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>S</mi><mi>n</mi></msub></math> must be accessible to <literal>C</literal>.</simpara>
</listitem>
<listitem>
<simpara>All type parameters of the direct supertypes have to be bound by type arguments in the subtype and the type arguments have to be substitutable types of the type parameters.</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mn>0</mn><mo>&lt;</mo><mi>i</mi><mo></mo><mi>k</mi><mi>:</mi><mo></mo><mi>P</mi><mo></mo><msub><mi>S</mi><mi>i</mi></msub><mi>:</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>A</mi><mo></mo><msub><mi>C</mi><mo>.</mo></msub><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>A</mi><mi>r</mi><mi>g</mi><mi>s</mi><mi>:</mi><mi>b</mi><mi>i</mi><mi>n</mi><mi>d</mi><mfenced close=")" open="("><mi>A</mi><mi>P</mi></mfenced><mo></mo><mi>A</mi><mo>.</mo><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>B</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>d</mi><mo>&lt;</mo><mi>:</mi><mi>P</mi><mo>.</mo><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>B</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>d</mi></math></simpara>
</listitem>
<listitem>
<simpara>Wildcards may not be used as type argument when binding a supertype’s type parameters.</simpara>
</listitem>
<listitem>
<simpara>A classifier cannot be directly subtyped directly multiple times:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><msub><mi>S</mi><mi>i</mi></msub><mo>,</mo><msub><mi>S</mi><mi>j</mi></msub><mfenced close=")" open="("><mi>i</mi><mrow><mi>j</mi><mo></mo><mfenced close="}" open="{"><mrow><mn>1.</mn><mo>.</mo><mi>n</mi></mrow></mfenced></mrow></mfenced><mi>:</mi><msub><mi>S</mi><mi>i</mi></msub><mo>=</mo><msub><mi>S</mi><mi>j</mi></msub><mo></mo><mi>i</mi><mo>=</mo><mi>j</mi></math></simpara>
</listitem>
</orderedlist>
<simpara>In order to simplify the following constraints, we use the pseudo property <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi></math> to refer to all members of a classifier.
This includes all members directly declared by the classifier itself, i.e. the <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi></math>, and all members inherited from its super classifiers.
The concrete mechanisms for inheriting a member are different and further constraint (cf. <xref linkend="_redefinition-of-members"/>).
A classifier only inherits its members from its direct supertypes, although the supertypes may contains members inherited from their supertypes.</simpara>
</requirement>
</section>
<section xml:id="_classes" role="language-n4js">
<title>Classes</title>
<section xml:id="_definition-of-classes">
<title>Definition of Classes</title>
<simpara>Classes are either declared with a class declaration on top level, or they can be used as anonymous classes in expressions.
The latter may have a name, which may be used for error messages and reflection.</simpara>
<simpara>At the current stage, class expressions are effectively disabled at least until the semantics of them are finalized in ECMAScript 6.</simpara>
<simpara>In N4JS (as in many other languages) multi-inheritance of classes is not supported.
Although the <literal>diamond problem</literal> (of functions being defined in both superclasses) could be solved via union and intersection types, this would lead to problems when calling these super implementations.
This is particularly an issue due to JavaScript not supporting
multiple prototypes.<footnote><simpara>E.g., for given <literal role="language-n4js">class A{ foo(A):A{}} class B{ foo(B):B{}}</literal>, a class C could be defined as <literal role="language-n4js">class C{ foo(union{A,B}):intersection{A,B}{}}</literal>. In this case it would then be a syntactical problem (and even worse - a conceptual problem) of how to call the super methods defined in A and Bfrom C.</simpara></footnote>
Interfaces, however, allow for multi-inheritance. Since the former can also define functions with bodies, this is not a hard restriction.</simpara>
<section xml:id="class-syntax">
<title>Syntax</title>
<formalpara>
<title>Syntax N4 Class Declaration and Expression</title>
<para>
<programlisting language="xtext" linenumbering="unnumbered">N4ClassDeclaration &lt;Yield&gt;:
=&gt;(
{N4ClassDeclaration}
annotations+=Annotation*
(declaredModifiers+=N4Modifier)*
'class' typingStrategy=TypingStrategyDefSiteOperator? name=BindingIdentifier&lt;Yield&gt;?
)
TypeVariables?
ClassExtendsClause&lt;Yield&gt;?
Members&lt;Yield&gt;
;
N4ClassExpression &lt;Yield&gt;:
{N4ClassExpression}
'class' name=BindingIdentifier&lt;Yield&gt;?
ClassExtendsClause&lt;Yield&gt;?
Members&lt;Yield&gt;;
fragment ClassExtendsClause &lt;Yield&gt;*:
'extends' (
=&gt;superClassRef=ParameterizedTypeRefNominal ('implements' ClassImplementsList)?
| superClassExpression=LeftHandSideExpression&lt;Yield&gt;
)
| 'implements' ClassImplementsList
;
fragment ClassImplementsList*:
implementedInterfaceRefs+=ParameterizedTypeRefNominal
(',' implementedInterfaceRefs+=ParameterizedTypeRefNominal)*
;
fragment Members &lt;Yield&gt;*:
'{'
ownedMembers+=N4MemberDeclaration&lt;Yield&gt;*
'}'
;</programlisting>
</para>
</formalpara>
</section>
<section xml:id="class-properties">
<title>Properties</title>
<simpara>These are the properties of class, which can be specified by the user:
Syntax N4 Class Declaration and Expression</simpara>
<variablelist>
<varlistentry>
<term><literal>abstract</literal> </term>
<listitem>
<simpara>Boolean flag indicating whether class may be instantiable; default is <literal>false</literal>, see <xref linkend="_abstract-classes"/>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>external</literal> </term>
<listitem>
<simpara>Boolean flag indicating whether class is a declaration without implementation or with an external (non-N4JS) implementation; default is <literal>false</literal>, see <xref linkend="_definition-site-structural-typing"/>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>defStructural</literal> </term>
<listitem>
<simpara>Boolean flag indicating whether subtype relation uses nominal or structural typing, see <xref linkend="_definition-site-structural-typing"/> for details.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>superType/sup</literal> </term>
<listitem>
<simpara>The type referenced by <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>u</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi></math> is called direct superclass of a class, and vice versa the class is a direct subclass of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>u</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi></math>.
Instead of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>u</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi></math>, we sometimes simply write <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>u</mi><mi>p</mi></math>.
The derived set <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>u</mi><msup><mi>p</mi><mo>+</mo></msup></math> is defined as the transitive closures of all direct and indirect superclasses of a class.
If no supertype is explicitly stated, classes are derived from <literal>N4Object</literal>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>implementedInterfaces</literal>/<literal>interfaces</literal> </term>
<listitem>
<simpara>Collection of interfaces directly <emphasis>implemented</emphasis> by the class; empty by default.
Instead of <literal>implementedInterfaces</literal>, we simply write <literal>interfaces</literal>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>ownedCtor</literal> </term>
<listitem>
<simpara>Explicit constructor of a class (if any), see <xref linkend="_constructor-and-classifier-type"/>.</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>And we additionally define the following pseudo properties:</simpara>
<variablelist>
<varlistentry>
<term><literal>ctor</literal> </term>
<listitem>
<simpara>Explicit or implicit constructor of a class, see <xref linkend="_constructor-and-classifier-type"/>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>fields</literal> </term>
<listitem>
<simpara>Further derived properties for retrieving all methods (property <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>m</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><mi>s</mi></math>), fields (property <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mi>s</mi></math>), static
members (property <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mi>O</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi></math>), etc. can easily be added by filtering properties <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi></math> or <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi></math>.</simpara>
</listitem>
</varlistentry>
</variablelist>
</section>
<section xml:id="class-type-inference">
<title>Type Inference</title>
<simpara>The type of a class declaration or class expression <literal>C</literal> (i.e., a class definition in general) is of type <literal>constructor{C}</literal> if it is not abstract,
that is if it can be instantiated.
If it is abstract, the type of the definition simply is <literal>type{C}</literal>:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mfrac><mrow><mo>¬</mo><mi>C</mi><mo>.</mo><mi>a</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi></mrow><mrow><mi>Γ</mi><mo></mo><mi>C</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>constructor</mtext></mstyle><mfenced close="}" open="{"><mi>C</mi></mfenced></mrow></mfrac></mtd></mtr><mtr><mtd><mfrac><mrow><mi>C</mi><mo>.</mo><mi>a</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi></mrow><mrow><mi>Γ</mi><mo></mo><mi>C</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>type</mtext></mstyle><mfenced close="}" open="{"><mi>C</mi></mfenced></mrow></mfrac></mtd></mtr></mtable></math>
<requirement xml:id="IDE-43">
<title>Structural and Nominal Supertypes</title>
<simpara>
<anchor xml:id="Req-IDE-43" xreflabel="[Req-IDE-43]"/>
<emphasis role="strong">Requirement: IDE-43:</emphasis>
<link linkend="Req-IDE-43">Structural and Nominal Supertypes</link> (ver. 1)</simpara>
<simpara>
The type of supertypes and implemented interfaces is always the nominal type, even if the supertype is declared structurally.</simpara>
</requirement>
</section>
</section>
<section xml:id="class-semantics">
<title>Semantics</title>
<simpara>This section deals with the (more or less) type-independent constraints on classes.</simpara>
<simpara>Class expressions are not fully supported at the moment.</simpara>
<definition>
<title>Transitive closure of members</title>
<simpara>
<anchor xml:id="transitive_closure_of_members" xreflabel="[transitive_closure_of_members]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="transitive_closure_of_members">Transitive closure of members</link></simpara>
<simpara>
The reflexive transitive closure of members of a class is indirectly defined by the override and implementation constraints defined in <xref linkend="_redefinition-of-members"/>.</simpara>
<simpara>Note that since overloading is forbidden, the following constraint is true <footnote><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>c</mi><mi>c</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>o</mi><mi>r</mi><mi>P</mi><mi>a</mi><mi>i</mi><mi>r</mi><mfenced close=")" open="("><msub><mi>m</mi><mn>1</mn></msub><msub><mi>m</mi><mn>2</mn></msub></mfenced></math> is defined as follows: <math xmlns="http://www.w3.org/1998/Math/MathML"><mfenced close=")" open="("><mrow><mi>μ</mi><mfenced close=")" open="("><msub><mi>m</mi><mn>1</mn></msub></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>getter</mtext></mstyle><mo></mo><mi>μ</mi><mfenced close=")" open="("><msub><mi>m</mi><mn>2</mn></msub></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>setter</mtext></mstyle></mrow></mfenced><mo></mo><mfenced close=")" open="("><mrow><mi>μ</mi><mfenced close=")" open="("><msub><mi>m</mi><mn>1</mn></msub></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>setter</mtext></mstyle><mo></mo><mi>μ</mi><mfenced close=")" open="("><msub><mi>m</mi><mn>2</mn></msub></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>getter</mtext></mstyle></mrow></mfenced></math></simpara></footnote>:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mo></mo><msub><mi>m</mi><mn>1</mn></msub><mo>,</mo><msub><mi>m</mi><mn>2</mn></msub><mo></mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi><msub><mi>m</mi><mn>1</mn></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><msub><mi>m</mi><mn>2</mn></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo></mo><msub><mi>m</mi><mn>1</mn></msub><mo>=</mo><msub><mi>m</mi><mn>2</mn></msub><mo></mo><mi>a</mi><mi>c</mi><mi>c</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>o</mi><mi>r</mi><mi>P</mi><mi>a</mi><mi>i</mi><mi>r</mi><mfenced close=")" open="("><msub><mi>m</mi><mn>1</mn></msub><msub><mi>m</mi><mn>2</mn></msub></mfenced></math>
<simpara>Remarks: Class and method definition is quite similar to the proposed ECMAScript version 6 draft [<link linkend="ECMA15a">ECMA15a(p.S13.5)</link>], except that an N4 class and members may contain</simpara>
<itemizedlist>
<listitem>
<simpara>annotations, abstract and access modifiers</simpara>
</listitem>
<listitem>
<simpara>fields</simpara>
</listitem>
<listitem>
<simpara>types</simpara>
</listitem>
<listitem>
<simpara>implemented interfaces</simpara>
</listitem>
</itemizedlist>
<simpara>Note that even <literal>static</literal> is used in ECMAScript 6.</simpara>
</definition>
<simpara>Mixing in members (i.e. interface’s methods with default implementation or fields) is similar to mixing in members from roles as defined in [<link linkend="Dart13a">Dart13a(p.S9.1)</link>].
It is also similar to default implementations in Java 8 [<link linkend="Gosling15a">Gosling15a</link>].
In Java, however, more constraints exist, (for example, methods of interfaces must be public).</simpara>
<example>
<title>Simple Class</title>
<simpara>This first example shows a very simple class with a field, a constructor and a method.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class C {
data: any;
constructor(data: any) {
this.data = data;
}
foo(): void { }
}</programlisting>
</example>
<example>
<title>Extend and implement</title>
<simpara>The following example demonstrate how a class can extend a superclass and implement an interface.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">interface I {
foo(): void
}
class C{}
class X extends C implements I {
@Override
foo(): void {}
}</programlisting>
</example>
<simpara>A class <literal>C</literal> is a subtype of another classifier <literal>S</literal> (which can be a class or interface) if the other classifier <literal>S</literal> is (transitively) contained in the supertypes (superclasses or implemented interfaces) of the class:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mo>=</mo><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>TClass</mtext></mstyle><mi> </mi><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mfenced close="]" open="["><mrow><mi>T</mi><mi>C</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi></mrow></mfenced><mi> </mi><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></mfrac><mfenced close="]" open="["><mrow><mi>s</mi><mi>h</mi><mi>o</mi><mi>r</mi><mi>t</mi><mi>c</mi><mi>u</mi><mi>t</mi></mrow></mfenced><mtext>
</mtext></math>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>Γ</mi><mo></mo><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mo>.</mo><mi>s</mi><mi>u</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>TClass</mtext></mstyle><mi> </mi><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mfenced close="]" open="["><mrow><mi>T</mi><mi>C</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi></mrow></mfenced><mi> </mi><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></mfrac></math>
<requirement xml:id="IDE-44">
<title>Implicit Supertype of Classes</title>
<simpara>
<anchor xml:id="Req-IDE-44" xreflabel="[Req-IDE-44]"/>
<emphasis role="strong">Requirement: IDE-44:</emphasis>
<link linkend="Req-IDE-44">Implicit Supertype of Classes</link> (ver. 1)</simpara>
<simpara>
1. The implicit supertype of all classes is <literal>N4Object</literal>.
All classes with no explicit supertype are inherited from <literal>N4Object</literal>.
2. If the supertype is explicitly set to <literal>Object</literal>, then the class is not derived from <literal>N4Object</literal>.
Meta-information is created similar to an <literal>N4Object</literal>-derived class.
Usually, there is no reason to explicitly derive a class from <literal>Object</literal>.
3. External classes are implicitly derived from , unless they are annotated with <literal>@N4JS</literal>(cf.<xref linkend="_external-declarations"/>).</simpara>
</requirement>
</section>
<section xml:id="_final-modifier">
<title>Final Modifier</title>
<simpara>Extensibility refers to whether a given classifier can be subtyped.
Accessibility is a prerequisite for extensibility.
If a type cannot be seen, it cannot be subclassed.
The only modifier influencing the extensibility directly is the annotation <literal>@Final</literal>, which prevents all subtyping.
The following table shows how to prevent other projects or vendors from subtyping by also restricting the accessibility of the constructor:</simpara>
<table frame="all" rowsep="1" colsep="1">
<title>Extensibility of Types</title>
<tgroup cols="4">
<colspec colname="col_1" colwidth="62.5*"/>
<colspec colname="col_2" colwidth="12.5*"/>
<colspec colname="col_3" colwidth="12.5*"/>
<colspec colname="col_4" colwidth="12.5*"/>
<thead>
<row>
<entry align="center" valign="top">Type <literal>C</literal> Settings</entry>
<entry align="center" valign="top" namest="col_2" nameend="col_4">Subclassed in</entry>
</row>
</thead>
<tbody>
<row>
<entry align="left" valign="top"></entry>
<entry align="center" valign="top"><simpara><emphasis role="strong">Project</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis role="strong">Vendor</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis role="strong">World</emphasis></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>C.final</literal></simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>C.ctor.accessModifier=\lenum{project}</literal></simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>C.ctor.accessModifier=\lenum{public@Internal}</literal></simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>yes</simpara></entry>
<entry align="center" valign="top"><simpara>no</simpara></entry>
</row>
</tbody>
</tgroup>
</table>
<simpara>Since interfaces are always to be implemented, they must not be declared final.</simpara>
</section>
<section xml:id="_abstract-classes">
<title>Abstract Classes</title>
<simpara>A class with modifier <literal>abstract</literal> is called an <emphasis>abstract class</emphasis> and has its <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi></math> property set to true.
Other classes are called <emphasis>concrete</emphasis> classes.</simpara>
<requirement xml:id="IDE-45">
<title>Abstract Class</title>
<simpara>
<anchor xml:id="Req-IDE-45" xreflabel="[Req-IDE-45]"/>
<emphasis role="strong">Requirement: IDE-45:</emphasis>
<link linkend="Req-IDE-45">Abstract Class</link> (ver. 1)</simpara>
<simpara>
</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>A class <literal>C</literal> must be declared abstract if it owns or inherits one or more abstract members and neither C nor any interfaces implemented by C implements these members.
A concrete class has to, therefore, implement all abstract members of its superclasses’ implemented interfaces.
Note that a class may implement fields with field accessors and vice versa.</simpara>
</listitem>
<listitem>
<simpara>An abstract class may not be instantiated.</simpara>
</listitem>
<listitem>
<simpara>An abstract class cannot be set to final (with annotation <literal>@Final</literal>).</simpara>
</listitem>
</orderedlist>
</requirement>
<requirement xml:id="IDE-46">
<title>Abstract Member</title>
<simpara>
<anchor xml:id="Req-IDE-46" xreflabel="[Req-IDE-46]"/>
<emphasis role="strong">Requirement: IDE-46:</emphasis>
<link linkend="Req-IDE-46">Abstract Member</link> (ver. 1)</simpara>
<simpara>
</simpara>
<sidebar>
<simpara><link xl:href="https://github.com/eclipse/n4js/issues/1047"><inlinemediaobject>
<imageobject>
<imagedata fileref="images/issue.svg"/>
</imageobject>
<textobject><phrase></phrase></textobject>
</inlinemediaobject> #1047</link></simpara>
</sidebar>
<orderedlist numeration="arabic">
<listitem>
<simpara>A member declared as abstract must not have a method body (in contrary a method not declared as abstract have to have a method body).</simpara>
</listitem>
<listitem>
<simpara>Only methods, getters and setters can be declared as abstract (fields cannot be abstract).</simpara>
</listitem>
<listitem>
<simpara>It is not possible to inherit from an abstract class which contains abstract members which are not visible in the subclass.</simpara>
</listitem>
<listitem>
<simpara>An abstract member must not be set to final (with annotation <literal>@Final</literal>).</simpara>
</listitem>
<listitem>
<simpara>Static members must not be declared abstract.</simpara>
</listitem>
</orderedlist>
</requirement>
<simpara>Remarks:</simpara>
<itemizedlist>
<listitem>
<simpara>We decided to disallow abstract static members, since we cannot guarantee that a static members is not accessed in all cases</simpara>
</listitem>
<listitem>
<simpara>Only static members can override static members and only instance members can override other instance members of course.</simpara>
</listitem>
<listitem>
<simpara>An abstract member must not be declared in a final class (i.e. a class annotated with <literal>@Final</literal>). This is not explicitly defined as constraint in <xref linkend="Req-IDE-46"/> since abstract classes must not defined final anyway. We also do not produce error message for abstract members in final classes since these errors would be consequential errors.</simpara>
</listitem>
</itemizedlist>
<warning>
<simpara>Abstract members might be declared private, as they can be accessed from within the module.
This is to be changed in order to be aligned with TypeScript, cf. <link xl:href="https://github.com/eclipse/n4js/issues/1221"><inlinemediaobject>
<imageobject>
<imagedata fileref="images/issue.svg"/>
</imageobject>
<textobject><phrase></phrase></textobject>
</inlinemediaobject> #1221</link>. However we also want to add class expressions&#8201;&#8212;&#8201;and then the abstract members may be accessed (and overridden) in nested classes created by means of class expressions.</simpara>
</warning>
</section>
<section xml:id="_non-instantiable-classes">
<title>Non-Instantiable Classes</title>
<simpara>To make a class non-instantiable outside a defining compilation unit, i.e. disallow creation of instances for this class, simply declare the constructor as private.
This can be used for singletons.</simpara>
</section>
<section xml:id="_superclass">
<title>Superclass</title>
<requirement xml:id="IDE-47">
<title>Superclass</title>
<simpara>
<anchor xml:id="Req-IDE-47" xreflabel="[Req-IDE-47]"/>
<emphasis role="strong">Requirement: IDE-47:</emphasis>
<link linkend="Req-IDE-47">Superclass</link> (ver. 1)</simpara>
<simpara>
For a class <literal>C</literal> with a supertype <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mo>=</mo><mi>C</mi><mo>.</mo><mi>s</mi><mi>u</mi><mi>p</mi></math>, the following constraints must hold;</simpara>
<itemizedlist>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi><mo>.</mo><mi>s</mi><mi>u</mi><mi>p</mi></math> must reference a class declaration <literal>S</literal></simpara>
</listitem>
<listitem>
<simpara><literal>S</literal> must be be extendable in the project of <literal>C</literal></simpara>
</listitem>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi><mo></mo><mi>C</mi><mo>.</mo><mi>s</mi><mi>u</mi><msup><mi>p</mi><mo>+</mo></msup></math></simpara>
</listitem>
<listitem>
<simpara>All abstract members in <literal>S</literal> must be accessible from <literal>C</literal>:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>M</mi><mo></mo><mi>S</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi><mi>M</mi><mo>.</mo><mi>a</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi><mo></mo></math><?asciidoc-br?>
<literal>M</literal> is accessible from <literal>C</literal>.<?asciidoc-br?>
Note that <literal>M</literal> need not be an owned member of <literal>S</literal> and that this constraint applies even if <literal>C</literal> is abstract).</simpara>
</listitem>
</itemizedlist>
<simpara>All members of superclasses become members of a class.
This is true even if the owning classes are not directly accessible to a class.
The member-specific access control is not changed.</simpara>
</requirement>
</section>
</section>
<section xml:id="_interfaces" role="language-n4js">
<title>Interfaces</title>
<section xml:id="_definition-of-interfaces">
<title>Definition of Interfaces</title>
<section xml:id="interfaces-syntax">
<title>Syntax</title>
<formalpara>
<title>Syntax N4 Interface Declaration</title>
<para>
<programlisting language="xtext" linenumbering="unnumbered">N4InterfaceDeclaration &lt;Yield&gt;:
=&gt; (
{N4InterfaceDeclaration}
annotations+=Annotation*
(declaredModifiers+=N4Modifier)*
'interface' typingStrategy=TypingStrategyDefSiteOperator? name=BindingIdentifier&lt;Yield&gt;?
)
TypeVariables?
InterfaceImplementsList?
Members&lt;Yield&gt;
;
fragment InterfaceImplementsList*:
'implements' superInterfaceRefs+=ParameterizedTypeRefNominal
(',' superInterfaceRefs+=ParameterizedTypeRefNominal)*
;</programlisting>
</para>
</formalpara>
</section>
<section xml:id="interfaces-properties">
<title>Properties</title>
<simpara>These are the additional properties of interfaces, which can be specified by the user:</simpara>
<variablelist>
<varlistentry>
<term><literal>superInterfaces</literal></term>
<listitem>
<simpara>Collection of interfaces extended by this interface; empty by default.
Instead of <literal>superInterfaces</literal>, we simply write <literal>interfaces</literal>.</simpara>
</listitem>
</varlistentry>
</variablelist>
</section>
<section xml:id="interfaces-type-inference">
<title>Type Inference</title>
<simpara>The type of an interface declaration <literal>I</literal> is of type <literal>type{I}</literal>:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mi>I</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>type</mtext></mstyle><mfenced close="}" open="{"><mi>I</mi></mfenced></mrow></mfrac></math>
</section>
<section xml:id="interfaces-semantics">
<title>Semantics</title>
<simpara>Interfaces are used to describe the public <link linkend="_acronyms">API</link> of a classifier.
The main requirement is that the instance of an interface, which must be an instance of a class since interfaces cannot have instances, provides all members declared in the interface.
Thus, a (concrete) class implementing an interface must provide implementations for all the fields, methods, getters and setters of the interface (otherwise it the class must be declared abstract).
The implementations have to be provided either directly in the class itself, through a superclass, or by the interface if the member has a default implementation.</simpara>
<simpara>A field declaration in an interface denotes that all implementing classes can either provide a field of the same name and the same(!) type or corresponding field accessors.
If no such members are defined in the class or a (transitive) superclass, the field is mixed in from the interface automatically.
This is also true for the initializer of the field.</simpara>
<simpara>All instance methods, getters and setters declared in an interface are implicitly abstract if they do not provide a default implementation.
The modifier <literal>abstract</literal> is not required, therefore, in the source code.
The following constraints apply:</simpara>
<requirement xml:id="IDE-48">
<title>Interfaces</title>
<simpara>
<anchor xml:id="Req-IDE-48" xreflabel="[Req-IDE-48]"/>
<emphasis role="strong">Requirement: IDE-48:</emphasis>
<link linkend="Req-IDE-48">Interfaces</link> (ver. 1)</simpara>
<simpara>
For any interface <literal>I</literal>, the following must hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>Interfaces may not be instantiated.</simpara>
</listitem>
<listitem>
<simpara>Interfaces cannot be set to final (with annotation @Final): <math xmlns="http://www.w3.org/1998/Math/MathML"><mo>¬</mo><mi>I</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi></math>.</simpara>
</listitem>
<listitem>
<simpara>Members of an interface must not be declared private.
The default access modifier in interfaces is the the type’s visibility or <literal>project</literal>, if the type’s visibility is <literal>private</literal>.</simpara>
</listitem>
<listitem>
<simpara>Members of an interface, except methods, must not be declared <literal>@Final</literal>:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mo></mo><mi>m</mi><mo></mo><mi>I</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>:</mi><mi>m</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi><mo></mo><mi>m</mi><mo></mo><mi>I</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><mi>s</mi></math>
<note>
<simpara>not allowing field accessors to be declared final was a deliberate decision, because it would complicate the internal handling of member redefinition; might be reconsidered at a later time</simpara>
</note>
</listitem>
<listitem>
<simpara>The literal may not be used in the initializer expression of a field of an interface.<?asciidoc-br?>
This restriction is required, because the order of implementation of these fields in an implementing class cannot be guaranteed.
This applies to both instance and static fields in interfaces, but in case of static fields, <literal>this</literal> is also disallowed due to <xref linkend="_static-members-of-interfaces"/>.</simpara>
</listitem>
</orderedlist>
</requirement>
<simpara>It is possible to declare members in interfaces with a smaller visibility as the interface itself.
In that case, clients of the interface may be able to use the interface but not to implement it.</simpara>
<simpara>In order to simplify modeling of runtime types, such as elements, interfaces do not only support the notation of static methods but constant data fields as well.
Since <link linkend="_acronyms">IDL</link> [<link linkend="OMG14a">OMG14a</link>] is used to describe these elements in specifications (and mapped to JavaScript via rules described in [<link linkend="W3C12a">W3C12a</link>])
constant data fields are an often-used technique there and they can be modeled in N4JS 1:1.</simpara>
<simpara>As specified in <xref linkend="Req-IDE-56"/>, interfaces cannot contain a constructor i.e.<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>m</mi><mo></mo><mi>I</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><mi>s</mi><mi>:</mi><mi>m</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><msup><mo></mo><mi>'</mi></msup><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>o</mi><msup><mi>r</mi><mi>'</mi></msup></math>.</simpara>
<example>
<title>Simple Interfaces</title>
<simpara>The following example shows the syntax for defining interfaces.
The second interface extends the first one.
Note that methods are implicitly defined abstract in interfaces.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">interface I {
foo(): void
}
interface I2 extends I {
someText: string;
bar(): void
}</programlisting>
</example>
<simpara>If a classifier <literal>C</literal> <emphasis>implements</emphasis> an interface <literal>I</literal>, we say <literal>I</literal> is <emphasis>implemented</emphasis> by <literal>C</literal>.
If <literal>C</literal> redefines members declared in <literal>I</literal>, we say that these members are <emphasis>implemented</emphasis> by <literal>C</literal>.
Members not redefined by <literal>C</literal> but with a default implementations are <emphasis>mixed in</emphasis> or <emphasis>consumed by</emphasis> <literal>C</literal>.
We all cases we call <literal>C</literal> the <emphasis>implementor</emphasis>.</simpara>
<simpara>Besides the general constraints described in <xref linkend="_common-semantics-of-classifiers"/>, the following constraints must hold for extending or implementing interfaces:</simpara>
<requirement xml:id="IDE-49">
<title>Extending Interfaces</title>
<simpara>
<anchor xml:id="Req-IDE-49" xreflabel="[Req-IDE-49]"/>
<emphasis role="strong">Requirement: IDE-49:</emphasis>
<link linkend="Req-IDE-49">Extending Interfaces</link> (ver. 1)</simpara>
<simpara>
For a given type
<literal>I</literal>, and <math xmlns="http://www.w3.org/1998/Math/MathML"><mfenced close="}" open="{"><msub><mi>I</mi><mn>1</mn></msub><mrow><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></mrow><msub><mi>I</mi><mi>n</mi></msub></mfenced></math> directly extended by <literal>I</literal>, the following constraints must be true:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>Only interfaces can extend interfaces: <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>I</mi><mo>,</mo><msub><mi>I</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>I</mi><mi>n</mi></msub></math> must be interfaces.</simpara>
</listitem>
<listitem>
<simpara>An interface may not directly extend the same interface more than once:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>I</mi><mi>i</mi></msub><mo>=</mo><msub><mi>I</mi><mi>j</mi></msub><mo></mo><mi>i</mi><mo>=</mo><mi>j</mi></math> for any
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi><mo>,</mo><mi>j</mi><mo></mo><mfenced close="}" open="{"><mrow><mn>1</mn><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mi>n</mi></mrow></mfenced></math>.</simpara>
</listitem>
<listitem>
<simpara>An interface may (indirectly) extend the same interface <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>J</mi></math> more than once only if</simpara>
<orderedlist numeration="loweralpha">
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>J</mi></math> is not parameterized, or</simpara>
</listitem>
<listitem>
<simpara>in all cases <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>J</mi></math> is extended with the same type arguments for all invariant type parameters.<?asciidoc-br?>
Note that for type parameters of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>J</mi></math> that are declared covariant or contravariant on definition site, different type arguments may be used.</simpara>
</listitem>
</orderedlist>
</listitem>
<listitem>
<simpara>All abstract members in <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>I</mi><mi>i</mi></msub></math>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi><mo></mo><mfenced close="}" open="{"><mn>1</mn><mrow><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></mrow><mi>n</mi></mfenced></math>, must be accessible from <literal>I</literal>:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>i</mi><mo></mo><mfenced close="}" open="{"><mn>1</mn><mrow><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></mrow><mi>n</mi></mfenced><mi>:</mi><mi>M</mi><mo></mo><msub><mi>I</mi><mi>i</mi></msub><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo></mo><mi>M</mi><mo>.</mo><mi>a</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi><mo></mo></math> <literal>M</literal> is accessible from <literal>I</literal>.<?asciidoc-br?>
Note that <literal>M</literal> need not be an owned member of <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>I</mi><mi>i</mi></msub></math>.</simpara>
</listitem>
</orderedlist>
</requirement>
<requirement xml:id="IDE-50">
<title>Implementing Interfaces</title>
<simpara>
<anchor xml:id="Req-IDE-50" xreflabel="[Req-IDE-50]"/>
<emphasis role="strong">Requirement: IDE-50:</emphasis>
<link linkend="Req-IDE-50">Implementing Interfaces</link> (ver. 1)</simpara>
<simpara>
</simpara>
<simpara>For a given type
<literal>C</literal>, and <math xmlns="http://www.w3.org/1998/Math/MathML"><mfenced close="}" open="{"><msub><mi>I</mi><mn>1</mn></msub><mrow><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></mrow><msub><mi>I</mi><mi>n</mi></msub></mfenced></math> directly implemented
by <literal>C</literal>, the following constraints must be true:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>Only classes can implement interfaces: <literal>C</literal> must be a Class.</simpara>
</listitem>
<listitem>
<simpara>A class can only implement interfaces: <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>I</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>I</mi><mi>n</mi></msub></math> must be interfaces.</simpara>
</listitem>
<listitem>
<simpara>A class may not directly implement the same interface more than once:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>I</mi><mi>i</mi></msub><mo>=</mo><msub><mi>I</mi><mi>j</mi></msub><mo></mo><mi>i</mi><mo>=</mo><mi>j</mi></math> for any <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi><mo>,</mo><mi>j</mi><mo></mo><mfenced close="}" open="{"><mn>1</mn><mrow><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></mrow><mi>n</mi></mfenced></math>.</simpara>
</listitem>
<listitem>
<simpara>A class may (indirectly) implement the same interface <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>J</mi></math> more than once only if</simpara>
<orderedlist numeration="loweralpha">
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>J</mi></math> is not parameterized, or</simpara>
</listitem>
<listitem>
<simpara>in all cases <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>J</mi></math> is implemented with the same type arguments for all invariant type parameters.<?asciidoc-br?>
Note that for type parameters of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>J</mi></math> that are declared covariant or contravariant on definition site, different type arguments may be used.</simpara>
</listitem>
</orderedlist>
</listitem>
<listitem>
<simpara>All abstract members in <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>I</mi><mi>i</mi></msub></math>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi><mo></mo><mfenced close="}" open="{"><mn>1</mn><mrow><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></mrow><mi>n</mi></mfenced></math>, must be accessible from <literal>C</literal>:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>i</mi><mo></mo><mfenced close="}" open="{"><mn>1</mn><mrow><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></mrow><mi>n</mi></mfenced><mi>:</mi><mi>M</mi><mo></mo><msub><mi>I</mi><mi>i</mi></msub><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo></mo><mi>M</mi><mo>.</mo><mi>a</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi><mo></mo></math> <literal>M</literal> is accessible from <literal>C</literal>.<?asciidoc-br?>
Note that <literal>M</literal> need not be an owned member of <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>I</mi><mi>i</mi></msub></math>.</simpara>
</listitem>
</orderedlist>
</requirement>
<simpara>For default methods in interfaces, see <xref linkend="_default-methods-in-interfaces"/>.</simpara>
</section>
</section>
</section>
<section xml:id="_generic-classifiers" role="language-n4js">
<title>Generic Classifiers</title>
<simpara>Classifiers can be declared generic by defining a type parameter via <literal>type-param</literal>.</simpara>
<definition>
<title>Generic Classifiers</title>
<simpara>
<anchor xml:id="generic_classifiers" xreflabel="[generic_classifiers]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="generic_classifiers">Generic Classifiers</link></simpara>
<simpara>
A generic classifier is a classifier with at least one type parameter.
That is, a given classifier <literal>C</literal> is generic if and only if <math xmlns="http://www.w3.org/1998/Math/MathML"><mo>|</mo><mi>C</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>P</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>|</mo><mo></mo><mn>1</mn></math>.</simpara>
<simpara>If a classifier does not define any type parameters, it is not generic, even if its superclass or any implemented interface is generic.</simpara>
<simpara>The format of the type parameter expression is described in <xref linkend="_parameterized-types"/>.
The type variable defined by the type parameter’s type expression can be used just like a normal type inside the class definition.</simpara>
<simpara>If using a generic classifier as type of a variable, it may be parameterized.
This is usually done via a type expression (cf. <xref linkend="_parameterized-types"/>) or via <literal>typearg</literal> in case of supertypes.
If a generic classifier defines multiple type variables, these variables are bound in the order of their definition.
In any case, all type variables have to be bound.
That means in particular that raw types are not allowed. (cf <xref linkend="_parameterized-types"/> for details).</simpara>
<simpara>If a generic classifier is used as super classifier, the type arguments can be type variables.
Note that the type variable of the super classifier is not lifted, that is to say that all type variables are to be explicitly bound in the type references used in the <literal>extend</literal>, <literal>with</literal>, or <literal>implements</literal> section using <literal>typearg</literal>.
If a type variable is used in <literal>typearg</literal> to bound a type variable of a type parameter, it has to fulfil possible type constraints (upper/lower bound) specified in the type parameter.</simpara>
</definition>
<example>
<title>Generic Type Definition and Usage as Type of Variable</title>
<simpara>This example demonstrates how to define a generic type and how to refer to it in a variable definition.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">export class Container&lt;T&gt; {
private item: T;
getItem(): T {
return this.item;
}
setItem(item: T): void {
this.item = item;
}
}</programlisting>
</example>
<simpara>This type can now be used as a type of a variable as follows</simpara>
<programlisting language="n4js" linenumbering="unnumbered">import Container from "p/Container"
var stringContainer: Container&lt;string&gt; = new Container&lt;string&gt;();
stringContainer.setItem("Hello");
var s: string = stringContainer.getItem();</programlisting>
<simpara>In line 3, the type variable <literal>T</literal> of the generic class <literal>Container</literal> is bound to <literal>string</literal>.</simpara>
<example>
<title>Binding of type variables with multiple types</title>
<simpara>For a given generic class <literal>G</literal></simpara>
<programlisting language="n4js" linenumbering="unnumbered">class A{}
class B{}
class C extends A{}
class G&lt;S, T extends A, U extends B&gt; {
}</programlisting>
<simpara>the variable definition</simpara>
<programlisting language="n4js" linenumbering="unnumbered">var x: G&lt;Number,C,B&gt;;</programlisting>
<simpara>would bind the type variables as follows:</simpara>
<informaltable frame="all" rowsep="1" colsep="1">
<tgroup cols="3">
<colspec colname="col_1" colwidth="16.6666*"/>
<colspec colname="col_2" colwidth="16.6666*"/>
<colspec colname="col_3" colwidth="66.6668*"/>
<tbody>
<row>
<entry align="center" valign="top"><simpara><literal>S</literal></simpara></entry>
<entry align="center" valign="top"><simpara><literal>Number</literal></simpara></entry>
<entry align="center" valign="top"><simpara>Bound by first type argument, no bound constraints defined for <literal>S</literal>.</simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><literal>T</literal></simpara></entry>
<entry align="center" valign="top"><simpara><literal>C</literal></simpara></entry>
<entry align="center" valign="top"><simpara>Bound by second type argument, <literal>C</literal> must be a subtype of in order to fulfill the type constraint.</simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><literal>U</literal></simpara></entry>
<entry align="center" valign="top"><simpara><literal>B</literal></simpara></entry>
<entry align="center" valign="top"><simpara>Bound by third type argument, <literal>extends</literal> is reflexive, that is <literal>B</literal> fulfills the
type constraint.</simpara></entry>
</row>
</tbody>
</tgroup>
</informaltable>
</example>
<requirement xml:id="IDE-51">
<title>Generic Superclass, Type Argument with Type Variable</title>
<simpara>
<anchor xml:id="Req-IDE-51" xreflabel="[Req-IDE-51]"/>
<emphasis role="strong">Requirement: IDE-51:</emphasis>
<link linkend="Req-IDE-51">Generic Superclass, Type Argument with Type Variable</link> (ver. 1)</simpara>
<simpara>
For a given generic superclass <literal>SuperClass</literal></simpara>
<programlisting language="n4js" linenumbering="unnumbered">class SuperClass&lt;S, T extends A, U extends B&gt; {};</programlisting>
<simpara>and a generic subclass <literal>SubClass</literal></simpara>
<programlisting language="n4js" linenumbering="unnumbered">class SubClass&lt;X extends A&gt; extends SuperClass&lt;Number, X, B&gt; {..};</programlisting>
<simpara>the variable definition</simpara>
<programlisting language="n4js" linenumbering="unnumbered">var s: SubClass&lt;C&gt;;</programlisting>
<simpara>would bind the type variables as follows:<?asciidoc-br?></simpara>
<informaltable frame="all" rowsep="1" colsep="1">
<tgroup cols="3">
<colspec colname="col_1" colwidth="16.6666*"/>
<colspec colname="col_2" colwidth="16.6666*"/>
<colspec colname="col_3" colwidth="66.6668*"/>
<thead>
<row>
<entry align="center" valign="top">TypeVariable</entry>
<entry align="center" valign="top">Bound to</entry>
<entry align="center" valign="top">Explanation</entry>
</row>
</thead>
<tbody>
<row>
<entry align="center" valign="top"><simpara><literal>SuperClass.S</literal></simpara></entry>
<entry align="center" valign="top"><simpara><literal>Number</literal></simpara></entry>
<entry align="left" valign="top"><simpara>Type variable <literal>s</literal> of supertype <literal>SuperClass</literal> is bound to <literal>Number</literal>.</simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><literal>SuperClass.T</literal></simpara></entry>
<entry align="center" valign="top"><simpara><literal>SubClass.X=C</literal></simpara></entry>
<entry align="left" valign="top"><simpara>Type variable <literal>T</literal> of supertype <literal>SuperClass</literal> is bound to type variable <literal>X</literal> of <literal>SubClass</literal>. It gets
then indirectly bound to <literal>C</literal> as specified by the type argument of the
variable definition.</simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><literal>SuperClass.U</literal></simpara></entry>
<entry align="center" valign="top"><simpara><literal>B</literal></simpara></entry>
<entry align="left" valign="top"><simpara>Type variable <literal>U</literal> of supertype <literal>SuperClass</literal> is auto-bound to <literal>C</literal> as no explicit binding for the third type variable is specified.</simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><literal>SubClass.X</literal></simpara></entry>
<entry align="center" valign="top"><simpara><literal>C</literal></simpara></entry>
<entry align="left" valign="top"><simpara>Bound by first type argument specified in variable definition.</simpara></entry>
</row>
</tbody>
</tgroup>
</informaltable>
</requirement>
</section>
<section xml:id="sec:definition-site-variance" role="language-n4js">
<title>Definition-Site Variance</title>
<simpara>In addition to use-site declaration of variance in the form of Java-like wildcards, N4JS provides support for definition-site declaration of variance as known from languages such as C# and Scala.</simpara>
<simpara>The <emphasis>variance</emphasis> of a parameterized type states how its subtyping relates to its type arguments’ subtyping.
For example, given a parameterized type <literal>G&lt;T&gt;</literal> and plain types <literal>A</literal> and <literal>B</literal>, we know</simpara>
<itemizedlist>
<listitem>
<simpara>if <literal>G</literal> is <emphasis role="strong">covariant</emphasis> w.r.t. its parameter <literal>T</literal>, then</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>B</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>A</mtext></mstyle><mo></mo><mstyle mathvariant="monospace"><mtext>G</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>B</mtext></mstyle><mo>&gt;</mo><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>G</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>A</mtext></mstyle><mo>&gt;</mo></math></simpara>
</listitem>
<listitem>
<simpara>if <literal>G</literal> is <emphasis role="strong">contravariant</emphasis> w.r.t. its parameter <literal>T</literal>, then</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>B</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>A</mtext></mstyle><mo></mo><mstyle mathvariant="monospace"><mtext>G</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>A</mtext></mstyle><mo>&gt;</mo><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>G</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>B</mtext></mstyle><mo>&gt;</mo></math></simpara>
</listitem>
<listitem>
<simpara>if <literal>G</literal> is <emphasis role="strong">invariant</emphasis> w.r.t. its parameter <literal>T</literal>, then</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>B</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>A</mtext></mstyle><mo></mo><mstyle mathvariant="monospace"><mtext>G</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>A</mtext></mstyle><mo>&gt;</mo><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>G</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>B</mtext></mstyle><mo>&gt;</mo></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>B</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>A</mtext></mstyle><mo></mo><mstyle mathvariant="monospace"><mtext>G</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>A</mtext></mstyle><mo>&gt;</mo><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>G</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>B</mtext></mstyle><mo>&gt;</mo></math></simpara>
</listitem>
</itemizedlist>
<simpara>Note that variance is declared per type parameter, so a single parameterized type with more than one type parameter may be, for example, covariant w.r.t. one type parameter and contravariant w.r.t. another.</simpara>
<simpara>Strictly speaking, a type parameter/variable itself is not co- or contravariant;<?asciidoc-br?>
however, for the sake of simplicity we say <emphasis><literal>T</literal> is covariant</emphasis> as a short form for <emphasis><literal>G</literal> is covariant with respect to its type parameter <literal>T</literal></emphasis> (for contravariant and invariant accordingly).</simpara>
<simpara>To declare the variance of a parameterized classifier on definition site, simply add keyword <literal>in</literal> or <literal>out</literal> before the corresponding type parameter:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class ReadOnlyList&lt;out T&gt; { // covariance
// ...
}
interface Consumer&lt;in T&gt; { // contravariance
// ...
}</programlisting>
<simpara>In such cases, the following constraints apply.</simpara>
<requirement xml:id="IDE-174">
<title>Definition-Site Variance</title>
<simpara>
<anchor xml:id="Req-IDE-174" xreflabel="[Req-IDE-174]"/>
<emphasis role="strong">Requirement: IDE-174:</emphasis>
<link linkend="Req-IDE-174">Definition-Site Variance</link> (ver. 1)</simpara>
<simpara>
Given a parameterized type with a type parameter , the following must hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara><literal>T</literal> may only appear in variance-compatible positions:</simpara>
<orderedlist numeration="loweralpha">
<listitem>
<simpara>if <literal>T</literal> is declared on definition site to be <emphasis role="strong">covariant</emphasis>, then it may only appear in covariant positions within the type’s non-private member declarations.</simpara>
</listitem>
<listitem>
<simpara>if <literal>T</literal> is declared on definition site to be <emphasis role="strong">contravariant</emphasis>, then it may only appear in contravariant positions within the type’s non-private member declarations.</simpara>
</listitem>
<listitem>
<simpara>if <literal>T</literal> is <emphasis role="strong">invariant</emphasis>, i.e. neither declared covariant nor declared contravariant on definition site, then it may appear in any position (where type variables are allowed).</simpara>
<simpara>Thus, no restrictions apply within the declaration of private members and within the body of field accessors and methods.</simpara>
</listitem>
</orderedlist>
</listitem>
<listitem>
<simpara>definition-site variance may not be combined with incompatible use-site variance:</simpara>
<orderedlist numeration="loweralpha">
<listitem>
<simpara>if <literal>T</literal> is declared on definition site to be <emphasis role="strong">covariant</emphasis>, then no wildcard with a <emphasis role="strong">lower</emphasis> bound may be provided as type argument for <literal>T</literal>.</simpara>
</listitem>
<listitem>
<simpara>if <literal>T</literal> is declared on definition site to be <emphasis role="strong">contravariant</emphasis>, then no wildcard with an <emphasis role="strong">upper</emphasis> bound may be provided as type argument for <literal>T</literal>.</simpara>
</listitem>
<listitem>
<simpara>if <literal>T</literal> is <emphasis role="strong">invariant</emphasis>, i.e. neither declared covariant nor declared contravariant on definition site, then any kind of wildcard may be provided as type argument.</simpara>
<simpara>Unbounded wildcards are allowed in all cases.</simpara>
</listitem>
</orderedlist>
</listitem>
</orderedlist>
</requirement>
<example xml:id="ex:use-site-declaration-variance">
<title>Use-site declaration of variance</title>
<simpara>For illustration purposes, let’s compare use-site and definition-site declaration of variance.
Since use-site variance is more familiar to the Java developer, we start with this flavor.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class Person {
name: string;
}
class Employee extends Person {}
interface List&lt;T&gt; {
add(elem: T)
read(idx: int): T
}
function getNameOfFirstPerson(list: List&lt;? extends Person&gt;): string {
return list.read(0).name;
}</programlisting>
<simpara>Function <literal>getNameOfFirstPerson</literal> below takes a list and returns the name of the first person in the list.
Since it never adds new elements to the given list, it could accept <literal>List</literal>s of any subtype of <literal>Person</literal>, for example a <literal>List&lt;Employee&gt;</literal>.
To allow this, its formal parameter has a type of <literal>List&lt;? extends Person&gt;</literal> instead of <literal>List&lt;Person&gt;</literal>.
Such use-site variance is useful whenever an invariant type, like <literal>List</literal> above, is being used in a way such that it can be treated as if it were co- or contravariant.</simpara>
<simpara>Sometimes, however, we are dealing with types that are inherently covariant or contravariant, for example an <literal>ImmutableList</literal> from which we can only read elements would be covariant.
In such a case, use-site declaration of variance is tedious and error-prone: we would have to declare the variance wherever the type is being used and would have to
make sure not to forget the declaration or otherwise limit the flexibility and reusability of the code (for example, in the above code we could not call <literal>getNameOfFirstPerson</literal> with a <literal>List&lt;Employee&gt;</literal>).</simpara>
<simpara>The solution is to declare the variance on declaration site, as in the following code sample:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">interface ImmutableList&lt;out T&gt; {
// add(elem: T) // error: such a method would now be disallowed
read(idx: int): T
}
function getNameOfFirstPerson2(list: ImmutableList&lt;Person&gt;): string {
return list.read(0).name;
}</programlisting>
<simpara>Now we can invoke <literal>getNameOfFirstPerson2</literal> with a <literal>List&lt;Employee&gt;</literal> even though the implementor of <literal>getNameOfFirstPerson2</literal> did not add a
use-site declaration of covariance, because the type <literal>ImmutableList</literal> is declared to be covariant with respect to its parameter <literal>T</literal>, and this applies globally
throughout the program.</simpara>
</example>
</section>
</section>
<section xml:id="_members">
<title>Members</title>
<simpara>A member is either a method (which may be a special constructor function), a data field, or a getter or a setter.
The latter two implicitly define an accessor field.
Similar to object literals, there must be no data field with the same name as a getter or setter.</simpara>
<simpara>(overriding, implementation and consumption) is
described in <xref linkend="_redefinition-of-members"/>.</simpara>
<section xml:id="_syntax-4" role="language-n4js">
<title>Syntax</title>
<formalpara>
<title>Syntax N4JS member access modifier</title>
<para>
<programlisting language="xtext" linenumbering="unnumbered">enum N4JSMemberAccessModifier: private | project | protected | public;
N4MemberDeclaration: N4MethodDeclaration | N4FieldDeclaration | N4GetterDeclaration | N4SetterDeclaration;</programlisting>
</para>
</formalpara>
<section xml:id="_properties-3">
<title>Properties</title>
<simpara>Members share the following properties:</simpara>
<variablelist>
<varlistentry>
<term><literal>annotations</literal> </term>
<listitem>
<simpara>Arbitrary annotations, see <xref linkend="_annotations"/> for details.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>accessModifier</literal> </term>
<listitem>
<simpara>N4JS member access modifier: <literal>private</literal>, <literal>project</literal>, <literal>potected</literal>, or <literal>public</literal>; the latter two can be combined with <literal>@Internal</literal>; default is <literal>project</literal> for classes and private interfaces.
For a non-private interface defaults to the interface’s visibility.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>name</literal> </term>
<listitem>
<simpara>The simple name of the member, that is an identifier name (cf. <xref linkend="_valid-names"/>).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>static</literal> </term>
<listitem>
<simpara>Boolean property to distinguish instance from classifier members, see <xref linkend="_static-members"/>.</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>The following pseudo properties are defined via annotations:</simpara>
<variablelist>
<varlistentry>
<term><literal>deprecated</literal> </term>
<listitem>
<simpara>Boolean property set to true if annotation <literal>@Deprecated</literal> is set. <footnote><simpara>not yet implemented</simpara></footnote></simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>And we additionally define the following pseudo properties:</simpara>
<variablelist>
<varlistentry>
<term><literal>acc</literal> </term>
<listitem>
<simpara>Member access modifier as described in <xref linkend="_accessibility-of-members"/>, it is the aggregated value of
the <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>c</mi><mi>c</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>M</mi><mi>o</mi><mi>d</mi><mi>i</mi><mi>f</mi><mi>i</mi><mi>e</mi><mi>r</mi></math> and the <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><mi>x</mi><mi>p</mi><mi>o</mi><mi>r</mi><mi>t</mi></math> property.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>owner</literal> </term>
<listitem>
<simpara>Owner classifier of the member.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>typeRef</literal> </term>
<listitem>
<simpara>Type of the member—this is the type of a field or the type of the method which is a function type (and not the return type).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>assignability</literal> </term>
<listitem>
<simpara>Enumeration, may be one of the following values:</simpara>
<variablelist>
<varlistentry>
<term><literal>set</literal></term>
<listitem>
<simpara>Member may only be set, i.e. it could only be used on the left hand side of an assignment.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>get</literal></term>
<listitem>
<simpara>Member may only be retrieved, i.e. it could only be used on the right hand side of an assignment. This is the default setting for methods.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>any</literal></term>
<listitem>
<simpara> Member may be set or retrieved, i.e. it could only be used on the left or right hand side of an assignment.
This is the default setting for fields.</simpara>
</listitem>
</varlistentry>
</variablelist>
</listitem>
</varlistentry>
</variablelist>
<note>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>a</mi><mi>b</mi><mi>i</mi><mi>l</mi><mi>i</mi><mi>t</mi><mi>y</mi></math> is related but not equal to writable modifiers used for fields.
We define a partial order on this enumeration as follows:<?asciidoc-br?></simpara>
</note>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mo>&lt;</mo><mfenced close=")" open="("><mi>l</mi><mi>r</mi></mfenced><mspace width="3.0mm"/><mi>:</mi><mi>:</mi><mo>=</mo><mspace width="3.0mm"/><mfenced close="}" open="{"><mfenced close=")" open="("><mstyle mathvariant="monospace"><mtext>set</mtext></mstyle><mstyle mathvariant="monospace"><mtext>any</mtext></mstyle></mfenced><mfenced close=")" open="("><mstyle mathvariant="monospace"><mtext>get</mtext></mstyle><mstyle mathvariant="monospace"><mtext>any</mtext></mstyle></mfenced></mfenced></math>
<variablelist>
<varlistentry>
<term><literal>abstract</literal> </term>
<listitem>
<simpara>All members have a flag <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi></math>, which is user-defined for methods, getters and setter, but which is always false for fields.</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>The following pseudo property is set to make fields compatible with properties of an object literal, however it cannot be changed:</simpara>
<variablelist>
<varlistentry>
<term><literal>configurable</literal> </term>
<listitem>
<simpara>Boolean flag reflecting the property descriptor <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>o</mi><mi>n</mi><mi>f</mi><mi>i</mi><mi>g</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>b</mi><mi>l</mi><mi>e</mi></math>, this is always set to false for members.</simpara>
</listitem>
</varlistentry>
</variablelist>
</section>
</section>
<section xml:id="_semantics-3">
<title>Semantics</title>
<simpara>The members of a given classifier <literal>C</literal> must be named such that the following constraints are met:</simpara>
<requirement xml:id="IDE-52">
<title>Member Names</title>
<simpara>
<anchor xml:id="Req-IDE-52" xreflabel="[Req-IDE-52]"/>
<emphasis role="strong">Requirement: IDE-52:</emphasis>
<link linkend="Req-IDE-52">Member Names</link> (ver. 1)</simpara>
<simpara>
. The name of a member is given as an identifier, a string literal, a numeric literal, or as a computed property name with a compile-time expression (see <xref linkend="compile-time-expressions"/>). In particular, string literals, e.g. <literal>['myProp']</literal>, built-in symbols, e.g. <literal>[Symbol.iterator]</literal>, and literals of <literal>@StringBased</literal> enums are all valid computed property names.
. No two members may have the same name, except one is static and the
other is non-static:</simpara>
<simpara>+</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mo></mo><msub><mi>m</mi><mn>1</mn></msub><mo>,</mo><msub><mi>m</mi><mn>2</mn></msub><mo></mo><mi>C</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>,</mo><msub><mi>m</mi><mn>1</mn></msub><mo></mo><msub><mi>m</mi><mn>2</mn></msub><mi>:</mi><msub><mi>m</mi><mn>1</mn></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo></mo><msub><mi>m</mi><mn>2</mn></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo></mo><msub><mi>m</mi><mn>1</mn></msub><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo></mo><msub><mi>m</mi><mn>2</mn></msub><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi></mtd></mtr></mtable></math>
<orderedlist numeration="arabic">
<listitem>
<simpara>The member name must be a valid identifier name, see <link linkend="_identifier-names-and-identifiers">Identifier Grammar</link>.</simpara>
</listitem>
</orderedlist>
</requirement>
<simpara>Thus, over<emphasis>loading</emphasis> of methods is not supported <footnote><simpara>In order to emulate method overloading, union types are to be used.</simpara></footnote> and no field may have the same name as a method.
However, over<emphasis>riding</emphasis> of methods, getters, and setters are possible, see <xref linkend="_redefinition-of-members"/>.
Static members may also have the same name as non-static members.<footnote><simpara>[<link linkend="ECMA15a">ECMA15a(p.p214)</link>], <literal>ClassBody : ClassElementList</literal> indicates that it is possible to have the same name for instance and static members.</simpara></footnote></simpara>
<simpara>The dollar character <literal>$</literal> is not allowed for user-defined member identifiers as the dollar sign is used for rewriting private members.</simpara>
</section>
<section xml:id="_methods" role="language-n4js">
<title>Methods</title>
<simpara>Methods are simply JavaScript functions.
They are defined similarly to methods as proposed in [<link linkend="ECMA15a">ECMA15a(p.S13.5)</link>] except for the type information and some modifiers.</simpara>
<section xml:id="_syntax-5">
<title>Syntax</title>
<formalpara>
<title>Syntax Method Declaration</title>
<para>
<programlisting language="xtext" linenumbering="unnumbered">N4MethodDeclaration &lt;Yield&gt;:
=&gt; ({N4MethodDeclaration}
annotations+=Annotation*
accessModifier=N4JSMemberAccessModifier?
(abstract?=’abstract’ | static?=’static’)?
TypeVariables?
(
generator?='*' LiteralOrComputedPropertyName&lt;Yield&gt; -&gt; MethodParamsReturnAndBody &lt;Generator=true&gt;
| AsyncNoTrailingLineBreak LiteralOrComputedPropertyName&lt;Yield&gt; -&gt; MethodParamsReturnAndBody &lt;Generator=false&gt;
)
) ';'?
;
fragment MethodParamsAndBody &lt;Generator&gt;*:
StrictFormalParameters&lt;Yield=Generator&gt;
(body=Block&lt;Yield=Generator&gt;)?
;
fragment MethodParamsReturnAndBody &lt;Generator&gt;*:
StrictFormalParameters&lt;Yield=Generator&gt;
(':' returnTypeRef=TypeRef)?
(body=Block&lt;Yield=Generator&gt;)?
;
fragment LiteralOrComputedPropertyName &lt;Yield&gt;*:
name=IdentifierName | name=STRING | name=NumericLiteralAsString
| '[' (=&gt;((name=SymbolLiteralComputedName&lt;Yield&gt; | name=StringLiteralAsName) ']') | computeNameFrom=AssignmentExpression&lt;In=true,Yield&gt; ']')
;
SymbolLiteralComputedName &lt;Yield&gt;:
BindingIdentifier&lt;Yield&gt; ('.' IdentifierName)?
;
BindingIdentifier &lt;Yield&gt;:
IDENTIFIER
| &lt;!Yield&gt; 'yield'
| N4Keyword
;
IdentifierName: IDENTIFIER | ReservedWord | N4Keyword;
NumericLiteralAsString: DOUBLE | INT | OCTAL_INT | HEX_INT | SCIENTIFIC_INT;
StringLiteralAsName: STRING;
fragment AsyncNoTrailingLineBreak *: (declaredAsync?='async' NoLineTerminator)?; // See Asynchronous Functions
fragment StrictFormalParameters &lt;Yield&gt;*:
'(' (fpars+=FormalParameter&lt;Yield&gt; (',' fpars+=FormalParameter&lt;Yield&gt;)*)? ')'
;
FormalParameter &lt;Yield&gt;:
{FormalParameter} BindingElementFragment&lt;Yield&gt;
;
fragment BindingElementFragment &lt;Yield&gt;*:
(=&gt; bindingPattern=BindingPattern&lt;Yield&gt;
| annotations+=Annotation*
(
variadic?='...'? name=BindingIdentifier&lt;Yield&gt; ColonSepTypeRef?
)
)
('=' initializer=AssignmentExpression&lt;In=true, Yield&gt;)?
;
fragment ColonSepTypeRef*:
':' declaredTypeRef=TypeRef
;</programlisting>
</para>
</formalpara>
</section>
<section xml:id="_properties-4" role="language-n4js">
<title>Properties</title>
<simpara>Methods have all the properties of members and the following additional properties can be explicitly defined:</simpara>
<variablelist>
<varlistentry>
<term><literal>abstract</literal> </term>
<listitem>
<simpara>Method is declared but not defined.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>typePars</literal> </term>
<listitem>
<simpara>Collection of type parameters of a generic method; empty by default.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>returnTypeRef</literal> </term>
<listitem>
<simpara>Return type of the method, default return type is <math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="italic"><mi>V</mi><mi>o</mi><mi>i</mi><mi>d</mi></mstyle></math>.
The type of the method as a member of the owning classifier is not the method’s return type but is instead a function type.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>fpars</literal> </term>
<listitem>
<simpara>List of formal parameters, may be left empty.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>body</literal> </term>
<listitem>
<simpara>The body of the method (this is not available in the pure types model)</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>The following pseudo properties are defined via annotations:</simpara>
<variablelist>
<varlistentry>
<term><literal>final</literal> </term>
<listitem>
<simpara>Boolean flag set to true if annotation <literal>@Final</literal> is set.
The flag indicates that method must not be overridden in subclasses; see <xref linkend="_final-methods"/>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>declaresOverride</literal> </term>
<listitem>
<simpara>Flag set to true if annotation <literal>@Overrides</literal> is set. The flag indicates that method must override a method of a superclass; see <xref linkend="_overriding-of-members"/>.</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>Additionally, we define the following pseudo properties:</simpara>
<variablelist>
<varlistentry>
<term><literal>overrides</literal> </term>
<listitem>
<simpara>True if method overrides a super method or implements an interface method, false otherwise.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>typeRef</literal> </term>
<listitem>
<simpara>Type of the method. This is, in fact, a function type (and not the return type).</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>The following pseudo property is set to make methods compatible with properties of an object literal, however it cannot be changed:</simpara>
<variablelist>
<varlistentry>
<term><literal>enumerable</literal> </term>
<listitem>
<simpara>Boolean flag reflecting the property descriptor <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><mi>n</mi><mi>u</mi><mi>m</mi><mi>e</mi><mi>r</mi><mi>a</mi><mi>b</mi><mi>l</mi><mi>e</mi></math>, this is always set to false for methods.</simpara>
</listitem>
</varlistentry>
</variablelist>
</section>
<section xml:id="_semantics-4">
<title>Semantics</title>
<simpara>Since methods are ECMAScript functions, all constraints specified in <xref linkend="_function-type"/> apply to methods as well.
This section describes default values and function type conformance which is required for overriding and implementing methods.</simpara>
<simpara>In addition, method declarations and definitions have to comply with the constraints for naming members of classifiers (cf. <xref linkend="Req-IDE-52"/>)
and with the constraints detailed in the following sections on final methods (<xref linkend="_final-methods"/>), abstract methods (<xref linkend="_abstract-methods"/> and
method overriding and implementation (<xref linkend="_overriding-of-members"/>, <xref linkend="_implementation-of-members"/>).</simpara>
<simpara>The following constraints are defined for methods in ECMAScript 6 [<link linkend="ECMA15a">ECMA15a(p.207)</link>]</simpara>
<requirement xml:id="IDE-53">
<title>Method Definition ECMAScript 6</title>
<simpara>
<anchor xml:id="Req-IDE-53" xreflabel="[Req-IDE-53]"/>
<emphasis role="strong">Requirement: IDE-53:</emphasis>
<link linkend="Req-IDE-53">Method Definition ECMAScript 6</link> (ver. 1)</simpara>
<simpara>
* It is a Syntax Error if any element of the BoundNames of StrictFormalParameters also occurs in the VarDeclaredNames of FunctionBody.
* It is a Syntax Error if any element of the BoundNames of StrictFormalParameters also occurs in the LexicallyDeclaredNames of FunctionBody.</simpara>
</requirement>
<simpara>Methods – like functions – define a variable execution environment and therefore provide access to the actual passed-in parameters through the implicit <literal>arguments</literal> variable inside of their bodies (c.f. <xref linkend="_arguments-object"/>).</simpara>
<simpara>Methods are similar to function definitions but they must not be assigned to or from variables.
The following code issues an error although the type of the method would be compatible to the type of the variable <literal>v</literal>:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class C {
m(): void {}
}
var v: {function():void} = new C().m;</programlisting>
<requirement xml:id="IDE-54">
<title>Method Assignment</title>
<simpara>
<anchor xml:id="Req-IDE-54" xreflabel="[Req-IDE-54]"/>
<emphasis role="strong">Requirement: IDE-54:</emphasis>
<link linkend="Req-IDE-54">Method Assignment</link> (ver. 1)</simpara>
<simpara>
. In contrast to ECMAScript 2015, methods are defined as readonly, that is, it is not possible to dynamically re-assign a property defined as method with a new value.
This is because assigning or re-assigning a method breaks encapsulation. Methods are the <xref linkend="_acronyms"/> of a class, their implementation is internal to the class.
. When assigning a method to a variable, a warning is issued since this would lead to an detached this reference inside the method when it is called without explicitly providing the receiver. No warning is issued only if it is guaranteed that no problems will occur:
.. The method’s body can be determined at compile time (i.e., it has been declared <literal>@Final</literal>) and it lacks usages of <literal>this</literal> or <literal>super</literal>. This is true for instance and static methods.
.. The method is the constructor.</simpara>
</requirement>
<note>
<simpara>The following code demonstrates problems arising when methods are assigned to variables in terms of function expressions.
Given are two classes and instances of each class as follows:</simpara>
</note>
<programlisting language="n4js" linenumbering="unnumbered">class C {
m(): void { }
static k(): void {}
}
class D extends C {
@Override m(): void { this.f()}
f(): void {}
@Override static k(): void { this.f()}
static f(): void {}
}
var c: C = new C();
var d: C = new D(); // d looks like a C</programlisting>
<simpara>Assigning an instance method to a variable could cause problems, as the method assumes this to be bound to the class in which it is defined.
This may work in some cases, but will cause problems in particular in combination with method overriding:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">var v1: {@This(C)function():void} = c.m;
var v2: {@This(C)function():void} = d.m;
v1.call(c);
v2.call(c);</programlisting>
<simpara>Calling <literal>c.m</literal> indirectly via <literal>v1</literal> with <literal>c</literal> as this object will work.
However, it won’t work for <literal>v2</literal>: the method is overridden in <literal>D</literal>, and the method in expects other methods available in <literal>D</literal> but not in <literal>C</literal>.
That is, the last call would lead to a runtime error as method <literal>f</literal> which is called in <literal>D.m</literal> won’t be available.</simpara>
<simpara>The same scenario occurs in case of static methods if they are retrieved polymorphically via the variables of type <literal>constructor{C}</literal>:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">var ctor: constructor{C} = C;
var dtor: constructor{C} = D;
var v3: {@This(constructor{C})function():void} = ctor.k;
var v4: {@This(constructor{C})function():void} = dtor.k;</programlisting>
<simpara>In both cases, the problem could be solved by restricting these kinds of assignments to final methods only.
In the static case, the problem would also be solved by accessing the static method directly via the class type (and not polymorphically via the constructor).
Both restrictions are severe but would be necessary to avoid unexpected runtime problems.</simpara>
<simpara>The following example shows a problem with breaking the encapsulation of a class.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class C {
x: any = "";
f(): void { this.g(this); }
g(c: C): void { c.h(); }
h(): void {}
}
class D extends C {
@Override f(): void {
this.g(this.x);
}
@Override g(c: any) {
// do nothing, do not call h())
}
}
var c = new C();
var d = new D();
var v5: {@This(C)function():void} = c.f;
var v6: {@This(C)function():void} = d.f;
v5.call(c)
v6.call(c)</programlisting>
<simpara>In <literal>D</literal>, method <literal>g</literal> is overridden to accept more types as the original method defined in <literal>C</literal>.
Calling this new method with receiver type <literal>C</literal> (as done in the last line) will cause problems, as in <literal>D</literal> not only <literal>f</literal> has been adapted but also <literal>g</literal>.
Eventually, this would lead to a runtime error as well.</simpara>
</section>
<section xml:id="_final-methods">
<title>Final Methods</title>
<simpara>By default, methods can be overridden.
To prevent a method from being overridden, it must be annotated with <literal>@Final</literal>.</simpara>
<simpara>Of course, a method cannot be declared both abstract and final (cf. <xref linkend="Req-IDE-46"/>).
Private methods are implicitly declared final.
Because static methods can be overridden in subclasses (which is different to Java), they also can be marked as final.</simpara>
<simpara>Default methods in interfaces, cf. <xref linkend="_default-methods-in-interfaces"/>, may also be declared <literal>@Final</literal>.</simpara>
<example>
<title>Final Methods in Interfaces</title>
<simpara>If a method in an interface is provided with a body, it may be declared final.
This will ensure that the given method’s body will be in effect for all instances of the interface.
Note that this means that;</simpara>
<orderedlist numeration="loweralpha">
<listitem>
<simpara>a class implementing that interface must not define a method with the same name and</simpara>
</listitem>
<listitem>
<simpara>a class inheriting a method of that name cannot implement this interface.</simpara>
</listitem>
</orderedlist>
<simpara>The latter case is illustrated here:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">interface I {
@Final m(): void {}
}
class C1 {
m(): void {}
}
// error at "I": "The method C1.m cannot override final method I.m."
class C2 extends C1 implements I {
}</programlisting>
</example>
</section>
<section xml:id="_abstract-methods">
<title>Abstract Methods</title>
<simpara>A method can be declared without defining it, i.e. without providing a method body, and is then called an <emphasis>abstract method</emphasis>.
Such methods must be declared with modifier <literal>abstract</literal> and have their property <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi></math> set to true.
Constraints for abstract methods are covered in <xref linkend="Req-IDE-46"/> (see <xref linkend="_abstract-classes"/>).</simpara>
<simpara>In interfaces, methods are always abstract by default and they do not have to be marked as abstract.
If a method in an interface provides a body, then this is the default implementation.
See <xref linkend="_implementation-of-members"/> about how the default implementation may be mixed in the consumer.</simpara>
</section>
<section xml:id="_generic-methods" role="language-n4js">
<title>Generic Methods</title>
<simpara>Methods of generic classes can, of course, refer to the type variables defined by type parameters of the generic class.
These type variables are used similarly to predefined or declared types.
Additionally, methods may be declared generic independently from their containing class.
That is to say that type parameters (with type variables) can be defined for methods as well, just like for generic functions (see <xref linkend="_generic-functions"/>).</simpara>
<requirement xml:id="IDE-55">
<title>Type variable names for generic methods</title>
<simpara>
<anchor xml:id="Req-IDE-55" xreflabel="[Req-IDE-55]"/>
<emphasis role="strong">Requirement: IDE-55:</emphasis>
<link linkend="Req-IDE-55">Type variable names for generic methods</link> (ver. 1)</simpara>
<simpara>
For a given generic method <literal>M</literal> of a class <literal>C</literal>, the following
constraint must hold:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi> </mi><mi>t</mi><msub><mi>p</mi><mi>m</mi></msub><mo></mo><mi>m</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>P</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>,</mo><mi>t</mi><msub><mi>p</mi><mi>C</mi></msub><mo></mo><mi>C</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>P</mi><mi>a</mi><mi>r</mi><mi>s</mi><mi>:</mi><mi>t</mi><msub><mi>p</mi><mi>m</mi></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo></mo><mi>t</mi><msub><mi>p</mi><mi>C</mi></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math></simpara>
</requirement>
<simpara>Since type variables can be used similarly to types in the scope of a generic class, a generic method may refer to a type variable of its containing class.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class C {
&lt;T&gt; foo(p: T p): T { return p;}
};</programlisting>
<simpara>If a generic type parameter is not used as a formal parameter type or the return type, a warning is generated unless the method overrides a member inherited from a super class or interface.</simpara>
</section>
</section>
<section xml:id="_default-methods-in-interfaces">
<title>Default Methods in Interfaces</title>
<simpara>If a method declared in an interface defines a body, then this is the so-called <emphasis>default implementation</emphasis> and the method is called a <emphasis>default method</emphasis>.
This will be mixed into an implementor of the interface if, and only if, neither the implementing class nor any of its direct or indirect superclasses already provides an implementation for this method;
for details see <xref linkend="_member-consumption"/>.
Since the implementor is not known, some constraints exist for the body. I.e., no access to super is possible, cf. <xref linkend="Req-IDE-124"/>.</simpara>
<simpara>In order to declare an interface to provide a default implementation in a definition file, annotation <literal>@ProvidesDefaultImplementation</literal> can be used, cf. <xref linkend="Req-IDE-167"/>.</simpara>
<simpara>When a method in an interface is provided with a default implementation, it may even be declared <literal>@Final</literal>, see <xref linkend="_final-methods"/>.</simpara>
<section xml:id="_asynchronous-methods">
<title>Asynchronous Methods</title>
<simpara>N4JS implements the async/await concept proposed for ECMAScript 7, which provides a more convenient and readable syntax for writing asynchronous code compared to using built-in type Promise directly.
This concept can be applied to methods in exactly the same way as to declared functions.
See <xref linkend="_asynchronous-functions"/> and <xref linkend="_asynchronous-arrow-functions"/> for details.</simpara>
</section>
</section>
<section xml:id="_constructors" role="language-n4js">
<title>Constructors</title>
<simpara>A constructor is a special function defined on a class which returns an instance of that class.
The constructor looks like a normal method with name "constructor".
The constructor can be defined explicitly or implicitly and every class has an (implicit) constructor.</simpara>
<simpara>For a given a class <literal>C</literal>, the constructor is available via two properties:</simpara>
<variablelist>
<varlistentry>
<term><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>C</mi><mi>t</mi><mi>o</mi><mi>r</mi></math></term>
<listitem>
<simpara>the explicitly defined constructor (if any).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math></term>
<listitem>
<simpara>the explicit or implicit constructor.</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>If <literal>C</literal> is provided with an explicit constructor, we have <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi><mo>.</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mo>=</mo><mi>C</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>C</mi><mi>t</mi><mi>o</mi><mi>r</mi></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>C</mi><mi>t</mi><mi>o</mi><mi>r</mi><mo></mo><mi>C</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi></math>.
Note that <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi><mo>.</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mo></mo><mi>C</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><mi>s</mi></math> in all cases.</simpara>
<simpara>The return type of the constructor of a class <literal>C</literal> is <literal>C</literal>.
If <literal>C</literal> has type parameters <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>T</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub></math>, then the return type is <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi><mo>&lt;</mo><msub><mi>T</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub><mo>&gt;</mo></math>. The constructor is called with the operator.
Since the return type of a constructor is implicitly defined by the class, it is to be omitted.
By this definition, a constructor looks like the following:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class C {
public constructor(s: string) {
// init something
}
}</programlisting>
<simpara>Constructors define a variable execution environment and therefore provide access to the actual passed-in parameters through the implicit variable inside of their bodies (c.f. <xref linkend="_arguments-object"/>).</simpara>
<requirement xml:id="IDE-56">
<title>Defining and Calling Constructors</title>
<simpara>
<anchor xml:id="Req-IDE-56" xreflabel="[Req-IDE-56]"/>
<emphasis role="strong">Requirement: IDE-56:</emphasis>
<link linkend="Req-IDE-56">Defining and Calling Constructors</link> (ver. 1)</simpara>
<simpara>
For a constructor <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math> of a class <literal>C</literal>, the following conditions
must hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math> must neither be abstract nor static nor final and it must not be annotated with <literal>@Override</literal>.</simpara>
</listitem>
<listitem>
<simpara>If a class does not explicitly define a constructor then the constructor’s signature of the superclass constructor is assumed.</simpara>
</listitem>
<listitem>
<simpara>If a class defines a constructor with formal parameters then this constructor has to be called explicitly in constructors defined in subclasses.</simpara>
</listitem>
<listitem>
<simpara>If a super constructor is called explicitly, this call must be the only expression of an expression statement which has to be the first statement of the body.</simpara>
</listitem>
<listitem>
<simpara>Constructors may appear in interfaces, but some restrictions apply:</simpara>
<orderedlist numeration="loweralpha">
<listitem>
<simpara>constructors in interfaces must not have a body.</simpara>
</listitem>
<listitem>
<simpara>constructors in interfaces or their containing interface or one of its direct or indirect super interfaces must be annotated with <literal>@CovariantConstructor</literal>.</simpara>
</listitem>
</orderedlist>
</listitem>
<listitem>
<simpara>A constructor must not have an explicit return type declaration.</simpara>
</listitem>
<listitem>
<simpara>The implicit return type of a constructor is <literal>this?</literal>.</simpara>
</listitem>
<listitem>
<simpara>A constructor must not have any type parameters.</simpara>
</listitem>
</orderedlist>
</requirement>
<simpara>Properties of object literals may be called <literal>constructor</literal>.
However they are not recognized as constructors in these cases.</simpara>
<requirement xml:id="IDE-57">
<title>Initialization of Final Fields in the Constructor</title>
<simpara>
<anchor xml:id="Req-IDE-57" xreflabel="[Req-IDE-57]"/>
<emphasis role="strong">Requirement: IDE-57:</emphasis>
<link linkend="Req-IDE-57">Initialization of Final Fields in the Constructor</link> (ver. 1)</simpara>
<simpara>
</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>Required attributes must be initialized:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>a</mi><mo></mo><mi>C</mi><mo>.</mo><mi>a</mi><mi>t</mi><mi>t</mi><mi>r</mi><mi>:</mi><mi>a</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>q</mi><mi>u</mi><mi>i</mi><mi>r</mi><mi>e</mi><mi>d</mi><mo></mo><mo></mo><mi>e</mi><mo></mo><mi>r</mi><mo>.</mo><mi>e</mi><mi>l</mi><mi>e</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi>s</mi><mi>:</mi><mi>a</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>e</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math></simpara>
</listitem>
</orderedlist>
</requirement>
<simpara>Note on syntax: ECMAScript 6 defines constructors similarly, [<link linkend="ECMA15a">ECMA15a(p.S13.5)</link>]. In
ECMAScript 6 the super constructor is not called automatically as well.</simpara>
<simpara>The super literal used in order to call super methods is further
described in <xref linkend="_the-super-keyword"/>.</simpara>
<section xml:id="_structural-this-type-in-constructor">
<title>Structural This Type in Constructor</title>
<simpara>The use of a structural this reference as a formal parameter type is possible only in constructors.
This parameter can be annotated with <literal>@Spec</literal> which causes the compiler to generate initialization code.</simpara>
<simpara>Simply using <literal>this</literal> as a type in the constructor causes the constructor to require an object providing all public fields of the class for initialization purposes.
The fields have to be set manually as shown in the following code snippet.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class A{
public s: string;
public constructor(src: ~~this) {
this.s = src.s;
}
}</programlisting>
<simpara>Remarks:</simpara>
<itemizedlist>
<listitem>
<simpara>The type of the formal parameter <literal>~~this</literal> refers to the structural field type, see <xref linkend="_structural-typing"/> for details on structural typing.
It contains all public fields of the type.</simpara>
</listitem>
<listitem>
<simpara>Subclasses may override the constructor and introduce additional parameters.
They have to call the super constructor explicitly, however, providing a parameter with at least all required attributes of the superclass.
Usually the type <literal>this</literal> is replaced with the actual subclass, but in the case of a <literal>super()</literal> call the <literal>this</literal> type of structural formal parameters is replaced with the <literal>this</literal> type of the superclass,
hence only required fields of the superclass must be present.</simpara>
</listitem>
</itemizedlist>
<simpara>As with other structural references, it is possible to add the structural reference with additional structural members, which can be used to initialize private fields which
become not automatically part of the structural field type. For example:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class A{
public s: string;
private myPrivateNumber: number;
public constructor(src: ~~this with { x: number; }) {
this.s = src.s;
this.myPrivateNumber = src.x;
}
}</programlisting>
<simpara>Defining additional members may become a problem if a subclass defines public fields with the same name, as the <literal>~~this</literal> type will contain these fields in the subclass.
This is marked as an error in the subclass.</simpara>
<requirement xml:id="IDE-58">
<title>Names of additional members of structural this type in constructor</title>
<simpara>
<anchor xml:id="Req-IDE-58" xreflabel="[Req-IDE-58]"/>
<emphasis role="strong">Requirement: IDE-58:</emphasis>
<link linkend="Req-IDE-58">Names of additional members of structural this type in constructor</link> (ver. 1)</simpara>
<simpara>
If the structural this type is used in a constructor of a class <literal>C</literal>, and if this structural reference contains an additional structural member <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mi>M</mi></math>, the following constraints must hold true:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>For any subclass <literal>S</literal> of <literal>C</literal>, with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mo>.</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mo>=</mo><mi>C</mi><mo>.</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math> (the subclass does not define its own constructor), <literal>S</literal> must not contain a public member with same name as <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mi>M</mi></math>:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mo>&lt;</mo><mi>:</mi><mi>C</mi><mo>,</mo><mi>S</mi><mo>.</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mo>=</mo><mi>C</mi><mo>.</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0em"/><mo></mo><mo></mo><mi>M</mi><mo></mo><mi>S</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi></math>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="5.0em"/><mi>M</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo></mo><mi>M</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>S</mi><mi>M</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math></simpara>
</listitem>
<listitem>
<simpara><literal>C</literal> itself must not contain a public member with same name as <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mi>M</mi></math>:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mo></mo><mi>M</mi><mo></mo><mi>C</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi><mi>M</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo></mo><mi>M</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>S</mi><mi>M</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math>
</listitem>
</orderedlist>
</requirement>
<example>
<title>Field name conflicts with structural member name</title>
<simpara>The situation described in <xref linkend="Req-IDE-58"/> is demonstrated in the following code fragment:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class A {
private myPrivateNumber: number;
public constructor(src: ~~this with { x: number; }) {
this.myPrivateNumber = src.x;
}
}
class B extends A {
public x: number; // will cause an error message
}</programlisting>
</example>
</section>
<section xml:id="spec-constructor">
<title>@Spec Constructor</title>
<simpara>The tedious process of copying the members of the parameter to the fields of the class can be automated via the <literal>@Spec</literal>
annotation if the argument has <literal>~i~this</literal> structural initializer field typing. More details about this typing can be
found in <xref linkend="structural-readWriteInit-field-typing"/>. This can be used as shown in the following listing:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class A {
public field: string;
public constructor(@Spec spec: ~i~this) {}
}
let a = new A({field: 'hello'});
console.log(a.field); // prints: hello</programlisting>
<simpara>The code for initializing the public field of <literal>A</literal> is automatically generated, thanks to the <literal>@Spec</literal> annotation being
given in the constructor.</simpara>
<requirement xml:id="IDE-59">
<title>@Spec Constructor</title>
<simpara>
<anchor xml:id="Req-IDE-59" xreflabel="[Req-IDE-59]"/>
<emphasis role="strong">Requirement: IDE-59:</emphasis>
<link linkend="Req-IDE-59">@Spec Constructor</link> (ver. 1)</simpara>
<simpara>
</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>Annotation <literal>@Spec</literal> may only appear on a formal parameter of a constructor. Such a formal parameter is then called
<emphasis>@Spec parameter</emphasis> or simply <emphasis>spec parameter</emphasis> and its owning constructor is referred to as a <emphasis>@Spec constructor</emphasis> or
<emphasis>spec constructor</emphasis>. An argument to the spec parameter is called <emphasis>spec object</emphasis>.</simpara>
</listitem>
<listitem>
<simpara>Only a single formal parameter of a constructor may be annotated with <literal>@Spec</literal>.</simpara>
</listitem>
<listitem>
<simpara>If a formal parameter is annotated with <literal>@Spec</literal>, the parameter’s type must be <literal>~i~this</literal> (i.e. a use-site
structural initializer field type of <literal>this</literal>, see <xref linkend="structural-readWriteInit-field-typing"/>).</simpara>
</listitem>
<listitem>
<simpara>Using the data provided in the spec object, i.e. in the argument to the spec parameter, a spec constructor will
automatically initialize</simpara>
<orderedlist numeration="loweralpha">
<listitem>
<simpara>all <emphasis role="strong">owned</emphasis> data fields and <emphasis role="strong">owned</emphasis> setters of the containing class, and</simpara>
</listitem>
<listitem>
<simpara>all data fields and setters from interfaces implemented by the containing class</simpara>
</listitem>
</orderedlist>
<simpara>if and only if those members are also part of the spec parameter&#8217;s structural initializer field type.</simpara>
</listitem>
<listitem>
<simpara>Fields explicitly added to the spec parameter, e.g. <literal>@Spec spec: ~i~this with {name:string}</literal>, are used for initialization
if a non-public field of the same name exists in the class, either as an owned member or from an implemented interface.
The type of such an additional field must be a subtype of the declared type of the field being initialized:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>s</mi><mo></mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mo>.</mo><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>,</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mo>.</mo><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><mo>.</mo><mi>s</mi><mi>p</mi><mi>e</mi><mi>c</mi><mi>:</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="2.0em"/><mo></mo><mi>f</mi><mo></mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>F</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mi>s</mi><mo></mo><mi>Γ</mi><mo></mo><mi>s</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>f</mi></math></simpara>
</listitem>
<listitem>
<simpara>Even if the <literal>@Spec</literal> annotation is used, the super constructor must be invoked explicitly (as usual).</simpara>
</listitem>
</orderedlist>
</requirement>
<simpara>It follows from no. 4 above that</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>non-public data fields and setters are never initialized (because they will never be part of the spec parameter&#8217;s
structural initializer field type),</simpara>
</listitem>
<listitem>
<simpara>properties provided in the spec object but not defined in the parameter&#8217;s structural initializer field type, are
<emphasis>not</emphasis> used for initialization, even if a (protected or private) field of the same name exists in the class,</simpara>
</listitem>
<listitem>
<simpara>data fields and setters inherited from a super class are never initialized by a spec constructor (instead, this will
happen in the spec constructor of the super class).</simpara>
</listitem>
</orderedlist>
<simpara>The last of these implications will be detailed further at the end of the coming section.</simpara>
<simpara><emphasis role="strong">@Spec Constructors and Inheritance</emphasis></simpara>
<simpara>Spec constructors are inherited by subclasses that do not have a constructor and, when creating instances of the
subclass, will then require properties for writable public fields of the subclass in the spec object <emphasis role="strong">and</emphasis> include
initialization code for them.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class A {
public fa;
public constructor(@Spec spec: ~i~this) {}
}
class B extends A {
public fb;
}
const b = new B({fa: 'hello', fb: 'world'}); // requires &amp; initializes fb too!
console.log(b.fa); // prints: hello
console.log(b.fb); // prints: world</programlisting>
<simpara>Public writable fields from implemented interfaces are included as well, i.e. required as property in spec object <emphasis role="strong">and</emphasis>
initialized by auto-generated code in the <literal>@Spec</literal> constructor:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">interface I {
public fi;
}
class B implements I {
public fb;
public constructor(@Spec spec: ~i~this) {}
}
const a = new B({fb: 'hello', fi: 'world'}); // requires &amp; initializes fi too!
console.log(a.fb); // prints: hello
console.log(a.fi); // prints: world</programlisting>
<simpara>When having a spec constructor in a class <literal>B</literal> that extends a super class <literal>A</literal> without an owned or inherited spec
constructor, it should be noted that the <literal>~i~this</literal> type will require properties for public writable fields of <literal>A</literal>,
but the initialization code automatically generated due to the <literal>@Spec</literal> annotation will <emphasis role="strong">not</emphasis> initialize those members.
For public writable fields from an interface <literal>I</literal> implemented by <literal>B</literal>, however, both a property will be required by
<literal>~i~this</literal> <emphasis role="strong">and</emphasis> initialization code will be generated in the <literal>@Spec</literal> constructor. This is illustrated in the
following code example.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class A {
public fa;
}
interface I {
public fi;
}
class B extends A implements I {
public fb;
public constructor(@Spec spec: ~i~this) { // &lt;- fa, fi, fb required in spec object
// Constructor is responsible for initializing fa, fi, fb.
// The @Spec annotation will generate initialization code
// for fb and fi, but not for fa!
}
}
let b = new B({
fa: 'hello', // &lt;- fa is required (removing it would be a compile error)
fi: 'world',
fb: '!!'
});
console.log(b.fa); // undefined
console.log(b.fi); // world
console.log(b.fb); // !!</programlisting>
<simpara>The rationale for this different handling of fields from super classes and implemented interfaces is
1. fields from an implemented interface are not seen as inherited but rather implemented by implementing class, so from
the <literal>@Spec</literal> annotation&#8217;s perspective the field is a field of the implementing class, and
2. in case of a field inherited from a super class the correct way of initialization may depend on details of the super
class and has to be taken care of by custom code in the constructor of the subclass (usually by invoking the non-<literal>@Spec</literal>
constructor of the superclass with <literal>super</literal>).</simpara>
<simpara><emphasis role="strong">Special Use Cases</emphasis></simpara>
<simpara>The following examples illustrate further details of other use cases of spec constructors.</simpara>
<example>
<title>Anonymous Interface in Constructor</title>
<simpara>The base class <literal>A</literal> in the examples redefines the constructor already defined in <literal>N4Object</literal>. This is not
generally necessary and is only used here to make the example legible.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class A {
public s: string;
public constructor(@Spec spec: ~i~this) {
// initialization of s is automatically generated
}
}
class B extends A {
public t: string;
private n: number;
public constructor(spec: ~~this with {n: number;}) {
super(spec); // only inherited field s is set in super constructor
}
}</programlisting>
</example>
<example>
<title>Spec Object and Subclasses</title>
<programlisting language="n4js" linenumbering="unnumbered">class A1 {
public s: string;
public n: number;
public constructor(@Spec spec: ~i~this) {}
}
class B extends A1 {
public constructor() {
super({s:"Hello"}); // &lt;-- error, n must be set in object literal
}
}
class C extends A1 {
public constructor() {
super({s:"Hello"}); // &lt;-- error, n must be set in object literal
this.n = 10; // &lt;-- this has no effect on the super constructor!
}
}
class A2 {
public s: string;
public n: number?; // now n is optional!
public constructor(@Spec spec: ~i~this) {}
}
class D extends A2 {
public constructor() {
super({s:"Hello"}); // and this is ok now!
this.n = 10; // this explains why it is optional
}
}
class A3 {
public s: string;
public n: number = 10; // now n is not required in ~~this
public constructor(@Spec spec: ~i~this) {}
}
class E extends A3 {
public constructor() {
super({s:"Hello"}); // and this is ok now!
}
}</programlisting>
<simpara>The last case (class E) demonstrates a special feature of the typing strategy modifier in combination with the <literal>this</literal> type, see <xref linkend="_structural-typing"/> for details.</simpara>
<simpara>The constructor in class <literal>B</literal> contains an error because the super constructor expects all required attributes in <literal>A1</literal> to be set.
The additional initialization of the required field <literal>A1.n</literal> as seen in <literal>C</literal> does not change that expectation.
In this example, the field <literal>n</literal> should not have been defined as required in the first place.</simpara>
<simpara>Optional fields like <literal>n?</literal> in class <literal>A2</literal> or fields with default values like <literal>n=10</literal> in class <literal>A3</literal> are not required to be part of the <literal>spec</literal> object.</simpara>
</example>
<example>
<title>Superfluous Properties in @Spec Constructors</title>
<simpara>Each non-<math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle></math> field has to be set in the constructor via the <math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>with</mtext></mstyle></math> to the parameter otherwise properties are <emphasis>not</emphasis> used to set non-<math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle></math> fields.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class C {
public s: string;
n: number;
constructor(@Spec spec: ~i~this) {}
}
// n is ignored here
new C( { s: "Hello", n: 42 });
// but:
var ol = { s: "Hello", n: 42 };
// "ol may be used elsewhere, we cannot issue warning here" at "ol"
new C(ol) ;
// of course this is true for all superfluous properties
// weird is not used in constructor
new C( { s: "Hello", weird: true } );</programlisting>
</example>
<variablelist>
<varlistentry>
<term>Restriction when initializing interface fields via @Spec constructor <anchor xml:id="restriction-interface-field-spec-constructor" xreflabel="[restriction-interface-field-spec-constructor]"/> </term>
<listitem>
<simpara>In most cases, interface definitions in <literal>n4jsd</literal> files simply declare functions and fields that are supposed to be provided by the runtime environment.
As a result, there are restrictions as to whether fields of interfaces defined in <literal>n4jsd</literal> files can initialized via <literal>@Spec</literal> constructors or not.
In particular, fields of an interface declared in a <literal>n4jsd</literal> file cannot be initialized via @Spec constructor if the interface</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>is a built-in or</simpara>
</listitem>
<listitem>
<simpara>does not have an <literal>@N4JS</literal> annotation</simpara>
</listitem>
</orderedlist>
</listitem>
</varlistentry>
</variablelist>
<simpara>The following example illustrates this restriction.</simpara>
<example>
<title>Interface fields that cannot be initialized via @Spec constructors</title>
<formalpara>
<title>Inf.n4jsd</title>
<para>
<programlisting language="n4js" linenumbering="unnumbered">export external interface I {
public m: string;
}
@N4JS
export external interface J {
public n: string;
}</programlisting>
</para>
</formalpara>
<formalpara>
<title>Test.n4js</title>
<para>
<programlisting language="n4js" linenumbering="unnumbered">import { I } from "Inf";
// I is an external interface WITHOUT @N4JS annotation
class C implements I {
constructor(@Spec spec:~i~this) {}
}
// J is an external interface with @N4JS annotation
class D implements J {
constructor(@Spec spec:~i~this) {}
}
// XPECT warnings --&gt; "m is a property of built-in / provided by runtime / external without @N4JS annotation interface I and can not be initialized in Spec constructor." at "m"
let c:C = new C({m: "Hello"});
// XPECT nowarnings
let d:D = new D({n: "Bye"});
console.log(c.m)
console.log(d.n)
/* XPECT output ---
&lt;==
stdout:
undefined
Bye
stderr:
==&gt;
--- */</programlisting>
</para>
</formalpara>
</example>
<simpara>In this example, the interface <literal>I</literal> is defined in the <literal>Inf.n4jsd</literal> file without the <literal>@N4JS</literal> annotation. As a result, its field <literal>m</literal> cannot be initialized via the <literal>@Spec</literal> constructor and hence the output of <literal>console.log(c.m)</literal> is <literal>undefined</literal>. On the other hand, since the interface <literal>J</literal> is declared with the annotation <literal>@N4JS</literal>, it is possible to initialize its field <literal>n</literal> in the <literal>@Spec</literal> constructor. That&#8217;s why the result of <literal>console.log(d.n)</literal> is <literal>Bye</literal>.</simpara>
</section>
<section xml:id="_callable-constructors">
<title>Callable Constructors</title>
</section>
<section xml:id="_covariant-constructors">
<title>Covariant Constructors</title>
<simpara>Usually, the constructor of a subclass need not be override compatible with the constructor of its super class.
By way of annotation <literal>@CovariantConstructor</literal> it is possible to change this default behavior and enforce all subclasses to have constructors with override compatible signatures.
A subclass can achieve this by either inheriting the constructor from the super class (which is usually override compatible,
with the special case of <literal>@Spec</literal> constructors) or by defining a new constructor with a signature compatible to the inherited constructor.
The same rules as for method overriding apply.</simpara>
<simpara>The <literal>@CovariantConstructor</literal> annotation may be applied to the constructor, the containing classifier, or both.
It can also be used for interfaces; in fact, constructors are allowed in interfaces only if they themselves or the interface is annotated with <literal>@CovariantConstructor</literal> (see <xref linkend="Req-IDE-60"/>).</simpara>
<definition>
<title>Covariant Constructor</title>
<simpara>
<anchor xml:id="covariant_constructor" xreflabel="[covariant_constructor]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="covariant_constructor">Covariant Constructor</link></simpara>
<simpara>
A classifier <literal>C</literal> is said to <literal><emphasis>have a covariant constructor</emphasis></literal> if and
only if one of the following applies:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara><literal>C</literal> has a direct super class <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>C</mi><mi>'</mi></msup></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>C</mi><mi>'</mi></msup></math> is annotated with <literal>@CovariantConstructor</literal> or <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>C</mi><mi>'</mi></msup></math> has a constructor annotated with <literal>@CovariantConstructor</literal>.</simpara>
</listitem>
<listitem>
<simpara><literal>C</literal> has a directly implemented interface <literal>I and `I</literal> is annotated with <literal>@CovariantConstructor</literal> or <literal>I</literal> has a constructor annotated with <literal>@CovariantConstructor</literal>.</simpara>
</listitem>
<listitem>
<simpara><literal>C</literal> has a direct super class or directly implemented interface that <literal><emphasis>has a covariant constructor</emphasis></literal> (as defined here).</simpara>
</listitem>
</orderedlist>
</definition>
<simpara>Note that <literal>C</literal> does not need to have an owned(!) constructor; also a constructor inherited from a super class can be declared covariant.</simpara>
<simpara>The following rules apply to covariant constructors.</simpara>
<requirement xml:id="IDE-60">
<title>Covariant Constructors</title>
<simpara>
<anchor xml:id="Req-IDE-60" xreflabel="[Req-IDE-60]"/>
<emphasis role="strong">Requirement: IDE-60:</emphasis>
<link linkend="Req-IDE-60">Covariant Constructors</link> (ver. 1)</simpara>
<simpara>
. Annotation <literal>@CovariantConstructor</literal> may only be applied to classes, interfaces, and constructors.
Annotating a constructor with this annotation, or its containing classifier, or both have all the same effect.
. Given a class <literal>C</literal> with an owned constructor <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math> and a super class <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mi>u</mi><mi>p</mi></math> that has a covariant constructor (owned or inherited, see <xref linkend="_covariant-constructors"/>),
then <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mi>u</mi><mi>p</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math> must be accessible from <literal>C</literal>,
.. <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math> must be override compatible with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math>:</simpara>
<simpara>+
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi><mi>C</mi><mi>o</mi><mi>m</mi><mi>p</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>b</mi><mi>l</mi><mi>e</mi><mfenced close=")" open="("><mrow><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></mrow><mrow><mi>S</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></mrow></mfenced></math></simpara>
<simpara>+
This constraint corresponds to <xref linkend="Req-IDE-72"/> except for the <literal>Override</literal> annotation which is not required here.
. Given a classifier <literal>C</literal> implementing interface <literal>I</literal> and <literal>I</literal> has a covariant constructor (owned or inherited, see <xref linkend="_covariant-constructors"/>), we require
.. <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>I</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math> must be accessible from <literal>C</literal>,
.. an implementation-compatible constructor <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math> must be defined in C with</simpara>
<simpara>+
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi><mi>C</mi><mi>o</mi><mi>m</mi><mi>p</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>b</mi><mi>l</mi><mi>e</mi><mfenced close=")" open="("><mrow><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></mrow><mrow><mi>I</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></mrow></mfenced></math></simpara>
<simpara>+
This constraint corresponds to <xref linkend="Req-IDE-74"/> except for the <literal>@Override</literal> annotation, which is not required, here.
.. Given a classifier <literal>C</literal> without an owned constructor and an extended class or interface <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mi>u</mi><mi>p</mi></math> that has a covariant constructor (owned or inherited, see <xref linkend="_covariant-constructors"/>),
we require the inherited constructor <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math> of <literal>C</literal> within the context of <literal>C</literal> to be override compatible to itself in the context of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mi>u</mi><mi>p</mi></math>.
Using notation <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>m</mi><mfenced close="]" open="["><mi>T</mi></mfenced></math> to denote that a member <literal>M</literal> is to be treated as defined in container type <literal>T</literal>, which means the this-binding is set to <literal>T</literal>, we can write:</simpara>
<simpara>+
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi><mi>C</mi><mi>o</mi><mi>m</mi><mi>p</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>b</mi><mi>l</mi><mi>e</mi><mfenced close=")" open="("><mrow><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mfenced close="]" open="["><mi>C</mi></mfenced></mrow><mrow><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mfenced close="]" open="["><mrow><mi>S</mi><mi>u</mi><mi>p</mi></mrow></mfenced></mrow></mfenced></math></simpara>
<simpara>+
This constraint does not correspond to any of the constraints for the redefinition of ordinary members.</simpara>
</requirement>
<simpara>The following example demonstrates a use case for covariant constructors.
It shows a small class hierarchy using covariant constructors, <literal>Cls</literal> and <literal>Cls2</literal>, together with a helper function <literal>createAnother</literal> that creates and returns a new instance of the same type as its argument <literal>value</literal>.</simpara>
<example xml:id="ex:covariant_constructors">
<title>Covariant Constructors</title>
<programlisting language="n4js" linenumbering="unnumbered">class A {}
class B extends A {}
@CovariantConstructor
class Cls {
constructor(p: B) {}
}
class Cls2 extends Cls {
constructor(p: A) { // it's legal to generalize the type of parameter 'p'
super(null);
}
}
function &lt;T extends Cls&gt; createAnother(value: T, p: B): T {
let ctor = value.constructor;
return new ctor(p);
}
let x = new Cls2(new A());
let y: Cls2;
y = createAnother(x, new B());</programlisting>
</example>
<simpara>In the code of <xref linkend="ex:covariant_constructors"/>, we would get an error if we changed the type of parameter <literal>p</literal> in the constructor of <literal>Cls2</literal> to some other type that
is not a super type of <literal>B</literal>, i.e. the type of the corresponding parameter of <literal>Cls</literal>’s constructor.
If we removed the <literal>@CovariantConstructor</literal> annotation on <literal>Cls</literal>, we would get an error in the new expression inside function <literal>createAnother</literal>.</simpara>
<simpara>The next example illustrates how to use <literal>@CovariantConstructor</literal> with interfaces and shows a behavior that might be surprising at first sight.</simpara>
<example xml:id="ex:covariant-constructors-in-interfaces">
<title>Covariant Constructors in Interfaces</title>
<programlisting language="n4js" linenumbering="unnumbered">@CovariantConstructor
interface I {
constructor(p: number)
}
class C implements I {
// no constructor required!
}
class D extends C {
// XPECT errors --&gt; "Signature of constructor of class D does not conform to overridden constructor of class N4Object: {function(number)} is not a subtype of {function()}." at "constructor"
constructor(p: number) {}
}</programlisting>
</example>
<simpara>Interface <literal>I</literal> declares a covariant constructor expecting a single parameter of type <literal>number</literal>.
Even though class <literal>C</literal> implements <literal>I</literal>, it does not need to define an owned constructor with such a parameter.
According to <xref linkend="Req-IDE-60"/>, it is enough for <literal>C</literal> to have a constructor, either owned or inherited, that is override compatible with the one declared by <literal>I</literal>.
Class <literal>C</literal> inherits the default constructor from <literal>N4Object</literal>, which does not have any arguments and is thus override compatible to <literal>I</literal>’s constructor.</simpara>
<simpara>In addition, subclasses are now required to have constructors which are override compatible with the constructor of class <literal>C</literal>, i.e. the one inherited from <literal>N4Object</literal>.
<xref linkend="ex:covariant-constructors-in-interfaces"/> shows that this is violated even when repeating the exact same constructor signature from interface <literal>I</literal>,
because that constructor now appears on the other side of the subtype test during checking override compatibility.</simpara>
</section>
</section>
<section xml:id="_data-fields" role="language-n4js">
<title>Data Fields</title>
<simpara>A data field is a simple property of a class.
There must be no getter or setter defined with the same name as the data field.
In ECMAScript 6, a class has no explicit data fields.
It is possible, however, to implicitly define a data field by simply assigning a value to a variable of the this element (e.g. <literal>this.x = 10</literal> implicitly defines a field <literal>x</literal>).
Data fields in N4JS are similar to these implicit fields in ECMAScript 6 except that they are defined explicitly in order to simplify validation and user assistance.</simpara>
<section xml:id="data-fields-syntax">
<title>Syntax</title>
<programlisting language="xtext" linenumbering="unnumbered">N4FieldDeclaration &lt;Yield&gt;:
{N4FieldDeclaration}
FieldDeclarationImpl&lt;Yield&gt;
;
fragment FieldDeclarationImpl &lt;Yield&gt;*:
(declaredModifiers+=N4Modifier)* BogusTypeRefFragment?
declaredName=LiteralOrComputedPropertyName&lt;Yield&gt;
(declaredOptional?='?')?
ColonSepTypeRef?
('=' expression=Expression&lt;In=true,Yield&gt;)?
Semi
;</programlisting>
</section>
<section xml:id="data-fields-properties">
<title>Properties</title>
<simpara>Fields have the following properties which can be explicitly defined:</simpara>
<variablelist>
<varlistentry>
<term><literal>declaredOptional</literal> </term>
<listitem>
<simpara>Tells whether the accessor was declared optional.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>typeRef</literal> </term>
<listitem>
<simpara>Type of the field; default value is <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>A</mi><mi>n</mi><mi>y</mi></math>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>expr</literal> </term>
<listitem>
<simpara>Initializer expression, i.e. sets default value.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>static</literal> </term>
<listitem>
<simpara>Boolean flag set to true if field is a static field.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>const</literal> </term>
<listitem>
<simpara>Boolean flag set to true if field cannot be changed. Note that const fields are automatically static.
Const fields need an initializer. Also see <xref linkend="_assignment-modifiers"/>.</simpara>
</listitem>
</varlistentry>
</variablelist>
<note>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi></math> is <emphasis>not</emphasis> the (reversed) value of the property descriptor <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>w</mi><mi>r</mi><mi>i</mi><mi>t</mi><mi>a</mi><mi>b</mi><mi>l</mi><mi>e</mi></math> as the latter is checked at runtime while const may or may not be checked at runtime.</simpara>
</note>
<simpara>The following pseudo properties are defined via annotations for setting the values of the property descriptor:</simpara>
<variablelist>
<varlistentry>
<term><literal>enumerable</literal> </term>
<listitem>
<simpara>Boolean flag reflecting the property descriptor <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><mi>n</mi><mi>u</mi><mi>m</mi><mi>e</mi><mi>r</mi><mi>a</mi><mi>b</mi><mi>l</mi><mi>e</mi></math>, set via annotation <literal>@Enumerable(true|false)</literal>.
The default value is <math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>true</mtext></mstyle></math>.<footnote><simpara>version 4.0</simpara></footnote></simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>declaredWriteable</literal> </term>
<listitem>
<simpara>Boolean flag reflecting the property descriptor <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>w</mi><mi>r</mi><mi>i</mi><mi>t</mi><mi>e</mi><mi>a</mi><mi>b</mi><mi>l</mi><mi>e</mi></math>, set via annotation <literal>@Writeable(true|false)</literal>.
The default value is <math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>true</mtext></mstyle></math>.<footnote><simpara>This cannot be done w/o <literal>null</literal>/<literal>undefined</literal> analysis</simpara></footnote></simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>final</literal> </term>
<listitem>
<simpara>Boolean flag making the field read-only, and it must be set in the constructor. Also see <xref linkend="_assignment-modifiers"/>.</simpara>
</listitem>
</varlistentry>
</variablelist>
<bridgehead xml:id="data-fields-derived-values" renderas="sect5">Derived values for fields</bridgehead>
<variablelist>
<varlistentry>
<term><literal>readable</literal> </term>
<listitem>
<simpara>Always true for fields.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>abstract</literal> </term>
<listitem>
<simpara>Always false for fields.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>writeable</literal> </term>
<listitem>
<simpara>Set to false if field is declared const or final. In the latter case, it may be set in the constructor (cf. <xref linkend="_assignment-modifiers"/>).</simpara>
</listitem>
</varlistentry>
</variablelist>
<section xml:id="data-fields-semantics">
<title>Semantics</title>
<requirement xml:id="IDE-61">
<title>Attributes</title>
<simpara>
<anchor xml:id="Req-IDE-61" xreflabel="[Req-IDE-61]"/>
<emphasis role="strong">Requirement: IDE-61:</emphasis>
<link linkend="Req-IDE-61">Attributes</link> (ver. 1)</simpara>
<simpara>
For any attribute <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi></math> if a
class <literal>C</literal>, the following constraints must hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>A required data field must not define an initializer:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>q</mi><mi>u</mi><mi>i</mi><mi>r</mi><mi>e</mi><mi>d</mi><mo></mo><mi>a</mi><mo>.</mo><mi>i</mi><mi>n</mi><mi>i</mi><mi>t</mi><mo>=</mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi></math></simpara>
</listitem>
<listitem>
<simpara>There must be no other member with the same name of a data field <literal>f</literal>.
In particular, there must be no getter or setter defined with the same name:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo></mo><mi> </mi><mi>m</mi><mo></mo><mi>f</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi><mi>m</mi><mo></mo><mi>f</mi><mo></mo><mi>m</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo></mo><mi>f</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math></simpara>
</listitem>
</orderedlist>
<simpara>If a subclass should set a different default value, this has to be done in the constructor of the subclass.</simpara>
<simpara>For the relation of data fields and field accessors in the context of extending classes or implementing interfaces see <xref linkend="_redefinition-of-members"/>.</simpara>
</requirement>
</section>
<section xml:id="data-fields-type-inference">
<title>Type Inference</title>
<simpara>The type of a field is the type of its declaration:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mi>f</mi><mi>:</mi><mi>Γ</mi><mo></mo><mi>d</mi></mrow></mfrac></math>
<simpara>The type of a field declaration is either the declared type or the inferred type of the initializer expression:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mfrac><mrow><mi>d</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mo></mo><mstyle mathvariant="monospace"><mtext>null</mtext></mstyle><mspace width="3.0mm"/><mi>T</mi><mo>=</mo><mi>d</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi></mrow><mrow><mi>Γ</mi><mo></mo><mi>d</mi><mi>:</mi><mi>T</mi></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mfrac><mrow><mi>d</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>null</mtext></mstyle><mspace width="3.0mm"/><mi>d</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo></mo><mstyle mathvariant="monospace"><mtext>null</mtext></mstyle></mrow><mrow><mi>Γ</mi><mo></mo><mi>d</mi><mi>:</mi><mi>T</mi></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mi>E</mi><mo>=</mo><mi>Γ</mi><mo></mo><mi>d</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mspace width="3.0mm"/><mi>E</mi><mo></mo><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>null, undefined</mtext></mstyle></mfenced><mspace width="3.0mm"/><mi>T</mi><mo>=</mo><mi>E</mi><mi>}</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mfrac><mrow><mi>e</mi><mi>l</mi><mi>s</mi><mi>e</mi></mrow><mrow><mi>Γ</mi><mo></mo><mi>d</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>any</mtext></mstyle></mrow></mfrac></math></simpara>
<simpara>If the type contains type variables they are substituted according to
type parameters which are provided by the reference:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>Γ</mi><mo></mo><mi>t</mi><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>:</mi><mi>T</mi></mrow><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>TField</mtext></mstyle><mi> </mi><mi>t</mi><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mi>:</mi><mi>T</mi></mrow></mfrac></math>
</section>
</section>
<section xml:id="_assignment-modifiers">
<title>Assignment Modifiers</title>
<simpara>Assignment of data fields can be modified by the assignment modifiers <literal>const</literal> (similar to constant variable declarations, see <xref linkend="_const"/>) and <literal>@Final</literal>.</simpara>
<requirement xml:id="IDE-62">
<title>Const Data Fields</title>
<simpara>
<anchor xml:id="Req-IDE-62" xreflabel="[Req-IDE-62]"/>
<emphasis role="strong">Requirement: IDE-62:</emphasis>
<link linkend="Req-IDE-62">Const Data Fields</link> (ver. 1)</simpara>
<simpara>
For a data field <literal>f</literal> marked as <literal>const</literal>, the following constraints must hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>An initializer expression must be provided in the declaration (except in n4jsd files):<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo></mo><mstyle mathvariant="monospace"><mtext>null</mtext></mstyle></math></simpara>
</listitem>
<listitem>
<simpara>A constant data field is implicitly static and must be accessed only via the classifier type.
It is not possible, therefore, to use the <literal>this</literal> keyword in the initializer expression of a constant field:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>s</mi><mi>u</mi><mi>b</mi><mo></mo><mi>f</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><msup><mi>r</mi><mo>*</mo></msup><mi>:</mi><mi>s</mi><mi>u</mi><mi>b</mi><mo>=</mo><mi>"</mi><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi><mi>"</mi></math></simpara>
</listitem>
<listitem>
<simpara>A constant data field must not be annotated with <literal>@Final</literal>:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mo></mo><mo>¬</mo><mi>f</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi></math></simpara>
</listitem>
<listitem>
<simpara>Constant data fields are not writeable (cf. <xref linkend="Req-IDE-68"/>):<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mo></mo><mo>¬</mo><mi>f</mi><mo>.</mo><mi>w</mi><mi>r</mi><mi>i</mi><mi>t</mi><mi>e</mi><mi>a</mi><mi>b</mi><mi>l</mi><mi>e</mi></math></simpara>
</listitem>
</orderedlist>
</requirement>
<requirement xml:id="IDE-63">
<title>Final Data Fields</title>
<simpara>
<anchor xml:id="Req-IDE-63" xreflabel="[Req-IDE-63]"/>
<emphasis role="strong">Requirement: IDE-63:</emphasis>
<link linkend="Req-IDE-63">Final Data Fields</link> (ver. 1)</simpara>
<simpara>
For a data field <literal>f</literal> marked as <literal>@Final</literal>, the following constraints must hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>A final data field must not be modified with <literal>const</literal> or <literal>static</literal>:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi><mo></mo><mo>¬</mo><mi>f</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mo></mo><mo>¬</mo><mi>f</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>S</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi></math></simpara>
</listitem>
<listitem>
<simpara>A final data field is not writeable:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi><mo></mo><mo>¬</mo><mi>f</mi><mo>.</mo><mi>w</mi><mi>r</mi><mi>i</mi><mi>t</mi><mi>e</mi><mi>a</mi><mi>b</mi><mi>l</mi><mi>e</mi></math><?asciidoc-br?>
A final field may, however, be set in the constructor.
See <xref linkend="Req-IDE-68"/> for details.</simpara>
</listitem>
<listitem>
<simpara>A final data field must be either initialized by an initializer expression or in the constructor.
If the field is initialized in the constructor, this may be done either explicitly or via a spec style constructor.</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mspace width="3.0mm"/><mi>f</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo></mo><mstyle mathvariant="monospace"><mtext>null</mtext></mstyle></mtd></mtr><mtr><mtd><mspace width="3.0mm"/><mo></mo><mrow><mo>(</mo><mo></mo><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>E</mi><mi>x</mi><mi>p</mi><mi>:</mi><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>E</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>i</mi><mi>n</mi><mi>g</mi><mi>F</mi><mi>u</mi><mi>n</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo>=</mo><mi>f</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></mrow></mtd></mtr><mtr><mtd><mspace width="3.0mm"/><mspace width="3.0em"/><mo></mo><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>E</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo>.</mo><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mo>.</mo><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>"this"</mtext></mstyle></mtd></mtr><mtr><mtd><mrow><mspace width="3.0mm"/><mspace width="3.0em"/><mo></mo><mi>b</mi><mi>i</mi><mi>n</mi><mi>d</mi><mfenced close=")" open="("><mrow><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>E</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo>.</mo><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>y</mi></mrow><mi>f</mi></mfenced><mo>)</mo></mrow></mtd></mtr><mtr><mtd><mspace width="3.0mm"/><mo></mo><mrow><mo>(</mo><mi>f</mi><mo>.</mo><mi>p</mi><mi>u</mi><mi>b</mi><mi>l</mi><mi>i</mi><mi>c</mi><mo></mo><mo></mo><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><mo></mo><mi>f</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mo>.</mo><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><mi>s</mi><mi>:</mi></mrow></mtd></mtr><mtr><mtd><mrow><mspace width="3.0mm"/><mspace width="3.0em"/><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><mo>.</mo><mi>s</mi><mi>p</mi><mi>e</mi><mi>c</mi><mo></mo><mo></mo><mi>s</mi><mi>m</mi><mo></mo><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi><mi>s</mi><mi>m</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>f</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>)</mo></mrow></mtd></mtr></mtable></math>
</listitem>
</orderedlist>
</requirement>
</section>
<section xml:id="_field-accessors-getter-setter">
<title>Field Accessors (Getter/Setter)</title>
<simpara>Instead of a simple data field, a field can be defined by means of the getter and setter accessor methods.
These accessor methods are similar to the accuser methods in object literals:</simpara>
<section xml:id="field-acessors-syntax">
<title>Syntax</title>
<programlisting language="xtext" linenumbering="unnumbered">N4GetterDeclaration &lt;Yield&gt;:
=&gt; ({N4GetterDeclaration}
(declaredModifiers+=N4Modifier)*
GetterHeader&lt;Yield&gt;)
(body=Block&lt;Yield&gt;)? ';'?
;
fragment GetterHeader &lt;Yield&gt;*:
BogusTypeRefFragment? 'get' -&gt; declaredName=LiteralOrComputedPropertyName&lt;Yield&gt;
(declaredOptional?='?')?
'(' ')'
ColonSepTypeRef?
;
N4SetterDeclaration &lt;Yield&gt;:
=&gt;({N4SetterDeclaration}
(declaredModifiers+=N4Modifier)*
'set'
-&gt;declaredName=LiteralOrComputedPropertyName &lt;Yield&gt;
)
(declaredOptional?='?')?
'(' fpar=FormalParameter&lt;Yield&gt; ')' (body=Block&lt;Yield&gt;)? ';'?
;</programlisting>
<simpara>Notes with regard to syntax: Although ECMAScript 6 does not define fields in classes, it defines getter and setter methods similarly (cf. [<link linkend="ECMA15a">ECMA15a(p.S13.3, p.p.209)</link>]).</simpara>
<example>
<title>Getter and Setter</title>
<simpara>The getter and setter implementations usually reference data fields internally.
These are to be declared explicitly (although ECMAScript allows creating fields on the fly on their first usage).
The following example demonstrates a typical usage of getter and setter in combination with a data field.
The getter lazily initializes the field on demand.
The setter performs some notification.</simpara>
<formalpara>
<title>Getter Setter</title>
<para>
<programlisting language="n4js" linenumbering="unnumbered">class A {}
class C {
private _data: A = null;
public get data(): A {
if (this._data==null) {
this._data = new A();
}
return this._data;
}
public set data(data: A) {
this._data = data;
this.notifyListeners();
}
notifyListeners(): void {
// ...
}
}</programlisting>
</para>
</formalpara>
</example>
</section>
<section xml:id="field-acessors-properties">
<title>Properties</title>
<simpara>Properties for field accessors:</simpara>
<variablelist>
<varlistentry>
<term><literal>declaredOptional</literal> </term>
<listitem>
<simpara>Tells whether the accessor was declared optional.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>readable</literal> </term>
<listitem>
<simpara>Derived value: true for getters and false for setters.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>writable</literal> </term>
<listitem>
<simpara>Derived value: false for getters and true for setters.</simpara>
</listitem>
</varlistentry>
</variablelist>
</section>
<section xml:id="field-accessors-semantics">
<title>Semantics</title>
<simpara>There must be no field or method with the same name as a field accessor (follows from <xref linkend="Req-IDE-52"/>). In addition, the following constraints must hold:</simpara>
<requirement xml:id="IDE-64">
<title>Field Accessors</title>
<simpara>
<anchor xml:id="Req-IDE-64" xreflabel="[Req-IDE-64]"/>
<emphasis role="strong">Requirement: IDE-64:</emphasis>
<link linkend="Req-IDE-64">Field Accessors</link> (ver. 1)</simpara>
<simpara>
</simpara>
<itemizedlist>
<listitem>
<simpara>The return type of a getter must not be <literal>void</literal>.</simpara>
</listitem>
<listitem>
<simpara>The type of the parameter of a setter must not be <literal>void</literal>.</simpara>
</listitem>
<listitem>
<simpara>If a getter <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>g</mi></math> is defined or consumed (from an interface) or merged-in (via static polyfill) in a class <literal>C</literal> and a setter <literal>S</literal> with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>g</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo></mo><mi>s</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo>=</mo><mi>g</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi></math> is inherited by
<literal>C</literal> from one of its super classes, then <literal>C</literal> must define a setter <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>s</mi><mi>'</mi></msup></math> with
<math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>s</mi><mi>'</mi></msup><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>g</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo></mo><msup><mi>s</mi><mi>'</mi></msup><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo>=</mo><mi>g</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi></math> <footnote><simpara>This is required, because in Javascript a getter shadows a corresponding setter defined further up in the prototype chain; likewise a setter shadows a corresponding getter.</simpara></footnote>.</simpara>
</listitem>
<listitem>
<simpara>A setter must have exactly one formal parameter, i.e. variadic or default modifiers are not allowed.</simpara>
</listitem>
</itemizedlist>
<simpara>The same applies to setters, accordingly.</simpara>
<itemizedlist>
<listitem>
<simpara><xref linkend="Req-IDE-72"/>, <xref linkend="Req-IDE-73"/>, and <xref linkend="Req-IDE-74"/> apply to field accessors accordingly (getter / setter overriding).</simpara>
</listitem>
</itemizedlist>
<note role="language-n4js">
<simpara>A getter and setter with the same name need not have the same type, i.e. the getter’s return type need not be the same as a subtype of
the type of the setter’s parameter (the types can be completely unrelated).<footnote><simpara>Thus, the type of one accessor is not used to infer the type of the other one. E.g., from <literal>set x(string s)</literal>, we cannot infer <literal>get x()</literal> to return <literal>string</literal> — instead, the getter is inferred to return <literal>any</literal>.</simpara></footnote></simpara>
</note>
</requirement>
<simpara>Getters and setters – like functions – define a variable execution environment and therefore provide access to the actual passed-in parameters through the implicit <literal>arguments</literal>
variable inside of their bodies (c.f. <xref linkend="_arguments-object"/>).</simpara>
</section>
</section>
<section xml:id="optional-fields">
<title>Optional Fields</title>
<simpara>Data fields and field accessors of a classifier C can be declared optional, meaning that a structural subtype of C
need not provide this field, but if it does, the field must be of correct type. However, to ensure overall type safety,
the scope of application of this optionality is limited to a small number of specific use cases, as described in the
following.</simpara>
<section xml:id="_syntax-6">
<title>Syntax</title>
<simpara>To denote a data field or accessor as optional, a question mark is placed right after the name:</simpara>
<formalpara>
<title>Syntax of optional fields</title>
<para>
<programlisting language="n4js" linenumbering="unnumbered">class C {
public field?: string;
public get getter?(): number {
return 42;
}
public set setter?(value: number) {}
}</programlisting>
</para>
</formalpara>
<simpara>The detailed grammar is given in the sections for data fields, cf. <xref linkend="data-fields-syntax"/>,
and field accessors, cf. <xref linkend="field-acessors-syntax"/>.</simpara>
</section>
<section xml:id="_semantics-5">
<title>Semantics</title>
<simpara>It is important to note that the optionality of a field is, by default and in most cases, ignored and
has an effect only in certain special cases.</simpara>
<simpara>The effect of a field being optional is defined by the following requirement.</simpara>
<requirement xml:id="IDE-240500">
<title>Optional Fields</title>
<simpara>
<anchor xml:id="Req-IDE-240500" xreflabel="[Req-IDE-240500]"/>
<emphasis role="strong">Requirement: IDE-240500:</emphasis>
<link linkend="Req-IDE-240500">Optional Fields</link> (ver. 1)</simpara>
<simpara>
</simpara>
<simpara>By default, a data field, getter, or setter that is declared optional is handled in the
exact same way as if no optionality were involved (i.e. by default, optionality is ignored).</simpara>
<simpara>Optionality has an effect only in case of structural subtype checks <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>L</mi><mo>&lt;</mo><mi>:</mi><mi>R</mi></math> in which
the left-hand side is one of the following:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>an object literal.</simpara>
</listitem>
<listitem>
<simpara>a new expression.</simpara>
</listitem>
<listitem>
<simpara>an instance of a final class, i.e. the type of the value on left-hand side must be nominal and refer to a final class.</simpara>
</listitem>
<listitem>
<simpara>a reference to a const variable if its initializer expression is one of the following:</simpara>
<orderedlist numeration="loweralpha">
<listitem>
<simpara>an object literal.</simpara>
</listitem>
<listitem>
<simpara>a new expression.</simpara>
</listitem>
<listitem>
<simpara>an instance of a final class (as explained above).</simpara>
</listitem>
<listitem>
<simpara>an ternary expression</simpara>
</listitem>
</orderedlist>
</listitem>
</orderedlist>
<simpara>and then</simpara>
<itemizedlist>
<listitem>
<simpara>in cases 1 and 4a, <emphasis role="strong">both</emphasis> fields and accessors (getters and setters) are optional.
That means, an optional data field, getter, or setter of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>R</mi></math> needs not be present in <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>L</mi></math>.</simpara>
</listitem>
<listitem>
<simpara>in cases 2, 3, 4b, and 4c, only <emphasis role="strong">getters</emphasis> are optional, setters are not optional.
That means, an optional getter of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>R</mi></math> needs not be present in <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>L</mi></math> and an optional field of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>R</mi></math> requires only a setter in <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>L</mi></math>. Note that these cases are more restricted than the cases 1 and 4a.</simpara>
</listitem>
</itemizedlist>
<simpara>Moreover, optionality has an effect in case of ternary expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>L</mi><mo>&lt;</mo><mi>:</mi><mi>R</mi></math> in which the left-hand side is a ternary expression, e.g. <literal>l = b? trueExpr : falseExpr</literal> whose <literal>trueExpr</literal> or <literal>falseExpr</literal> possibly recursively contains an expression of the kind mentioned above. In this case, the optionality effect is the more restricted optinality of <literal>trueExpr</literal> and <literal>falseExpr</literal>.</simpara>
<simpara>If, according to these rules, a data field / getter / setter of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>R</mi></math> need not be present in
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>L</mi></math> but a member with the same name and access is actually present in <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>L</mi></math>, that member in <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>L</mi></math>
must be a data field / getter / setter of the same type / a subtype / a super type, respectively.
In other words, if a not actually required member is present in the subtype, ordinary rules
for member compatibility apply as if no optionality were involved (cf. general subtyping rules
for structural types).</simpara>
</requirement>
<simpara>In other words, in object literals (cases 1 and 4a) neither optional getters, optional setters,
nor optional data fields are required. However, in case of new expressions and instances of
final classes (cases 2, 3, 4b, 4c) only optional getters are not required in a subtype;
optional setters are required as normal (i.e. optionality ignored) and optional data fields
require at least a setter.</simpara>
<simpara>The following table summarizes the most common cases and shows how this relates to the different
forms of structural typing.</simpara>
<table xml:id="tab:optionalFields" frame="all" rowsep="1" colsep="1">
<title>Optional Fields</title>
<tgroup cols="8">
<colspec colname="col_1" colwidth="25*"/>
<colspec colname="col_2" colwidth="8.3333*"/>
<colspec colname="col_3" colwidth="8.3333*"/>
<colspec colname="col_4" colwidth="8.3333*"/>
<colspec colname="col_5" colwidth="8.3333*"/>
<colspec colname="col_6" colwidth="8.3333*"/>
<colspec colname="col_7" colwidth="8.3333*"/>
<colspec colname="col_8" colwidth="25.0002*"/>
<tbody>
<row>
<entry align="left" valign="top"></entry>
<entry align="center" valign="top" namest="col_2" nameend="col_6"><simpara>&#916;</simpara></entry>
<entry align="center" valign="top"><simpara>Case</simpara></entry>
<entry align="left" valign="top"><simpara>Comment</simpara></entry>
</row>
<row>
<entry align="left" valign="top"></entry>
<entry align="center" valign="top"><simpara><literal>~</literal></simpara></entry>
<entry align="center" valign="top"><simpara><literal>~~</literal></simpara></entry>
<entry align="center" valign="top"><simpara><literal>~w~</literal></simpara></entry>
<entry align="center" valign="top"><simpara><literal>~r~</literal></simpara></entry>
<entry align="center" valign="top"><simpara><literal>~i~</literal></simpara></entry>
<entry align="center" valign="top"></entry>
<entry align="left" valign="top"></entry>
</row>
<row>
<entry align="left" valign="top"></entry>
<entry align="center" valign="top" namest="col_2" nameend="col_4"><simpara>may have setter</simpara></entry>
<entry align="center" valign="top" namest="col_3" nameend="col_4"><simpara>never has setter</simpara></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><emphasis role="strong"><literal>let x: &#916;C = {};</literal></emphasis></simpara></entry>
<entry align="center" valign="top"><simpara></simpara></entry>
<entry align="center" valign="top"><simpara></simpara></entry>
<entry align="center" valign="top"><simpara></simpara></entry>
<entry align="center" valign="top"><simpara></simpara></entry>
<entry align="center" valign="top"><simpara></simpara></entry>
<entry align="center" valign="top"><simpara>1</simpara></entry>
<entry align="left" valign="top"><simpara>nothing mandatory</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><emphasis role="strong"><literal>let x: &#916;C = new D0();</literal></emphasis></simpara></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"><simpara></simpara></entry>
<entry align="center" valign="top"><simpara></simpara></entry>
<entry align="center" valign="top"><simpara>2</simpara></entry>
<entry align="left" valign="top"><simpara>setters mandatory</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><emphasis role="strong"><literal>let x: &#916;C = new DG();</literal></emphasis></simpara></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"><simpara></simpara></entry>
<entry align="center" valign="top"><simpara></simpara></entry>
<entry align="center" valign="top"><simpara>2</simpara></entry>
<entry align="left" valign="top"><simpara>setters mandatory</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><emphasis role="strong"><literal>let x: &#916;C = new DS();</literal></emphasis></simpara></entry>
<entry align="center" valign="top"><simpara></simpara></entry>
<entry align="center" valign="top"><simpara></simpara></entry>
<entry align="center" valign="top"><simpara></simpara></entry>
<entry align="center" valign="top"><simpara></simpara></entry>
<entry align="center" valign="top"><simpara></simpara></entry>
<entry align="center" valign="top"><simpara>2</simpara></entry>
<entry align="left" valign="top"><simpara>setters mandatory</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><emphasis role="strong"><literal>let x: &#916;C = fooD0();</literal></emphasis></simpara></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"><simpara>none</simpara></entry>
<entry align="left" valign="top"><simpara>D0 not final</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><emphasis role="strong"><literal>let x: &#916;C = fooSF0();</literal></emphasis></simpara></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"><simpara>none</simpara></entry>
<entry align="left" valign="top"><simpara>fooSF0() not nominal</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><emphasis role="strong"><literal>let x: &#916;C = fooF0();</literal></emphasis></simpara></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"><simpara></simpara></entry>
<entry align="center" valign="top"><simpara></simpara></entry>
<entry align="center" valign="top"><simpara>3</simpara></entry>
<entry align="left" valign="top"><simpara>setters mandatory</simpara></entry>
</row>
</tbody>
</tgroup>
</table>
<simpara>In the table, a "✓" means that the particular example is valid; in all other cases an error would
be shown in N4JS source code. Here are the classes and functions used in the above table:</simpara>
<formalpara>
<title>Classes and functions used in table</title>
<para>
<programlisting language="n4js" linenumbering="unnumbered">class C {
public field?: string;
}
class D0 {}
class DG {
public get field(): string { return "hello"; }
}
class DS {
public set field(value: string) {}
}
@Final class F0 {}
function fooD0(): D0 { return new D0(); }
function fooSF0(): ~F0 { return new F0(); }
function fooF0(): F0 { return new F0(); }</programlisting>
</para>
</formalpara>
<simpara>It follows from the above definitions in Requirements <xref linkend="Req-IDE-240500"/> that cases 4a and 4b are not
transitive across a chain of several <literal>const</literal> variables, whereas case 4c is transitive. For example:</simpara>
<formalpara>
<title>Transitivity of the use cases of optional fields</title>
<para>
<programlisting language="n4js" linenumbering="unnumbered">class C {
public get getter?(): string {return null;}
}
class D {}
@Final class F {}
let c: ~C;
// no transitivity via several const variables in use case "object literal":
const ol1 = {};
const ol2 = ol1;
// XPECT errors --&gt; "~Object is not a structural subtype of ~C: missing getter getter." at "ol2"
c = ol2;
// no transitivity via several const variables in use case "new expression":
const new1 = new D();
const new2 = new1;
// XPECT errors --&gt; "D is not a structural subtype of ~C: missing getter getter." at "new2"
c = new2;
// BUT: we do have transitivity via several const variables in use case "final nominal type":
const finalNominal1 = new F();
const finalNominal2 = finalNominal1;
// XPECT noerrors --&gt;
c = finalNominal1;
// XPECT noerrors --&gt; "transitivity applies in this case"
c = finalNominal2;</programlisting>
</para>
</formalpara>
<simpara>The following example demonstrates how optionality behaves in ternay expressions.</simpara>
<formalpara>
<title>Optional fields in ternay expressions</title>
<para>
<programlisting language="n4js" linenumbering="unnumbered">interface ~I {
public m?: int;
}
class ~C { }
@Final class F { }
let b: boolean;
const cc: C = {}
let f1 = new F();
let f2: ~F = {};
// True expression is a const object literal, so both fields and accessors in I are optional.
// False expression is a new expression, so only getters in I are optionals.
// As a result, only getters in I are optional.
// XPECT errors --&gt; "C is not a structural subtype of I: missing field m." at "b? cc : new C()"
var te1: I = b? cc : new C()
// No errors because both true and false expressions are object literal constants and hence
// Both fields and accessors in I are optional.
// XPECT noerrors
var te2: I = b? cc : {}</programlisting>
</para>
</formalpara>
</section>
<section xml:id="_background">
<title>Background</title>
<simpara>The following example illustrates why optionality of fields has to be restricted
to the few special cases defined above (i.e. object literals, new expressions, etc.).</simpara>
<formalpara>
<title>Problem 1 of optional fields</title>
<para>
<programlisting language="n4js" linenumbering="unnumbered">class C {
public field?: string = "hello";
}
class D {}
class DD extends D {
public field: number = 42;
}
let c: ~C;
let d: D;
d = new DD();
c = d; // without the restrictive semantics of optional fields, this assignment would be allowed (but shows compile-time error in N4JS)
console.log(c.field); // prints 42 even though the type is string
c.field.charAt(0); // exception at runtime: c.field.charAt is not a function</programlisting>
</para>
</formalpara>
<simpara>In the last line of the above example, <literal>c.field</literal> is actually <literal>42</literal> but the type systems claims it is of type <literal>string</literal> and
thus allows accessing member <literal>charAt</literal> of type <literal>string</literal> which is undefined at runtime the actual value <literal>42</literal>.</simpara>
<simpara>The next example shows why cases 2 and 3 (i.e. new expressions and instances of final classes) have to be handled in
a more restrictive manner than case 1 (i.e. object literals).</simpara>
<formalpara>
<title>Problem 2 of optional fields</title>
<para>
<programlisting language="n4js" linenumbering="unnumbered">class C {
public field?: string;
}
class D {}
let c: ~C;
c = new D(); // error: new expression but D is missing setter
c.field = "hello";</programlisting>
</para>
</formalpara>
<simpara>In the previous code, if <literal>c = new D()</literal> were allowed, we would add a new property <literal>field</literal> to the instance of class
<literal>D</literal> in the last line, which N4JS aims to avoid in general, unless unsafe language features such as dynamic types
are being employed.</simpara>
</section>
</section>
</section>
<section xml:id="_static-members" role="language-n4js">
<title>Static Members</title>
<simpara>Static data fields, field accessors and methods are quite similar to instance members, however they are not members of instances of the type but the type itself.
They are defined similarly to instance members except that they are specified with the modifier <literal>static</literal>.
Since they are members of the type, the <literal>this</literal> keyword is not bound to instances of the class, but again to the type itself.
This is similar as in ECMAScript 6 ([<link linkend="ECMA15a">ECMA15a(p.14.5.15)</link>]).
Since static members are not instance but type members, it is even possible that a static member has the same name as an instance member.</simpara>
<simpara>Note that static members are not only allowed in classes but also in interfaces, but there are important differences
(for example, no inheritance of static members of interfaces, cf. Section <xref linkend="_static-members-of-interfaces"/>).</simpara>
<requirement xml:id="IDE-65">
<title>Static member not abstract</title>
<simpara>
<anchor xml:id="Req-IDE-65" xreflabel="[Req-IDE-65]"/>
<emphasis role="strong">Requirement: IDE-65:</emphasis>
<link linkend="Req-IDE-65">Static member not abstract</link> (ver. 1)</simpara>
<simpara>
For a static field accessor or method <literal>S</literal>, the following constraint must hold:</simpara>
<itemizedlist>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo></mo><mo>¬</mo><mi>s</mi><mo>.</mo><mi>a</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi></math></simpara>
</listitem>
</itemizedlist>
</requirement>
<simpara>Like instance methods, static methods of classes are inherited by subclasses and it is possible to override static methods in subclasses.
The very same override constraints are valid in this case as well.</simpara>
<section xml:id="_access-from-and-to-static-members">
<title>Access From and To Static Members</title>
<requirement xml:id="IDE-66">
<title>Accessing Static Members</title>
<simpara>
<anchor xml:id="Req-IDE-66" xreflabel="[Req-IDE-66]"/>
<emphasis role="strong">Requirement: IDE-66:</emphasis>
<link linkend="Req-IDE-66">Accessing Static Members</link> (ver. 1)</simpara>
<simpara>
</simpara>
<simpara>Let <literal>M</literal> be a static member of class <literal>C</literal>. Except for write-access to
fields, which will be explained later, you can access <literal>M</literal>
via:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>The class declaration instance, i.e. the classifier or constructor type, <literal>constructor{C}</literal>, i.e. <literal>C.m</literal></simpara>
</listitem>
<listitem>
<simpara>The class declaration instance of a subtype, i.e. the classifier or constructor type, i.e. <literal>D.m</literal>, if <literal>D</literal> is a subclass of <literal>C</literal>.</simpara>
</listitem>
<listitem>
<simpara><literal>v.m</literal>, if <literal>v</literal> is a variable of type <literal>C</literal> (i.e. classifier type as defined in <xref linkend="_constructor-and-classifier-type"/>) or a subtype thereof.</simpara>
</listitem>
<listitem>
<simpara><literal>this.m</literal> inside the body of any static method declared in <literal>C</literal> or any sub-class of <literal>C</literal>.</simpara>
</listitem>
<listitem>
<simpara>Via a type variable <literal>T</literal> which upper bound is a subclassof <literal>C</literal> e.g., <literal>function &lt;T extends C&gt; f(){T.m}</literal></simpara>
</listitem>
</orderedlist>
</requirement>
<requirement xml:id="IDE-67">
<title>Static Member Access</title>
<simpara>
<anchor xml:id="Req-IDE-67" xreflabel="[Req-IDE-67]"/>
<emphasis role="strong">Requirement: IDE-67:</emphasis>
<link linkend="Req-IDE-67">Static Member Access</link> (ver. 1)</simpara>
<simpara>
It is not possible to access instance members from static members.
This is true in particular for type variables defined by a generic classifier.</simpara>
</requirement>
<requirement xml:id="IDE-68">
<title>Write-access to static data fields and static setter</title>
<simpara>
<anchor xml:id="Req-IDE-68" xreflabel="[Req-IDE-68]"/>
<emphasis role="strong">Requirement: IDE-68:</emphasis>
<link linkend="Req-IDE-68">Write-access to static data fields and static setter</link> (ver. 1)</simpara>
<simpara>
</simpara>
<simpara>For static data fields and static setter <literal>f</literal> the following constraint must hold:</simpara>
<itemizedlist>
<listitem>
<simpara>For every assign expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>E</mi><mi>x</mi><mi>p</mi><mi>r</mi></math> with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo></mo><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>E</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo>.</mo><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mo>=</mo><mi>T</mi><mo>.</mo><mi>f</mi><mo></mo><mi>T</mi><mo>=</mo><mi>f</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi></math>.</simpara>
</listitem>
<listitem>
<simpara>For every writing unary expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi></math> with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi><mo>.</mo><mi>o</mi><mi>p</mi><mo></mo><mfenced close="}" open="{"><mrow><mo>+</mo><mo>+</mo></mrow><mrow><mo>-</mo><mo>-</mo></mrow></mfenced><mo></mo><mi>f</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo></mo><mi>u</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo>=</mo><mi>T</mi><mo>.</mo><mi>f</mi><mo></mo><mi>T</mi><mo>=</mo><mi>f</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi></math>.</simpara>
</listitem>
</itemizedlist>
</requirement>
<simpara>In the special case of <literal>m</literal> being a static data field, write-access is only possible via the defining type name <literal>C.m</literal>.
In the list above, only the first line can be used when assigning values to a field. Note that this only applies to fields and set-accessors.<footnote><simpara>The technical reason for this rule is the way properties are stored in JavaScript. Take for an example subclass-write access : <literal role="language-n4js">class C { static f="a";}</literal> with <literal role="language-n4js">class D extends C { }</literal>. Now the data field <literal>f</literal> on <literal>C</literal> can also be queried using <literal>D</literal> (<literal role="language-n4js">var q=D.f;</literal>) but writing (<literal role="language-n4js">D.f="b";</literal>) would introduce a newly created property <literal>f</literal> on class <literal>D</literal>, which differs from the one defined on <literal>C</literal>. It would do this without explicitly overriding the inherited property. Subsequent reads to <literal role="language-n4js">D.f</literal> would route to this ’accidentally’ introduced property. Such a behavior would not be expected and therefore has been disallowed. Note that this write restriction applies to data-fields and to field setters.</simpara></footnote></simpara>
<simpara>It is even possible to call a static field accessor or method of a class using dynamic polymorphism, as demonstrated in the following example:</simpara>
<example xml:id="ex:Polymorphism_and_static_methods">
<title>Static members of classes, inheritance and polymorphism</title>
<programlisting language="n4js" linenumbering="unnumbered">class A {
static m(): void { console.log('A#m'); }
static foo(): void { console.log('A#foo'); }
static bar(): void {
this.foo();
}
}
class B extends A {
@Override
static foo(): void { console.log('B#foo'); }
}
A.m(); // will print "A#m"
B.m(); // will print "A#m" (m is inherited by B)
var t: type{A} = A;
t.foo(); // will print "A#foo"
t = B;
t.foo(); // will print "B#foo"
// using 'this':
A.bar(); // will print "A#foo"
B.bar(); // will print "B#foo"</programlisting>
</example>
<simpara>This is quite different from Java where static methods are not inherited and references to static methods are statically bound at compile time
depending on the declared type of the receiver (and not its value):</simpara>
<example>
<title>Static members in Java</title>
<programlisting language="java" linenumbering="unnumbered">// !!! JAVA CODE !!!
public class C {
static void m() { System.out.println("C#m"); }
public static void main(String[] args) {
final C c = null;
c.m(); // will print "C#m" (no NullPointerException at runtime)
}
}</programlisting>
</example>
</section>
<section xml:id="_generic-static-methods">
<title>Generic static methods</title>
<simpara>It is not possible to refer to type variables of a generic class, as these type variables are never bound to any concrete types.
A static method can, however, be declared generic.
Generic static methods are defined similarly to generic instance methods.
Since they cannot refer to type variables of a generic class, the constraint to avoid type variables with equal names (see <xref linkend="Req-IDE-55"/>) does not need to hold for generic static methods.</simpara>
</section>
<section xml:id="_static-members-of-interfaces">
<title>Static Members of Interfaces</title>
<simpara>Data fields, field accessors and methods of interfaces may be declared
static. A few restrictions apply:</simpara>
<requirement xml:id="IDE-69">
<title>Static Members of Interfaces</title>
<simpara>
<anchor xml:id="Req-IDE-69" xreflabel="[Req-IDE-69]"/>
<emphasis role="strong">Requirement: IDE-69:</emphasis>
<link linkend="Req-IDE-69">Static Members of Interfaces</link> (ver. 1)</simpara>
<simpara>
</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>Static members of interfaces may only be accessed directly via the containing interface’s type name
(this means, of the four ways of accessing static members of classes defined in <xref linkend="Req-IDE-66"/> above, only the first one applies to static members of interfaces).</simpara>
</listitem>
<listitem>
<simpara>The <literal>this</literal> literal may not be used in static methods or field accessors of interfaces and it may not be used in the initializer expression of static fields of interfaces. See <xref linkend="Req-IDE-173"/>.</simpara>
</listitem>
<listitem>
<simpara>The <literal>super</literal> literal may not be used in static methods or field accessors of interfaces (in fact, it may not be used in interfaces at all, cf. <xref linkend="Req-IDE-123"/>).</simpara>
</listitem>
</orderedlist>
</requirement>
<simpara>Note that the <literal>this</literal> type as a return type for methods is only allowed for instance methods and as an argument type only in constructors (structurally typed).
There is no need to disallow these cases for static interface methods in the constraints above.</simpara>
<simpara>In general, static members may not be abstract, cf. <xref linkend="Req-IDE-46"/>, which applies here as well.
Static methods and field accessors of interfaces, therefore, always have to provide a body.</simpara>
<simpara>Static members of interfaces are much more restricted than those of classes.
Compare the following example to <xref linkend="_polymorphism-and-static-methods"/> for classes above:</simpara>
<example>
<title>Static members of interfaces</title>
<programlisting language="n4js" linenumbering="unnumbered">interface I {
static m(): void { console.log('I#m'); }
}
interface J extends I {}
I.m(); // prints "I#m"
J.m(); // ERROR! (m is not inherited by J)
var ti: type{I} = I;
ti.m(); // ERROR! (access to m only allowed directly via type name I)
ti = J;
ti.m(); // ERROR! (access to m only allowed directly via type name I)</programlisting>
</example>
<simpara>The last line in is the reason why access to static members has to be restricted to direct access via the type name of the containing interfaces.</simpara>
</section>
</section>
<section xml:id="_redefinition-of-members">
<title>Redefinition of Members</title>
<simpara>Members defined in classes or interfaces can be redefined by means of being overridden or implemented in subclasses, sub-interfaces, or implementing classes.
Fields and methods with default implementation defined in interfaces can be consumed by the implementor, but certain restrictions apply.</simpara>
<requirement xml:id="IDE-70">
<title>Override Compatible</title>
<simpara>
<anchor xml:id="Req-IDE-70" xreflabel="[Req-IDE-70]"/>
<emphasis role="strong">Requirement: IDE-70:</emphasis>
<link linkend="Req-IDE-70">Override Compatible</link> (ver. 1)</simpara>
<simpara>
A member <literal>M</literal> is <emphasis>override compatible</emphasis> to a member <literal>S</literal> if and only if the
following constraints hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>The name and static modifiers are equal:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>S</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo></mo><mi>M</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo>=</mo><mi>S</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi></math></simpara>
</listitem>
<listitem>
<simpara>The metatypes are compatible:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>S</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Method</mtext></mstyle><mspace width="3.0mm"/><mo></mo><mspace width="3.0mm"/><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Method</mtext></mstyle></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>S</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Field</mtext></mstyle><mspace width="3.0mm"/><mo></mo><mspace width="3.0mm"/><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo></mo><mstyle mathvariant="monospace"><mtext>Field, Getter, Setter</mtext></mstyle></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>S</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Getter</mtext></mstyle><mspace width="3.0mm"/><mo></mo><mspace width="3.0mm"/><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo></mo><mstyle mathvariant="monospace"><mtext>Field, Getter</mtext></mstyle></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>S</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Setter</mtext></mstyle><mspace width="3.0mm"/><mo></mo><mspace width="3.0mm"/><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo></mo><mstyle mathvariant="monospace"><mtext>Field, Setter</mtext></mstyle></math></simpara>
</listitem>
<listitem>
<simpara>The overridden member must not be declared final:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>¬</mo><mi>S</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi></math></simpara>
</listitem>
<listitem>
<simpara>Overridden member declared const can only be overridden (redefined) by const members:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mo></mo><mi>M</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi></math></simpara>
</listitem>
<listitem>
<simpara>It is not possible to override a non-final / non-const field or a setter with a final / const field:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfenced close=")" open="("><mrow><mi>μ</mi><mfenced close=")" open="("><mi>S</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Field</mtext></mstyle><mo></mo><mo>¬</mo><mfenced close=")" open="("><mrow><mi>S</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi><mo></mo><mi>S</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi></mrow></mfenced></mrow></mfenced><mo></mo><mi>μ</mi><mfenced close=")" open="("><mi>S</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Setter</mtext></mstyle><mo></mo><mo>¬</mo><mfenced close=")" open="("><mrow><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Field</mtext></mstyle><mo></mo><mfenced close=")" open="("><mrow><mi>M</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi><mo></mo><mi>M</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi></mrow></mfenced></mrow></mfenced></math></simpara>
</listitem>
<listitem>
<simpara>It is not possible to override a non-abstract member with an abstract one:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>¬</mo><mi>M</mi><mo>.</mo><mi>a</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi><mo></mo><mi>S</mi><mo>.</mo><mi>a</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi></math></simpara>
</listitem>
<listitem>
<simpara>The types are compatible:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mfenced close=")" open="("><mrow><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo></mo><mstyle mathvariant="monospace"><mtext>Method, Getter, Field</mtext></mstyle><mo></mo><mi>μ</mi><mfenced close=")" open="("><mi>S</mi></mfenced><mo></mo><mstyle mathvariant="monospace"><mtext>Setter</mtext></mstyle></mrow></mfenced><mspace width="3.0mm"/><mo></mo><mi>Γ</mi><mo></mo><mi>M</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>S</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfenced close=")" open="("><mrow><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo></mo><mstyle mathvariant="monospace"><mtext>Setter, Field</mtext></mstyle><mo></mo><mi>μ</mi><mfenced close=")" open="("><mi>S</mi></mfenced><mo></mo><mstyle mathvariant="monospace"><mtext>Getter</mtext></mstyle><mo></mo><mo>¬</mo><mi>S</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi></mrow></mfenced><mspace width="3.0mm"/><mo></mo><mi>Γ</mi><mo></mo><mi>S</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>M</mi></math>4</simpara>
</listitem>
<listitem>
<simpara>The access modifier is compatible:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo></mo><mi>S</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi></math></simpara>
</listitem>
</orderedlist>
</requirement>
<simpara>We define a relation <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi><mi>C</mi><mi>o</mi><mi>m</mi><mi>p</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>b</mi><mi>l</mi><mi>e</mi><mfenced close=")" open="("><mi>M</mi><mi>S</mi></mfenced></math> accordingly.</simpara>
<simpara>Members overriding or implementing other members must be declared as override.
If a member does not override another, however, it must not be declared as override.</simpara>
<requirement xml:id="IDE-71">
<title>Non-Override Declaration</title>
<simpara>
<anchor xml:id="Req-IDE-71" xreflabel="[Req-IDE-71]"/>
<emphasis role="strong">Requirement: IDE-71:</emphasis>
<link linkend="Req-IDE-71">Non-Override Declaration</link> (ver. 1)</simpara>
<simpara>
If and only if a member <literal>M</literal> of a class <literal>C</literal> (extending a class <literal>S</literal> and interfaces <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>I</mi><mi>i</mi></msub></math>) does not override or implement another member, then it must not be declared as override.
That is the following constraint must hold:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo>¬</mo><mi>M</mi><mo>.</mo><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo></mo></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo></mo><msup><mi>M</mi><mi>'</mi></msup><mo></mo><mi>C</mi><mo>.</mo><mi>s</mi><mi>u</mi><mi>p</mi><mi>e</mi><mi>r</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo></mo><msubsup><mo></mo><mrow><mi>i</mi><mo>=</mo><mn>1</mn></mrow><mi>n</mi></msubsup><msub><mi>I</mi><mi>i</mi></msub><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><msup><mi>M</mi><mi>'</mi></msup><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>M</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo></mo><msup><mi>M</mi><mi>'</mi></msup><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo>=</mo><mi>M</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo></mo><msup><mi>M</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>&gt;</mo><mstyle mathvariant="monospace"><mtext>private</mtext></mstyle></math></simpara>
</requirement>
<section xml:id="_overriding-of-members" role="language-n4js">
<title>Overriding of Members</title>
<simpara>In general, the N4JS platform supports overriding members by redefining them in sub-classes.
This definition allows for overriding of static methods, but it does not apply to constructors because <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi><mo>.</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mo></mo><mi>C</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><mi>s</mi></math>.</simpara>
<requirement xml:id="IDE-72">
<title>Overriding Members</title>
<simpara>
<anchor xml:id="Req-IDE-72" xreflabel="[Req-IDE-72]"/>
<emphasis role="strong">Requirement: IDE-72:</emphasis>
<link linkend="Req-IDE-72">Overriding Members</link> (ver. 1)</simpara>
<simpara>
Given a class <literal>C</literal> and a superclass <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mi>u</mi><mi>p</mi></math>.
If for an instance or static member <literal>M</literal> defined in <literal>C</literal> a member <literal>S</literal> exists with null
then we call <literal>M</literal> the overriding member and <literal>S</literal> the overridden member.
In that case the following constraints must hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara><literal>S</literal> must be accessible from <literal>C</literal></simpara>
</listitem>
<listitem>
<simpara><literal>M</literal> must be override compatible with <literal>S</literal>:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi><mi>C</mi><mi>o</mi><mi>m</mi><mi>p</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>b</mi><mi>l</mi><mi>e</mi><mfenced close=")" open="("><mi>M</mi><mi>S</mi></mfenced></math></simpara>
</listitem>
<listitem>
<simpara>If <literal>S</literal> is a field and <literal>M</literal> is an accessor, then an additional accessor <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>M</mi><mi>'</mi></msup></math> must exists so that <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi><mo>,</mo><msup><mi>M</mi><mi>'</mi></msup></math> are an accessor pair for <literal>S</literal>:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mi>μ</mi><mfenced close=")" open="("><mi>S</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Field</mtext></mstyle><mo></mo><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>=</mo><mrow><mi>A</mi><mi>c</mi><mi>c</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>o</mi><mi>r</mi></mrow></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo></mo><mo></mo><msup><mi>M</mi><mi>'</mi></msup><mo></mo><mi>C</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>:</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="4.0em"/><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi><mi>C</mi><mi>o</mi><mi>m</mi><mi>p</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>b</mi><mi>l</mi><mi>e</mi><mfenced close=")" open="("><msup><mi>M</mi><mi>'</mi></msup><mi>S</mi></mfenced><mo></mo><mfenced close="}" open="{"><mrow><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced></mrow><mrow><mi>μ</mi><mfenced close=")" open="("><msup><mi>M</mi><mi>'</mi></msup></mfenced></mrow></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Getter,Setter</mtext></mstyle></math></simpara>
</listitem>
<listitem>
<simpara><literal>M</literal> must be declared as override:<?asciidoc-br?>
<literal>M.override</literal></simpara>
</listitem>
</orderedlist>
</requirement>
<simpara>Remarks:</simpara>
<itemizedlist>
<listitem>
<simpara>An overridden method, getter, or setter may called via <literal>super</literal>.
Note that this is not possible for fields.</simpara>
</listitem>
<listitem>
<simpara>There is no ’hiding’ of fields as in Java, instead there is field overriding.</simpara>
</listitem>
<listitem>
<simpara>It is not possible to override a field with a consumed getter and an overridden setter, because the getter is not consumed if there exists a field in a superclass.
In this case, the consuming and extending class needs to define the accessor pair explicitly.
The same is true for other combination of accessors and fields.</simpara>
</listitem>
<listitem>
<simpara>Overriding a field usually makes only sense if the visibility of the field is to be increased.</simpara>
</listitem>
</itemizedlist>
</section>
<section xml:id="_implementation-of-members">
<title>Implementation of Members</title>
<definition>
<title>Interface and Class Member Sets</title>
<simpara>
<anchor xml:id="interface_and_class_member_sets" xreflabel="[interface_and_class_member_sets]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="interface_and_class_member_sets">Interface and Class Member Sets</link></simpara>
<simpara>
For the following constraints, we define two helper sets <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>M</mi><mi>C</mi></msub></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>M</mi><mi>I</mi></msub></math> as follows:</simpara>
<simpara>Given a <literal>C</literal>, and interface <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>I</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>I</mi><mi>n</mi></msub></math>, implemented by <literal>C</literal>, with</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>M</mi><mi>C</mi></msub><mspace width="3.0mm"/><mo>=</mo><mi>C</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo></mo><mrow><mo>{</mo><mrow><mi>m</mi><mo></mo><mi>C</mi><mo>.</mo><mi>s</mi><mi>u</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>|</mo><mi>m</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>&gt;</mo><mstyle mathvariant="monospace"><mtext>private</mtext></mstyle><mo>}</mo></mrow></mrow></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>M</mi><mi>I</mi></msub><mspace width="3.0mm"/><mo>=</mo><msubsup><mo></mo><mrow><mi>i</mi><mo>=</mo><mn>1</mn></mrow><mi>n</mi></msubsup><msub><mi>I</mi><mi>i</mi></msub><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi></math></simpara>
<simpara>Note that these sets already contain only non-private data fields.</simpara>
</definition>
<section xml:id="_member-consumption">
<title>Member Consumption</title>
<definition>
<title>Member Consumption and Implementation</title>
<simpara>
<anchor xml:id="member_consumption_and_implementation" xreflabel="[member_consumption_and_implementation]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="member_consumption_and_implementation">Member Consumption and Implementation</link></simpara>
<simpara>
A member <literal>M</literal> defined in an interface <literal>I</literal> is <emphasis>consumed</emphasis> by an implementor <literal>C</literal>, if it becomes a member of the class, that is, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi><mo></mo><mi>C</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi></math>.</simpara>
<simpara>A member <literal>M</literal> is consumed if there is no member defined in the implementor with the same name and if there is no non-private,
non-abstract member with that name inherited by the implementor from its superclass. <footnote><simpara>There had been the idea of preventing static members of being consumed. However, this would break the type subtype relation.</simpara></footnote></simpara>
<simpara>If the implementor defines the member itself, then the member is implemented rather than consumed.</simpara>
<simpara>The concrete rules are described in the following;</simpara>
<simpara>It is not always possible to directly consume a member.
In general, a rather conservative strategy is used: if two implemented interfaces define the same (non-abstract) member then the implementor must redefine the member in order to solve conflicts.
Even if the two conflicting members have the same types, the implementor must redefine them as we generally assume semantic differences which the consumer has to be aware of.
Data fields defined in interfaces, in particular, are assumed to be concrete.
It is not, therefore, possible to consume a field defined in two implemented interfaces.</simpara>
</definition>
<requirement xml:id="IDE-73">
<title>Consumption of Interface Members</title>
<simpara>
<anchor xml:id="Req-IDE-73" xreflabel="[Req-IDE-73]"/>
<emphasis role="strong">Requirement: IDE-73:</emphasis>
<link linkend="Req-IDE-73">Consumption of Interface Members</link> (ver. 1)</simpara>
<simpara>
Given a classifier <literal>C</literal> <footnote><simpara><literal>C</literal> could either be a class or an interface.</simpara></footnote>, and interfaces <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>I</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>I</mi><mi>n</mi></msub></math> implemented (or extended) by <literal>C</literal>, and sets <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>M</mi><mi>C</mi></msub></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>M</mi><mi>I</mi></msub></math> as defined in <xref linkend="interface_and_class_member_sets"/>.
A non-static member <literal>M</literal> defined in any interface <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>I</mi><mi>i</mi></msub></math> is merged into the consumer (<literal>C</literal>), if for all other (possible) members <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>M</mi><mi>'</mi></msup></math> of <literal>C</literal></simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mo></mo><msup><mi>M</mi><mi>'</mi></msup><mo></mo><msub><mi>M</mi><mi>C</mi></msub><mo></mo><msub><mi>M</mi><mi>I</mi></msub><mo></mo><mfenced close="}" open="{"><mi>M</mi></mfenced><mi>:</mi><mi>M</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><msup><mi>M</mi><mi>'</mi></msup><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo></mo><mo>¬</mo><msup><mi>M</mi><mi>'</mi></msup><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi></math>
<simpara>the following constraints hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>The other member’s meta type matches the meta type of the merge candiate:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Method</mtext></mstyle><mspace width="3.0mm"/><mo></mo><mi>μ</mi><mfenced close=")" open="("><msup><mi>M</mi><mi>'</mi></msup></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Method</mtext></mstyle></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo></mo><mstyle mathvariant="monospace"><mtext>Method</mtext></mstyle><mspace width="3.0mm"/><mo></mo><mi>μ</mi><mfenced close=")" open="("><msup><mi>M</mi><mi>'</mi></msup></mfenced><mo></mo><mstyle mathvariant="monospace"><mtext>Field, FieldAccessor</mtext></mstyle></math></simpara>
</listitem>
<listitem>
<simpara>The other member is abstract and not owned by the consumer:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>=</mo><mi>μ</mi><mfenced close=")" open="("><msup><mi>M</mi><mi>'</mi></msup></mfenced><mo></mo><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Field</mtext></mstyle></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="2.0em"/><mo></mo><msup><mi>M</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi><mo></mo><msup><mi>M</mi><mi>'</mi></msup><mo></mo><mi>C</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi></math></simpara>
</listitem>
<listitem>
<simpara>The merge candidate’s access modifier is not less than the modifier of the other member:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>=</mo><mi>μ</mi><mfenced close=")" open="("><msup><mi>M</mi><mi>'</mi></msup></mfenced><mo></mo><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Field</mtext></mstyle></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="2.0em"/><mo></mo><mi>M</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo></mo><msup><mi>M</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi></math></simpara>
</listitem>
<listitem>
<simpara>The merge candidate’s type compatible with the other member:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo></mo><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>Method, Getter, Field</mtext></mstyle></mfenced><mo></mo><mi>μ</mi><mfenced close=")" open="("><msup><mi>M</mi><mi>'</mi></msup></mfenced><mo></mo><mstyle mathvariant="monospace"><mtext>Setter</mtext></mstyle><mspace width="3.0mm"/><mo></mo><mi>Γ</mi><mo></mo><mi>M</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><msup><mi>M</mi><mi>'</mi></msup></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo></mo><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>Setter, Field</mtext></mstyle></mfenced><mo></mo><mi>μ</mi><mfenced close=")" open="("><msup><mi>M</mi><mi>'</mi></msup></mfenced><mo></mo><mstyle mathvariant="monospace"><mtext>Getter</mtext></mstyle><mspace width="3.0mm"/><mo></mo><mi>Γ</mi><mo></mo><msup><mi>M</mi><mi>'</mi></msup><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>M</mi></math></simpara>
</listitem>
</orderedlist>
</requirement>
</section>
<section xml:id="_member-implementation" role="language-n4js">
<title>Member Implementation</title>
<requirement xml:id="IDE-74">
<title>Implementation of Interface Members</title>
<simpara>
<anchor xml:id="Req-IDE-74" xreflabel="[Req-IDE-74]"/>
<emphasis role="strong">Requirement: IDE-74:</emphasis>
<link linkend="Req-IDE-74">Implementation of Interface Members</link> (ver. 1)</simpara>
<simpara>
For any non-static abstract member <literal>M</literal> defined in an interface <literal>I implemented (or extended) by a classifier `C</literal>, <literal>M</literal> must be accessible
from <literal>C</literal> and one or two member(s) in <literal>C</literal> must exist which are implementation-compatible with <literal>M</literal>.
The implementing member(s) must be declared as override if they are directly defined in the consumer.</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara><literal>M</literal> must be accessible from <literal>C</literal>.</simpara>
</listitem>
<listitem>
<simpara>An implementation-compatible member <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>M</mi><mi>'</mi></msup></math> must exist in <literal>C</literal>:</simpara>
<orderedlist numeration="loweralpha">
<listitem>
<simpara>if <literal>M</literal> is not a field:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo></mo><mstyle mathvariant="monospace"><mtext>Field</mtext></mstyle><mspace width="3.0mm"/><mo></mo></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo></mo><msup><mi>M</mi><mi>'</mi></msup><mo></mo><mi>C</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="3.0em"/><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi><mi>C</mi><mi>o</mi><mi>m</mi><mi>p</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>b</mi><mi>l</mi><mi>e</mi><mfenced close=")" open="("><msup><mi>M</mi><mi>'</mi></msup><mi>M</mi></mfenced></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="3.0em"/><mo></mo><mfenced close=")" open="("><mrow><msup><mi>M</mi><mi>'</mi></msup><mo></mo><mi>C</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo></mo><msup><mi>M</mi><mi>'</mi></msup><mo>.</mo><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi></mrow></mfenced></math></simpara>
</listitem>
<listitem>
<simpara>if <literal>M</literal> is a field, then either an
implementation-compatible field <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>F</mi><mi>'</mi></msup></math> or accessor pair <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>G</mi><mi>'</mi></msup><mo>,</mo><msup><mi>S</mi><mi>'</mi></msup></math> must exist:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Field</mtext></mstyle><mspace width="3.0mm"/><mo></mo></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo></mo><msup><mi>F</mi><mi>'</mi></msup><mo></mo><mi>C</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mi>s</mi><mi>:</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="3.0em"/><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi><mi>C</mi><mi>o</mi><mi>m</mi><mi>p</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>b</mi><mi>l</mi><mi>e</mi><mfenced close=")" open="("><msup><mi>F</mi><mi>'</mi></msup><mi>M</mi></mfenced></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="3.0em"/><mo></mo><mfenced close=")" open="("><mrow><msup><mi>F</mi><mi>'</mi></msup><mo></mo><mi>C</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo></mo><msup><mi>F</mi><mi>'</mi></msup><mo>.</mo><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi></mrow></mfenced></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo></mo></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo></mo><msup><mi>G</mi><mi>'</mi></msup><mo></mo><mi>C</mi><mo>.</mo><mi>g</mi><mi>e</mi><mi>t</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>,</mo><msup><mi>S</mi><mi>'</mi></msup><mo></mo><mi>C</mi><mo>.</mo><mi>s</mi><mi>e</mi><mi>t</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="3.0em"/><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi><mi>C</mi><mi>o</mi><mi>m</mi><mi>p</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>b</mi><mi>l</mi><mi>e</mi><mfenced close=")" open="("><msup><mi>G</mi><mi>'</mi></msup><mi>M</mi></mfenced></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="3.0em"/><mo></mo><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi><mi>C</mi><mi>o</mi><mi>m</mi><mi>p</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>b</mi><mi>l</mi><mi>e</mi><mfenced close=")" open="("><msup><mi>S</mi><mi>'</mi></msup><mi>M</mi></mfenced></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="3.0em"/><mo></mo><mfenced close=")" open="("><mrow><msup><mi>G</mi><mi>'</mi></msup><mo></mo><mi>C</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo></mo><msup><mi>G</mi><mi>'</mi></msup><mo>.</mo><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi></mrow></mfenced></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="3.0em"/><mo></mo><mfenced close=")" open="("><mrow><msup><mi>S</mi><mi>'</mi></msup><mo></mo><mi>C</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo></mo><msup><mi>S</mi><mi>'</mi></msup><mo>.</mo><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi></mrow></mfenced></math></simpara>
</listitem>
</orderedlist>
</listitem>
</orderedlist>
</requirement>
<simpara>Methods defined in interfaces are automatically declared abstract if they do not provide a default implementation.
This can also be expressed explicitly via adding the <literal>abstract</literal> modifier.
If a class implementing an abstract interface does not implement a method declared in the interface, the class needs to be declared abstract (cf. <xref linkend="_abstract-classes"/>).</simpara>
<simpara>Consequences for method implementation:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>It may be require the implementor to explicitly define a method in order to solve type conflicts produced by methods of different interfaces with same name but different signatures.</simpara>
</listitem>
<listitem>
<simpara>Methods in an implementor cannot decrease the accessibility of methods from implemented interfaces, that is</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo></mo><mi>M</mi><mo></mo><mi>C</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><mi>s</mi><mo>,</mo><msup><mi>M</mi><mi>'</mi></msup><mo></mo><msub><mi>I</mi><mi>i</mi></msub><mo>.</mo><mi>m</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><mi>s</mi><mfenced close=")" open="("><mrow><mi>i</mi><mo>=</mo><mn>1</mn><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mi>n</mi></mrow></mfenced><mi>:</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="2.0em"/><mi>M</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><msup><mi>M</mi><mi>'</mi></msup><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo></mo><mi>M</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo></mo><mi>p</mi><mi>r</mi><mi>i</mi><mi>v</mi><mi>a</mi><mi>t</mi><mi>e</mi><mo></mo><mi>M</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo></mo><msup><mi>M</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi></math></simpara>
</listitem>
<listitem>
<simpara>Methods in the implementor must be a supertype <footnote><simpara>As defined in <xref linkend="_function-type"/> for function types.</simpara></footnote> of methods from implemented interfaces.
That is to say the implemented methods are override-compatible.</simpara>
</listitem>
<listitem>
<simpara>There may be several methods <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>M</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>M</mi><mi>n</mi></msub></math> defined in different implemented interfaces and a single owned method <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>M</mi><mi>'</mi></msup></math> in <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>M</mi><mi>C</mi></msub></math>.
In this case, the above constraints must hold for <emphasis>all</emphasis> methods. In particular, <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>M</mi><mi>'</mi></msup></math>’s signature must conform to all conflicting methods’ signatures.
This is possible by using union types for the arguments and an intersection type as return type.
Such a method <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>M</mi><mi>'</mi></msup></math> is said to <emphasis>resolve</emphasis> the conflict between the implemented (and also inherited) methods.</simpara>
</listitem>
<listitem>
<simpara>Since abstracts methods may become part of the implementor methods, the implementor must either define these methods or it must be declared abstract itself.
Since interfaces are abstract by default, responsibility for implementing abstract methods is passed on to any implementor of interfaces.</simpara>
</listitem>
<listitem>
<simpara>If two implemented interfaces provide (non-abstract) members with the same name, they are not automatically consumed by the implementor even if the types would be similar.
In these cases, the implementor has to redefine the members in order to be aware of possible semantic differences.</simpara>
</listitem>
</orderedlist>
<simpara>There is currently no separate annotation to indicate that methods are implemented or overridden in order to solve conflicts.
We always use the <literal>@Override</literal> annotation.</simpara>
<example>
<title>Method Consumption</title>
<simpara><xref linkend="tab:methodConsumption"/> shows simple examples of above rules.
Assuming that <literal>class C</literal> extends super <literal>class S</literal> and implements interface <literal>I1</literal> and <literal>I2</literal>:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class C extends S implements I1, I2 {...}</programlisting>
</example>
<simpara>The columns describe different scenarios in which a method (with same name) is defined in different classifiers.
We assume that the defined methods are always non-abstract (i.e. have default implementations), non-private and have the same signature.
The last row shows which method will be actually used in class <literal>C</literal>.
If the method is defined in class <literal>C</literal>, and if this method is printed bold, then this means that the method is required to be defined in <literal>C</literal> in order to solve conflicts.</simpara>
<table xml:id="tab:methodConsumption" frame="all" rowsep="1" colsep="1">
<title>Consumption of methods</title>
<tgroup cols="7">
<colspec colname="col_1" colwidth="25*"/>
<colspec colname="col_2" colwidth="12.5*"/>
<colspec colname="col_3" colwidth="12.5*"/>
<colspec colname="col_4" colwidth="12.5*"/>
<colspec colname="col_5" colwidth="12.5*"/>
<colspec colname="col_6" colwidth="12.5*"/>
<colspec colname="col_7" colwidth="12.5*"/>
<tbody>
<row>
<entry align="left" valign="top"><simpara><emphasis role="strong">Interface <literal>I1</literal></emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis>M<subscript>I1</subscript></emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis>M<subscript>I1</subscript></emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis>M<subscript>I1</subscript></emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis>M<subscript>I1</subscript></emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis>M<subscript>I1</subscript></emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis>M<subscript>I1</subscript></emphasis></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><emphasis role="strong">Interface <literal>I2</literal></emphasis></simpara></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"><simpara><emphasis>M<subscript>I2</subscript></emphasis></simpara></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"><simpara><emphasis>M<subscript>I2</subscript></emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis>M<subscript>I2</subscript></emphasis></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><emphasis role="strong">class <literal>S</literal></emphasis></simpara></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"><simpara><emphasis>M<subscript>S</subscript></emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis>M<subscript>S</subscript></emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis>M<subscript>S</subscript></emphasis></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><emphasis role="strong">class <literal>C</literal></emphasis></simpara></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"><simpara><emphasis>M<subscript>C</subscript></emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis role="strong">M<subscript>C</subscript></emphasis></simpara></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"><simpara><emphasis>M<subscript>C</subscript></emphasis></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><emphasis role="strong"><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>C</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi></math></emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis>M<subscript>I1</subscript></emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis>M<subscript>C</subscript></emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis>M<subscript>C</subscript></emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis>M<subscript>S</subscript></emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis>M<subscript>S</subscript></emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis>M<subscript>C</subscript></emphasis></simpara></entry>
</row>
</tbody>
</tgroup>
</table>
<variablelist xml:id="consuming-field-initializers">
<varlistentry>
<term>Consuming Field Initializers </term>
<listitem>
<simpara>Aside from the fields themselves, an implementor <emphasis>always</emphasis> consumes the field initialization if the field is consumed – this is how the consumption is noticed at runtime.</simpara>
</listitem>
</varlistentry>
</variablelist>
<example>
<title>Field and Field Initializer Consumption</title>
<programlisting language="n4js" linenumbering="unnumbered">/* XPECT output ~~~
&lt;==
stdout:
s: C , t: D ,u: I1 ,v: I2
stderr:
==&gt;
~~~ */
interface I0 {
v: string = "I0";
}
interface I1 {
s: string = "I1";
t: string = "I1";
u: string = "I1";
}
interface I2 extends I1, I0 {
@Override
t: string = "I2";
@Override
v: string = "I2";
}
class C {
s: string = "C";
}
class D extends C implements I1, I2 {
@Override
t: string = "D";
}
var d = new D();
console.log(
"s:", d.s, ", t:", d.t, ",u:", d.u, ",v:", d.v
)</programlisting>
<simpara>We expect the following output (for each field):</simpara>
<itemizedlist>
<listitem>
<simpara><literal>d.s = "C"</literal> : <literal>s</literal>: is inherited from <literal>C</literal>, so it is not consumed from <literal>I1</literal> (or <literal>I2</literal>).
Consequently, the initializer of <literal>s</literal> in <literal>C</literal> is used.</simpara>
</listitem>
<listitem>
<simpara><literal>d.t = "D"</literal>: <literal>t</literal> is defined in <literal>D</literal>, solving a conflict stemming from the definition of <literal>t</literal> in <literal>I1</literal> and <literal>I2</literal>. Thus, the initializer of <literal>t</literal> in <literal>D</literal> is used.</simpara>
</listitem>
<listitem>
<simpara><literal>d.u = "I1"</literal> : <literal>u</literal> is only defined in <literal>I1</literal>, thus the initializer defined in <literal>I1</literal> is used.</simpara>
</listitem>
<listitem>
<simpara><literal>d.v = "I2"</literal> : <literal>v</literal> is overridden in <literal>I2</literal>, so is the field initializer. This is why <literal>d.v</literal> must be assigned to <literal>I2</literal> and not <literal>I0</literal>.</simpara>
</listitem>
</itemizedlist>
</example>
</section>
</section>
</section>
</section>
<section xml:id="_structural-typing">
<title>Structural Typing</title>
<simpara>In general, N4JS uses nominal typing.
This is to say that a duck is a duck only if it is declared to be a duck.
In particular when working with external APIs, it is more convenient to use structural or duck typing.
That is, a thing that can swim and quacks like a duck, is a duck.</simpara>
<simpara role="language-n4js">Interfaces or classes can be used for this purpose with a <emphasis>typing strategy modifier</emphasis>.
Given a type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math>, the simple <literal>~</literal> (tilde) can be added to its declaration (on definition-site) or in a reference (on use-site) to indicate that the type system should use structural typing
rather than nominal typing.<footnote><simpara>This kind of typing is used by TypeScript only. By defining a structural typed classifier or reference, it basically behaves as it would behave – without that modifier – in TypeScript.</simpara></footnote>
This means that some other type must provide the same members as type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math> to be deemed a structural subtype.
However, the operator cannot be used anymore with the type or reference as this operator relies on the declaration information (or at least the closest thing available at runtime).
In this case, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math> is, therefore, always a structural subtype of <emphasis>~T</emphasis>.</simpara>
<simpara role="language-n4js">Sometimes it is convenient to refer only to the fields of a classifier, for example when the initial field values are to be provided in a variable passed to the constructor.
In that case, the type can be modified with <literal>~~</literal> (two tildes). This is only possible on use-site, i.e. on type references.
Furthermore, only on the use-site, it is possible to consider only either readable or writable or fields by using the read-only <literal>~r~</literal> or write-only <literal>~w~</literal> structural field typing.
For initialization blocks, it is even possible to use structural initializer field typing via the <literal>~i~</literal> operator.</simpara>
<section xml:id="_syntax-7" role="language-n4js">
<title>Syntax</title>
<simpara>Structural typing is specified using the typing strategy modifier. There
are two modifiers defined; one for definition-site and one for use-site
structural typing.</simpara>
<formalpara xml:id="lst:Structural_Type_Operator_and_References">
<title>Structural Type Operator and References</title>
<para>
<programlisting language="xtext" linenumbering="unnumbered">TypingStrategyUseSiteOperator returns TypingStrategy:
'~' ('~' | STRUCTMODSUFFIX)?;
TypingStrategyDefSiteOperator returns TypingStrategy:
'~';
terminal STRUCTMODSUFFIX:
('r' | 'i' | 'w') '~'
;
ParameterizedTypeRefStructural returns ParameterizedTypeRefStructural:
definedTypingStrategy=TypingStrategyUseSiteOperator
declaredType=[Type|TypeReferenceName]
(=&gt; '&lt;' typeArgs+=TypeArgument (',' typeArgs+=TypeArgument)* '&gt;')?
(=&gt; 'with' '{' astStructuralMembers+=TStructMember* '}')?
;
ThisTypeRefStructural returns ThisTypeRefStructural:
definedTypingStrategy=TypingStrategyUseSiteOperator
'this'
('with' '{' astStructuralMembers+=TStructMember* '}')?
;</programlisting>
</para>
</formalpara>
</section>
<section xml:id="_definition-site-structural-typing" role="language-n4js">
<title>Definition Site Structural Typing</title>
<simpara>An interface or class can be defined to be used with structural typing by adding the structural modifier to its definition (or, in case of external classes, to the declaration).
This changes the default type system strategy from nominal to structural typing for that type.
That means that all types with the same members as the specified type are subtypes of that type, except for subtypes of <literal>N4Object</literal>.
In the latter case, programmers are enforced to nominal declare the type relation.</simpara>
<simpara>If a type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math> is declared as structural at its definition, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi></math> is true.</simpara>
<requirement xml:id="IDE-75">
<title>Definition Site Structural Typing</title>
<simpara>
<anchor xml:id="Req-IDE-75" xreflabel="[Req-IDE-75]"/>
<emphasis role="strong">Requirement: IDE-75:</emphasis>
<link linkend="Req-IDE-75">Definition Site Structural Typing</link> (ver. 1)</simpara>
<simpara>
</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>The structurally defined type cannot be used on the right hand side of the <literal>instanceof</literal> operator:
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>x</mi><mi> </mi><mstyle mathvariant="monospace"><mtext>instanceof</mtext></mstyle><mi> </mi><mi>T</mi><mo></mo><mo>¬</mo><mi>T</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi></math></simpara>
</listitem>
<listitem>
<simpara>A type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math> is a subtype of a structurally defined type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math> either</simpara>
<orderedlist numeration="loweralpha">
<listitem>
<simpara>if it is not a subtype of <literal>N4Object</literal> <footnote><simpara>We enforce programmers of N4JS to use nominal typing, therefore, it is not possible to bypass that principle by declaring a type as structural for normally defined classes (except those explicitly derived from <literal>N4Object</literal>).</simpara></footnote>
but it contains all public, non-static members of that type</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi><mspace width="1.0mm"/><mo></mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>N4Object</mtext></mstyle></mfenced><mspace width="3.0em"/><mi>T</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>}</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>m</mi><mo></mo><mi>T</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>,</mo><mi>m</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo>,</mo><mo>¬</mo><mi>m</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo>,</mo><mi>m</mi><mo></mo><mi>T</mi><mo>.</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mi>:</mi><mi>}</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="1.0em"/><mo></mo><msup><mi>m</mi><mi>'</mi></msup><mo></mo><mi>X</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi><mi>}</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="2.0em"/><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo></mo><mo>¬</mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo></mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>m</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mi>}</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="2.0em"/><mo></mo><mi>Γ</mi><mo></mo><msup><mi>m</mi><mi>'</mi></msup><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>m</mi><mi>}</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mspace width="2.0em"/><mo></mo><mi>μ</mi><mfenced close=")" open="("><mi>m</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Field</mtext></mstyle><mo></mo><mi>Γ</mi><mo></mo><mi>m</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><msup><mi>m</mi><mi>'</mi></msup></mrow><mrow><mspace width="13.0em"/><mi>Γ</mi><mo></mo><mi>X</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>T</mi><mspace width="13.0em"/></mrow></mfrac></math><?asciidoc-br?>
or</simpara>
</listitem>
<listitem>
<simpara>if it is a subtype of <literal>N4Object</literal> which explicitly extends or implements the
structurally defined type.</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>X</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>N4Object</mtext></mstyle></mfenced><mspace width="3.0mm"/><mi>T</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mspace width="3.0mm"/><mrow><mi>T</mi><mo></mo><mi>X</mi><mo>.</mo><mi>s</mi><mi>u</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><msup><mi>s</mi><mo>*</mo></msup></mrow></mrow><mrow><mi>Γ</mi><mo></mo><mi>X</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>T</mi></mrow></mfrac></math>
</listitem>
<listitem>
<simpara>A structurally defined type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math> is implicitly derived
from <literal>Object</literal> if no other type is specified. In particular, a structurally
defined type must not be inherited from</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mi>T</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi></mrow><mrow><mi>Γ</mi><mo></mo><mi>T</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>Object</mtext></mstyle></mrow></mfrac></math></simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mo></mo><mi>Γ</mi><mo></mo><mi>T</mi><mspace width="1.0mm"/><mo></mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>N4Object</mtext></mstyle><mo></mo><mstyle mathvariant="monospace"><mtext>N4Object</mtext></mstyle><mo></mo><mi>T</mi><mo>.</mo><mi>s</mi><mi>u</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><msup><mi>s</mi><mo>*</mo></msup></math></simpara>
</listitem>
</orderedlist>
</listitem>
</orderedlist>
</requirement>
<example>
<title>Declaration Site Structural Typing</title>
<simpara>The following snippet demonstrates the effect of definition-site structural types by comparing them to
nominal declared types: <anchor xml:id="ex:declaration-site-structural-typing" xreflabel="[ex:declaration-site-structural-typing]"/></simpara>
<formalpara>
<title>Declaration Site Structural Typing</title>
<para>
<programlisting language="n4js" linenumbering="unnumbered">interface ~Tilde { x; y; }
interface Nominal { x; y; }
class C { public x; public y;}
class D extends C implements Tilde { }
function f(p: Tilde) {}
function g(p: Nominal) {}
f(new C()); // error: nominal typing, C does not implement ~Tilde
f(new D()); // ok, D is a nominal subtype (as it implements Tilde)
f({x:10,y:10}); // ok: Tilde is used with structural typing for non-N4-classifiers</programlisting>
</para>
</formalpara>
<simpara>Definition site structural typing may lead to unexpected results. For
example;</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class C{}
class ~E extends C{}</programlisting>
<simpara>It may be unexpected, but <literal>E</literal> is not a subtype of <literal>C</literal>, i.e.
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>E</mi><mspace width="1.0mm"/><mo></mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>C</mi></math>! E.g., <literal>instanceof</literal> won’t work with <literal>E</literal>, while it works
with <literal>C</literal>!</simpara>
</example>
</section>
<section xml:id="_use-site-structural-typing" role="language-n4js">
<title>Use-Site Structural Typing</title>
<simpara>Use-site structural typing offers several typing strategy modifiers to define the accessability of public properties of classes and interfaces.
They can be used e.g. on variable declarations like this: <literal>var c : ~C</literal>.
The table <xref linkend="tab:available-fields-of-structural-types"/> shows which properties of structural types can be accessed in the different type strategies.
For example, when using the write-only structural strategy (i.e. <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>w</mtext></mstyle><mi>~</mi><mstyle mathvariant="monospace"><mtext>X</mtext></mstyle></math>), only the writeable fields, can be accessed for writing.
In the table, the term field to both, public datafields and accessors of type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math>.
Non-public properties are never accessable in use-site structural types.
In any field-structural type, methods of the referenced nominal type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math> are not available.
The initializer structural typing provides readable fields for every writeable field of the references type.</simpara>
<table xml:id="tab:available-fields-of-structural-types" frame="all" rowsep="1" colsep="1">
<title>Available Fields of Structural Types</title>
<tgroup cols="6">
<colspec colname="col_1" colwidth="16.6666*"/>
<colspec colname="col_2" colwidth="16.6666*"/>
<colspec colname="col_3" colwidth="16.6666*"/>
<colspec colname="col_4" colwidth="16.6666*"/>
<colspec colname="col_5" colwidth="16.6666*"/>
<colspec colname="col_6" colwidth="16.667*"/>
<thead>
<row>
<entry align="left" valign="top">Property of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math></entry>
<entry align="center" valign="top"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>X</mtext></mstyle></math></entry>
<entry align="center" valign="top"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>X</mtext></mstyle></math></entry>
<entry align="center" valign="top"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>r</mtext></mstyle><mi>~</mi><mstyle mathvariant="monospace"><mtext>X</mtext></mstyle></math></entry>
<entry align="center" valign="top"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>w</mtext></mstyle><mi>~</mi><mstyle mathvariant="monospace"><mtext>X</mtext></mstyle></math></entry>
<entry align="center" valign="top"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>i</mtext></mstyle><mi>~</mi><mstyle mathvariant="monospace"><mtext>X</mtext></mstyle></math></entry>
</row>
</thead>
<tbody>
<row>
<entry align="left" valign="top"><simpara>public method</simpara></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi></mi></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi></mi></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi></mi></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi></mi></math></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>public writable field</simpara></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi></mi></math></simpara></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi></mi></math></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>public readable field</simpara></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi></mi></math></simpara></entry>
<entry align="center" valign="top"><simpara>writable fields</simpara></entry>
</row>
</tbody>
</tgroup>
</table>
<simpara>Multiple structural typing strategies can be nested when there are multiple use sites, like in the example <xref linkend="ex:nested-structural-typing-strategies"/> below at the locations ST1 and ST2.
In the example, the datafield <literal>a.field</literal> has the nested structural type <literal>~r~ ~i~ A</literal> and thus the datafield <literal>a.field.df</literal> is readable.
Nested structural types are evaluated on the fly when doing subtype checks.</simpara>
<example xml:id="ex:nested-structural-typing-strategies">
<title>Nested Structural Typing Strategies</title>
<programlisting language="n4js" linenumbering="unnumbered">class A {
public df : string;
}
interface I&lt;T&gt; {
public field : ~r~T; // ST1
}
var a : ~i~A; // ST2</programlisting>
</example>
<simpara>The following example demonstrates the effect of the structural type modifiers:</simpara>
<example>
<title>Effect of structural type modifiers on use-site</title>
<simpara>Let’s assume the type defined on the left.
The following <emphasis>pseudo</emphasis> code snippets explicitly list the type with its members virtually created by a structural modifier.
Note that this is pseudo code, as there are no real or virtual types created.
Instead, only the subtype relation is defined accordingly:</simpara>
<simpara>Effect of structural type modifiers on use-site</simpara>
<informaltable frame="all" rowsep="1" colsep="1">
<tgroup cols="3">
<colspec colname="col_1" colwidth="33.3333*"/>
<colspec colname="col_2" colwidth="33.3333*"/>
<colspec colname="col_3" colwidth="33.3334*"/>
<tbody>
<row>
<entry align="center" valign="top" namest="col_1" nameend="col_3"><simpara><emphasis role="strong">Effect of structural type modifiers on use-site</emphasis></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><programlisting language="n4js" linenumbering="unnumbered">var c:C
class C {
private x;
public y;
public f()
private g()
public get z():Z
public set z(z:Z)
}
interface I {
a;
func();
}</programlisting></entry>
<entry align="left" valign="top"><programlisting language="n4js" linenumbering="unnumbered">var cstructural:~C
class cstructural {
public y;
public f()
public get z():Z
public set z(z:Z)
}
interface ~I {
a;
func();
}</programlisting></entry>
<entry align="left" valign="top"><programlisting language="n4js" linenumbering="unnumbered">var cfields:~~C
class cfields {
public y;
public get z():Z
public set z(z:Z)
}
interface ~~I {
a;
}</programlisting></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">Type</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis role="strong">Structural Type</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis role="strong">Structural Field Type</emphasis></simpara></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<informaltable frame="all" rowsep="1" colsep="1">
<tgroup cols="3">
<colspec colname="col_1" colwidth="33.3333*"/>
<colspec colname="col_2" colwidth="33.3333*"/>
<colspec colname="col_3" colwidth="33.3334*"/>
<tbody>
<row>
<entry align="left" valign="top"><programlisting language="n4js" linenumbering="unnumbered">var crofields:~r~C
class crofields {
public get y():Y
public get z():Z
}
interface ~r~I {
public get a():A
}</programlisting></entry>
<entry align="left" valign="top"><programlisting language="n4js" linenumbering="unnumbered">var cwofields:~w~C
class cwofields {
public set y(y:Y)
public set z(z:Z)
}
interface ~w~I {
public set a(a:A)
}</programlisting></entry>
<entry align="left" valign="top"><programlisting language="n4js" linenumbering="unnumbered">var cinitfields:~i~C
class cinitfields {
public get y():Y
public get z():Z
}
interface ~i~I {
public get a():A
}</programlisting></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">Structural Read-only Field Type</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis role="strong">Structural Write-only Field Type</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis role="strong">Structural Initializer Field Type</emphasis></simpara></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<simpara><?asciidoc-pagebreak?></simpara>
<simpara>Note that even if a type is defined without the structural modifier, it is not possible to use <literal>instanceof</literal> for variables declared as structural, as shown in the next example:</simpara>
<informaltable frame="all" rowsep="1" colsep="1">
<tgroup cols="3">
<colspec colname="col_1" colwidth="33.3333*"/>
<colspec colname="col_2" colwidth="33.3333*"/>
<colspec colname="col_3" colwidth="33.3334*"/>
<tbody>
<row>
<entry align="left" valign="top"><programlisting language="n4js" linenumbering="unnumbered">class C {..}
interface I {..}
foo(c: C, i: I) {
c instanceof C; // ok
c instanceof I; // ok
}</programlisting></entry>
<entry align="left" valign="top"><programlisting language="n4js" linenumbering="unnumbered">class C {..}
interface I {..}
foo(c: ~C, i: ~I) {
c instanceof C; // error
c instanceof I; // error
}</programlisting></entry>
<entry align="left" valign="top"><programlisting language="n4js" linenumbering="unnumbered">class C {..}
interface I {..}
foo(c: ~~C, i: ~~I) {
c instanceof C; // error
C instanceof I; // error
}</programlisting></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">Type</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis role="strong">Structural Type</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><emphasis role="strong">Structural Field Type</emphasis></simpara></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<itemizedlist>
<listitem>
<simpara>If a type is referenced with the structural type modifier <literal>~</literal> , the property <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi></math> is true.</simpara>
</listitem>
<listitem>
<simpara>If a type is referenced with the structural field type modifier <literal>~~</literal>, the property <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>F</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi></math> is true.</simpara>
</listitem>
<listitem>
<simpara>If a type is referenced with the structural read-only field type modifier <literal>~r~</literal>, the property <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>R</mi><mi>e</mi><mi>a</mi><mi>d</mi><mi>O</mi><mi>n</mi><mi>l</mi><mi>y</mi><mi>F</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi></math> is true.</simpara>
</listitem>
<listitem>
<simpara>If a type is referenced with the structural write-only field type modifier <literal>~w~</literal>, then the property <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>W</mi><mi>r</mi><mi>i</mi><mi>t</mi><mi>e</mi><mi>O</mi><mi>n</mi><mi>l</mi><mi>y</mi><mi>F</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi></math> is true.</simpara>
</listitem>
<listitem>
<simpara>If a type is referenced with the structural initializer field type modifier <literal>~i~</literal>, then the property <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>I</mi><mi>n</mi><mi>i</mi><mi>t</mi><mi>F</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi></math> is true.</simpara>
</listitem>
</itemizedlist>
<simpara>We call the following:</simpara>
<itemizedlist>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math> the (nominal) type T,</simpara>
</listitem>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math> the structural version of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math>,</simpara>
</listitem>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math> the structural field version of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math>,</simpara>
</listitem>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>r</mtext></mstyle><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math> the structural read-only field,</simpara>
</listitem>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>w</mtext></mstyle><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math> the structural write-only field and</simpara>
</listitem>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>i</mtext></mstyle><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math> the structural initializer field version of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math>.</simpara>
</listitem>
</itemizedlist>
</example>
<requirement xml:id="IDE-76">
<title>Use-Site Structural Typing</title>
<simpara>
<anchor xml:id="Req-IDE-76" xreflabel="[Req-IDE-76]"/>
<emphasis role="strong">Requirement: IDE-76:</emphasis>
<link linkend="Req-IDE-76">Use-Site Structural Typing</link> (ver. 1)</simpara>
<simpara>
1. The structural version of a type is a supertype of the nominal type:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>:</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math>
2. The structural field version of a type is a supertype of the structural type:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>:</mi><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math>
3. The structural read-only field version of a type is a supertype of the structural field type:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>:</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>r</mtext></mstyle><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math>
4. The structural write-only field version of a type is a supertype of the structural field type:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>:</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>w</mtext></mstyle><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math>
5. The structural (field) version of a type cannot be used on the right hand side of the <literal>instanceof</literal> operator:</simpara>
<simpara>+</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mi>x</mi><mi> </mi><mstyle mathvariant="monospace"><mtext>instanceof</mtext></mstyle><mi> </mi><mi>E</mi><mo></mo><mi>Γ</mi><mo></mo><mi>E</mi><mi>:</mi><mi>T</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="3.0em"/><mo></mo><mo>¬</mo><mrow><mo>(</mo><mi>T</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi></mrow></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="6.0em"/><mo></mo><mi>T</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>F</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="6.0em"/><mo></mo><mi>T</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>R</mi><mi>e</mi><mi>a</mi><mi>d</mi><mi>O</mi><mi>n</mi><mi>l</mi><mi>y</mi><mi>F</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="6.0em"/><mo></mo><mi>T</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>W</mi><mi>r</mi><mi>i</mi><mi>t</mi><mi>e</mi><mi>O</mi><mi>n</mi><mi>l</mi><mi>y</mi><mi>F</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mspace width="3.0mm"/><mspace width="6.0em"/><mo></mo><mi>T</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>I</mi><mi>n</mi><mi>i</mi><mi>t</mi><mi>F</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mo>)</mo></mrow></math></simpara>
<simpara>+
That is, the following code will always issue an error: <literal>x instanceof ~T</literal> <footnote><simpara>Since this is already prevented by the parser (the tilde is interpreted as an unary operator), error messages are likely to look a little strange.</simpara></footnote>.
6. A type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math> is a subtype of a structural version of a type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math>, if it contains all public, non-static members of the type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math>: <footnote><simpara>Note that due to this relaxed definition (compared with definition-site structural types) it is possible to pass an <literal>N4Object</literal> instance to a function of method with a declared structural type parameter.</simpara></footnote></simpara>
<simpara>+</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>m</mi><mo></mo><mi>T</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>,</mo><mi>m</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo></mo><mstyle mathvariant="monospace"><mtext>N4Object</mtext></mstyle><mo>,</mo><mi>m</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo>,</mo><mo>¬</mo><mi>m</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo>,</mo><mi>m</mi><mo></mo><mi>T</mi><mo>.</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mi>:</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="1.0em"/><mo></mo><msup><mi>m</mi><mi>'</mi></msup><mo></mo><mi>X</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="2.0em"/><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo></mo><mo>¬</mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo></mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>m</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mspace width="2.0em"/><mo></mo><mi>Γ</mi><mo></mo><msup><mi>m</mi><mi>'</mi></msup><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>Γ</mi><mo></mo><mi>m</mi></mrow><mrow><mspace width="13.0em"/><mi>Γ</mi><mo></mo><mi>X</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>Γ</mi><mo></mo><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mspace width="13.0em"/></mrow></mfrac></math><?asciidoc-br?>
7. A type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math> is a subtype of a structural field version of a type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math>, if it contains all public, non-static fields of the type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math>.<?asciidoc-br?>
Special cases regarding optional fields are described in <link linkend="optional-fields">Optional Fields</link>.</simpara>
<simpara>+</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>m</mi><mo></mo><mi>T</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mi>s</mi><mo>,</mo><mi>m</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo></mo><mstyle mathvariant="monospace"><mtext>N4Object</mtext></mstyle><mo>,</mo><mi>m</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo>,</mo><mo>¬</mo><mi>m</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="1.0em"/><mo></mo><mi> </mi><mo></mo><msup><mi>m</mi><mi>'</mi></msup><mo></mo><mi>X</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mi>s</mi><mi>:</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0em"/><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo></mo><mo>¬</mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo></mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>m</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0em"/><mo></mo><mi>Γ</mi><mo></mo><msup><mi>m</mi><mi>'</mi></msup><mi>:</mi><msub><mi>T</mi><mi>m</mi></msub><mo></mo><mi>Γ</mi><mo></mo><mi>m</mi><mi>:</mi><msub><mi>T</mi><msup><mi>m</mi><mi>'</mi></msup></msub><mo></mo><msub><mi>T</mi><mi>m</mi></msub><mo>=</mo><msub><mi>T</mi><msup><mi>m</mi><mi>'</mi></msup></msub><mi>}</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mspace width="3.0em"/><mo></mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>a</mi><mi>b</mi><mi>i</mi><mi>l</mi><mi>i</mi><mi>t</mi><mi>y</mi><mo></mo><mi>m</mi><mo>.</mo><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>a</mi><mi>b</mi><mi>i</mi><mi>l</mi><mi>i</mi><mi>t</mi><mi>y</mi></mrow><mrow><mspace width="13.0em"/><mi>Γ</mi><mo></mo><mi>X</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mspace width="13.0em"/></mrow></mfrac></math></simpara>
<simpara>+
8. A type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math> is a subtype of a structural read-only field version of a type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>r</mtext></mstyle><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math>, if it contains all public and non-static readable fields of the type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math>.
Special cases regarding optional fields are described in <link linkend="optional-fields">Optional Fields</link>.</simpara>
<simpara>+</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>m</mi><mo></mo><mi>T</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mi>s</mi><mo></mo><mi>T</mi><mo>.</mo><mi>g</mi><mi>e</mi><mi>t</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>,</mo><mi>m</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo></mo><mstyle mathvariant="monospace"><mtext>N4Object</mtext></mstyle><mo>,</mo><mi>m</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo>,</mo><mo>¬</mo><mi>m</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="1.0em"/><mo></mo><mi> </mi><mo></mo><msup><mi>m</mi><mi>'</mi></msup><mo></mo><mi>X</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mi>s</mi><mo></mo><mi>X</mi><mo>.</mo><mi>g</mi><mi>e</mi><mi>t</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0em"/><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo></mo><mo>¬</mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo></mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>m</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0em"/><mo></mo><mi>Γ</mi><mo></mo><msup><mi>m</mi><mi>'</mi></msup><mi>:</mi><msub><mi>T</mi><mi>m</mi></msub><mo></mo><mi>Γ</mi><mo></mo><mi>m</mi><mi>:</mi><msub><mi>T</mi><msup><mi>m</mi><mi>'</mi></msup></msub><mo></mo><msub><mi>T</mi><mi>m</mi></msub><mo>=</mo><msub><mi>T</mi><msup><mi>m</mi><mi>'</mi></msup></msub></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mspace width="3.0em"/><mo></mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>a</mi><mi>b</mi><mi>i</mi><mi>l</mi><mi>i</mi><mi>t</mi><mi>y</mi><mo></mo><mi>m</mi><mo>.</mo><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>a</mi><mi>b</mi><mi>i</mi><mi>l</mi><mi>i</mi><mi>t</mi><mi>y</mi></mrow><mrow><mspace width="13.0em"/><mi>Γ</mi><mo></mo><mi>X</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>~</mi><mstyle mathvariant="monospace"><mtext>r</mtext></mstyle><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mspace width="13.0em"/></mrow></mfrac></math></simpara>
<simpara>+
9. A type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math> is a subtype of a structural write-only field version of a type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>w</mtext></mstyle><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math>, if it contains all public and non-static writable fields of the type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math>. Special cases regarding optional fields are described in <link linkend="optional-fields">Optional Fields</link>.</simpara>
<simpara>+</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>m</mi><mo></mo><mi>T</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mi>s</mi><mo></mo><mi>T</mi><mo>.</mo><mi>s</mi><mi>e</mi><mi>t</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>,</mo><mi>m</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo></mo><mstyle mathvariant="monospace"><mtext>N4Object</mtext></mstyle><mo>,</mo><mi>m</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo>,</mo><mo>¬</mo><mi>m</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo>,</mo><mo>¬</mo><mi>m</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="1.0em"/><mo></mo><mi> </mi><mo></mo><msup><mi>m</mi><mi>'</mi></msup><mo></mo><mi>X</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mi>s</mi><mo></mo><mi>X</mi><mo>.</mo><mi>s</mi><mi>e</mi><mi>t</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0em"/><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo></mo><mo>¬</mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo></mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>m</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0em"/><mo></mo><mi>Γ</mi><mo></mo><msup><mi>m</mi><mi>'</mi></msup><mi>:</mi><msub><mi>T</mi><mi>m</mi></msub><mo></mo><mi>Γ</mi><mo></mo><mi>m</mi><mi>:</mi><msub><mi>T</mi><msup><mi>m</mi><mi>'</mi></msup></msub><mo></mo><msub><mi>T</mi><mi>m</mi></msub><mo>=</mo><msub><mi>T</mi><msup><mi>m</mi><mi>'</mi></msup></msub></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mspace width="3.0em"/><mo></mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>a</mi><mi>b</mi><mi>i</mi><mi>l</mi><mi>i</mi><mi>t</mi><mi>y</mi><mo></mo><mi>m</mi><mo>.</mo><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>a</mi><mi>b</mi><mi>i</mi><mi>l</mi><mi>i</mi><mi>t</mi><mi>y</mi></mrow><mrow><mspace width="13.0em"/><mi>Γ</mi><mo></mo><mi>X</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>~</mi><mstyle mathvariant="monospace"><mtext>w</mtext></mstyle><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mspace width="13.0em"/></mrow></mfrac></math></simpara>
<simpara>+
10. A type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math> is a subtype of a structural field version of a type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>this</mtext></mstyle></math>, if it contains all public, non-static fields,
either defined via data fields or field get accessors, of the inferred type of <literal>this</literal>.
Special cases regarding optional fields are described in <link linkend="optional-fields">Optional Fields</link>.</simpara>
<simpara>+</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>Γ</mi><mo></mo><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi><mi>:</mi><mi>T</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>m</mi><mo></mo><mi>T</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mi>s</mi><mo></mo><mi>T</mi><mo>.</mo><mi>s</mi><mi>e</mi><mi>t</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>,</mo><mi>m</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo></mo><mstyle mathvariant="monospace"><mtext>N4Object</mtext></mstyle><mo>,</mo><mi>m</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo>,</mo><mo>¬</mo><mi>m</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>m</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo></mo><mstyle mathvariant="monospace"><mtext>null</mtext></mstyle></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="1.0em"/><mo></mo><mi> </mi><mo></mo><msup><mi>m</mi><mi>'</mi></msup><mo></mo><mi>X</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mi>s</mi><mo></mo><mi>X</mi><mo>.</mo><mi>g</mi><mi>e</mi><mi>t</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0em"/><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo></mo><mo>¬</mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo></mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>m</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mspace width="3.0em"/><mo></mo><mi>Γ</mi><mo></mo><msup><mi>m</mi><mi>'</mi></msup><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>m</mi><mo></mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>a</mi><mi>b</mi><mi>i</mi><mi>l</mi><mi>i</mi><mi>t</mi><mi>y</mi><mo></mo><mi>m</mi><mo>.</mo><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>a</mi><mi>b</mi><mi>i</mi><mi>l</mi><mi>i</mi><mi>t</mi><mi>y</mi></mrow><mrow><mspace width="13.0em"/><mi>Γ</mi><mo></mo><mi>X</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>t</mtext></mstyle><mi>h</mi><mi>i</mi><mi>s</mi><mspace width="13.0em"/></mrow></mfrac></math></simpara>
<simpara>+
11. A structural field type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math> is a subtype of a structural type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>X</mtext></mstyle></math>, if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>X</mtext></mstyle></math> only contains fields (except methods inherited from <literal>Object</literal>) and if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>X</mtext></mstyle></math>.</simpara>
<simpara>+</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>X</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><mi>s</mi><mo></mo><mstyle mathvariant="monospace"><mtext>Object</mtext></mstyle><mo>.</mo><mi>m</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><mi>s</mi><mo>=</mo><mi></mi><mo></mo><mi>Γ</mi><mo></mo><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>X</mtext></mstyle></mrow><mrow><mi>Γ</mi><mo></mo><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>~</mi><mstyle mathvariant="monospace"><mtext>X</mtext></mstyle></mrow></mfrac></math>
<simpara>+
12. Use-site structural typing cannot be used for declaring supertypes of classes or interfaces.
That is to say that structural types cannot be used after <literal>extends</literal>, <literal>implements</literal> or <literal>with</literal> in type declarations <footnote><simpara>This is already constrained by the grammar.</simpara></footnote>.</simpara>
</requirement>
<simpara>Note that all members of <literal>N4Object</literal> are excluded.
This implies that extended reflective features (cf. <xref linkend="_reflection-meta-information"/> ) are not available in the context of structural typing.
The <literal>instanceof</literal> operator is still working as described in <xref linkend="_relational-expression"/>, in that it can be used to check the type of an instance.</simpara>
<simpara>If a type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math> is a (nominal) subtype of T, it is, of course, also a subtype of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math>:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>Γ</mi><mo></mo><mi>X</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>Γ</mi><mo></mo><mi>T</mi></mrow><mrow><mi>Γ</mi><mo></mo><mi>X</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>Γ</mi><mo></mo><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></mrow></mfrac></math>
<simpara>This is only a shortcut for the type inference defined above.</simpara>
<requirement xml:id="IDE-77">
<title>Definition and Use-Site Precedence</title>
<simpara>
<anchor xml:id="Req-IDE-77" xreflabel="[Req-IDE-77]"/>
<emphasis role="strong">Requirement: IDE-77:</emphasis>
<link linkend="Req-IDE-77">Definition and Use-Site Precedence</link> (ver. 1)</simpara>
<simpara>
If a type is structurally typed on both definition and use-site, the rules for
use-site structural typing (<xref linkend="Req-IDE-76"/>) are
applied.</simpara>
<example>
<title>Use-Site Structural Typing</title>
<simpara>The following example demonstrates the effect of the structural (field) modifier, used in this case for function parameters.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">interface I { public x: number; public foo()};
class C { public x: number; public foo() {}};
function n(p: I) {}
function f(p: ~I) {}
function g(p: ~~I) {}
n(new C()); // error: nominal typing, C does not implement I
f(new C()); // ok: C is a (structural) subtype of ~I
f({x:10}); // error, the object literal does not provide function foo()
g({x:10}); // ok: object literal provides all fields of I</programlisting>
</example>
<example>
<title>Structural types variable and instanceof operator</title>
<simpara>It is possible to use a variable typed with a structural version of a type on the left hand side of the <literal>instanceof</literal> operator, as demonstrated in this example:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class C {
public x;
public betterX() { return this.x||1;}
}
function f(p: ~~C) {
if (p instanceof C) {
console.log((p as C).betterX);
} else {
console.log(p.x||1);
}
}</programlisting>
</example>
<simpara>The following table describes the member availability of <literal>X</literal> in various
typing scenarios. Such as <literal>~~X</literal>, <literal>~r~X</literal>, <literal>~w~X</literal>, <literal>~i~X</literal>.</simpara>
<table role="language-n4js" frame="all" rowsep="1" colsep="1">
<title>Member Availability in various Typing Scenarios</title>
<tgroup cols="5">
<colspec colname="col_1" colwidth="33.3333*"/>
<colspec colname="col_2" colwidth="16.6666*"/>
<colspec colname="col_3" colwidth="16.6666*"/>
<colspec colname="col_4" colwidth="16.6666*"/>
<colspec colname="col_5" colwidth="16.6669*"/>
<thead>
<row>
<entry align="left" valign="top">Member of type <emphasis>X</emphasis></entry>
<entry align="center" valign="top"><literal>~~X</literal></entry>
<entry align="center" valign="top"><literal>~r~X</literal></entry>
<entry align="center" valign="top"><literal>~w~X</literal></entry>
<entry align="center" valign="top"><literal>~i~X</literal></entry>
</row>
</thead>
<tbody>
<row>
<entry align="left" valign="top"><simpara><literal>private m0;</literal></simpara></entry>
<entry align="center" valign="top"><simpara>&#8201;&#8212;&#8201;</simpara></entry>
<entry align="center" valign="top"><simpara>&#8201;&#8212;&#8201;</simpara></entry>
<entry align="center" valign="top"><simpara>&#8201;&#8212;&#8201;</simpara></entry>
<entry align="center" valign="top"><simpara>&#8201;&#8212;&#8201;</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>public set m1(m) { }</literal></simpara></entry>
<entry align="center" valign="top"><simpara>write</simpara></entry>
<entry align="center" valign="top"><simpara>&#8201;&#8212;&#8201;</simpara></entry>
<entry align="center" valign="top"><simpara>write</simpara></entry>
<entry align="center" valign="top"><simpara>read</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>public get m2() {&#8230;&#8203;}</literal></simpara></entry>
<entry align="center" valign="top"><simpara>read</simpara></entry>
<entry align="center" valign="top"><simpara>read</simpara></entry>
<entry align="center" valign="top"><simpara>&#8201;&#8212;&#8201;</simpara></entry>
<entry align="center" valign="top"></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>public m3;</literal></simpara></entry>
<entry align="center" valign="top"><simpara>read/write</simpara></entry>
<entry align="center" valign="top"><simpara>read</simpara></entry>
<entry align="center" valign="top"><simpara>write</simpara></entry>
<entry align="center" valign="top"><simpara>read</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>public m4 = 'init.m4';</literal></simpara></entry>
<entry align="center" valign="top"><simpara>read/write</simpara></entry>
<entry align="center" valign="top"><simpara>read</simpara></entry>
<entry align="center" valign="top"><simpara>write</simpara></entry>
<entry align="center" valign="top"><simpara>read <emphasis>?</emphasis></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>public m5: any?;</literal></simpara></entry>
<entry align="center" valign="top"><simpara>read<emphasis>?</emphasis>/write</simpara></entry>
<entry align="center" valign="top"><simpara>read<emphasis>?</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara>write</simpara></entry>
<entry align="center" valign="top"><simpara>read<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>?</mi></math></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>@Final public m6 = 'init.m6';</literal></simpara></entry>
<entry align="center" valign="top"><simpara>read</simpara></entry>
<entry align="center" valign="top"><simpara>read</simpara></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>@Final public m7;</literal></simpara></entry>
<entry align="center" valign="top"><simpara>read</simpara></entry>
<entry align="center" valign="top"><simpara>read</simpara></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"><simpara>read</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>public get m8() {&#8230;&#8203;}</literal></simpara></entry>
<entry align="center" valign="middle" morerows="1"><simpara>read/write</simpara></entry>
<entry align="center" valign="middle" morerows="1"><simpara>read</simpara></entry>
<entry align="center" valign="middle" morerows="1"><simpara>write</simpara></entry>
<entry align="center" valign="middle" morerows="1"><simpara>read</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>public set m8(m) { }</literal></simpara></entry>
</row>
</tbody>
</tgroup>
</table>
</requirement>
</section>
<section xml:id="structural-readWriteInit-field-typing" role="language-n4js">
<title>Structural Read-only, Write-only and Initializer Field Typing</title>
<simpara>Structural read-only, write-only and initializer field typings are extensions of structural field typing.
Everything that is defined for the field structural typing must comply with these extension field typings.
For the read-only type, readable fields (mutable and <literal>@Final</literal> ones) and setters are considered, for the write-only type; only the setters and mutable fields are considered.
Due to the hybrid nature of the initializer type it can act two different ways.
To be more precise, a type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math> (structural initializer field type) is a supertype of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>Y</mi></math> (structural initializer field type) if for each public, non-static, non-optional writable field (mutable data field of setter) of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math>, there is a corresponding, public, non-static readable field of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>Y</mi></math>.
All public member fields with <literal>@Final</literal> annotation are considered to be mandatory in the initializer field typing <literal>@Spec</literal> constructors.
The already-initialized <literal>@Final</literal> fields can be either omitted from, or can be re-initialized via, an initializer field typing <literal>@Spec</literal> style constructor.</simpara>
<example>
<title>Subtype relationship between structural field typing</title>
<programlisting language="n4js" linenumbering="unnumbered">class A1 {
public s: string;
}
class A2 {
public set s(s: string) { }
public get s(): string { return null; }
}
class A3 {
@Final public s: string = null;
}
class A4 {
public get s(): string { return null; }
}
class A5 {
public set s(s: string) { }
}</programlisting>
<simpara><?asciidoc-pagebreak?></simpara>
<informaltable role="small" frame="all" rowsep="1" colsep="1">
<tgroup cols="19">
<colspec colname="col_1" colwidth="5.2631*"/>
<colspec colname="col_2" colwidth="5.2631*"/>
<colspec colname="col_3" colwidth="5.2631*"/>
<colspec colname="col_4" colwidth="5.2631*"/>
<colspec colname="col_5" colwidth="5.2631*"/>
<colspec colname="col_6" colwidth="5.2631*"/>
<colspec colname="col_7" colwidth="5.2631*"/>
<colspec colname="col_8" colwidth="5.2631*"/>
<colspec colname="col_9" colwidth="5.2631*"/>
<colspec colname="col_10" colwidth="5.2631*"/>
<colspec colname="col_11" colwidth="5.2631*"/>
<colspec colname="col_12" colwidth="5.2631*"/>
<colspec colname="col_13" colwidth="5.2631*"/>
<colspec colname="col_14" colwidth="5.2631*"/>
<colspec colname="col_15" colwidth="5.2631*"/>
<colspec colname="col_16" colwidth="5.2631*"/>
<colspec colname="col_17" colwidth="5.2631*"/>
<colspec colname="col_18" colwidth="5.2631*"/>
<colspec colname="col_19" colwidth="5.2642*"/>
<tbody>
<row>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara>A1</simpara></entry>
<entry align="left" valign="top"><simpara>~A1</simpara></entry>
<entry align="left" valign="top"><simpara>~~A1</simpara></entry>
<entry align="left" valign="top"><simpara>~r~A1</simpara></entry>
<entry align="left" valign="top"><simpara>~r~A2</simpara></entry>
<entry align="left" valign="top"><simpara>~r~A3</simpara></entry>
<entry align="left" valign="top"><simpara>~r~A4</simpara></entry>
<entry align="left" valign="top"><simpara>~r~A5</simpara></entry>
<entry align="left" valign="top"><simpara>~w~A1</simpara></entry>
<entry align="left" valign="top"><simpara>~w~A2</simpara></entry>
<entry align="left" valign="top"><simpara>~w~A3</simpara></entry>
<entry align="left" valign="top"><simpara>~w~A4</simpara></entry>
<entry align="left" valign="top"><simpara>~w~A5</simpara></entry>
<entry align="left" valign="top"><simpara>~i~A1</simpara></entry>
<entry align="left" valign="top"><simpara>~i~A2</simpara></entry>
<entry align="left" valign="top"><simpara>~i~A3</simpara></entry>
<entry align="left" valign="top"><simpara>~r~A4</simpara></entry>
<entry align="left" valign="top"><simpara>~r~A5</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>A1</simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>~A1</simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>~~A1</simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>~r~A1</simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>~r~A2</simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>~r~A3</simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>~r~A4</simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>~r~A5</simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>~w~A1</simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>~w~A2</simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>~w~A3</simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>~w~A4</simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>~w~A5</simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>~i~A1</simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>~i~A2</simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>~i~A3</simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>~r~A4</simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>~r~A5</simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"><simpara></simpara></entry>
<entry align="left" valign="top"></entry>
</row>
</tbody>
</tgroup>
</informaltable>
</example>
</section>
<section xml:id="_public-setter-annotated-with-literal-providesinitializer-literal" role="language-n4js">
<title>Public Setter Annotated With <literal>ProvidesInitializer</literal></title>
<simpara>Public setters with <literal>ProvidesInitializer</literal> annotation can declare optional fields implemented by means of field accessors instead of data fields.
Data fields with an initializer are treated as optional in the initializer field type.</simpara>
<simpara>It is important to note that it is valid to use the <literal>ProvidesInitializer</literal> annotation for setters in <literal>n4js</literal> files and not only definition files.</simpara>
<example>
<title>Setters with <literal>@ProvidesInitializer</literal> treated as optional</title>
<programlisting language="n4js" linenumbering="unnumbered">class C {
private _y: int = 1;
public get y() { return this._y; }
@ProvidesInitializer
public set y(v: int) { this._y = v; }
public constructor(@Spec spec: ~i~this) { }
}
console.log(new C({}).y); // 1
console.log(new C({y: 42}).y); //24</programlisting>
</example>
</section>
<section xml:id="_structural-types-with-optional-fields" role="language-n4js">
<title>Structural Types With Optional Fields</title>
<simpara>Public optional fields become a member of the structural (field) type as
well. To ensure the overall type safety, the semantics of optionality (e.g. on or off) depends on the context, in which the optional fields are currently being used (See <link linkend="optional-fields">Optional Fields</link>).</simpara>
</section>
<section xml:id="_structural-types-with-access-modifier" role="language-n4js">
<title>Structural Types With Access Modifier</title>
<simpara>The access modifier of the subtype have to provide equal or higher
visibility.</simpara>
<example>
<title>Access modifier in structural typing</title>
<programlisting language="n4js" linenumbering="unnumbered">class C {
public s: number;
}
class D {
project s: number;
}
function f(c: ~C) {}
f(new D()); // error: D is no (structural) subtype of ~C, as visibility of s in D is lower
function g(d: ~D) {}
g(new C()); // ok: C is a (structural) subtype of ~D, as visibility of s in C is greater-than-or-equal to s in D</programlisting>
</example>
</section>
<section xml:id="_structural-types-with-additional-members" role="language-n4js">
<title>Structural Types With Additional Members</title>
<simpara>It is possible to add additional members when structurally referencing a
declared type.</simpara>
<section xml:id="_syntax-8">
<title>Syntax</title>
<programlisting language="xtext" linenumbering="unnumbered">TStructMember:
TStructGetter | TStructGetterES4 | TStructSetter | TStructMethod | TStructMethodES4 | TStructField;
TStructMethod:
=&gt;
({TStructMethod} ('&lt;' typeVars+=TypeVariable (',' typeVars+=TypeVariable)* '&gt;')?
returnTypeRef=TypeRef name=TypesIdentifier '(')
(fpars+=TAnonymousFormalParameter (',' fpars+=TAnonymousFormalParameter)*)? ')'
';'?;
TStructMethodES4 returns TStructMethod:
=&gt;
({TStructMethod} ('&lt;' typeVars+=TypeVariable (',' typeVars+=TypeVariable)* '&gt;')?
name=TypesIdentifier '(')
(fpars+=TAnonymousFormalParameter (',' fpars+=TAnonymousFormalParameter)*)? ')'
(':' returnTypeRef=TypeRef)?
';'?;
TStructField:
(
typeRef=TypeRef name=TypesIdentifier
| name=TypesIdentifier (':' typeRef=TypeRef)?
)
';';
TStructGetter:
=&gt; ({TStructGetter}
declaredTypeRef=TypeRef
'get'
name=TypesIdentifier)
'(' ')' ';'?;
TStructGetterES4 returns TStructGetter:
=&gt; ({TStructGetter}
'get'
name=TypesIdentifier)
'(' ')' (':' declaredTypeRef=TypeRef)? ';'?;
TStructSetter:
=&gt; ({TStructSetter}
'set'
name=TypesIdentifier)
'(' fpar=TAnonymousFormalParameter ')' ';'?;
TAnonymousFormalParameter:
typeRef=TypeRef variadic?='...'? name=TIdentifier?
| variadic?='...'? (=&gt; name=TIdentifier ':') typeRef=TypeRef;</programlisting>
<section xml:id="_semantics-6">
<title>Semantics</title>
<requirement xml:id="IDE-78">
<title>Additional structural members</title>
<simpara>
<anchor xml:id="Req-IDE-78" xreflabel="[Req-IDE-78]"/>
<emphasis role="strong">Requirement: IDE-78:</emphasis>
<link linkend="Req-IDE-78">Additional structural members</link> (ver. 1)</simpara>
<simpara>
</simpara>
<simpara>It is only possible to add additional members to a type if use-site structural typing is used.</simpara>
<simpara>The following constraints must hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>For all additional members defined in a structural type reference, the constraints for member overriding (<xref linkend="Req-IDE-72"/>) apply as well.</simpara>
</listitem>
<listitem>
<simpara>All additional members have the access modifier set to <literal>public</literal>.</simpara>
</listitem>
<listitem>
<simpara>Type variables must not be augmented with additional structural members.</simpara>
</listitem>
</orderedlist>
<simpara>Additional fields may be declared optional in the same way as fields in classes.
The rules given in <xref linkend="_structural-types-with-optional-fields"/> apply accordingly.
Consider the following example:</simpara>
</requirement>
<example>
<title>Additional optional members in structural typing</title>
<programlisting language="n4js" linenumbering="unnumbered">class C {
public f1: number;
}
var c1: ~C with { f3: string; } c1;
var c2: ~C with { f3: string?; } c2;
c1 = { f1:42 }; // error: "~Object with { number f1; } is not a subtype of ~C with { string f3; }."
c2 = { f1:42 }; // ok!!</programlisting>
</example>
<simpara>Augmenting a type variable T with additional structural members can cause collisions with another member of a type argument for T.
Hence, type variables must not be augmented with additional structural members like in the following example.</simpara>
<example>
<title>Forbidden additional structural members on type variables</title>
<programlisting language="n4js" linenumbering="unnumbered">interface I&lt;T&gt; {
public field : ~T with {prop : int} // error "No additional structural members allowed on type variables."
}</programlisting>
</example>
<simpara>Using an additional structural member on a type variable T could be seen as a constraint to T.
However, constraints like this should be rather stated using an explicit interface like in the example below.</simpara>
<example>
<title>Use explicitly defined Interfaces</title>
<programlisting language="n4js" linenumbering="unnumbered">interface ~J {
prop : int;
}
interface II&lt;T extends J&gt; {
}</programlisting>
</example>
</section>
</section>
</section>
</section>
</chapter>
<chapter xml:id="_functions">
<title>Functions</title>
<simpara>Functions, be they function declarations, expressions or even methods, are internally modeled by means of a function type.
In this chapter, the general function type is described along with its semantics and type constraints.
Function definitions and expressions are then introduced in terms of statements and expressions.
Method definitions and special usages are described in <xref linkend="_methods"/>.</simpara>
<section xml:id="_function-type" role="language-n4js">
<title>Function Type</title>
<simpara>A function type is modeled as <literal>Object</literal> (see [<link linkend="ECMA11a">ECMA11a(p.S13, p.p.98)</link>] in ECMAScript.</simpara>
<simpara>Function types can be defined by means of;</simpara>
<itemizedlist>
<listitem>
<simpara>A function object (<xref linkend="_function-object-type"/>).</simpara>
</listitem>
<listitem>
<simpara>A function type expression (<xref linkend="_type-expressions"/>).</simpara>
</listitem>
<listitem>
<simpara>A function declaration (<xref linkend="_function-declaration"/>).</simpara>
</listitem>
<listitem>
<simpara>A method declaration (<xref linkend="_methods"/>).</simpara>
</listitem>
</itemizedlist>
<section xml:id="_properties-5">
<title>Properties</title>
<simpara>In any case, a function type declares the signature of a function and allows validation of calls to that function.
A function type has the following properties:</simpara>
<variablelist>
<varlistentry>
<term><literal>typePars</literal> </term>
<listitem>
<simpara>(0-indexed) list of type parameters (i.e. type variables) for generic functions.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>fpars</literal> </term>
<listitem>
<simpara>(0-indexed) list of formal parameters.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>returnType</literal> </term>
<listitem>
<simpara>(possibly inferred) return type (expression) of the function or method.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>name</literal> </term>
<listitem>
<simpara>Name of function or method, may be empty or automatically generated (for messages).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>body</literal> </term>
<listitem>
<simpara>The body of the function, it contains statements <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>t</mi><mi>m</mi><mi>t</mi><mi>s</mi></math>.
The body is null if a function type is defined in a type expression, and it is the last argument in case of a function object constructor, or the content of the function definition body.</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>Additionally, the following pseudo properties for functions are defined:</simpara>
<variablelist>
<varlistentry>
<term><literal>thisTypeRef</literal> </term>
<listitem>
<simpara>The this type ref is the type to which the <literal>this</literal>-keyword would be evaluated
if used inside the function or member. The inference rules are described
in <xref linkend="_this-keyword"/>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>fpars</literal> </term>
<listitem>
<simpara>List of formal parameters and the this type ref.
This is only used for sub typing rules.
If <literal>this</literal> is not used inside the function, then <literal>any</literal> is set instead of the inferred thisTypeRef to allow for more usages.
The property is computed as follows:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>t</mi><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>=</mo><mstyle mathvariant="bold"><mtext>if</mtext></mstyle><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mspace width="3.0mm"/><mstyle mathvariant="bold"><mtext>this is used or explicitly declared</mtext></mstyle><mspace width="3.0mm"/><mstyle mathvariant="bold"><mtext>then</mtext></mstyle><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mo>+</mo><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><mi>s</mi><mspace width="3.0mm"/><mstyle mathvariant="bold"><mtext>else</mtext></mstyle><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mi>a</mi><mi>n</mi><mi>y</mi><mo>+</mo><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><mi>s</mi></math>
</listitem>
</varlistentry>
</variablelist>
<simpara>Parameters (in <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>p</mi><mi>a</mi><mi>r</mi><mi>s</mi></math>) have the following properties:</simpara>
<variablelist>
<varlistentry>
<term><literal>name</literal> </term>
<listitem>
<simpara>Name of the parameter.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>type</literal> </term>
<listitem>
<simpara>Type (expression) of the parameter. Note that only parameter types can
be variadic or optional.</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>The function definition can be annotated similar to <xref linkend="_methods"/> except that the <literal>final</literal> and <literal>abstract</literal> modifiers aren’t supported for function declarations.
A function declaration is always final and never abstract.
Also, a function has no property advice set.</simpara>
<bridgehead xml:id="_semantics-7" renderas="sect3">Semantics</bridgehead>
<requirement xml:id="IDE-79">
<title>Function Type</title>
<simpara>
<anchor xml:id="Req-IDE-79" xreflabel="[Req-IDE-79]"/>
<emphasis role="strong">Requirement: IDE-79:</emphasis>
<link linkend="Req-IDE-79">Function Type</link> (ver. 1)</simpara>
<simpara>
Type Given a function type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>F</mi></math>, the following constraints must be true:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>Optional parameters must be defined at the end of the (formal) parameter list.
In particular, an optional parameter must not be followed by a non-optional parameter:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>F</mi><mo>.</mo><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><msub><mi>s</mi><mi>i</mi></msub><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mi>a</mi><mi>l</mi><mo></mo><mo></mo><mi>k</mi><mo>&gt;</mo><mi>i</mi><mi>:</mi><mo>¬</mo><mi>F</mi><mo>.</mo><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><msub><mi>s</mi><mi>k</mi></msub><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi><mi>v</mi><mi>a</mi><mi>r</mi></math>
</listitem>
<listitem>
<simpara>Only the last parameter of a method may be defined as variadic parameter:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>F</mi><mo>.</mo><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><msub><mi>s</mi><mi>i</mi></msub><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi><mi>i</mi><mi>a</mi><mi>d</mi><mi>i</mi><mi>c</mi><mo></mo><mi>i</mi><mo>=</mo><mo>|</mo><mi>F</mi><mo>.</mo><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>|</mo><mo>-</mo><mn>1</mn></math>
</listitem>
<listitem>
<simpara>If a function explicitly defines a return type, the last statement of the transitive closure of statements of the body must be a return statement:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>F</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mo></mo><mi>U</mi><mi>n</mi><mi>d</mi><mi>e</mi><mi>f</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>d</mi><mo></mo></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>|</mo><mi>f</mi><mo>.</mo><mi>b</mi><mi>o</mi><mi>d</mi><mi>y</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>m</mi><mi>t</mi><mi>s</mi><mo>|</mo><mo>&gt;</mo><mn>0</mn></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>f</mi><mo>.</mo><mi>b</mi><mi>o</mi><mi>d</mi><mi>y</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>m</mi><mi>t</mi><msubsup><mi>s</mi><mrow><mo>|</mo><mi>f</mi><mo>.</mo><mi>b</mi><mi>o</mi><mi>d</mi><mi>y</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>m</mi><mi>t</mi><msup><mi>s</mi><mo>*</mo></msup><mo>|</mo><mo>-</mo><mn>1</mn></mrow><mo>*</mo></msubsup><mi>i</mi><mi>s</mi><mi>a</mi><mi> </mi><mstyle mathvariant="monospace"><mtext>ReturnStatement</mtext></mstyle></math></simpara>
</listitem>
<listitem>
<simpara>If a function explicitly defines a return type, all return
statements must return a type conform to that type:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mi>F</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mo></mo><mi>U</mi><mi>n</mi><mi>d</mi><mi>e</mi><mi>f</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>d</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo></mo></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo></mo><mi>r</mi><mo></mo><mi>F</mi><mo>.</mo><mi>b</mi><mi>o</mi><mi>d</mi><mi>y</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>m</mi><mi>t</mi><mi>s</mi><mo>,</mo><mi>r</mi><mi> </mi><mi>i</mi><mi>s</mi><mi>a</mi><mi> </mi><mstyle mathvariant="monospace"><mtext>ReturnStatement</mtext></mstyle><mi>:</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mi>r</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo></mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi><mo></mo><mrow><mo>[</mo><mspace width="-0.167em"/><mrow><mo>[</mo></mrow></mrow><mrow><mi>r</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi></mrow><mrow><mrow><mo>]</mo></mrow><mspace width="-0.167em"/><mo>]</mo></mrow><mo>&lt;</mo><mi>:</mi><mrow><mo>[</mo><mspace width="-0.167em"/><mrow><mo>[</mo></mrow></mrow><mrow><mi>F</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi></mrow><mrow><mrow><mo>]</mo></mrow><mspace width="-0.167em"/><mo>]</mo></mrow></math></simpara>
</listitem>
</orderedlist>
</requirement>
</section>
<section xml:id="function-type-inference">
<title>Type Inference</title>
<definition xml:id="function_type_conformance_non_parameterized">
<title>Function Type Conformance Non-Parameterized</title>
<simpara>
<anchor xml:id="function_type_conformance_non-parameterized" xreflabel="[function_type_conformance_non-parameterized]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="function_type_conformance_non-parameterized">Function Type Conformance Non-Parameterized</link></simpara>
<simpara>
<emphasis>For the given non-parameterized function types</emphasis>
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>F</mi><mrow><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi></mrow></msub></math> with
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>F</mi><mrow><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi></mrow></msub><mo>.</mo><mi>t</mi><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>=</mo><msub><mi>L</mi><mn>0</mn></msub><mo>,</mo><msub><mi>L</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><msub><mi>L</mi><mi>k</mi></msub></math> and
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>|</mo><msub><mi>F</mi><mrow><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi></mrow></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>s</mi><mi>P</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>|</mo><mo>=</mo><mn>0</mn></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>F</mi><mrow><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></msub></math> with
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>F</mi><mrow><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></msub><mo>.</mo><mi>t</mi><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>=</mo><msub><mi>R</mi><mn>0</mn></msub><mo>,</mo><msub><mi>R</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><msub><mi>R</mi><mi>n</mi></msub></math> and
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>|</mo><msub><mi>F</mi><mrow><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>s</mi><mi>P</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>|</mo><mo>=</mo><mn>0</mn></math>,<?asciidoc-br?>
we say <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>F</mi><mrow><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi></mrow></msub></math> conforms to <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>F</mi><mrow><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></msub></math>,
written as <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>F</mi><mrow><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi></mrow></msub><mo>&lt;</mo><mi>:</mi><msub><mi>F</mi><mrow><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></msub></math>, if and only if:</simpara>
<itemizedlist>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>F</mi><mrow><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></msub><mo>.</mo><mi>r</mi><mi>e</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>n</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>void</mtext></mstyle></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mfenced close=")" open="("><mrow><msub><mi>F</mi><mrow><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi></mrow></msub><mo>.</mo><mi>r</mi><mi>e</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>n</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>void</mtext></mstyle><mo></mo><msub><mi>F</mi><mrow><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></msub><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi></mrow></mfenced></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mrow><mo>(</mo><mrow><msub><mi>F</mi><mrow><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi></mrow></msub><mo>.</mo><mi>r</mi><mi>e</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>n</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mo>&lt;</mo><mi>:</mi><msub><mi>F</mi><mrow><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></msub><mo>.</mo><mi>r</mi><mi>e</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>n</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mo></mo><mo>¬</mo><mfenced close=")" open="("><mrow><msub><mi>F</mi><mrow><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi></mrow></msub><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi><mo></mo><mo>¬</mo><msub><mi>F</mi><mrow><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></msub><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi></mrow></mfenced><mo>)</mo></mrow></mrow></math></simpara>
</listitem>
<listitem>
<simpara>if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>k</mi><mo></mo><mi>n</mi></math>:</simpara>
<itemizedlist>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi> </mi><mi>i</mi><mo>,</mo><mn>1</mn><mo></mo><mi>i</mi><mo></mo><mi>k</mi><mi>:</mi><mfenced close=")" open="("><mrow><msub><mi>R</mi><mi>i</mi></msub><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi><mo></mo><mfenced close=")" open="("><mrow><msub><mi>L</mi><mi>i</mi></msub><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi><mo></mo><msub><mi>L</mi><mi>i</mi></msub><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi></mrow></mfenced></mrow></mfenced><mo></mo><mfenced close=")" open="("><mrow><msub><mi>R</mi><mi>i</mi></msub><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi><mo></mo><msub><mi>L</mi><mi>i</mi></msub><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi></mrow></mfenced></math></simpara>
</listitem>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi> </mi><mi>i</mi><mo>,</mo><mn>1</mn><mo></mo><mi>i</mi><mo></mo><mi>k</mi><mi>:</mi><msub><mi>L</mi><mi>i</mi></msub><mi>:</mi><mo>&gt;</mo><msub><mi>R</mi><mi>i</mi></msub></math></simpara>
</listitem>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>L</mi><mi>k</mi></msub><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi><mo>=</mo><mi>t</mi><mi>r</mi><mi>u</mi><mi>e</mi><mo></mo><mo></mo><mi> </mi><mi>i</mi><mo>,</mo><mi>k</mi><mo>&lt;</mo><mi>i</mi><mo></mo><mi>n</mi><mi>:</mi><msub><mi>L</mi><mi>K</mi></msub><mi>:</mi><mo>&gt;</mo><msub><mi>R</mi><mi>i</mi></msub></math></simpara>
<simpara>else (<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>k</mi><mo>&gt;</mo><mi>n</mi></math>):</simpara>
</listitem>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi> </mi><mi>i</mi><mo>,</mo><mn>1</mn><mo></mo><mi>i</mi><mo></mo><mi>n</mi><mi>:</mi><mfenced close=")" open="("><mrow><msub><mi>R</mi><mi>i</mi></msub><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi><mo></mo><mfenced close=")" open="("><mrow><msub><mi>L</mi><mi>i</mi></msub><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi><mo></mo><msub><mi>L</mi><mi>i</mi></msub><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi></mrow></mfenced></mrow></mfenced><mo></mo><mfenced close=")" open="("><mrow><msub><mi>R</mi><mi>i</mi></msub><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi><mo></mo><msub><mi>L</mi><mi>i</mi></msub><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi></mrow></mfenced></math></simpara>
</listitem>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi> </mi><mi>i</mi><mo>,</mo><mn>1</mn><mo></mo><mi>i</mi><mo></mo><mi>n</mi><mi>:</mi><msub><mi>L</mi><mi>i</mi></msub><mi>:</mi><mo>&gt;</mo><msub><mi>R</mi><mi>i</mi></msub></math></simpara>
</listitem>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi> </mi><mi>n</mi><mo>&lt;</mo><mi>i</mi><mo></mo><mi>k</mi><mi>:</mi><msub><mi>L</mi><mi>i</mi></msub><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi><mo></mo><msub><mi>L</mi><mi>i</mi></msub><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi></math></simpara>
</listitem>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>R</mi><mi>n</mi></msub><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi><mo>=</mo><mi>t</mi><mi>r</mi><mi>u</mi><mi>e</mi><mo></mo><mo></mo><mi> </mi><mi>i</mi><mo>,</mo><mi>n</mi><mo>&lt;</mo><mi>i</mi><mo></mo><mi>k</mi><mi>:</mi><msub><mi>L</mi><mi>i</mi></msub><mi>:</mi><mo>&gt;</mo><msub><mi>R</mi><mi>n</mi></msub></math></simpara>
</listitem>
</itemizedlist>
</listitem>
</itemizedlist>
<simpara><xref linkend="cdVarianceFunctionChart"/> shows a simple example with the function type conformance relations.</simpara>
<figure xml:id="cdVarianceFunctionChart">
<title>Function Variance Chart</title>
<mediaobject>
<imageobject>
<imagedata fileref="chapters/06_functions/fig/cdVarianceFunctionChart.svg" width="60%" scalefit="1"/>
</imageobject>
<textobject><phrase>cdVarianceFunctionChart</phrase></textobject>
</mediaobject>
</figure>
<simpara><literal>{function()}</literal> <math xmlns="http://www.w3.org/1998/Math/MathML"><mo>&lt;</mo><mi>:</mi></math> <literal>{function(A)}</literal> <math xmlns="http://www.w3.org/1998/Math/MathML"><mo>&lt;</mo><mi>:</mi></math> <literal>{function(A, A)}</literal> might be surprising for Java programmers. However, in JavaScript it is
possible to call a function with any number of arguments independently
from how many formal parameters the function defines.</simpara>
<simpara>If a function does not define a return type, <literal>any</literal> is assumed if at least one
of the (indirectly) contained return statements contains an expression.
Otherwise <literal>void</literal> is assumed. This is also true if there is an error due to
other constraint violations.</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mspace width="3.0mm"/><mfrac><mrow><mi>b</mi><mi>i</mi><mi>n</mi><mi>d</mi><mi>s</mi><mfenced close=")" open="("><mi>f</mi><mi>F</mi></mfenced><mspace width="3.0mm"/><mi>F</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>n</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>null</mtext></mstyle><mspace width="3.0mm"/><mo></mo><mi>r</mi><mo></mo><mi>r</mi><mi>e</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>n</mi><mi>s</mi><mfenced close=")" open="("><mi>F</mi></mfenced><mi>:</mi><mi>r</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo></mo><mstyle mathvariant="monospace"><mtext>null</mtext></mstyle></mrow><mrow><mi>Γ</mi><mo></mo><mi>f</mi><mstyle mathvariant="monospace"><mtext>’(’</mtext></mstyle><mi>a</mi><mi>r</mi><mi>g</mi><mi>l</mi><mi>i</mi><mi>s</mi><mi>t</mi><mi> </mi><mstyle mathvariant="monospace"><mtext>’)’</mtext></mstyle><mi>:</mi><mstyle mathvariant="monospace"><mtext>any</mtext></mstyle></mrow></mfrac></mtd></mtr><mtr><mtd><mspace width="3.0mm"/><mfrac><mrow><mi>b</mi><mi>i</mi><mi>n</mi><mi>d</mi><mi>s</mi><mfenced close=")" open="("><mi>f</mi><mi>F</mi></mfenced><mspace width="3.0mm"/><mi>F</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>n</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>null</mtext></mstyle><mspace width="3.0mm"/><mo></mo><mi>r</mi><mo></mo><mi>r</mi><mi>e</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>n</mi><mi>s</mi><mfenced close=")" open="("><mi>F</mi></mfenced><mi>:</mi><mi>r</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo></mo><mstyle mathvariant="monospace"><mtext>null</mtext></mstyle></mrow><mrow><mi>Γ</mi><mo></mo><mi>f</mi><mstyle mathvariant="monospace"><mtext>’(’</mtext></mstyle><mi>a</mi><mi>r</mi><mi>g</mi><mi>l</mi><mi>i</mi><mi>s</mi><mi>t</mi><mi> </mi><mstyle mathvariant="monospace"><mtext>’)’</mtext></mstyle><mi>:</mi><mstyle mathvariant="monospace"><mtext>void</mtext></mstyle></mrow></mfrac></mtd></mtr></mtable></math>
<simpara>with</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mspace width="3.0mm"/><mfrac><mrow><mfenced close="}" open="{"><mrow><mi>r</mi><mo></mo><mi>F</mi><mo>.</mo><mi>b</mi><mi>o</mi><mi>d</mi><mi>y</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>e</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi>s</mi><mo>|</mo><mi>μ</mi><mfenced close=")" open="("><mi>r</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>ReturnStatement</mtext></mstyle></mrow></mfenced><mo></mo><munder><mo></mo><mrow><mi>s</mi><mo></mo><mi>F</mi><mo>.</mo><mi>b</mi><mi>o</mi><mi>d</mi><mi>y</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>e</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi>s</mi></mrow></munder><mi>r</mi><mi>e</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>n</mi><mi>s</mi><mfenced close=")" open="("><mi>s</mi></mfenced></mrow><mrow><mi>r</mi><mi>e</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>n</mi><mi>s</mi><mfenced close=")" open="("><mi>F</mi></mfenced><mi>:</mi><mi>R</mi><mi>E</mi><mi>T</mi><mi>S</mi></mrow></mfrac></mtd></mtr><mtr><mtd><mspace width="3.0mm"/><mfrac><mrow><mfenced close="}" open="{"><mrow><mi>s</mi><mi>u</mi><mi>b</mi><mo></mo><mi>s</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>e</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi>s</mi><mo>|</mo><mi>μ</mi><mfenced close=")" open="("><mrow><mi>s</mi><mi>u</mi><mi>b</mi></mrow></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>ReturnStatement</mtext></mstyle></mrow></mfenced><mo></mo><munder><mo></mo><mrow><mi>s</mi><mi>u</mi><mi>b</mi><mo></mo><mi>s</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>e</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi>s</mi></mrow></munder><mi>r</mi><mi>e</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>n</mi><mi>s</mi><mfenced close=")" open="("><mrow><mi>s</mi><mi>u</mi><mi>b</mi></mrow></mfenced></mrow><mrow><mi>r</mi><mi>e</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>n</mi><mi>s</mi><mfenced close=")" open="("><mi>s</mi></mfenced><mi>:</mi><mi>R</mi><mi>E</mi><mi>T</mi><mi>S</mi></mrow></mfrac></mtd></mtr></mtable></math>
</definition>
<example>
<title>Function type conformance</title>
<simpara>The following incomplete snippet demonstrates the usage of two function variables <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mn>1</mn></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mn>2</mn></math>, in which <math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>[</mo><mspace width="-0.167em"/><mrow><mo>[</mo></mrow></mrow><mrow><mi>f</mi><mn>2</mn></mrow><mrow><mrow><mo>]</mo></mrow><mspace width="-0.167em"/><mo>]</mo></mrow><mo>&lt;</mo><mi>:</mi><mrow><mo>[</mo><mspace width="-0.167em"/><mrow><mo>[</mo></mrow></mrow><mrow><mi>f</mi><mn>1</mn></mrow><mrow><mrow><mo>]</mo></mrow><mspace width="-0.167em"/><mo>]</mo></mrow></math> must hold true according to the aforementioned constraints.
A function <literal>bar</literal> declares a parameter <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mn>1</mn></math>, which is actually a function itself.
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mn>2</mn></math> is a variable, to which a function expression is a assigned.
Function <literal>bar</literal> is then called with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mn>2</mn></math> as an argument.
Thus, the type of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mn>2</mn></math> must be a subtype of the <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mn>1</mn></math>’s type.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">function bar(f1: {function(A,B):C}) { ... }
var f2: {function(A,B):C} = function(p1,p2){...};
bar(f1);</programlisting>
</example>
<simpara>The type of <literal>this</literal> can be explicitly set via the <literal>@This</literal> annotation.</simpara>
<example>
<title>Function Subtyping</title>
<programlisting language="n4js" linenumbering="unnumbered">function f(): A {..}
function p(): void {..}
fAny(log: {function():any}) {...}
fVoid(f: {function():void}) {..}
fA(g: {function():A}) {...}
fAny(f); // --&gt; ok A &lt;: any
fVoid(f); // --&gt;error A !&lt;: void
fA(f); // --&gt; ok (easy) A &lt;: A
fAny(p); // --&gt; ok void &lt;: any
fVoid(p); // --&gt; ok void &lt;: void
fA(p); // --&gt; error void !&lt;: A</programlisting>
</example>
<example>
<title>Subtyping with function types</title>
<simpara>If classes A, B, and C are defined as previously mentioned, i.e. <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi><mo>&lt;</mo><mi>:</mi><mi>B</mi><mo>&lt;</mo><mi>:</mi><mi>A</mi></math>, then
the following subtyping relations with function types are to be evaluated as follows:</simpara>
<programlisting language="n4js" linenumbering="unnumbered"> {function(B):B} &lt;: {function(B):B} -&gt; true
{function():A} &lt;: {function():B} -&gt; false
{function():C} &lt;: {function():B} -&gt; true
{function(A)} &lt;: {function(B)} -&gt; true
{function(C)} &lt;: {function(B)} -&gt; false
{function():void} &lt;: {function():void} -&gt; true
{function():undefined} &lt;: {function():void} -&gt; true
{function():void} &lt;: {function():undefined} -&gt; true (!)
{function():B} &lt;: {function():void} -&gt; true (!)
{function():B} &lt;: {function():undefined} -&gt; false (!)
{function():void} &lt;: {function():B} -&gt; false
{function():undefined} &lt;: {function():B} -&gt; true</programlisting>
<simpara>The following examples demonstrate the effect of optional and variadic parameters:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">{function(A)} &lt;: {function(B)} -&gt; true
{function(A...)} &lt;: {function(A)} -&gt; true
{function(A, A)} &lt;: {function(A)} -&gt; false
{function(A)} &lt;: {function(A,A)} -&gt; true (!)
{function(A, A...)} &lt;: {function(A)} -&gt; true
{function(A)} &lt;: {function(A,A...)} -&gt; true (!)
{function(A, A...)} &lt;: {function(B)} -&gt; true
{function(A?)} &lt;: {function(A?)} -&gt; true
{function(A...)} &lt;: {function(A...)} -&gt; true
{function(A?)} &lt;: {function(A)} -&gt; true
{function(A)} &lt;: {function(A?)} -&gt; false
{function(A...)} &lt;: {function(A?)} -&gt; true
{function(A?)} &lt;: {function(A...)} -&gt; true (!)
{function(A,A...)} &lt;: {function(A...)} -&gt; false
{function(A,A?)} &lt;: {function(A...)} -&gt; false
{function(A?,A...)} &lt;: {function(A...)} -&gt; true
{function(A...)} &lt;: {function(A?,A...)} -&gt; true
{function(A...)} &lt;: {function(A?)} -&gt; true
{function(A?,A?)} &lt;: {function(A...)} -&gt; true (!)
{function(A?,A?,A?)} &lt;: {function(A...)} -&gt; true (!)
{function(A?)} &lt;: {function()} -&gt; true (!)
{function(A...)} &lt;: {function()} -&gt; true (!)</programlisting>
<simpara>The following examples demonstrate the effect of optional return types:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">{function():void} &lt;: {function():void} -&gt; true
{function():X} &lt;: {function():void} -&gt; true
{function():X?} &lt;: {function():void} -&gt; true
{function():void} &lt;: {function():Y} -&gt; false
{function():X} &lt;: {function():Y} -&gt; X &lt;: Y
{function():X?} &lt;: {function():Y} -&gt; false (!)
{function():void} &lt;: {function():Y?} -&gt; true (!)
{function():X} &lt;: {function():Y?} -&gt; X &lt;: Y
{function():X?} &lt;: {function():Y?} -&gt; X &lt;: Y
{function():B?} &lt;: {function():undefined} -&gt; false (!)
{function():undefined} &lt;: {function():B?} -&gt; true</programlisting>
<simpara>The following examples show the effect of the <literal>@This</literal> annotation:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">{@This(A) function():void} &lt;: {@This(X) function():void} -&gt; false
{@This(B) function():void} &lt;: {@This(A) function():void} -&gt; false
{@This(A) function():void} &lt;: {@This(B) function():void} -&gt; true
{@This(any) function():void} &lt;: {@This(X) function():void} -&gt; true
{function():void} &lt;: {@This(X) function():void} -&gt; true
{@This(A) function():void} &lt;: {@This(any) function():void} -&gt; false
{@This(A) function():void} &lt;: {function():void} -&gt; false</programlisting>
</example>
<definition>
<title>Function Type Conformance</title>
<simpara>
<anchor xml:id="function_type_conformance" xreflabel="[function_type_conformance]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="function_type_conformance">Function Type Conformance</link></simpara>
<simpara>
For the given function types<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>F</mi><mrow><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi></mrow></msub></math> with
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>F</mi><mrow><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi></mrow></msub><mo>.</mo><mi>t</mi><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>=</mo><msub><mi>L</mi><mn>0</mn></msub><mo>,</mo><msub><mi>L</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><msub><mi>L</mi><mi>k</mi></msub></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>F</mi><mrow><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></msub></math> with
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>F</mi><mrow><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></msub><mo>.</mo><mi>t</mi><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>=</mo><msub><mi>R</mi><mn>0</mn></msub><mo>,</mo><msub><mi>R</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><msub><mi>R</mi><mi>n</mi></msub></math>,<?asciidoc-br?>
we say <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>F</mi><mrow><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi></mrow></msub></math> conforms to <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>F</mi><mrow><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></msub></math>, written as <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>F</mi><mrow><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi></mrow></msub><mo>&lt;</mo><mi>:</mi><msub><mi>F</mi><mrow><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></msub></math>, if and only if:</simpara>
<itemizedlist>
<listitem>
<simpara>if <math xmlns="http://www.w3.org/1998/Math/MathML"><mo>|</mo><msub><mi>F</mi><mrow><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi></mrow></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>P</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>|</mo><mo>=</mo><mo>|</mo><msub><mi>F</mi><mrow><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>P</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>|</mo><mo>=</mo><mn>0</mn></math>:</simpara>
<itemizedlist>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>F</mi><mrow><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi></mrow></msub><mo>&lt;</mo><mi>:</mi><msub><mi>F</mi><mrow><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></msub></math>
(cf. <link linkend="function_type_conformance_non_parameterized">Function Type Conformance Non-Parameterized</link>)</simpara>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<simpara>else if<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>|</mo><msub><mi>F</mi><mrow><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi></mrow></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>P</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>|</mo><mo>&gt;</mo><mn>0</mn><mo></mo><mo>|</mo><msub><mi>F</mi><mrow><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>P</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>|</mo><mo>=</mo><mn>0</mn></math>:</simpara>
<itemizedlist>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>θ</mi><mi>:</mi><mfenced close=")" open="("><mrow><mi>Γ</mi><mo></mo><mi>θ</mi></mrow></mfenced><mo></mo><msub><mi>F</mi><mrow><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi></mrow></msub><mo>&lt;</mo><mi>:</mi><msub><mi>F</mi><mrow><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></msub></math> (cf. <link linkend="function_type_conformance_non_parameterized">Function Type Conformance Non-Parameterized</link> )</simpara>
<simpara>(i.e. there exists a substitution <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>θ</mi></math> of type variables in <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>F</mi><mrow><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi></mrow></msub></math> so that after substitution it becomes a subtype of <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>F</mi><mrow><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></msub></math> as defined by <link linkend="function_type_conformance_non_parameterized">Function Type Conformance Non-Parameterized</link>)</simpara>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<simpara>else if <math xmlns="http://www.w3.org/1998/Math/MathML"><mo>|</mo><msub><mi>F</mi><mrow><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi></mrow></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>P</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>|</mo><mo>=</mo><mo>|</mo><msub><mi>F</mi><mrow><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>P</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>|</mo></math>:</simpara>
<itemizedlist>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>Γ</mi><mo></mo><mfenced close="}" open="{"><mrow><msubsup><mi>V</mi><mi>i</mi><mi>r</mi></msubsup><mo></mo><msubsup><mi>V</mi><mi>i</mi><mi>l</mi></msubsup><mo>|</mo><mn>0</mn><mo></mo><mi>i</mi><mo></mo><mi>n</mi></mrow></mfenced><mo></mo><msub><mi>F</mi><mrow><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi></mrow></msub><mo>&lt;</mo><mi>:</mi><msub><mi>F</mi><mrow><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></msub></math>
( accordingly)</simpara>
</listitem>
<listitem>
<simpara>-</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mo></mo><mn>0</mn><mo></mo><mi>i</mi><mo></mo><mi>n</mi><mi>:</mi></mtd></mtr><mtr><mtd><mi>i</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msubsup><mi>V</mi><mi>i</mi><mi>l</mi></msubsup><mo>.</mo><mstyle mathvariant="italic"><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>B</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>d</mi><mi>s</mi></mstyle></mrow></mfenced><mi>:</mi><mo>&gt;</mo><mi>i</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><msubsup><mi>V</mi><mi>i</mi><mi>r</mi></msubsup><mo>.</mo><mstyle mathvariant="italic"><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>B</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>d</mi><mi>s</mi></mstyle></mrow></mfenced></mtd></mtr></mtable></math>
<simpara>with <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>F</mi><mrow><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi></mrow></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>P</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>=</mo><msubsup><mi>V</mi><mn>0</mn><mi>l</mi></msubsup><mo>,</mo><msubsup><mi>V</mi><mn>1</mn><mi>l</mi></msubsup><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><msubsup><mi>V</mi><mi>n</mi><mi>l</mi></msubsup></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>F</mi><mrow><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></msub><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>P</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>=</mo><msubsup><mi>V</mi><mn>0</mn><mi>r</mi></msubsup><mo>,</mo><msubsup><mi>V</mi><mn>1</mn><mi>r</mi></msubsup><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><msubsup><mi>V</mi><mi>n</mi><mi>r</mi></msubsup></math><?asciidoc-br?>
(i.e. we replace each type variable in <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>F</mi><mrow><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></msub></math> by the corresponding type variable at the same index in <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>F</mi><mrow><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi></mrow></msub></math>
and check the constraints from <link linkend="function_type_conformance_non_parameterized">Function Type Conformance Non-Parameterized</link> as if <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>F</mi><mrow><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi></mrow></msub></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>F</mi><mrow><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></msub></math> were non-parameterized functions and, in
addition, the upper bounds on the left side need to be supertypes of the upper bounds on the right side).</simpara>
</listitem>
</itemizedlist>
</listitem>
</itemizedlist>
</definition>
<simpara>Note that the upper bounds on the left must be supertypes of the right-side upper bounds (for similar reasons why types of formal parameters on the left are
required to be supertypes of the formal parameters’ types in ).
Where a particular type variable is used, on co- or contra-variant position, is not relevant:</simpara>
<example>
<title>Bounded type variable at co-variant position in function type</title>
<programlisting language="n4js" linenumbering="unnumbered">class A {}
class B extends A {}
class X {
&lt;T extends B&gt; m(): T { return null; }
}
class Y extends X {
@Override
&lt;T extends A&gt; m(): T { return null; }
}</programlisting>
<simpara>Method <literal>m</literal> in <literal>Y</literal> may return an <literal>A</literal>, thus breaking the contract of m in <literal>X</literal>, but only if it is parameterized to do so, which is not allowed for clients of <literal>X</literal>, only those of <literal>Y</literal>.
Therefore, the override in the above example is valid.</simpara>
</example>
<simpara>The subtype relation for function types is also applied for method overriding to ensure that an overriding method’s signature conforms to that of the overridden method,
see <xref linkend="Req-IDE-72"/> (applies to method comnsumption and implementation accordingly, see <xref linkend="Req-IDE-73"/> and <xref linkend="Req-IDE-74"/>).
Note that this is very different from Java which is far more restrictive when checking overriding methods.
As Java also supports method overloading: given two types <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>A</mi><mo>,</mo><mi>B</mi></math> with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>B</mi><mo>&lt;</mo><mi>:</mi><mi>A</mi></math> and a super class method <literal>void m(B param)</literal>, it is valid to override <literal>m</literal> as <literal>void m(A param)</literal> in N4JS but not in Java.
In Java this would be handled as method overloading and therefore an <literal>@Override</literal> annotation on <literal>m</literal> would produce an error.</simpara>
<requirement xml:id="IDE-80">
<title>Upper and Lower Bound of a Function Type</title>
<simpara>
<anchor xml:id="Req-IDE-80" xreflabel="[Req-IDE-80]"/>
<emphasis role="strong">Requirement: IDE-80:</emphasis>
<link linkend="Req-IDE-80">Upper and Lower Bound of a Function Type</link> (ver. 1)</simpara>
<simpara>
The upper bound of a function type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>F</mi></math> is a function type with the lower bound types of the parameters and the upper bound of the return type:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mfenced close=")" open="("><mrow><mstyle mathvariant="monospace"><mtext>function</mtext></mstyle><mfenced close=")" open="("><msub><mi>P</mi><mn>1</mn></msub><mrow><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></mrow><msub><mi>P</mi><mi>n</mi></msub></mfenced><mi>:</mi><mi>R</mi></mrow></mfenced><mi>:</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>function</mtext></mstyle><mfenced close=")" open="("><mrow><mi>l</mi><mi>o</mi><mi>w</mi><mi>e</mi><mi>r</mi><mfenced close=")" open="("><msub><mi>P</mi><mn>1</mn></msub></mfenced></mrow><mrow><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></mrow><mrow><mi>l</mi><mi>o</mi><mi>w</mi><mi>e</mi><mi>r</mi><mfenced close=")" open="("><msub><mi>P</mi><mi>n</mi></msub></mfenced></mrow></mfenced><mi>:</mi><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mfenced close=")" open="("><mi>R</mi></mfenced></math></simpara>
<simpara>The lower bound of a function type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>F</mi></math> is a function type with the upper bound types of the parameters and the lower bound of the return type:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>l</mi><mi>o</mi><mi>w</mi><mi>e</mi><mi>r</mi><mfenced close=")" open="("><mrow><mstyle mathvariant="monospace"><mtext>function</mtext></mstyle><mfenced close=")" open="("><msub><mi>P</mi><mn>1</mn></msub><mrow><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></mrow><msub><mi>P</mi><mi>n</mi></msub></mfenced><mi>:</mi><mi>R</mi></mrow></mfenced><mi>:</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>function</mtext></mstyle><mfenced close=")" open="("><mrow><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mfenced close=")" open="("><msub><mi>P</mi><mn>1</mn></msub></mfenced></mrow><mrow><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></mrow><mrow><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mfenced close=")" open="("><msub><mi>P</mi><mi>n</mi></msub></mfenced></mrow></mfenced><mi>:</mi><mi>l</mi><mi>o</mi><mi>w</mi><mi>e</mi><mi>r</mi><mfenced close=")" open="("><mi>R</mi></mfenced></math></simpara>
</requirement>
</section>
<section xml:id="_autoboxing-of-function-type">
<title>Autoboxing of Function Type</title>
<simpara>Function types, compared to other types like String, come only in on flavour: the Function object representation.
There is no primitive function type.
Nevertheless, for function type expressions and function declarations, it is possible to call the properties of Function object directly.
This is similar to autoboxing for strings.</simpara>
<formalpara>
<title>Access of Function properties on functions</title>
<para>
<programlisting language="n4js" linenumbering="unnumbered">// function declaration
var param: number = function(a,b){}.length // 2
function a(x: number) : number { return x*x; }
// function reference
a.length; // 1
// function variable
var f = function(m,l,b){/*...*/};
f.length; // 3
class A {
s: string;
sayS(): string{ return this.s; }
}
var objA: A = new A();
objA.s = "A";
var objB = {s:"B"}
// function variable
var m = objA.sayS; // method as function, detached from objA
var mA: {function(any)} = m.bind(objA); // bind to objA
var mB: {function(any)} = m.bind(objB); // bind to objB
m() // returns: undefined
mA() // returns: A
mB() // returns: B
m.call(objA,1,2,3); // returns: A
m.apply(objB,[1,2,3]); // returns: B
m.toString(); // returns: function sayS(){ return this.s; }</programlisting>
</para>
</formalpara>
</section>
<section xml:id="_arguments-object">
<title>Arguments Object</title>
<simpara>A special arguments object is defined within the body of a function.
It is accessible through the implicitly-defined local variable named ,
unless it is shadowed by a local variable, a formal parameter or a
function named <literal>arguments</literal> or in the rare case that the function itself is called ’arguments’ [<link linkend="ECMA11a">ECMA11a(p.S10.5, p.p.59)</link>].
The argument object has array-like behavior even though it is not of type <literal>array</literal>:</simpara>
<itemizedlist>
<listitem>
<simpara>All actual passed-in parameters of the current execution context can be retrieved by <math xmlns="http://www.w3.org/1998/Math/MathML"><mn>0</mn><mo>-</mo><mi>b</mi><mi>a</mi><mi>s</mi><mi>e</mi><mi>d</mi></math> index access.</simpara>
</listitem>
<listitem>
<simpara>The <literal>length</literal> property of the arguments object stores the actual number of passed-in arguments which may differ from the number of formally defined number of parameters <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><mi>s</mi></math> of the containing function.</simpara>
</listitem>
<listitem>
<simpara>It is possible to store custom values in the arguments object, even outside the original index boundaries.</simpara>
</listitem>
<listitem>
<simpara>All obtained values from the arguments object are of type <literal>any</literal>.</simpara>
</listitem>
</itemizedlist>
<simpara>In non-strict ES mode the <literal>callee</literal> property holds a reference to the function executed [<link linkend="ECMA11a">ECMA11a(p.S10.6, p.p.61)</link>].</simpara>
<requirement xml:id="IDE-81">
<title>Arguments.callee</title>
<simpara>
<anchor xml:id="Req-IDE-81" xreflabel="[Req-IDE-81]"/>
<emphasis role="strong">Requirement: IDE-81:</emphasis>
<link linkend="Req-IDE-81">Arguments.callee</link> (ver. 1)</simpara>
<simpara>
In N4JS and in ES strict mode the use of <literal>arguments.callee</literal> is prohibited.</simpara>
</requirement>
<requirement xml:id="IDE-82">
<title>Arguments as formal parameter name</title>
<simpara>
<anchor xml:id="Req-IDE-82" xreflabel="[Req-IDE-82]"/>
<emphasis role="strong">Requirement: IDE-82:</emphasis>
<link linkend="Req-IDE-82">Arguments as formal parameter name</link> (ver. 1)</simpara>
<simpara>
In N4JS, the formal parameters of the function cannot be named <literal>arguments</literal>.
This applies to all variable execution environments like field accessors (getter/setter, <xref linkend="_field-accessors-getter-setter"/>),
methods (<xref linkend="_methods"/>) and constructors (<xref linkend="_constructor-and-classifier-type"/>), where <literal>FormalParameter</literal> type is used.</simpara>
</requirement>
<programlisting language="n4js" linenumbering="unnumbered">// regular function
function a1(s1: string, n2: number) {
var l: number = arguments.length;
var s: string = arguments[0] as string;
}
class A {
// property access
get s(): string { return ""+arguments.length; } // 0
set s(n: number) { console.log( arguments.length ); } // 1
// method
m(arg: string) {
var l: number = arguments.length;
var s: string = arguments[0] as string;
}
}
// property access in object literals
var x = {
a:5,
get b(): string {
return ""+arguments.length
}
}
// invalid:
function z(){
arguments.length // illegal, see next lines
// define arguments to be a plain variable of type number:
var arguments: number = 4;
}</programlisting>
</section>
</section>
<section xml:id="_ecmascript-5-function-definition" role="language-n4js">
<title>ECMAScript 5 Function Definition</title>
<section xml:id="_function-declaration">
<title>Function Declaration</title>
<section xml:id="_syntax-9">
<title>Syntax</title>
<simpara>A function can be defined as described in [<link linkend="ECMA11a">ECMA11a(p.S13, p.p.98)</link>] and additional annotations can be specified.
Since N4JS is based on [<link linkend="ECMA15a">ECMA15a</link>], the syntax contains constructs not available in [<link linkend="ECMA11a">ECMA11a</link>].
The newer constructs defined only in [<link linkend="ECMA15a">ECMA15a</link>] and proposals already implemented in N4JS are described in <xref linkend="_ecmascript-2015-function-definition"/> and <xref linkend="_ecmascript-proposals-function-definition"/>.</simpara>
<note>
<simpara>In contrast to plain JavaScript, function declarations can be used in blocks in N4JS.
This is only true, however, for N4JS files, not for plain JS files.</simpara>
</note>
<formalpara>
<title>Syntax Function Declaration and Expression</title>
<para>
<programlisting language="xtext" linenumbering="unnumbered">FunctionDeclaration &lt;Yield&gt;:
=&gt; ({FunctionDeclaration}
annotations+=Annotation*
(declaredModifiers+=N4Modifier)*
-&gt; FunctionImpl &lt;Yield,Yield,Expression=false&gt;
) =&gt; Semi?
;
fragment AsyncNoTrailingLineBreak *: (declaredAsync?='async' NoLineTerminator)?;
fragment FunctionImpl&lt;Yield, YieldIfGenerator, Expression&gt;*:
'function'
(
generator?='*' FunctionHeader&lt;YieldIfGenerator,Generator=true&gt; FunctionBody&lt;Yield=true,Expression&gt;
| FunctionHeader&lt;Yield,Generator=false&gt; FunctionBody&lt;Yield=false,Expression&gt;
)
;
fragment FunctionHeader&lt;Yield, Generator&gt;*:
TypeVariables?
name=BindingIdentifier&lt;Yield&gt;?
StrictFormalParameters&lt;Yield=Generator&gt;
(-&gt; ':' returnTypeRef=TypeRef)?
;
fragment FunctionBody &lt;Yield, Expression&gt;*:
&lt;Expression&gt; body=Block&lt;Yield&gt;
| &lt;!Expression&gt; body=Block&lt;Yield&gt;?
;</programlisting>
</para>
</formalpara>
<simpara>Properties of the function declaration and expression are described in <xref linkend="_function-type"/>.</simpara>
<simpara>For this specification, we introduce a supertype <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>F</mi><mi>u</mi><mi>n</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mi>D</mi><mi>e</mi><mi>f</mi><mi>i</mi><mi>n</mi><mi>i</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi></math> for both, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>F</mi><mi>u</mi><mi>n</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mi>D</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>F</mi><mi>u</mi><mi>n</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mi>E</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi></math>.
This supertype contains all common properties of these two subtypes, that is, all properties of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>F</mi><mi>u</mi><mi>n</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mi>E</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi></math>.</simpara>
<example>
<title>Function Declaration with Type Annotation</title>
<programlisting language="n4js" linenumbering="unnumbered">// plain JS
function f(p) { return p.length }
// N4JS
function f(p: string): number { return p.length }</programlisting>
</example>
</section>
<section xml:id="_semantics-8">
<title>Semantics</title>
<simpara>A function defined in a class’s method (or method modifier) builder is a method, see <xref linkend="_methods"/> for details and additional constraints.
The metatype of a function definition is function type (<xref linkend="_function-type"/>), as a function declaration is only a different syntax for creating a <literal>Function</literal> object.
Constraints for function type are described in <xref linkend="_function-type"/>.
Another consequence is that the inferred type of a function definition <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi></math> is simply its function type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>F</mi></math>.</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mrow><mo>[</mo><mspace width="-0.167em"/><mrow><mo>[</mo></mrow></mrow><mrow><mi>f</mi><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi></mrow><mrow><mrow><mo>]</mo></mrow><mspace width="-0.167em"/><mo>]</mo></mrow></mrow><mrow><mrow><mo>[</mo><mspace width="-0.167em"/><mrow><mo>[</mo></mrow></mrow><mi>F</mi><mrow><mrow><mo>]</mo></mrow><mspace width="-0.167em"/><mo>]</mo></mrow></mrow></mfrac></math>
<simpara>Note that the type of a function definition is different from its return type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi></math>!</simpara>
<requirement xml:id="IDE-83">
<title>Function Declaration only on Top-Level</title>
<simpara>
<anchor xml:id="Req-IDE-83" xreflabel="[Req-IDE-83]"/>
<emphasis role="strong">Requirement: IDE-83:</emphasis>
<link linkend="Req-IDE-83">Function Declaration only on Top-Level</link> (ver. 1)</simpara>
<simpara>
</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>In plain JavaScript, function declarations must only be located on top-level, that is they must not be nested in blocks.
Since this is supported by most JavaScript engines, only a warning is issued.</simpara>
</listitem>
</orderedlist>
</requirement>
</section>
</section>
<section xml:id="_function-expression">
<title>Function Expression</title>
<simpara>A function expression [<link linkend="ECMA11a">ECMA11a(p.S11.2.5)</link>] is quite similar to a function declaration.
Thus, most details are explained in <xref linkend="_ecmascript-5-function-definition"/>.</simpara>
<section xml:id="function-expression-syntax">
<title>Syntax</title>
<programlisting language="xtext" linenumbering="unnumbered">FunctionExpression:
({FunctionExpression}
FunctionImpl&lt;Yield=false,YieldIfGenerator=true,Expression=true&gt;
)
;</programlisting>
</section>
<section xml:id="_semantics-and-type-inference">
<title>Semantics and Type Inference</title>
<simpara>In general, the inferred type of a function expression simply is the function type as described in <xref linkend="_function-type"/>.
Often, the signature of a function expression is not explicitly specified but it can be inferred from the context.
The following context information is used to infer the full signature:</simpara>
<itemizedlist>
<listitem>
<simpara>If the function expression is used on the right hand side of an assignment, the expected return type can be inferred from the left hand side.</simpara>
</listitem>
<listitem>
<simpara>If the function expression is used as an argument in a call to another function, the full signature can be inferred from the corresponding type of the formal parameter declaration.</simpara>
</listitem>
</itemizedlist>
<simpara>Although the signature of the function expression may be inferred from the formal parameter if the function expression is used as argument, this inference has some conceptual limitations.
This is demonstrated in the next example.</simpara>
<example>
<title>Inference Of Function Expression’s Signature</title>
<simpara>In general, <literal>{function():any}</literal> is a subtype of <literal>{function():void}</literal> (cf. <xref linkend="_function-type"/>).
When the return type of a function expression is inferred, this relation is taken into account which may lead to unexpected results as shown in the following code snippet:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">function f(cb: {function():void}) { cb() }
f(function() { return 1; });</programlisting>
<simpara>No error is issued: The type of the function expression actually is inferred to <literal>{function():any}</literal>, because there is a return statement with an expression.
It is not inferred to <literal>{function():void}</literal>, even if the formal parameter of <literal>f</literal> suggests that.
Due to the previously-stated relation <literal>{function():any} &lt;: {function():void}</literal> this is correct – the client (in this
case function <literal>f</literal>) works perfectly well even if <literal>cb</literal> returns something.
The contract of arguments states that the type of the argument is a subtype of the type of the formal parameter.
This is what the inferencer takes into account!</simpara>
</example>
</section>
</section>
</section>
<section xml:id="_ecmascript-2015-function-definition" role="language-n4js">
<title>ECMAScript 2015 Function Definition</title>
<section xml:id="_formal-parameters">
<title>Formal Parameters</title>
<simpara>Parameter handling has been significantly upgraded in ECMAScript 6.
It now supports parameter default values, rest parameters (variadics) and destructuring.
Formal parameters can be modified to be either default or variadic.
In case a formal parameter has no modifier, it is called normal.
Modified parameters also become optional.</simpara>
<simpara>Modifiers of formal parameters such as default or rest are neither evaluated nor rewritten in the transpiler.</simpara>
<section xml:id="Type_Modifiers_Optional">
<title>Optional Parameters</title>
<simpara>An optional formal parameter can be omitted when calling a function/method.
An omitted parameter has the value <literal>undefined</literal>.
In case the omitted parameter is variadic, the value is an empty array.</simpara>
<simpara>Parameters can not be declared as optional explicitly.
Instead, being optional is true when a parameter is declared as default or variadic.
Note that any formal parameter that follows a default parameter is itself also a default thus an optional parameter.</simpara>
</section>
<section xml:id="Type_Modifiers_Default">
<title>Default Parameters</title>
<simpara>A default parameter value is specified for a parameter via an equals sign (<literal>=</literal>).
If a caller doesn’t provide a value for the parameter, the default value is used.</simpara>
<simpara>Default initializers of parameters are specified at a formal parameter of a function or method after the equal sign using an arbitrary initializer expression, such as <literal>var = "s"</literal>.
However, this default initializer can be omitted.
When a formal parameter has a declared type, the default initializer is specified at the end, such as: <literal>var : string = "s"</literal>.
The initializer expression is only evaluated in case no actual argument is given for the formal parameter.
Also, the initializer expression is evaluated when the actual argument value is <literal>undefined</literal>.</simpara>
<simpara>Formal parameters become default parameters implicitly when they are preceded by an explicit default parameter.
In such cases, the default initializer is <literal>undefined</literal>.</simpara>
<requirement xml:id="IDE-14501">
<title>Default parameters</title>
<simpara>
<anchor xml:id="Req-IDE-14501" xreflabel="[Req-IDE-14501]"/>
<emphasis role="strong">Requirement: IDE-14501:</emphasis>
<link linkend="Req-IDE-14501">Default parameters</link> (ver. 1)</simpara>
<simpara>
Any normal parameter which is preceded by a default parameter also becomes a default parameter.
Its initializer is <literal>undefined</literal>.</simpara>
</requirement>
<simpara>When a method is overwritten, its default parameters are not part of the overwriting method.
Consequently, initializers of default parameters in abstract methods are obsolete.</simpara>
</section>
<section xml:id="Type_Modifiers_Variadic">
<title>Variadic</title>
<simpara>Variadic parameters are also called <emphasis>rest parameters</emphasis>.
Marking a parameter as variadic indicates that method accepts a variable number of parameters.
A variadic parameter implies that the parameter is also optional as the cardinality is defined as <math xmlns="http://www.w3.org/1998/Math/MathML"><mfenced close="]" open="["><mrow><mn>0.</mn><mo>.</mo><mo>*</mo></mrow></mfenced></math>.
No further parameter can be defined after a variadic parameter.
When no argument is given for a variadic parameter, an empty array is provided when using the parameter in the body of the function or method.</simpara>
<requirement xml:id="IDE-16">
<title>Variadic and optional parameters</title>
<simpara>
<anchor xml:id="Req-IDE-16" xreflabel="[Req-IDE-16]"/>
<emphasis role="strong">Requirement: IDE-16:</emphasis>
<link linkend="Req-IDE-16">Variadic and optional parameters</link> (ver. 1)</simpara>
<simpara>
For a parameter <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>p</mi></math>, the following condition must hold:
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>p</mi><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi><mo></mo><mi>p</mi><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi></math>.</simpara>
<simpara>A parameter can not be declared both variadic and with a default value.
That is to say that one can either write <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>v</mi><mi>a</mi><mi>r</mi><mi>N</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo></math> (default) or <math xmlns="http://www.w3.org/1998/Math/MathML"><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mi>v</mi><mi>a</mi><mi>r</mi><mi>N</mi><mi>a</mi><mi>m</mi><mi>e</mi></math>, but not <math xmlns="http://www.w3.org/1998/Math/MathML"><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mi>v</mi><mi>a</mi><mi>r</mi><mi>N</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo></math>.</simpara>
</requirement>
<simpara>Declaring a variadic parameter of type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math> causes the type of the method parameter to become <literal>Array&lt;T&gt;</literal>.
That is, declaring <literal>function(&#8230;&#8203;tags : string)</literal> causes <literal>tags</literal> to be an <literal>Array&lt;string&gt;</literal> and not just a scalar <literal>string</literal> value.</simpara>
<simpara>To make this work at runtime, the compiler will generate code that constructs the <literal>parameter</literal> from the <literal>arguments</literal> parameter explicitly passed to the function.</simpara>
<requirement xml:id="IDE-17">
<title>Variadic at Runtime</title>
<simpara>
<anchor xml:id="Req-IDE-17" xreflabel="[Req-IDE-17]"/>
<emphasis role="strong">Requirement: IDE-17:</emphasis>
<link linkend="Req-IDE-17">Variadic at Runtime</link> (ver. 1)</simpara>
<simpara>
</simpara>
<simpara>At runtime, a variadic parameter is never set to undefined.
Instead, the array may be empty.
This must be true even if preceding parameters are optional and no arguments are passed at runtime.</simpara>
</requirement>
<simpara>For more constraints on using the variadic modifier, see <xref linkend="_function-object-type"/>.</simpara>
</section>
</section>
<section xml:id="_generator-functions">
<title>Generator Functions</title>
<simpara>Generators come together with the <literal>yield</literal> expression and can play three roles:
the role of an iterator (data producer), of an observer (data consumer), and a combined role which is called coroutines.
When calling a generator function or method, the returned generator object of type <literal>Generator&lt;TYield,TReturn,TNext&gt;</literal> can be controlled by its methods
(cf. [<link linkend="ECMA15a">ECMA15a(p.S14.4)</link>], also see [<link linkend="Kuizinas14a">Kuizinas14a</link>]).</simpara>
<section xml:id="generator-functions-syntax">
<title>Syntax</title>
<simpara>Generator functions and methods differ from ordinary functions and methods only in the additional <literal>*</literal> symbol before the function or method name.
The following syntax rules are extracted from the real syntax rules.
They only display parts relevant to declaring a function or method as a generator.</simpara>
<programlisting language="xtext" linenumbering="unnumbered">GeneratorFunctionDeclaration &lt;Yield&gt;:
(declaredModifiers+=N4Modifier)*
'function' generator?='*'
FunctionHeader&lt;YieldIfGenerator,Generator=true&gt;
FunctionBody&lt;Yield=true,Expression=false&gt;
;
GeneratorFunctionExpression:
'function' generator?='*'
FunctionHeader&lt;YieldIfGenerator,Generator=true&gt;
FunctionBody&lt;Yield=true,Expression=true&gt;
;
GeneratorMethodDeclaration:
annotations+=Annotation+ (declaredModifiers+=N4Modifier)* TypeVariables?
generator?='*' NoLineTerminator LiteralOrComputedPropertyName&lt;Yield&gt;
MethodParamsReturnAndBody&lt;Generator=true&gt;</programlisting>
</section>
<section xml:id="generator-functions-semantics">
<title>Semantics</title>
<simpara>The basic idea is to make code dealing with Generators easier to write and more readable without changing their functionality.
Take this example:</simpara>
<example xml:id="ex:two-simple-generator-functions">
<title>Two simple generator functions</title>
<programlisting language="n4js" linenumbering="unnumbered">// explicit form of the return type
function * countTo(iMax:int) : Generator&lt;int,string,undefined&gt; {
for (int i=0; i&lt;=iMax; i++)
yield i;
return "finished";
}
val genObj1 = countTo(3);
val values1 = [...genObj1]; // is [0,1,2,3]
val lastObj1 = genObj1.next(); // is {value="finished",done=true}
// shorthand form of the return type
function * countFrom(start:int) : int {
for (int i=start; i&gt;=0; i--)
yield i;
return finished;
}
val genObj2 = countFrom(3);
val values2 = [...genObj2]; // is [3,2,1,0]
val lastObj2 = genObj2.next(); // is {value="finished",done=true}</programlisting>
<simpara>In the example above, two generator functions are declared.
The first declares its return type explicitly whereas the second uses a shorthand form.</simpara>
</example>
<simpara>Generator functions and methods return objects of the type <literal>Generator&lt;TYield,TReturn,TNext&gt;</literal> which is a subtype of the <literal>Iterable&lt;TYield&gt;</literal> and <literal>Iterator&lt;TYield&gt;</literal> interfaces.
Moreover, it provides the methods <literal>throw(exception:any)</literal> and <literal>return(value:TNext?)</literal> for advanced control of the generator object.
The complete interface of the generator class is given below.</simpara>
<formalpara>
<title>The generator class</title>
<para>
<programlisting language="n4js" linenumbering="unnumbered">public providedByRuntime interface Generator&lt;out TYield, out TReturn, in TNext&gt;
extends Iterable&lt;TYield&gt;, Iterator&lt;TYield&gt; {
public abstract next(value: TNext?): IteratorEntry&lt;TYield&gt;
public abstract [Symbol.iterator](): Generator&lt;TYield, TReturn, TNext&gt;
public abstract throw(exception: any): IteratorEntry&lt;TYield&gt;;
public abstract return(value: TNext?): IteratorEntry&lt;TReturn&gt;;
}</programlisting>
</para>
</formalpara>
<requirement xml:id="IDE-14370">
<title>Modifier <literal>*</literal></title>
<simpara>
<anchor xml:id="Req-IDE-14370" xreflabel="[Req-IDE-14370]"/>
<emphasis role="strong">Requirement: IDE-14370:</emphasis>
<link linkend="Req-IDE-14370">Modifier `pass:[*]`</link> (ver. 1)</simpara>
<simpara>
. <literal>*</literal> may be used on declared functions and methods, and for function expressions.
. A function or method <emphasis>f</emphasis> with a declared return type <emphasis>R</emphasis> that is declared <literal>*</literal> has an actual return type of <literal>Generator&lt;TYield,TReturn,TNext&gt;</literal>.
. A generator function or method can have no declared return type, a shorthand form of a return type or an explicitly declared return type.
.. The explicitly declared return type is of the form <literal>Generator&lt;TYield,TReturn,TNext&gt;</literal> with the type variables:
&#8230;&#8203; <emphasis>TYield</emphasis> as the expected type of the yield expression argument,
&#8230;&#8203; <emphasis>TReturn</emphasis> as the expected type of the return expression, and
&#8230;&#8203; <emphasis>TNext</emphasis> as both the return type of the yield expression.
.. The shorthand form only declares the type of <emphasis>TYield</emphasis> which implicitly translates to <literal>Generator&lt;TYield,TReturn,any&gt;</literal> as the return type.
&#8230;&#8203; The type <emphasis>TReturn</emphasis> is inferred to either <literal>undefined</literal> or <literal>any</literal> from the body.
&#8230;&#8203; In case the declared type is <literal>void</literal>, actual return type evaluates to <literal>Generator&lt;undefined,undefined,any&gt;</literal>.
.. If no return type is declared, both <emphasis>TYield</emphasis> and <emphasis>TReturn</emphasis> are inferred from the body to either <literal>any</literal> or <literal>undefined</literal>. <emphasis>TNext</emphasis> is <literal>any</literal>.
. Given a generator function or method <emphasis>f</emphasis> with an actual return type <literal>Generator&lt;TYield,TReturn,TNext&gt;</literal>:
.. all yield statements in <emphasis>f</emphasis> must have an expression of type <emphasis>TYield</emphasis>.
.. all return statements in <emphasis>f</emphasis> must have an expression of type <emphasis>TReturn</emphasis>.
. Return statements in generator functions or methods are always optional.</simpara>
</requirement>
<requirement xml:id="IDE-14371">
<title>Modifier <literal>yield</literal> and <literal>yield*</literal></title>
<simpara>
<anchor xml:id="Req-IDE-14371" xreflabel="[Req-IDE-14371]"/>
<emphasis role="strong">Requirement: IDE-14371:</emphasis>
<link linkend="Req-IDE-14371">Modifier `yield` and `yield*`</link> (ver. 1)</simpara>
<simpara>
. <literal>yield</literal> and <literal>yield*</literal> may only be in body of generator functions or methods.
. <literal>yield expr</literal> takes only expressions <emphasis>expr</emphasis> of type <emphasis>TYield</emphasis> in a generator function or methods with the actual type <literal>Generator&lt;TYield,TReturn,TNext&gt;</literal>.
. The return type of the <literal>yield</literal> expression is <emphasis>TNext</emphasis>.
. <literal>yield* fg()</literal> takes only iterators of type <literal>Iterator&lt;TYield&gt;</literal>, and generator functions or methods <emphasis>fg</emphasis> with the actual return type <literal>Generator&lt;? extends TYield,? extends TReturn,? super TNext&gt;</literal>.
. The return type of the <literal>yield*</literal> expression is <emphasis>any</emphasis>, since a custom iterator could return an entry <literal>{done=true,value}</literal> and any value for the variable <literal>value</literal>.</simpara>
</requirement>
<simpara>Similar to <literal>async</literal> functions, shorthand and explicit form <literal>* function():int{};</literal> and <literal>* function():Generator&lt;int,TResult,any&gt;</literal> are equal,
given that the inferred <emphasis>TResult</emphasis> of the former functions equals to <emphasis>TResult</emphasis> in the latter function).
In other words, the return type of generator functions or methods is wrapped when it is not explicitly defined as <literal>Generator</literal> already.
Thus, whenever a nested generator type is desired, it has to be defined explicitly.
Consider the example below.</simpara>
<formalpara>
<title>Type variables with async methods.</title>
<para>
<programlisting language="n4js" linenumbering="unnumbered">class C&lt;T&gt; {
genFoo(): T{} // equals to genFoo(): Generator&lt;T, undefined, any&gt;;
// note that TResult depends on the body of genFoo()
}
function fn(C&lt;int&gt; c1, C&lt;Generator&lt;int,any,any&gt;&gt; c2) {
c1.genFoo(); // returns Generator&lt;int, undefined, any&gt;
c2.genFoo(); // returns Generator&lt;Generator&lt;int,any,any&gt;, undefined, any&gt;
}</programlisting>
</para>
</formalpara>
</section>
<section xml:id="_generator-arrow-functions">
<title>Generator Arrow Functions</title>
<simpara>As of now, generator arrow functions are not supported by EcmaScript 6 and also, the support is not planned.
However, introducing generator arrow function in EcmaScript is still under discussion.
For more information, please refer to <link xl:href="https://esdiscuss.org/topic/generator-arrow-functions">ESDiscuss.org</link> and <link xl:href="https://esdiscuss.org/topic/why-do-generator-expressions-return-generators">StackOverflow.com</link>.</simpara>
</section>
</section>
<section xml:id="_arrow-function-expression">
<title>Arrow Function Expression</title>
<simpara>This is an ECMAScript 6 expression (see [<link linkend="ECMA15a">ECMA15a(p.S14.2)</link>]) for simplifying the definition of anonymous function expressions, a.k.a. lambdas or closures.
The ECMAScript Specification calls this a function definition even though they may only appear in the context of expressions.</simpara>
<simpara>Along with Assignments, Arrow function expressions have the least precedence, e.g. they serve as the entry point for the expression tree.</simpara>
<simpara>Arrow function expressions can be considered syntactic window-dressing for old-school function expressions and therefore do not support the
benefits regarding parameter annotations although parameter types may be given explicitly.
The return type can be given as type hint if desired, but this is not mandatory (if left out, the return type is inferred).
The notation <literal>@=></literal> stands for an async arrow function (<xref linkend="_asynchronous-arrow-functions"/>).</simpara>
<section xml:id="arrow-function-expression-syntax">
<title>Syntax</title>
<simpara>The simplified syntax reads like this:</simpara>
<programlisting language="xtext" linenumbering="unnumbered">ArrowExpression returns ArrowFunction:
=&gt;(
{ArrowFunction}
(
'('
( fpars+=FormalParameterNoAnnotations ( ',' fpars+=FormalParameterNoAnnotations )* )?
')'
(':' returnTypeRef=TypeRef)?
| fpars+=FormalParameterNoType
)
'=&gt;'
) (
(=&gt; hasBracesAroundBody?='{' body=BlockMinusBraces '}') | body=ExpressionDisguisedAsBlock
)
;
FormalParameterNoAnnotations returns FormalParameter:
(declaredTypeRef=TypeRef variadic?='...'?)? name=JSIdentifier
;
FormalParameterNoType returns FormalParameter: name=JSIdentifier;
BlockMinusBraces returns Block: {Block} statements+=Statement*;
ExpressionDisguisedAsBlock returns Block:
{Block} statements+=AssignmentExpressionStatement
;
AssignmentExpressionStatement returns ExpressionStatement: expression=AssignmentExpression;</programlisting>
</section>
<section xml:id="arrow-function-expression-semantics-and-type-inference">
<title>Semantics and Type Inference</title>
<simpara>Generally speaking, the semantics are very similar to the function
expressions but the devil’s in the details:</simpara>
<itemizedlist>
<listitem>
<simpara><literal>arguments</literal>: Unlike normal function expressions, an arrow function does not introduce an implicit <literal>arguments</literal> variable (<xref linkend="_arguments-object"/>),
therefore any occurrence of it in the arrow function’s body has always the same binding as an occurrence of <literal>arguments</literal> in the lexical context enclosing the arrow function.</simpara>
</listitem>
<listitem>
<simpara><literal>this</literal>: An arrow function does not introduce a binding of its own for the <literal>this</literal> keyword. That explains why uses in the body of arrow function have the same meaning as occurrences in the enclosing lexical scope.
As a consequence, an arrow function at the top level has both usages of <literal>arguments</literal> and <literal>this</literal> flagged as error (the outer lexical context doesn’t provide definitionsfor them).</simpara>
</listitem>
<listitem>
<simpara><literal>super</literal>: As with function expressions in general, whether of the arrow variety or not, the usage of <literal>super</literal> isn’t allowed in the body of arrow functions.</simpara>
</listitem>
</itemizedlist>
<requirement xml:id="IDE-84">
<title>No This in Top Level Arrow Function in N4JS Mode</title>
<simpara>
<anchor xml:id="Req-IDE-84" xreflabel="[Req-IDE-84]"/>
<emphasis role="strong">Requirement: IDE-84:</emphasis>
<link linkend="Req-IDE-84">No This in Top Level Arrow Function in N4JS Mode</link> (ver. 1)</simpara>
<simpara>
In N4JS, a top-level arrow function can’t refer to <literal>this</literal> as there’s no outer lexical context that provides a binding for it.</simpara>
</requirement>
<requirement xml:id="IDE-85">
<title>No Arguments in Top Level Arrow Function</title>
<simpara>
<anchor xml:id="Req-IDE-85" xreflabel="[Req-IDE-85]"/>
<emphasis role="strong">Requirement: IDE-85:</emphasis>
<link linkend="Req-IDE-85">No Arguments in Top Level Arrow Function</link> (ver. 1)</simpara>
<simpara>
In N4JS, a top-level arrow function can’t include usages of <literal>arguments</literal> in its body, again because of the missing binding for it.</simpara>
</requirement>
</section>
</section>
</section>
<section xml:id="_ecmascript-proposals-function-definition" role="language-n4js">
<title>ECMAScript Proposals Function Definition</title>
<section xml:id="_asynchronous-functions">
<title>Asynchronous Functions</title>
<simpara>To improve language-level support for asynchronous code, there exists an ECMAScript proposal <footnote><simpara>see <link xl:href="http://tc39.github.io/ecmascript-asyncawait/">http://tc39.github.io/ecmascript-asyncawait/</link></simpara></footnote> based on Promises which are provided by ES6 as built-in types.
N4JS implements this proposal.
This concept is supported for declared functions and methods (<xref linkend="_asynchronous-methods"/>) as well
as for function expressions and arrow functions (<xref linkend="_asynchronous-arrow-functions"/>).</simpara>
<section xml:id="asynchronous-functions-syntax">
<title>Syntax</title>
<simpara>The following syntax rules are extracted from the real syntax rules.
They only display parts relevant to declaring a function or method as
asynchronous.</simpara>
<programlisting language="xtext" linenumbering="unnumbered">AsyncFunctionDeclaration &lt;Yield&gt;:
(declaredModifiers+=N4Modifier)*
declaredAsync?='async' NoLineTerminator 'function'
FunctionHeader&lt;Yield,Generator=false&gt;
FunctionBody&lt;Yield=false,Expression=false&gt;
;
AsyncFunctionExpression:
declaredAsync?='async' NoLineTerminator 'function'
FunctionHeader&lt;Yield=false,Generator=false&gt;
FunctionBody&lt;Yield=false,Expression=true&gt;
;
AsyncArrowExpression &lt;In, Yield&gt;:
declaredAsync?='async' NoLineTerminator '('
(fpars+=FormalParameter&lt;Yield&gt;
(',' fpars+=FormalParameter&lt;Yield&gt;)*)?
')' (':' returnTypeRef=TypeRef)? '=&gt;'
( '{' body=BlockMinusBraces&lt;Yield&gt; '}'
| body=ExpressionDisguisedAsBlock&lt;In&gt;
)
;
AsyncMethodDeclaration:
annotations+=Annotation+ (declaredModifiers+=N4Modifier)* TypeVariables?
declaredAsync?='async' NoLineTerminator LiteralOrComputedPropertyName&lt;Yield&gt;
MethodParamsReturnAndBody</programlisting>
<simpara>’async’ is not a reserved word in ECMAScript and it can therefore be
used either as an identifier or as a keyword, depending on the context.
When used as a modifier to declare a function as asynchronous, then
there must be no line terminator after the <literal>async</literal> modifier. This enables the
parser to distinguish between using <literal>async</literal> as an identifier reference and a
keyword, as shown in the next example.</simpara>
<example>
<title>Async as keyword and identifier</title>
<programlisting language="n4js" linenumbering="unnumbered">async <co xml:id="CO2-1"/>
function foo() {}
// vs
async function bar(); <co xml:id="CO2-2"/></programlisting>
<calloutlist>
<callout arearefs="CO2-1">
<para>In this snippet, the <literal>async</literal> on line 1 is an identifier reference (referencing a
variable or parameter) and the function defined on line 2 is a
non-asynchronous function. The automatic semicolon insertion adds a
semicolon after the reference on line 1.</para>
</callout>
<callout arearefs="CO2-2">
<para>In contrast, <literal>async</literal> on line 4 is recognized as a modifier declaring the function as asynchronous.</para>
</callout>
</calloutlist>
</example>
</section>
<section xml:id="asynchronous-functions-semantics">
<title>Semantics</title>
<simpara>The basic idea is to make code dealing with Promises easier to write and
more readable without changing the functionality of Promises. Take this
example:</simpara>
<formalpara>
<title>A simple asynchronous function using async/await.</title>
<para>
<programlisting language="n4js" linenumbering="unnumbered">// some asynchronous legacy API using promises
interface DB {}
interface DBAccess {
getDataBase(): Promise&lt;DB,?&gt;
loadEntry(db: DB, id: string): Promise&lt;string,?&gt;
}
var access: DBAccess;
// our own function using async/await
async function loadAddress(id: string) : string {
try {
var db: DB = await access.getDataBase();
var entry: string = await access.loadEntry(db, id);
return entry.address;
}
catch(err) {
// either getDataBase() or loadEntry() failed
throw err;
}
}</programlisting>
</para>
</formalpara>
<simpara>The modifier <literal>async</literal> changes the return type of <literal>loadAddress()</literal> from <literal>string</literal> (the declared return type) to <literal>Promise&lt;string,?&gt;</literal> (the actual return type).
For code inside the function, the return type is still <literal>string</literal>:
the value in the return statement of the last line will be wrapped in a Promise.
For client code outside the function and in case of recursive invocations, the return type is <literal>Promise&lt;string,?&gt;</literal>.
To raise an error, simply throw an exception, its value will become the error value of the returned Promise.</simpara>
<simpara>If the expression after an <literal>await</literal> evaluates to a <literal>Promise</literal>, execution of the enclosing asynchronous function will be suspended until either a success value is available
(which will then make the entire await-expression evaluate to this success value and continue execution)
or until the Promise is rejected (which will then cause an exception to be thrown at the location of the await-expression).
If, on the other hand, the expression after an <literal>await</literal> evaluates to a non-promise, the value will be simply passed through.
In addition, a warning is shown to indicate the unnecessary <literal>await</literal> expression.</simpara>
<simpara>Note how method <literal>loadAddress()</literal> above can be implemented without any explicit references to the built-in type Promise.
In the above example we handle the errors of the nested asynchronous calls to <literal>getDataBase()</literal> and <literal>loadEntry()</literal> for demonstration purposes only;
if we are not interested in the errors we could simply remove the try/catch block and any errors would be forwarded to the caller of <literal>loadAddress()</literal>.</simpara>
<simpara>Invoking an async function commonly adopts one of two forms:</simpara>
<itemizedlist>
<listitem>
<simpara><literal>var p: Promise&lt;successType,?&gt; = asyncFn()</literal></simpara>
</listitem>
<listitem>
<simpara><literal>await asyncFn()</literal></simpara>
</listitem>
</itemizedlist>
<simpara>These patterns are so common that a warning is available whenever both</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara><literal>Promise</literal> is omitted as expected type; and</simpara>
</listitem>
<listitem>
<simpara><literal>await</literal> is also omitted.</simpara>
</listitem>
</orderedlist>
<simpara>The warning aims at hinting about forgetting to wait for the result, while remaining non-noisy.</simpara>
<requirement xml:id="IDE-86">
<title>Modifier <literal>async</literal> and <literal>await</literal></title>
<simpara>
<anchor xml:id="Req-IDE-86" xreflabel="[Req-IDE-86]"/>
<emphasis role="strong">Requirement: IDE-86:</emphasis>
<link linkend="Req-IDE-86">Modifier `async` and `await`</link> (ver. 1)</simpara>
<simpara>
</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara><literal>async</literal> may be used on declared functions and methods as well as for function expressions and arrow functions.</simpara>
</listitem>
<listitem>
<simpara>A function or method that is declared <literal>async</literal> can have no declared return type, a shorthand form of a return type or an explicitly declared return type.</simpara>
<orderedlist numeration="loweralpha">
<listitem>
<simpara>The explicitly declared return type is of the form <literal>Promise&lt;R,E&gt;</literal> where <emphasis>R</emphasis> is the type of all return statements in the body, and E is the type of exceptions that are thrown in the body.</simpara>
</listitem>
<listitem>
<simpara>The shorthand form only declares the type of <emphasis>R</emphasis> which implicitly translates to <literal>Promise&lt;R,?&gt;</literal> as the actual return type.</simpara>
</listitem>
<listitem>
<simpara>In case no return type is declared, the type <emphasis>R</emphasis> of <literal>Promise&lt;R,?&gt;</literal> is inferred from the body.</simpara>
</listitem>
</orderedlist>
</listitem>
<listitem>
<simpara>A function or method <emphasis>f</emphasis> with a declared return type <emphasis>R</emphasis> that is declared <literal>async</literal> has an actual return type of</simpara>
<orderedlist numeration="loweralpha">
<listitem>
<simpara><literal>R</literal> if <emphasis>R</emphasis> is a subtype of <literal>Promise&lt;?,?&gt;</literal>,</simpara>
</listitem>
<listitem>
<simpara><literal>Promise&lt;undefined,?&gt;</literal> if <emphasis>R</emphasis> is type <literal>void</literal>.</simpara>
</listitem>
<listitem>
<simpara><literal>Promise&lt;R,?&gt;</literal> in all other cases (i.e. the declared return type <emphasis>R</emphasis> is being wrapped in a <literal>Promise</literal>).</simpara>
</listitem>
</orderedlist>
</listitem>
<listitem>
<simpara>Return type inference is only performed when no return type is declared.</simpara>
<orderedlist numeration="loweralpha">
<listitem>
<simpara>The return type <literal>R</literal> of <literal>Promise&lt;R,?&gt;</literal> is inferred either as <literal>void</literal> or as <literal>any</literal>.</simpara>
</listitem>
</orderedlist>
</listitem>
<listitem>
<simpara>Given a function or method <emphasis>f</emphasis> that is declared <literal>async</literal> with a declared return type <emphasis>R</emphasis>, or with a declared return type <literal>Promise&lt;R,?&gt;</literal>,
all return statements in <emphasis>f</emphasis> must have an expression of type <emphasis>R</emphasis> (and not of type <literal>Promise&lt;R,?&gt;</literal>).</simpara>
</listitem>
<listitem>
<simpara><literal>await</literal> can be used in expressions directly enclosed in an async function, and behaves like a unary operator with the same precedence as <literal>yield</literal> in ES6.</simpara>
</listitem>
<listitem>
<simpara>Given an expression <emphasis>expr</emphasis> of type
<emphasis>T</emphasis>, the type of (<literal>await</literal> <emphasis>expr</emphasis>) is inferred to <emphasis>T</emphasis> if
<emphasis>T</emphasis> is not a Promise, or it is inferred to <emphasis>S</emphasis> if
<emphasis>T</emphasis> is a Promise with a success value of type
<emphasis>S</emphasis>, i.e. <emphasis>T &lt;: Promise&lt;S,?&gt;</emphasis> .</simpara>
</listitem>
</orderedlist>
</requirement>
<simpara>In other words, the return type <emphasis>R</emphasis> of <literal>async</literal> functions and methods will always be wrapped to <literal>Promise&lt;R,?&gt;</literal> unless <emphasis>R</emphasis> is a <literal>Promise</literal> already.
As a consequence, nested <literal>Promise</literal>s as a return type of a async function or method have to be stated explicitly like <literal>Promise&lt;Promise&lt;R,?&gt;,?&gt;</literal>.</simpara>
<simpara>When a type variable <literal>T</literal> is used to define the the return type of an async function or method, it will always be wrapped.
Consider the example below.</simpara>
<example>
<title>Type variables with async methods.</title>
<programlisting language="n4js" linenumbering="unnumbered">interface I&lt;T&gt; {
async foo(): T; // amounts to foo(): Promise&lt;T,?&gt;
}
function snafu(i1: I&lt;int&gt;, i2: I&lt;Promise&lt;int,?&gt;&gt;) {
i1.foo(); // returns Promise&lt;int,?&gt;
i2.foo(); // returns Promise&lt;Promise&lt;int,?&gt;,?&gt;
}</programlisting>
</example>
</section>
<section xml:id="_asynchronous-arrow-functions">
<title>Asynchronous Arrow Functions</title>
<simpara>An <literal>await</literal> expression is allowed in the body of an async arrow function but not
in the body of a non-async arrow function. The semantics here are
intentional and are in line with similar constraint for function
expressions.</simpara>
</section>
</section>
</section>
<section xml:id="_n4js-extended-function-definition" role="language-n4js">
<title>N4JS Extended Function Definition</title>
<section xml:id="_generic-functions">
<title>Generic Functions</title>
<simpara>A generic function is a function with a list of generic type parameters.
These type parameters can be used in the function signature to declare the types of formal parameters and the return type.
In addition, the type parameters can be used in the function body, for example when declaring the type of a local variable.</simpara>
<simpara>In the following listing, a generic function <literal>foo</literal> is defined that has two type parameters <literal>S</literal> and <literal>T</literal>.
Thereby <literal>S</literal> is used as to declare the parameter type <literal>Array&lt;S&gt;</literal> and <literal>T</literal> is used as the return type and to construct the returned value in the function body.</simpara>
<formalpara>
<title>Generic Function Definition</title>
<para>
<programlisting language="n4js" linenumbering="unnumbered">function &lt;S,T&gt; foo(s: Array&lt;S&gt;): T { return new T(s); }</programlisting>
</para>
</formalpara>
<simpara>If a generic type parameter is not used as a formal parameter type or
the return type, a warning is generated.</simpara>
</section>
<section xml:id="_promisifiable-functions">
<title>Promisifiable Functions</title>
<simpara>In many existing libraries, which have been developed in pre-ES6-promise-API times, callback methods are used for asynchronous behavior.
An asynchronous function follows the following conventions:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">'function' name '(' arbitraryParameters ',' callbackFunction ')'</programlisting>
<simpara>Usually the function returns nothing (<literal>void</literal>).
The callback function usually takes two arguments,in which the first is an error object and the other is the result value of the asynchronous operation.
The callback function is called from the asynchronous function, leading to nested function calls (aka ’callback hell’).</simpara>
<simpara>In order to simplify usage of this pattern, it is possible to mark such a function or method as <literal>@Promisifiable</literal>.
It is then possible to ’promisify’ an invocation of this function or method, which means no callback function argument has to be provided and a will be returned.
The function or method can then be used as if it were declared with <literal>async</literal>.
This is particularly useful in N4JS definition files (.n4jsd) to allow using an existing callback-based API from N4JS code with the more convenient <literal>await</literal>.</simpara>
<example>
<title>Promisifiable</title>
<simpara>Given a function with an N4JS signature</simpara>
<programlisting language="n4js" linenumbering="unnumbered">f(x: int, cb: {function(Error, string)}): void</programlisting>
<simpara>This method can be annotated with <literal>Promisifiable</literal> as follows:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">@Promisifiable f(x: int, cb: {function(Error, string)}): void</programlisting>
<simpara>With this annotation, the function can be invoked in four different
ways:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">f(42, function(err, result1) { /* ... */ }); // traditional
var promise: Promise&lt;string,Error&gt; = @Promisify f(42); // promise
var result3: string = await @Promisify f(42); // long
var result4: string = await f(42); // short</programlisting>
<simpara>The first line is only provided for completeness and shows that a promisifiable function can still be used in the ordinary way by providing a callback - no special handling will occur in this case.
The second line shows how <literal>f</literal> can be promisified using the <literal>@Promisify</literal> annotation - no callback needs to be provided and instead, a <literal>Promise</literal> will be returned.
We can either use this promise directly or immediately <literal>await</literal> on it, as shown in line 3.
The syntax shown in line 4 is merely shorthand for <literal>await @Promisify</literal>, i.e. the annotation is optional after <literal>await</literal>.</simpara>
</example>
<requirement xml:id="IDE-87">
<title>Promisifiable</title>
<simpara>
<anchor xml:id="Req-IDE-87" xreflabel="[Req-IDE-87]"/>
<emphasis role="strong">Requirement: IDE-87:</emphasis>
<link linkend="Req-IDE-87">Promisifiable</link> (ver. 1)</simpara>
<simpara>
A function or method <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi></math> can be annotated with <literal>@Promisifiable</literal> if and only if the following constraints hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>Last parameter of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi></math> is a function (the <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>a</mi><mi>l</mi><mi>l</mi><mi>b</mi><mi>a</mi><mi>c</mi><mi>k</mi></math>).</simpara>
</listitem>
<listitem>
<simpara>The <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>a</mi><mi>l</mi><mi>l</mi><mi>b</mi><mi>a</mi><mi>c</mi><mi>k</mi></math> has a signature of</simpara>
<itemizedlist>
<listitem>
<simpara><literal>{function(E, T0, T1, &#8230;&#8203;, Tn): V}</literal>, or</simpara>
</listitem>
<listitem>
<simpara><literal>{function(T0, T1, &#8230;&#8203;, Tn): V}</literal></simpara>
<simpara>in which <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>E</mi></math> is type <literal>Error</literal> or a subtype thereof, <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>T</mi><mn>0</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub></math> are arbitrary types except or its subtypes.
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>E</mi></math>, if given, is then the type of the error value, and <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>T</mi><mn>0</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub></math> are the types of the success values of the asynchronous operation.<?asciidoc-br?>
Since the return value of the synchronous function call is not available when using <literal>@Promisify</literal>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>V</mi></math> is recommended to be <literal>void</literal>, but it can be any type.</simpara>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<simpara>The callback parameter may be optional.<footnote><simpara>Even in this case, the function will actually be called with the callback method which is then created by the transpiler. However, the callback is not given in the N4JS code).</simpara></footnote></simpara>
</listitem>
</orderedlist>
</requirement>
<simpara>According to <xref linkend="Req-IDE-87"/>, a promisifiable function or method may or may not have a non-void return type, and that only the first parameter of the callback is allowed to be of type <literal>Error</literal>, all other parameters must be of other types.</simpara>
<requirement xml:id="IDE-88">
<title>@Promisify and await with promisifiable functions</title>
<simpara>
<anchor xml:id="Req-IDE-88" xreflabel="[Req-IDE-88]"/>
<emphasis role="strong">Requirement: IDE-88:</emphasis>
<link linkend="Req-IDE-88">@Promisify and await with promisifiable functions</link> (ver. 1)</simpara>
<simpara>
A promisifiable function <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi></math> with one of the two valid
signatures given in <xref linkend="Req-IDE-87"/> can be promisified with <literal>Promisify</literal> or
used with <literal>await</literal>, if and only if the following constraints hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>Function <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi></math> must be annotated with <literal>@Promisifiable</literal>.</simpara>
</listitem>
<listitem>
<simpara>Using <literal>@Promisify f()</literal> without <literal>await</literal> returns a promise of type <literal>Promise&lt;S,F&gt;</literal> where</simpara>
<itemizedlist>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi></math> is <literal>IterableN&lt;T0,&#8230;&#8203;,Tn&gt;</literal> if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi><mo></mo><mn>2</mn></math>, <literal>T</literal> if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi><mo>=</mo><mn>1</mn></math>, and <literal>undefined</literal> if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi><mo>=</mo><mn>0</mn></math>.</simpara>
</listitem>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>F</mi></math> is <literal>E</literal> if given, <literal>undefined</literal> otherwise.</simpara>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<simpara>Using <literal>await @Promisify f()</literal> returns a value of type <literal>IterableN&lt;T0,&#8230;&#8203;,Tn&gt;</literal> if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi><mo></mo><mn>2</mn></math>, of type <literal>T</literal> if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi><mo>=</mo><mn>1</mn></math>, and of type <literal>undefined</literal> if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi><mo>=</mo><mn>0</mn></math>.</simpara>
</listitem>
<listitem>
<simpara>In case of using an <literal>await</literal>, the annotation can be omitted.<?asciidoc-br?>
I.e., <literal>await @Promisify f()</literal> is equivalent to <literal>await f()</literal>.</simpara>
</listitem>
<listitem>
<simpara>Only call expressions using f as target can be promisified, in other
words this is illegal:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">var pf = @Promisify f; // illegal code!</programlisting>
</listitem>
</orderedlist>
</requirement>
</section>
</section>
</chapter>
<chapter xml:id="_conversions-and-reflection">
<title>Conversions and Reflection</title>
<section xml:id="_autoboxing-and-coercing" role="language-n4js">
<title>Autoboxing and Coercing</title>
<simpara>Coercing is the ability to implicitly cast one (primitive) type to another.
Autoboxing is a special kind of coercing in that is the ability to automatically convert a primitive value type, such as <literal>string</literal>, <literal>number</literal>, or <literal>boolean</literal>,
to its corresponding Object type version <literal>String</literal>, <literal>Number</literal>, <literal>Boolean</literal>.
The capital letters in the latter are an essential distinction.</simpara>
<simpara>Conversion between primitives and object-representations of a datatype are not automatic in N4JS. Only in the cases of object-method invocations on a primitive type
(for <literal>string</literal> to call <literal>"abc".length</literal>, for example) automatic conversion is applied.</simpara>
<simpara>Note that N4JS specific primitive types <literal>pathselector</literal> and <literal>i18nkey</literal> are handled similarly to <literal>string</literal>.</simpara>
<section xml:id="_coercing">
<title>Coercing</title>
<simpara>In [<link linkend="ECMA11a">ECMA11a</link>], coercing is defined by means of the abstract specification method <literal>ToPrimitive</literal> [<link linkend="ECMA11a">ECMA11a(p.S9.1)</link>], also see [<link linkend="ECMA11a">ECMA11a(p.S9.10)</link>]).
Other conversions, such as <literal>ToNumber</literal>, are not directly supported but reflected in the typing rules of expressions.</simpara>
<simpara>We express absence of automatic coercion here by means of subtype
relations:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>Boolean</mtext></mstyle><mspace width="1.0mm"/><mo></mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>boolean</mtext></mstyle></mrow></mfrac><mspace width="3.0mm"/><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>boolean</mtext></mstyle><mspace width="1.0mm"/><mo></mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>Boolean</mtext></mstyle></mrow></mfrac></mtd></mtr><mtr><mtd><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>Number</mtext></mstyle><mspace width="1.0mm"/><mo></mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>number</mtext></mstyle></mrow></mfrac><mspace width="3.0mm"/><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>number</mtext></mstyle><mspace width="1.0mm"/><mo></mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>Number</mtext></mstyle></mrow></mfrac></mtd></mtr><mtr><mtd><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>String</mtext></mstyle><mspace width="1.0mm"/><mo></mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>string</mtext></mstyle></mrow></mfrac><mspace width="3.0mm"/><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>string</mtext></mstyle><mspace width="1.0mm"/><mo></mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>String</mtext></mstyle></mrow></mfrac></mtd></mtr></mtable></math>
<simpara>and for the N4JS specific types:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>pathSelector</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mo>&gt;</mo><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>string</mtext></mstyle></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>i18nKey</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>string</mtext></mstyle></mrow></mfrac></math></simpara>
<simpara>If a conversion between primitive and object type is desired, we require the user of N4JS to actively convert the values.
The reason for that is the notably different behavior of object- and primitive-variants of a type in expression evaluation:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">var bool: boolean = false;
var Bool: Boolean = new Boolean( false );
console.log( bool ? "true" : "false"); // prints "false"
console.log( Bool ? "true" : "false"); // prints "true"!</programlisting>
<simpara>Conversion between a primitive type to its object-variant is achieved by the <literal>new</literal> operator.
The <literal>valueOf()</literal> method converts the object-variant back to a primitive value.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">// objects from literals:
var bo: Boolean = new Boolean( true ); // typeof bo: object
var no: Number = new Number( 42 ); // typeof no: object
var so: String = new String( "foo" ); // typeof so: object
// to primitive
var b: boolean = bo.valueOf(); // typeof b: boolean -- true
var n: number = no.valueOf(); // typeof n: number -- 42
var s: string = so.valueOf(); // typeof s: string -- "foo"
// to object-type
bo = new Boolean( b );
no = new Number( n );
so = new String( s );</programlisting>
<simpara>Conversion of variables of type <literal>Object</literal> or from one primitive type to another is expressed in terms of typing rules for expressions.
That is, it is not possible to convert any <literal>Object</literal> to a primitive in general, but it is possible to do so in the context of certain expressions such as additive operator.
The applied conversions are described in <xref linkend="_auto-conversion-of-class-instances"/></simpara>
</section>
<section xml:id="_autoboxing-of-primitives">
<title>Autoboxing of Primitives</title>
<simpara>In [<link linkend="ECMA11a">ECMA11a</link>], autoboxing is defined by <literal>ToObject</literal> [<link linkend="ECMA11a">ECMA11a(p.S9.9)</link>].</simpara>
<simpara>Autoboxing is not directly supported in N4JS. Instead, primitive types virtually have the same members as their corresponding object types.
It is then possible to use the Autoboxing feature when calling a member.
In general, Autoboxing is only supported for accessing built-in read-only (immutable) properties.
For example, <literal>"some string value".split(" ");</literal> is supported but <literal>"some string value".foo=1;</literal> will be rejected as String does not allow properties to be added (cf. <literal>String</literal> vs. <literal>String+</literal>, see <xref linkend="_dynamic"/>).</simpara>
<simpara>Autoboxing often leads to problems, in particular in combination with dynamic types – this is why it is not directly supported in N4JS.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">var s: String+ = "Hello"; <co xml:id="CO3-1"/>
s.prop = 1;
console.log(s.prop); <co xml:id="CO3-2"/></programlisting>
<calloutlist>
<callout arearefs="CO3-1">
<para>will produce an error to prevent the following scenario:</para>
</callout>
<callout arearefs="CO3-2">
<para>prints "undefined"</para>
</callout>
</calloutlist>
</section>
<section xml:id="_autoboxing-of-function-expressions-and-declarations">
<title>Autoboxing of Function Expressions and Declarations</title>
<simpara>Function expressions and declarations always define an object of type <literal>Function</literal>, thus coercing or Autoboxing is not required in case of functions:</simpara>
<simpara>It is always possible to use a function expression where a <literal>Function</literal> is required, and to use an object of type <literal>Function</literal> where a function expression is expected.
This is only possible if the function signatures are subtype-compatible, see <xref linkend="_function-type"/> for details.</simpara>
<simpara>Still, it is always possible to call members of <literal>Function</literal>, e.g., <literal>function(){}.length()</literal>.</simpara>
</section>
</section>
<section xml:id="_auto-conversion-of-objects" role="language-n4js">
<title>Auto-Conversion of Objects</title>
<section xml:id="_auto-conversion-of-class-instances">
<title>Auto-Conversion of Class Instances</title>
<simpara>All classes defined in N4JS modules implicitly subclass <literal>N4Object</literal>, which is a plain JavaScript Object type.
The same auto-conversion rules defined for JavaScript <literal>Object</literal> therefore apply to <literal>N4Object</literal> instances as well.</simpara>
<simpara>The basic conversion uses the abstract JavaScript function <literal>ToPrimitive</literal> [<link linkend="ECMA11a">ECMA11a(p.S9.1)</link>], which relays on the specification method <literal>Object</literal> [<link linkend="ECMA11a">ECMA11a(p.S8.12.8)</link>].
<literal>DefaultValue</literal> calls <literal>valueOf</literal> or <literal>toString</literal> methods if they are defined by the class (in the <literal>methods</literal>-builder).</simpara>
<simpara>Note that according to the [<link linkend="ECMA11a">ECMA11a</link>], in most cases, objects are first converted into primitives.
That is, in most cases, no extra hint is passed to <literal>DefaultValue</literal>. Thus <literal>valueOf</literal> usually takes precedence over toString as demonstrated in the following example:</simpara>
<example>
<title>Auto-Conversion</title>
<simpara>Assume some classes and corresponding instances defined as follows:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class A {}
class B{
@Override public toString(): string { return "MyB"}
}
class C{
@Override public valueOf(): any { return 10}
}
class D{
@Override public toString(): string { return "MyD"}
@Override public valueOf(): any { return 20}
}
var a = new A(), b = new B(), c = new C(), d = new D();</programlisting>
<simpara>Instances of these classes will be converted as demonstrated as follows:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">console.log(a+""); // [object Object]
console.log(a+1); // [object Object]1
console.log(""+b+""); // MyB
console.log(1+b+1); // 1MyB1
console.log(c+""); // 10
console.log(c+1); // 11
console.log(d+""); // 20
console.log(d+1); // 21</programlisting>
</example>
<section xml:id="_auto-conversion-of-interface-instances">
<title>Auto-Conversion of Interface Instances</title>
<simpara>Instances of interfaces actually are instances of classes at runtime.
The auto-conversion rules described in <xref linkend="_auto-conversion-of-class-instances"/> are applied to instances declared as instances of interfaces as well.</simpara>
</section>
</section>
<section xml:id="_auto-conversion-of-enum-literals">
<title>Auto-Conversion of Enum Literals</title>
<simpara>Enumeration values are objects and thus follow the behavior for ECMAScript <literal>Object</literal> and <literal>Function</literal>.
They have a custom <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>t</mi><mi>o</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>g</mi></math> method which returns the name of the enumeration value.</simpara>
</section>
</section>
<section xml:id="_type-cast-and-type-check" role="language-n4js">
<title>Type Cast and Type Check</title>
<section xml:id="_type-cast">
<title>Type Cast</title>
<literallayout class="monospaced">(IDEBUG-56): Casting to TypeVars</literallayout>
<simpara>Type casts are expressed with the cast expression (<literal>as</literal>), see <xref linkend="_cast-as-expression"/> for details.</simpara>
<simpara>We first define helper rules for the type cast constraints as follows:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mfrac><mrow><mi>μ</mi><mfenced close=")" open="("><mi>T</mi></mfenced><mo></mo><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>TEnum</mtext></mstyle><mstyle mathvariant="monospace"><mtext>Class</mtext></mstyle><mstyle mathvariant="monospace"><mtext>Primitive</mtext></mstyle><mstyle mathvariant="monospace"><mtext>ObjectType</mtext></mstyle></mfenced></mrow><mrow><mi>i</mi><mi>s</mi><mi>C</mi><mi>P</mi><mi>O</mi><mi>E</mi><mfenced close=")" open="("><mi>T</mi></mfenced></mrow></mfrac></mtd></mtr><mtr><mtd><mfrac><mrow><mi>μ</mi><mi>T</mi><mo></mo><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>ClassifierType</mtext></mstyle><mstyle mathvariant="monospace"><mtext>TypeType</mtext></mstyle></mfenced><mo></mo><mi>μ</mi><mfenced close=")" open="("><mrow><mi>T</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi></mrow></mfenced><mo></mo><mstyle mathvariant="monospace"><mtext>TypeVariable</mtext></mstyle></mrow><mrow><mi>i</mi><mi>s</mi><mi>C</mi><mi>P</mi><mi>O</mi><mi>E</mi><mfenced close=")" open="("><mi>T</mi></mfenced></mrow></mfrac></mtd></mtr><mtr><mtd><mfrac><mrow><mi>μ</mi><mfenced close=")" open="("><mi>T</mi></mfenced><mo></mo><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>Interface</mtext></mstyle></mfenced></mrow><mrow><mi>i</mi><mi>s</mi><mi>I</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>f</mi><mi>a</mi><mi>c</mi><mi>e</mi><mfenced close=")" open="("><mi>T</mi></mfenced></mrow></mfrac></mtd></mtr><mtr><mtd><mfrac><mrow><mi>μ</mi><mfenced close=")" open="("><mi>T</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>TypeVariable</mtext></mstyle><mo></mo><mi>T</mi><mo>.</mo><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>B</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>d</mi><mi>s</mi><mo></mo><mi></mi></mrow><mrow><mi>i</mi><mi>s</mi><mi>B</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>d</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>V</mi><mi>a</mi><mi>r</mi><mfenced close=")" open="("><mi>T</mi></mfenced></mrow></mfrac></mtd></mtr><mtr><mtd><mfrac><mrow><mi>μ</mi><mfenced close=")" open="("><mi>T</mi></mfenced><mo></mo><mfenced close="}" open="{"><mrow><mi> </mi><mstyle mathvariant="monospace"><mtext>TEnum</mtext></mstyle></mrow><mstyle mathvariant="monospace"><mtext>Primitive</mtext></mstyle></mfenced><mo></mo><mfenced close="}" open="("><mrow><mi>μ</mi><mfenced close=")" open="("><mi>T</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Class</mtext></mstyle><mo></mo><mi>T</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>t</mi><mi>e</mi><mi>n</mi><mi>s</mi><mi>i</mi><mi>b</mi><mi>i</mi><mi>l</mi><mi>i</mi><mi>t</mi><mi>y</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>final</mtext></mstyle></mrow></mfenced><mo>)</mo></mrow><mrow><mi>i</mi><mi>s</mi><mi>F</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi><mi>B</mi><mi>y</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mfenced close=")" open="("><mi>T</mi></mfenced></mrow></mfrac></mtd></mtr></mtable></math>
<requirement xml:id="IDE-89">
<title>Cast Validation At Compile Time</title>
<simpara>
<anchor xml:id="Req-IDE-89" xreflabel="[Req-IDE-89]"/>
<emphasis role="strong">Requirement: IDE-89:</emphasis>
<link linkend="Req-IDE-89">Cast Validation At Compile Time</link> (ver. 1)</simpara>
<simpara>
Given a type cast expression <literal>e</literal> in which
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>Γ</mi><mo></mo><mi>e</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>:</mi><mi>S</mi></math> and and target type <literal>T</literal>, the
following constraints must hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara><literal>T</literal> must be a classifier, enum, primitive, function type expression, classifier type, type variable, union or intersection type:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>T</mi></mfenced><mo></mo><mspace width="3.0mm"/><mrow><mo>{</mo><mstyle mathvariant="monospace"><mtext>any</mtext></mstyle><mo>,</mo><mstyle mathvariant="monospace"><mtext>Class</mtext></mstyle><mo>,</mo><mstyle mathvariant="monospace"><mtext>Interface</mtext></mstyle><mo>,</mo><mstyle mathvariant="monospace"><mtext>Enum</mtext></mstyle><mo>,</mo><mstyle mathvariant="monospace"><mtext>Primitive</mtext></mstyle><mo>,</mo><mstyle mathvariant="monospace"><mtext>ObjectType</mtext></mstyle><mo>,</mo></mrow></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mspace width="3.0mm"/><mstyle mathvariant="monospace"><mtext>FunctionTypeExpression</mtext></mstyle><mo>,</mo><mstyle mathvariant="monospace"><mtext>ClassifierType</mtext></mstyle><mo>,</mo><mstyle mathvariant="monospace"><mtext>TypeVariable</mtext></mstyle><mo>,</mo><mstyle mathvariant="monospace"><mtext>Union</mtext></mstyle><mo>,</mo><mstyle mathvariant="monospace"><mtext>Intersection</mtext></mstyle><mo>}</mo></mrow></math></simpara>
</listitem>
<listitem>
<simpara>if <literal>S</literal> is a subtype of <literal>T</literal>, the cast is unnecessary and a warning will be generated.</simpara>
</listitem>
<listitem>
<simpara>if <literal>S</literal> and <literal>T</literal> are classes, enums or primitive types, then <literal>T</literal> must be a subtype of <literal>S</literal>.
This is also true if <literal>T</literal> is an interface and the type of <literal>S</literal> cannot have subtypes, or vice versa.</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>(</mo><mi>i</mi><mi>s</mi><mi>C</mi><mi>P</mi><mi>O</mi><mi>E</mi><mfenced close=")" open="("><mi>T</mi></mfenced></mrow></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="2.0em"/><mo></mo><mrow><mo>(</mo><mi>i</mi><mi>s</mi><mi>C</mi><mi>P</mi><mi>O</mi><mi>E</mi><mfenced close=")" open="("><mi>S</mi></mfenced></mrow></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mspace width="4.0em"/><mo></mo><mfenced close=")" open="("><mrow><mi>μ</mi><mfenced close=")" open="("><mi>S</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Intersection</mtext></mstyle><mo></mo><mo></mo><msup><mi>S</mi><mi>'</mi></msup><mo></mo><mi>S</mi><mi>:</mi><mi>i</mi><mi>s</mi><mi>C</mi><mi>P</mi><mi>O</mi><mi>E</mi><mfenced close=")" open="("><msup><mi>S</mi><mi>'</mi></msup></mfenced></mrow></mfenced><mo>)</mo></mrow></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mfenced close=")" open="("><mrow><mi>i</mi><mi>s</mi><mi>I</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>f</mi><mi>a</mi><mi>c</mi><mi>e</mi><mfenced close=")" open="("><mi>T</mi></mfenced><mo></mo><mi>i</mi><mi>s</mi><mi>F</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi><mi>B</mi><mi>y</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mfenced close=")" open="("><mi>S</mi></mfenced></mrow></mfenced></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo></mo><mfenced close=")" open="("><mrow><mi>i</mi><mi>s</mi><mi>F</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi><mi>B</mi><mi>y</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mfenced close=")" open="("><mi>T</mi></mfenced><mo></mo><mi>i</mi><mi>s</mi><mi>I</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>f</mi><mi>a</mi><mi>c</mi><mi>e</mi><mfenced close=")" open="("><mi>S</mi></mfenced></mrow></mfenced><mo>)</mo></mrow><mo></mo><mi>Γ</mi><mo></mo><mi>T</mi><mo>&lt;</mo><mi>:</mi><mi>S</mi></math></simpara>
</listitem>
<listitem>
<simpara>if <literal>S</literal> is a class, enum or primitive type and <literal>T</literal> is a type-variable, then for each given boundary <math xmlns="http://www.w3.org/1998/Math/MathML"><msubsup><mi>T</mi><mi>i</mi><mrow><mi>u</mi><mi>p</mi></mrow></msubsup></math> of <literal>T</literal>
of type class, enum or primitive <literal>S</literal> must be a member of the type hierarchy: <footnote><simpara><literal>i</literal> iterates over all boundaries</simpara></footnote></simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mfenced close=")" open="("><mrow><mi>i</mi><mi>s</mi><mi>B</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>d</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>V</mi><mi>a</mi><mi>r</mi><mfenced close=")" open="("><mi>T</mi></mfenced><mo></mo><mi>i</mi><mi>s</mi><mi>C</mi><mi>P</mi><mi>O</mi><mi>E</mi><mfenced close=")" open="("><mi>S</mi></mfenced></mrow></mfenced></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><msub><mo></mo><mrow><msubsup><mi>T</mi><mi>i</mi><mrow><mi>u</mi><mi>p</mi></mrow></msubsup><mo></mo><mi>T</mi><mo>.</mo><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>B</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>d</mi><mi>s</mi></mrow></msub><mrow><mo>(</mo><mi>i</mi><mi>s</mi><mi>C</mi><mi>P</mi><mi>O</mi><mi>E</mi><mfenced close=")" open="("><msubsup><mi>T</mi><mi>i</mi><mrow><mi>u</mi><mi>p</mi></mrow></msubsup></mfenced><mo></mo><mi>Γ</mi><mo></mo><mrow><mo>(</mo><mrow><mrow><msubsup><mi>T</mi><mi>i</mi><mrow><mi>u</mi><mi>p</mi></mrow></msubsup><mo>&lt;</mo><mi>:</mi><mi>S</mi><mo></mo><msubsup><mi>T</mi><mi>i</mi><mrow><mi>u</mi><mi>p</mi></mrow></msubsup><mi>:</mi><mo>&gt;</mo><mi>S</mi><mo>)</mo></mrow><mo>)</mo></mrow></mrow></mrow></math></simpara>
</listitem>
<listitem>
<simpara>if <literal>S</literal> is a union or intersection type, then the type cast is valid if it is valid for at least one element of <literal>S</literal>.</simpara>
</listitem>
<listitem>
<simpara>if <literal>S</literal> and <literal>T</literal> are generics, and if <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>S</mi><mn>0</mn></msup><mo>=</mo><msup><mi>T</mi><mn>0</mn></msup></math>,
a cast is possible if type arguments are sub- or supertypes of each other: <footnote><simpara><literal>i</literal> iterates over all type args</simpara></footnote></simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>S</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Classifier</mtext></mstyle><mo></mo><mi>μ</mi><mfenced close=")" open="("><mi>T</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Classifier</mtext></mstyle><mo></mo><msup><mi>S</mi><mn>0</mn></msup><mo>=</mo><msup><mi>T</mi><mn>0</mn></msup><mo></mo></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="2.0em"/><mrow><mo>(</mo><mrow><mo></mo><mi> </mi><mi>S</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>A</mi><mi>r</mi><msub><mi>g</mi><mi>i</mi></msub><mo>&lt;</mo><mi>:</mi><mi>T</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>A</mi><mi>r</mi><msub><mi>g</mi><mi>i</mi></msub><mo>)</mo></mrow><mo></mo><mrow><mo>(</mo><mrow><mo></mo><mi> </mi><mi>T</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>A</mi><mi>r</mi><msub><mi>g</mi><mi>i</mi></msub><mo>&lt;</mo><mi>:</mi><mi>S</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>A</mi><mi>r</mi><msub><mi>g</mi><mi>i</mi></msub><mo>)</mo></mrow></mrow></mrow></math></simpara>
</listitem>
<listitem>
<simpara>If <literal>T</literal> is a union type, then the type cast is valid if it is valid for at least one element of <literal>T</literal>.</simpara>
</listitem>
<listitem>
<simpara>If <literal>T</literal> is an intersection type, then the type cast is valid if it is valid for all elements of <literal>T</literal>.</simpara>
</listitem>
</orderedlist>
<note>
<simpara><literal>any</literal> is a supertype of all other types, thus it is always possible to cast a variable of type <literal>any</literal> to other (non-composed) types.</simpara>
</note>
</requirement>
</section>
<section xml:id="_type-check">
<title>Type Check</title>
<simpara>There are basically two ways of testing the type of a variable: <literal>typeof</literal> and <literal>instanceof</literal>.
N4JS supports type comparison via the ECMAScript <literal>instanceof</literal> operator.
The operator <literal>instanceof</literal> retains its standard ECMAScript behavior (e.g. checking whether a value is an instance of a constructor function), but has additional functionality when used with N4JS types.</simpara>
<simpara>When used with an N4JS class, <literal>instanceof</literal> also supports checking against an interface. For N4JS enumeration values, it can be used to check whether the value is part of a specific enumeration.</simpara>
<simpara><literal>typeof</literal> only returns a string with the name of the ECMAScript type, which is <literal>Object</literal> for all class instances.</simpara>
<simpara>N4JS specific <literal>string</literal> types, that is <literal>pathSelector</literal> and <literal>i18nkey</literal> cannot be tested during runtime.
These types, therefore, must not be used in <literal>instanceof</literal> expressions.
The same is true for string-based enums and arrays which cannot be tested during runtime, thus string-based enum and array types are not permitted on the right-hand side of <literal>instancesof</literal> constructs.
For all types for which the evaluation result of <literal>instanceof</literal> could be computed at compile time, the check is unnecessary and thus it is refused by the compiler.
Using structural types on the right-hand side of <literal>instancesof</literal> constructs is also not permitted.</simpara>
<simpara>In order to avoid errors at runtime, the <literal>instanceof</literal> operator defines appropriate constraints, see <xref linkend="_relational-expression"/> for details.</simpara>
<example>
<title>Type Check Example</title>
<simpara>Given the following classes and variable:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">interface I{}
class S{}
class Sub extends S implements I{}
var x = new Sub();</programlisting>
</example>
<simpara><literal>typeof x</literal> will simply return <literal>object</literal>. The following table shows the difference between plain JavaScript <literal>instanceof</literal> and N4JS’s <literal>instanceof</literal>:</simpara>
<informaltable frame="all" rowsep="1" colsep="1">
<tgroup cols="3">
<colspec colname="col_1" colwidth="33.3333*"/>
<colspec colname="col_2" colwidth="33.3333*"/>
<colspec colname="col_3" colwidth="33.3334*"/>
<thead>
<row>
<entry align="left" valign="top">Check</entry>
<entry align="center" valign="top">JavaScript</entry>
<entry align="center" valign="top">N4JS</entry>
</row>
</thead>
<tbody>
<row>
<entry align="left" valign="top"><simpara><literal>x instanceof Sub</literal></simpara></entry>
<entry align="center" valign="top"><simpara><literal>true</literal></simpara></entry>
<entry align="center" valign="top"><simpara><literal>true</literal></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>x instanceof S</literal></simpara></entry>
<entry align="center" valign="top"><simpara><literal>true</literal></simpara></entry>
<entry align="center" valign="top"><simpara><literal>true</literal></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>x instanceof I</literal></simpara></entry>
<entry align="center" valign="top"><simpara><literal>false</literal></simpara></entry>
<entry align="center" valign="top"><simpara><literal>true</literal></simpara></entry>
</row>
</tbody>
</tgroup>
</informaltable>
</section>
</section>
<section xml:id="_reflection-meta-information" role="language-n4js">
<title>Reflection meta-information</title>
<simpara>All N4JS classes, interfaces and enumerations provide meta-information
that is used by the runtime and standard library.
All classifiers (including enums) provide meta-information by means of a static getter <literal>n4type</literal>.
Since it is static getter, it is actually an instance getter of the
constructor (or classifier) of a type, which is the only way to retrieve
that information in case of interfaces. For enums, this can be retrieved
from instances as well.</simpara>
<simpara>This getter is of type <literal>N4Class</literal> which is a built-in type just like <literal>N4Object</literal>. It contains the following members:</simpara>
<variablelist>
<varlistentry>
<term><literal>fqn</literal> </term>
<listitem>
<simpara>The <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>F</mi><mi>Q</mi><mi>N</mi></math> of the type.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>n4superType</literal> </term>
<listitem>
<simpara>The <literal>N4Class</literal> of the supertype, may be null if supertype is a not an <literal>N4Class</literal>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>allImplementedInterfaces</literal> </term>
<listitem>
<simpara>List of The <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>F</mi><mi>Q</mi><mi>N</mi></math> of implemented interfaces (transitively
but without interfaces implemented by supertype)</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>get isClass</literal> </term>
<listitem>
<simpara>True if the type is an N4Class.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>get isInterface</literal> </term>
<listitem>
<simpara>True if the type is an N4Interface.</simpara>
</listitem>
</varlistentry>
</variablelist>
<section xml:id="_reflection-for-classes">
<title>Reflection for Classes</title>
<simpara>The meta-information for classes is available by means of <literal>N4Object</literal>’s static
getter <literal>n4type</literal>. Since it is static getter, it is actually an instance getter of the constructor of a type.</simpara>
<simpara>In addition, the static method <literal>of</literal> in <literal>N4Type</literal> is available to retrieve the meta-information for a given instance or
constructor. This also allows to retrieve meta-information directly for an instance of some class <literal>C</literal> without having
the constructor of <literal>C</literal> available, for example because the constructor is not accessible.</simpara>
<example>
<title>Reflection with <literal>N4class</literal></title>
<simpara>This example demonstrates how these reflective features are accessed:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class A {}
class B extends A {}
var b = new B();
console.log(B.n4type.fqn);
console.log(b.constructor.n4type.fqn);
console.log(b.constructor.n4type.n4superType.fqn);
console.log(N4Type.of(b));
console.log(N4Type.of(B.n4type).fqn);</programlisting>
<simpara>Assuming this code is defined in file <literal>A</literal>, this will output</simpara>
<programlisting language="n4js" linenumbering="unnumbered">A.B
A.B
A.A
A.B
N4BuiltInClasses.N4Class</programlisting>
</example>
<simpara>The built-in types <literal>N4Object</literal> and <literal>N4Class</literal> are also accessible.
They are not defined in a module, thus their <link linkend="_acronyms">FQN</link> returns only their simple name.</simpara>
<example>
<title>Reflection with Built-In Types</title>
<programlisting language="n4js" linenumbering="unnumbered">console.log('N4Object.n4class.fqn: ' + N4Object.n4class.fqn)
console.log('N4Class.n4class.fqn: ' + N4Class.n4class.fqn)
class A {}
console.log('A.n4class.fqn: ' + A.n4class.fqn)
console.log('A.n4class.n4superType.fqn: ' + A.n4class.n4superType.fqn)</programlisting>
<simpara>Assuming this code is defined in file <literal>A</literal>, this will output</simpara>
<programlisting language="n4js" linenumbering="unnumbered">N4Object.n4class.fqn: N4Object
N4Class.n4class.fqn: N4Class
A.n4class.fqn: A.A
A.n4class.n4superType.fqn: N4Object</programlisting>
<simpara>Note that classes extending <literal>Object</literal> do not provide the static <literal>n4class</literal> getter, hat is</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class B extends Object {}
console.log('B.n4class.fqn: ' + B.n4class.fqn)</programlisting>
<simpara>would issue an error as cannot be resolved.</simpara>
</example>
<example>
<title>N4Class.of</title>
<simpara>The type has a method to retrieve the meta-information from instances (i.e. or enumeration literals using )
without using the constructor.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class C { }
interface I {} class IImpl implements I {}
enum E { L }
var c: C = new C();
var i: I = new IImpl();
var e: E = E.L;
console.log(C.n4type.fqn);
console.log(N4Class.of(c).fqn);
console.log(I.n4type.fqn);
console.log(N4Class.of(i).fqn);
console.log(E.n4type.fqn);
console.log(N4EnumType.of(e).fqn);</programlisting>
</example>
</section>
<section xml:id="_reflection-for-interfaces">
<title>Reflection for Interfaces</title>
<simpara>The meta-information of an interface <literal>X</literal> is available via getter <literal>n4class</literal> defined in the <literal>type{X}</literal>.
This field is of type <literal>N4Class</literal> as well.
Since an interface cannot have a super classs, the property <literal>n4superTypes</literal> will always be empty.
Calling <literal>isInterface</literal> respectively on the returned <literal>N4Class</literal> instance will return true.</simpara>
</section>
<section xml:id="_reflection-for-enumerations">
<title>Reflection for Enumerations</title>
<programlisting language="n4js" linenumbering="unnumbered">var n: number; var b: boolean; var s: string;</programlisting>
<simpara>The meta-information for enumerations is available by means of the getter <literal>n4class</literal>, either statically by using the enumeration type or (in terms of an instance getter) via a literal.
Calling <literal>isEnum</literal> on the returned <literal>N4Class</literal> instance will return true.</simpara>
</section>
</section>
<section xml:id="_conversion-of-primitive-types" role="language-n4js">
<title>Conversion of primitive types</title>
<simpara>Conversion between primitives is given as follows:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">var n: number; var b: boolean; var s: string;</programlisting>
<informaltable frame="all" rowsep="1" colsep="1">
<tgroup cols="4">
<colspec colname="col_1" colwidth="11.1111*"/>
<colspec colname="col_2" colwidth="11.1111*"/>
<colspec colname="col_3" colwidth="44.4444*"/>
<colspec colname="col_4" colwidth="33.3334*"/>
<thead>
<row>
<entry align="center" valign="middle">From</entry>
<entry align="center" valign="middle">To</entry>
<entry align="left" valign="middle">Conversion</entry>
<entry align="center" valign="top">Example</entry>
</row>
</thead>
<tbody>
<row>
<entry align="center" valign="middle"><simpara><literal>string</literal></simpara></entry>
<entry align="center" valign="middle"><simpara><literal>number</literal></simpara></entry>
<entry align="left" valign="middle"><simpara><literal>Number&#8230;&#8203;</literal></simpara></entry>
<entry align="left" valign="top"><programlisting language="n4js" linenumbering="unnumbered">n = Number("42");//42</programlisting></entry>
</row>
<row>
<entry align="center" valign="middle"><simpara><literal>string</literal></simpara></entry>
<entry align="center" valign="middle"><simpara><literal>boolean</literal></simpara></entry>
<entry align="left" valign="middle"><simpara><literal>N4Primitives.parseBoolean(&#8230;&#8203;)</literal></simpara></entry>
<entry align="left" valign="top"><programlisting language="n4js" linenumbering="unnumbered"> b=N4Primitives.parseBoolean("false");</programlisting></entry>
</row>
<row>
<entry align="center" valign="middle"><simpara><literal>number</literal></simpara></entry>
<entry align="center" valign="middle"><simpara><literal>boolean</literal></simpara></entry>
<entry align="left" valign="middle"><simpara><literal>Boolean(&#8230;&#8203;)</literal></simpara></entry>
<entry align="left" valign="top"><programlisting language="n4js" linenumbering="unnumbered">b=Boolean(17.5); //true</programlisting></entry>
</row>
<row>
<entry align="center" valign="middle"><simpara><literal>number</literal></simpara></entry>
<entry align="center" valign="middle"><simpara><literal>string</literal></simpara></entry>
<entry align="left" valign="middle"><simpara><literal>Number.toString()</literal></simpara></entry>
<entry align="left" valign="top"><programlisting language="n4js" linenumbering="unnumbered">s=42.toString(); //"42"</programlisting></entry>
</row>
<row>
<entry align="center" valign="middle"><simpara><literal>boolean</literal></simpara></entry>
<entry align="center" valign="middle"><simpara><literal>number</literal></simpara></entry>
<entry align="left" valign="middle"><simpara><literal>N4Primitives.toNumber(&#8230;&#8203;)</literal></simpara></entry>
<entry align="left" valign="top"><programlisting language="n4js" linenumbering="unnumbered">n=N4Primitives.toNumber(true);</programlisting></entry>
</row>
<row>
<entry align="center" valign="middle"><simpara><literal>boolean</literal></simpara></entry>
<entry align="center" valign="middle"><simpara><literal>string</literal></simpara></entry>
<entry align="left" valign="middle"><simpara><literal>Boolean.toString()</literal></simpara></entry>
<entry align="left" valign="top"><programlisting language="n4js" linenumbering="unnumbered"> s=true.toString();//"true" }</programlisting></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<simpara>Remarks:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>ECMAScript doesn’t define explicit conversion from string content.
Implicit handling states all strings with <math xmlns="http://www.w3.org/1998/Math/MathML"><mo>&gt;</mo><mn>0</mn><mo>=</mo><mo>=</mo><mi>t</mi><mi>r</mi><mi>u</mi><mi>e</mi></math>. <literal>N4Primitives.parseBoolean(x)</literal> yields true for <literal>x.trim().toLowerCase().equals("true")</literal></simpara>
</listitem>
<listitem>
<simpara>The call to <literal>Boolean(..)</literal> for the arguments <literal>0</literal>, <literal>-0</literal>, <literal>null</literal>, <literal>false</literal>, <literal>NaN</literal>, <literal>undefined</literal> and <literal>""</literal> evaluate to <literal>false</literal>.
All other values evaluate to <literal>true</literal>.</simpara>
</listitem>
<listitem>
<simpara><literal>Number</literal> has several methods for converting a value to string [<link linkend="ECMA11a">ECMA11a(p.S15.7.4)</link>]: <literal>toExponential()</literal>, to <literal>Fixed()</literal>, <literal>toPrecision()</literal>.</simpara>
</listitem>
<listitem>
<simpara>ECMAScript doesn’t define explicit conversion from boolean to number.
Implicit handling states true <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo></math> 1 and false <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo></math> 0, which <literal>N4Primitives.toNumber()</literal> yields.</simpara>
</listitem>
</orderedlist>
</section>
</chapter>
<chapter xml:id="_expressions">
<title>Expressions</title>
<simpara>For all expressions, we define the following pseudo properties:</simpara>
<variablelist>
<varlistentry>
<term><literal>containingExpression</literal> </term>
<listitem>
<simpara>The parent expression, in which an expression is contained, may be null.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>containingStatement</literal> </term>
<listitem>
<simpara>The statement in which the expression is (indirectly) contained.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>containingFunctionOrAccessor</literal> </term>
<listitem>
<simpara>The function, method, getter or setter in which the expression is (indirectly) contained, may be null.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>containingClass</literal> </term>
<listitem>
<simpara>The class in which the expression is (indirectly) contained, may be null.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>probableThisTarget</literal> </term>
<listitem>
<simpara>The potential target of a this keyword binding, this is not necessarily the containing class or object literal.
In case of instance methods of a class <literal>T</literal>, this usually is the classifier <literal>T</literal>; in case of static methods, it is the classifier type <literal>type{type}</literal>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>container</literal> </term>
<listitem>
<simpara>The direct owner of the expression.
z</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>The expressions and statements are ordered, describing first the constructs available in the 5th edition of ECMA-262, referred to as [<link linkend="ECMA11a">ECMA11a</link>] in the following.
It is worth noting that the grammar snippets already use newer constructs in some cases.</simpara>
<section xml:id="_ecmascript-5-expressions" role="language-n4js">
<title>ECMAScript 5 Expressions</title>
<simpara>N4JS supports the same expressions as ECMAScript.
The semantics are described in [<link linkend="ECMA11a">ECMA11a(p.S11)</link>].
In N4JS, some expressions are extended for supporting the declaration of types, annotations, or parameterized usages.
These extensions and type-related aspects as well as specific N4JS constraints are described in this section.</simpara>
<simpara>Some operators come in different ’flavors’, that is as binary operator, unary pre- or postfix operators, or assignment operators.
For these operators, type constraints are only defined for the binary operator version and the other variants are deduced to that binary version.
E.g., <literal>++</literal> and <literal>+=</literal> are deduced to <literal>+</literal> (and simple assignment).</simpara>
<section xml:id="_the-this-literal">
<title>The this Literal</title>
<simpara>This section describes the <literal>this</literal> literal and the semantics of the <literal>@This</literal> annotation, the type <literal>this</literal> is described in <xref linkend="_this-type"/>.</simpara>
<bridgehead xml:id="_semantics-9" renderas="sect4">Semantics</bridgehead>
<simpara>Semantics are similar to the original ECMAScript this keyword, see [<link linkend="ECMA11a">ECMA11a(p.11.1.1, p.p.63)</link>])
Also see [<link linkend="West06a">West06a</link>] and <link xl:href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this">MozillaJSRef</link></simpara>
<simpara>Regarding the location where <literal>this</literal> may be used, the following restrictions apply:</simpara>
<requirement xml:id="IDE-173">
<title>Valid location for this literal</title>
<simpara>
<anchor xml:id="Req-IDE-173" xreflabel="[Req-IDE-173]"/>
<emphasis role="strong">Requirement: IDE-173:</emphasis>
<link linkend="Req-IDE-173">Valid location for this literal</link> (ver. 1)</simpara>
<simpara>
The literal may not be used in</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>the initializer expression of static data fields in classes.</simpara>
</listitem>
<listitem>
<simpara>the initializer expression of data fields in interfaces (applies to both static and non-static).</simpara>
</listitem>
<listitem>
<simpara>static methods of interfaces and static field accessors of interfaces.</simpara>
</listitem>
</orderedlist>
<simpara>See also <xref linkend="Req-IDE-69"/>.</simpara>
</requirement>
<simpara>Note: <xref linkend="Req-IDE-173"/> also applies for this literals inside arrow expressions in initializers.</simpara>
<simpara>The use of <literal>this</literal> is illustrated with some examples as it can often be confusing.
Type inference heuristics and explanations are provided in the next section.</simpara>
<example>
<title>This in Unrestricted Mode</title>
<simpara>In unrestricted mode, <literal>this</literal> is bound to the receiver.
If there is no receiver it is bound to the global object, however, we often do not know exactly what the global object would be.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">var name = "global a"; // assume the top level is similar to the global object
this.name; // &lt;-- "global a"
function f() {
return this.name; // &lt;-- depends on call, usually "global a"
}
var ol1 = {
name: "John",
greeting: "Hello " + this.name, // "Hello global a" -- we do not greet John!
}
var ol2 = {
name: "John",
f: function() {
this.name; // usually "John", as we assume f is called like ol2.f()
var g = function() {
return this.name; // "global a"
}
return g(); // no receiver, this in nested function g will be global scope
}
}</programlisting>
</example>
<example>
<title>This in strict mode</title>
<simpara>In strict mode, <literal>this</literal> is bound to the receiver.
If there is no receiver, it is bound to <literal>undefined</literal>.
Thus, we will probably get a lot of errors:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">"use strict"
var name = "global a"; // assume the top level is similar to the global object
this.name; // &lt;-- error, this is undefined, there is no receiver
function f() {
return this.name; // &lt;-- depends on call, usually this produces an error as this is undefined
}
var ol1 = {
name: "John",
greeting: "Hello " + this.name, // will produce an error, as this is undefined
}
var ol2 = {
name: "John",
f: function() {
this.name; // usually "John", as we assume f is called like ol2.f()
var g = function() {
this.name; // an error, see call below:
}
return g(); // no receiver, this in nested function g is undefined
}
}</programlisting>
</example>
<example>
<title>This in N4JS mode</title>
<simpara>As in strict mode, <literal>this</literal> is bound to the receiver and if there is no receiver, it is bound to <literal>undefined</literal>. So the example above is also true for N4JS mode.
Classes behave slightly differently:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class A {
name = "John";
greeting = "Hello " + this.name; // works, in N4JS classes, greeting is "Hello John"
f() {
return this.name; // this usually is instance object, similar to object literals.
}
g() {
var h = function() {
return this.name; // as in object literals: no receiver, no this.
}
return h();
}
}</programlisting>
</example>
<note>
<simpara>In N4JS classes, <literal>this</literal> is always bound to the instance when used in field initialization.</simpara>
</note>
<bridgehead xml:id="_type-inference-2" renderas="sect4">Type Inference</bridgehead>
<simpara>The type is inferred from the <literal>this</literal> type is bound to. The inference,
therefore, has to consider the original semantics as described in [<link linkend="ECMA11a">ECMA11a(p.10.4., p.10.4.3, p.p.58)</link>].
In ECMAScript the type of this is unfortunately determined by the function call and not by the function definition:</simpara>
<itemizedlist>
<listitem>
<simpara>By default, <literal>this</literal> is bound to the global object [<link linkend="ECMA11a">ECMA11a(p.10.4.1.1)</link>].
Unfortunately it is often unknown what the global object will be at run time (e.g., node.js differs from browsers).</simpara>
</listitem>
<listitem>
<simpara>If a function is called without a receiver, <literal>this</literal> is bound to</simpara>
<itemizedlist>
<listitem>
<simpara>the global object or</simpara>
</listitem>
<listitem>
<simpara>to <literal>undefined</literal> in strict mode.</simpara>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<simpara>If a function is called with a receiver,<literal>this</literal> is bound to the receiver object.</simpara>
</listitem>
</itemizedlist>
<simpara>Actually, <literal>this</literal> is bound to the newly created object if a function is called with the <literal>new</literal> operator.
If a function is known to be invoked with an explicit <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi><mi>A</mi><mi>r</mi><mi>g</mi></math> (<literal>apply()</literal> etc.), the <literal>@This</literal> annotation can be used to explicitly set the this type.
This annotation has precedence over otherwise inferred bindings.</simpara>
<requirement xml:id="IDE-90">
<title>Type Inference Heuristic for This-Keyword</title>
<simpara>
<anchor xml:id="Req-IDE-90" xreflabel="[Req-IDE-90]"/>
<emphasis role="strong">Requirement: IDE-90:</emphasis>
<link linkend="Req-IDE-90">Type Inference Heuristic for This-Keyword</link> (ver. 1)</simpara>
<simpara>
In general, the actual this target can not be inferred from the context of the this keyword.
A heuristic is defined, however, to compute the probable this type:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara><anchor xml:id="this-keyword-constraint-1" xreflabel="[this-keyword-constraint-1]"/> If the this keyword is used in some function annotated with an annotation <literal>@This</literal>, the type specified in the annotation is used.
The inferred type is always nominal.</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>"this"</mtext></mstyle><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>i</mi><mi>n</mi><mi>g</mi><mi>F</mi><mi>u</mi><mi>n</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mi>O</mi><mi>r</mi><mi>A</mi><mi>c</mi><mi>c</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>o</mi><mi>r</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mi>f</mi><mo>.</mo><mi>h</mi><mi>a</mi><mi>s</mi><mi>A</mi><mi>n</mi><mi>n</mi><mi>o</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close=")" open="("><mstyle mathvariant="monospace"><mtext>"@This"</mtext></mstyle></mfenced><mspace width="3.0mm"/><mi>T</mi><mo>=</mo><mi>f</mi><mo>.</mo><mi>a</mi><mi>n</mi><mi>n</mi><mi>o</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mstyle mathvariant="monospace"><mtext>["@This"]</mtext></mstyle></mrow><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>"this"</mtext></mstyle><mi>:</mi><mo></mo><mi>T</mi></mrow></mfrac></math></simpara>
</listitem>
<listitem>
<simpara><anchor xml:id="this-keyword-constraint-2" xreflabel="[this-keyword-constraint-2]"/> If the this keyword is used in some <emphasis>instance</emphasis> method of a classifier or in an <emphasis>instance</emphasis> field initializer,<literal>this</literal> is bound to the <literal>T</literal> itself.
If the this keyword is used in some <emphasis>static</emphasis> method of a classifier <literal>T</literal> or in a <emphasis>static</emphasis> field initializer, the prototype type (or constructor) of the classifier is used, that is <literal>type[T]</literal>.
In both cases, the target is determined by using the expressions’s pseudo property <literal>probableThisTarget</literal>.
If the this keyword is used in a function expression assigned to an property of an object literal, the type of the object literal is used.
Note that usually this is the <literal>this</literal> type in instance methods, and the <literal>this</literal> type in static methods.</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>T</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>"this"</mtext></mstyle><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>b</mi><mi>a</mi><mi>b</mi><mi>l</mi><mi>e</mi><mi>T</mi><mi>h</mi><mi>i</mi><mi>s</mi><mi>T</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi><mspace width="3.0mm"/><mi>T</mi><mo></mo><mstyle mathvariant="monospace"><mtext>null</mtext></mstyle></mrow><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>"this"</mtext></mstyle><mi>:</mi><mo></mo><mi>T</mi></mrow></mfrac><mtext>
</mtext></math>
</listitem>
<listitem>
<simpara>In all other cases: Non-strict mode:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>m</mi><mi>o</mi><mi>d</mi><mi>e</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>unrestricted</mtext></mstyle></mrow><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>"this"</mtext></mstyle><mi>:</mi><mstyle mathvariant="monospace"><mtext>global</mtext></mstyle></mrow></mfrac><mtext>
</mtext></math>
</listitem>
</orderedlist>
<simpara>Strict mode and N4JS mode:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>m</mi><mi>o</mi><mi>d</mi><mi>e</mi><mo></mo><mstyle mathvariant="monospace"><mtext>unrestricted</mtext></mstyle></mrow><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>"this"</mtext></mstyle><mi>:</mi><mstyle mathvariant="monospace"><mtext>global</mtext></mstyle><mstyle mathvariant="monospace"><mtext>undefined</mtext></mstyle></mrow></mfrac><mtext>
</mtext></math>
</requirement>
<simpara>If the actual this type is defined as a structural type, the structural type information is moved to the this type itself.
This is transparent to the user in general but maybe visible in case of error messages.
That is to say that the actual this type is always a nominal type.
This is indicated by the nominal modifier <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo></math> (cf. <xref linkend="Req-IDE-90"/> constraints <link linkend="this-keyword-constraint-1">1</link> and <link linkend="this-keyword-constraint-2">2</link>.).</simpara>
<requirement xml:id="IDE-91">
<title>Valid Target and Argument for @This Annotation</title>
<simpara>
<anchor xml:id="Req-IDE-91" xreflabel="[Req-IDE-91]"/>
<emphasis role="strong">Requirement: IDE-91:</emphasis>
<link linkend="Req-IDE-91">Valid Target and Argument for @This Annotation</link> (ver. 1)</simpara>
<simpara>
</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>The <literal>@This</literal> annotation is only allowed on declared functions, function expressions (including arrow functions), methods, and field accessors, i.e. getters and setters, except static members of interfaces.</simpara>
</listitem>
<listitem>
<simpara>The type declared by way of <literal>@This(..)</literal> an annotation of a method or field accessor must be a subtype of the member’s containing classifier.</simpara>
</listitem>
</orderedlist>
</requirement>
<requirement xml:id="IDE-92">
<title>Single @This Annotation</title>
<simpara>
<anchor xml:id="Req-IDE-92" xreflabel="[Req-IDE-92]"/>
<emphasis role="strong">Requirement: IDE-92:</emphasis>
<link linkend="Req-IDE-92">Single @This Annotation</link> (ver. 1)</simpara>
<simpara>
</simpara>
<simpara>It is not allowed to use more then one <literal>@This(..)</literal> annotation on an element.</simpara>
</requirement>
<example>
<title>Effect of Nominal This Type</title>
<simpara>Given the following declaration</simpara>
<programlisting language="n4js" linenumbering="unnumbered">@This(~Object with {a: string;}) f() {}</programlisting>
<simpara>Since the this type is always nominal, <literal>~ Object</literal> becomes <literal>Object</literal>.
In case of method call, however, the returned value becomes structural again.
In case of error messages the type of the return type is then</simpara>
<programlisting language="n4js" linenumbering="unnumbered">~this[Object] with {a: string;}</programlisting>
<simpara>For the sake of simplicity, additional structural members are usually
omitted in error messages, leading to</simpara>
<programlisting language="n4js" linenumbering="unnumbered">~this[Object]</programlisting>
<simpara>instead of</simpara>
<programlisting language="n4js" linenumbering="unnumbered">this[~Object]</programlisting>
</example>
<example>
<title>This and Function Declaration</title>
<simpara>This example demonstrates the usage of functions annotated with <literal>@This</literal>.
By using the argument <literal>union{A,B}</literal> it is possible to have two completely unrelated classes as the receiver type of the function <literal>logger</literal>.
To pass an actual object the <literal>apply()</literal> method of the function is used.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class A {
log: string() { return "A was logged"; }
}
class B {
log: string() { return "B was logged"; }
}
@This(union{A,B})
function logger() { console.log("~ "+this.log()+" ~"); }
var a: A = new A();
logger.apply(a,[]); // prints "~ A was logged ~"
logger.apply( new B(),[]) // prints "~ B was logged ~"</programlisting>
</example>
<example>
<title>This and Function Expressions</title>
<simpara>In this example a function is created via a function expression.
The function is then assigned to member field of class B.
Via annotating the expression with <literal>@This(B)</literal>, access to the receiver of type B is enabled.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class B {
log(): string { return "B was logged"; } // method
logMe : {@This(B) function():void}; // reference to a function
}
var b: B = new B();
b.logMe = @This(B) function() { console.log("*&gt;"+this.log()+"&lt;*"); }
b.logMe(); // prints "*&gt;B was logged&lt;*"</programlisting>
<simpara>Note that if a function is called as a constructor function with new, the
type of <literal>this</literal> can be declared via annotation <literal>@This(..)</literal>, as shown in the following
snippet:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">@This(
~Object with {
w: number; h: number;
area: {function():number};
})
function Box(w: number w, h: number) {
this.w = w;
this.h = h;
this.area = @This(
~Object with {
w: number; h: number;
area: {function():number};
}) function() { return this.w * this.h }
}
var bError = Box(1,2)
var bOK = new Box(1,2)</programlisting>
</example>
<simpara>Inside the constructor function <literal>Box</literal>, <literal>this</literal> is bound to the structural type definition due to the annotation.</simpara>
<simpara>Inside the nested function <literal>area</literal>, <literal>this</literal> is bound to the receiver object (if the function is called like <literal>bOk.area()</literal>).
Again, this depends on the way the nested function is called, which can usually not be determined at the declaration location.
The nested function must then be annotated accordingly.</simpara>
<simpara>When calling this function, the type of this is checked against the declared this type, which would cause an error in the first case.</simpara>
<simpara>The use of the <literal>@This</literal> annotation is not allowed on methods.</simpara>
<tip>
<simpara>Using constructor functions is not recommended and an error or warning will be created.
This is only useful for adapting third-party library code.
Even in the latter case, it would probably make more sense to declare a (library) <emphasis role="strong">class</emphasis> Rectangle rather then defining the constructor function.</simpara>
</tip>
</section>
<section xml:id="_identifier">
<title>Identifier</title>
<bridgehead xml:id="_syntax-10" renderas="sect4">Syntax</bridgehead>
<simpara>Identifiers as expressions are identifier references.
They are defined as follows:</simpara>
<programlisting language="xtext" linenumbering="unnumbered">IdentifierRef &lt;Yield&gt;:
id=[types::IdentifiableElement|BindingIdentifier&lt;Yield&gt;]
;
BindingIdentifier &lt;Yield&gt;:
IDENTIFIER
| &lt;!Yield&gt; 'yield'
| N4Keyword
;</programlisting>
<bridgehead xml:id="_semantics-10" renderas="sect4">Semantics</bridgehead>
<simpara>The type of an identifier <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi></math> is resolved depending on its binding and scope respectively (cf. [<link linkend="ECMA11a">ECMA11a(p.10.2.2.1GetIdentifierReference, p.p.56)</link>].
The following scopes (aka <emphasis>Lexical Environments</emphasis>) are defined:</simpara>
<itemizedlist>
<listitem>
<simpara>function local; local variables, parameters</simpara>
</listitem>
<listitem>
<simpara>zero or more function closure in case of nested functions</simpara>
</listitem>
<listitem>
<simpara>module</simpara>
</listitem>
<listitem>
<simpara>global</simpara>
</listitem>
</itemizedlist>
<simpara>These scope are nested as illustrated in <xref linkend="fig:scopes"/>.</simpara>
<simpara>Note that classes definitions and object literal do not define a scope: members of a class or properties of an object literal are to be accessed via <literal>this</literal>.
Identifier references always reference declared elements, that is to say either variable, function, or class declarations.
Properties of object literals or members of a class are referenced via <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi><mi>r</mi><mi>o</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>y</mi><mi>A</mi><mi>c</mi><mi>c</mi><mi>e</mi><mi>s</mi><mi>s</mi><mo>-</mo><mi>E</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>y</mi></math> (see <xref linkend="_property-accessors"/>).</simpara>
<figure xml:id="fig:scopes">
<title>Scopes</title>
<mediaobject>
<imageobject>
<imagedata fileref="chapters/08_expressions/fig/scopes.svg" width="40%" scalefit="1" align="center"/>
</imageobject>
<textobject><phrase>scopes</phrase></textobject>
</mediaobject>
</figure>
<simpara>An identifier may be bound to a variable (global or local variable, parameter, variable defined in a function’s closure), or to a property of an object.
The latter case is known as property access as further described in <xref linkend="_property-accessors"/>.</simpara>
<requirement xml:id="IDE-93">
<title>Read Access to Identifier</title>
<simpara>
<anchor xml:id="Req-IDE-93" xreflabel="[Req-IDE-93]"/>
<emphasis role="strong">Requirement: IDE-93:</emphasis>
<link linkend="Req-IDE-93">Read Access to Identifier</link> (ver. 1)</simpara>
<simpara>
If an identifier <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi></math> is accessed, the bound declared element <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>D</mi></math> must be readable if it is not used on the left-hand side of an assignment expression.</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mi>b</mi><mi>i</mi><mi>n</mi><mi>d</mi><mfenced close=")" open="("><mi>i</mi><mi>D</mi></mfenced></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="2.0em"/><mo></mo><mo></mo><mi> </mi><mstyle mathvariant="monospace"><mtext>AssignmentExpression</mtext></mstyle><mi> </mi><mi>a</mi><mi>e</mi><mo></mo><mi>i</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>e</mi><msup><mi>r</mi><mo>*</mo></msup><mi>:</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="3.0em"/><mi>a</mi><mi>e</mi><mo>.</mo><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mo>=</mo><mi>i</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="4.0em"/><mo></mo><mfenced close=")" open="("><mrow><mi>μ</mi><mfenced close=")" open="("><mrow><mi>a</mi><mi>e</mi><mo>.</mo><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi></mrow></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>PropertyAccessExpression</mtext></mstyle><mo></mo><mi>a</mi><mi>e</mi><mo>.</mo><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>y</mi><mo>=</mo><mi>i</mi></mrow></mfenced><mi>:</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo></mo><mi>D</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>a</mi><mi>d</mi><mi>a</mi><mi>b</mi><mi>l</mi><mi>e</mi></math></simpara>
</requirement>
<bridgehead xml:id="_type-inference-3" renderas="sect4">Type Inference</bridgehead>
<simpara>An identifier reference <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi></math> is bound to an identifiable element <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi><mo>.</mo><mi>i</mi><mi>d</mi></math>, which is expressed with the function <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>b</mi><mi>i</mi><mi>n</mi><mi>d</mi><mfenced close=")" open="("><mi>i</mi><mrow><mi>i</mi><mo>.</mo><mi>i</mi><mi>d</mi></mrow></mfenced></math>.
The type of the reference is then inferred as follows:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>Γ</mi><mo></mo><mi>i</mi><mi>d</mi><mi>r</mi><mi>e</mi><mi>f</mi><mo>.</mo><mi>i</mi><mi>d</mi><mi>:</mi><mi>T</mi></mrow><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>IdentifierRef</mtext></mstyle><mi> </mi><mi>i</mi><mi>d</mi><mi>r</mi><mi>e</mi><mi>f</mi><mi>:</mi><mi>T</mi></mrow></mfrac></math>
</section>
<section xml:id="_literals">
<title>Literals</title>
<simpara>cf. [<link linkend="ECMA11a">ECMA11a(p.S11.1.3p.63, p.S7.8p.19ff)</link>].</simpara>
<bridgehead xml:id="_type-inference-4" renderas="sect4">Type Inference</bridgehead>
<simpara>The type of a literal can directly be derived from the grammar.
The following axioms are defined for literals:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="13.0em"/><mfrac><mrow/><mrow><mstyle mathvariant="monospace"><mtext>NullLiteral</mtext></mstyle><mi>:</mi><mstyle mathvariant="monospace"><mtext>null</mtext></mstyle></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="13.0em"/><mfrac><mrow/><mrow><mstyle mathvariant="monospace"><mtext>BooleanLiteral</mtext></mstyle><mi>:</mi><mstyle mathvariant="monospace"><mtext>boolean</mtext></mstyle></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="13.0em"/><mfrac><mrow/><mrow><mstyle mathvariant="monospace"><mtext>NumericLiteral</mtext></mstyle><mi>:</mi><mstyle mathvariant="monospace"><mtext>int</mtext></mstyle><mi>o</mi><mi>r</mi><mstyle mathvariant="monospace"><mtext>number</mtext></mstyle></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="13.0em"/><mfrac><mrow/><mrow><mstyle mathvariant="monospace"><mtext>StringLiteral</mtext></mstyle><mi>:</mi><mstyle mathvariant="monospace"><mtext>string</mtext></mstyle></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="13.0em"/><mfrac><mrow/><mrow><mstyle mathvariant="monospace"><mtext>RegularExpressionLiteral</mtext></mstyle><mi>:</mi><mstyle mathvariant="monospace"><mtext>RegExpr</mtext></mstyle></mrow></mfrac></math></simpara>
<simpara>Note that there are no literals specific for <literal>pathSelector</literal> or <literal>i18nkey</literal>.</simpara>
<section xml:id="_integer-literals">
<title>Integer Literals</title>
<simpara>Numeric literals representing integers in the range of JavaScript’s int32 are inferred to the built-in primitive type <literal>int</literal> instead of <literal>number</literal>.
The following rules apply:</simpara>
<requirement xml:id="IDE-94">
<title>Numeric literals</title>
<simpara>
<anchor xml:id="Req-IDE-94" xreflabel="[Req-IDE-94]"/>
<emphasis role="strong">Requirement: IDE-94:</emphasis>
<link linkend="Req-IDE-94">Numeric literals</link> (ver. 1)</simpara>
<simpara>
</simpara>
<itemizedlist>
<listitem>
<simpara>Numeric literals with a fraction or using scientific notation, e.g. <literal>2.0</literal> and <literal>2e0</literal>, respectively, are always inferred to <literal>number</literal>, even if they represent integers in the range of int32.</simpara>
</listitem>
<listitem>
<simpara>Numeric literals that represent integers in the range of JavaScript’s int32, i.e. from <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mn>-2</mn><mn>31</mn></msup></math> to <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mn>2</mn><mn>31</mn></msup><mo>-</mo><mn>1</mn></math>, are inferred to <literal>int</literal>.</simpara>
</listitem>
<listitem>
<simpara>Hexadecimal and octal literals are always interpreted as positive numbers, so all values above <literal>0x7fffffff</literal> and <literal>017777777777</literal> lie outside the range of int32 and will thus
be inferred to <literal>number</literal>; this is an important difference to Java. See below for further elaboration.</simpara>
</listitem>
</itemizedlist>
<simpara>There are differences to numeric literals in Java:</simpara>
<informaltable frame="all" rowsep="1" colsep="1">
<tgroup cols="5">
<colspec colname="col_1" colwidth="25*"/>
<colspec colname="col_2" colwidth="25*"/>
<colspec colname="col_3" colwidth="12.5*"/>
<colspec colname="col_4" colwidth="25*"/>
<colspec colname="col_5" colwidth="12.5*"/>
<thead>
<row>
<entry align="left" valign="top"></entry>
<entry align="center" valign="top" namest="col_2" nameend="col_3">Java</entry>
<entry align="center" valign="top" namest="col_3" nameend="col_4">JavaScript N4JS</entry>
</row>
</thead>
<tbody>
<row>
<entry align="left" valign="top"><simpara><emphasis role="strong">Literal</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Value</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Type</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Value</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Type</emphasis></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>2147483648</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis>-2147483648</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><literal>int</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis>-2147483648</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><literal>int</literal></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>2147483647</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis>2147483647</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><literal>int</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis>2147483647</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><literal>int</literal></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>0x7fffffff</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis>2147483647</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><literal>int</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis>2147483647</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><literal>int</literal></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>0x80000000</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis>-2147483648</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><literal>int</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis>+2147483648</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><literal>number</literal></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>0xffffffff</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis>-1</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><literal>int</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis>4294967295</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><literal>number</literal></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>0x100000000</literal></simpara></entry>
<entry align="center" valign="top" namest="col_2" nameend="col_3"><simpara><emphasis role="strong">n/a</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis>4294967296</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><literal>number</literal></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>017777777777</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis>2147483647</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><literal>int</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis>2147483647</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><literal>int</literal></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>020000000000</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis>-2147483648</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><literal>int</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis>+2147483648</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><literal>number</literal></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>037777777777</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis>-1</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><literal>int</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis>4294967295</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><literal>number</literal></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>040000000000</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis>0</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><literal>int</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis>4294967296</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><literal>number</literal></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>0100000000000</literal></simpara></entry>
<entry align="center" valign="top" namest="col_2" nameend="col_3"><simpara><emphasis role="strong">n/a</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis>8589934592</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><literal>number</literal></simpara></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<simpara>The literals <literal>0x100000000</literal> and <literal>0100000000000</literal> produce a syntax error in Java.</simpara>
<simpara>Until IDE-1881 is complete, all built-in operations always return a <literal>number</literal> even if all operands are of type <literal>int</literal>.
For the time being, we therefore interpret <literal>-1</literal> as a negative integer literal (inferred to <literal>int</literal>), but <literal>-(1)</literal> as the negation of a positive integer literal (inferred to <literal>number</literal>).</simpara>
</requirement>
</section>
</section>
<section xml:id="_array-literal">
<title>Array Literal</title>
<bridgehead xml:id="_syntax-11" renderas="sect4">Syntax</bridgehead>
<simpara>cf [<link linkend="ECMA11a">ECMA11a(p.S11.1.4, p.p.63)</link>]</simpara>
<programlisting language="xtext" linenumbering="unnumbered">ArrayLiteral &lt;Yield&gt; returns ArrayLiteral:
{ArrayLiteral} '['
elements+=ArrayPadding* (
elements+=ArrayElement&lt;Yield&gt;
(',' elements+=ArrayPadding* elements+=ArrayElement&lt;Yield&gt;)*
(trailingComma?=',' elements+=ArrayPadding*)?
)?
']'
;
/**
* This array element is used to pad the remaining elements, e.g. to get the
* length and index right
*/
ArrayPadding returns ArrayElement: {ArrayPadding} ',';
ArrayElement &lt;Yield&gt; returns ArrayElement: {ArrayElement} spread?='...'? expression=AssignmentExpression&lt;In=true,Yield&gt;;</programlisting>
<bridgehead xml:id="_type-inference-5" renderas="sect4">Type Inference</bridgehead>
<simpara>In general, an array literal is inferred as <literal>Array&lt;T&gt;</literal> (similar to the type of <literal>new Array()</literal>).
The interesting question is the binding of the type variable <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math>.</simpara>
<simpara>The type of an array padding <emphasis>p</emphasis> is inferred as follows:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mi>p</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>undefined</mtext></mstyle></mrow></mfrac><mtext>
</mtext></math>
<simpara>The element type of an array literal is simply inferred as the (simplified) union of the type elements of the array.
Thus, the type of an array literal <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi></math> is inferred as follows:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>Γ</mi><mo></mo><mi>a</mi><mo>.</mo><mover accent="true"><mrow><mi>e</mi><mi>l</mi><mi>e</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi>s</mi></mrow><mo>¯</mo></mover><mi>:</mi><mover accent="true"><msub><mi>T</mi><mi>e</mi></msub><mo>¯</mo></mover><mi>T</mi><mo>=</mo><mo></mo><mover accent="true"><msub><mi>T</mi><mi>e</mi></msub><mo>¯</mo></mover></mrow><mrow><mi>Γ</mi><mo></mo><mfenced close=")" open="("><mi>a</mi></mfenced><mi>:</mi><mi>A</mi><mi>r</mi><mi>r</mi><mi>a</mi><mi>y</mi><mo>&lt;</mo><mi>T</mi><mo>&gt;</mo></mrow></mfrac></math>
<simpara>In other languages not supporting union types, the element type is often inferred as the join (<link linkend="_acronyms">LCST</link>) of the element types.
Using a union type here preserves more information (as the actual types are still known).
For many use cases the behavior is similar though, as the members of a union type are the members of the join of the elements of the union.</simpara>
<simpara>Note that <literal>typeof [1,2,3]</literal> does not return <literal>Array&lt;number&gt;</literal> (as ECMAScript is not aware of the generic array type), but <literal>Object</literal>.</simpara>
<example>
<title>Array Type Inference</title>
<simpara>The type for all variables declared in this example is inferred to <literal>Array&lt;string&gt;</literal>:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">var names1 = ["Walter", "Werner"];
var names2 = new Array("Wim", "Wendelin");
var names3 = new Array&lt;string&gt;(3); // length is 3
var names4: Array&lt;string&gt;;</programlisting>
<simpara>Empty array literals are inferred to <literal>any</literal>, by default.
We are not using <literal>Array&lt;?&gt;</literal> here because then a typical JavaScript pattern would no longer be supported:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">var a = [];
a.push('hello'); <co xml:id="CO4-1"/></programlisting>
<calloutlist>
<callout arearefs="CO4-1">
<para>This would fail if <literal>a</literal> and thus <literal>[]</literal> were inferred to <literal>Array&lt;?&gt;</literal></para>
</callout>
</calloutlist>
</example>
<important>
<simpara>An important exception; if a type expectation exists for the empty array literal and the expected type is <literal>Array&lt;T&gt;</literal>, this will be used as the type of the array literal.</simpara>
</important>
<requirement xml:id="IDE-95">
<title>Empty array literal</title>
<simpara>
<anchor xml:id="Req-IDE-95" xreflabel="[Req-IDE-95]"/>
<emphasis role="strong">Requirement: IDE-95:</emphasis>
<link linkend="Req-IDE-95">Empty array literal</link> (ver. 1)</simpara>
<simpara>
An empty array literal will be inferred as follows:</simpara>
<itemizedlist>
<listitem>
<simpara>If there is a type expectation for the empty array literal and the expected type is <literal>Array&lt;T&gt;</literal>, for any type <literal>T</literal>, then the type of the empty array literal will be inferred to <literal>Array&lt;T&gt;</literal>.</simpara>
</listitem>
<listitem>
<simpara>Otherwise, the type of the empty array literal will be inferred to <literal>Array&lt;any&gt;</literal>.</simpara>
</listitem>
</itemizedlist>
</requirement>
</section>
<section xml:id="_object-literal">
<title>Object Literal</title>
<simpara>In addition to ordinary Javascript object literals, N4JS supports the spread operator within object literals as introduced in [<link linkend="ECMA18a">ECMA18a</link>].</simpara>
<bridgehead xml:id="object-literal-syntax" renderas="sect4">Syntax</bridgehead>
<simpara>Cf. [<link linkend="ECMA11a">ECMA11a(p.S11.1.5, p.p.65ff)</link>]
The syntax of an object literal is given by:</simpara>
<programlisting language="xtext" linenumbering="unnumbered">ObjectLiteral &lt;Yield&gt;: {ObjectLiteral}
'{'
( propertyAssignments+=PropertyAssignment&lt;Yield&gt;
(',' propertyAssignments+=PropertyAssignment&lt;Yield&gt;)* ','?
)?
'}'
;
PropertyAssignment &lt;Yield&gt;:
PropertyNameValuePair&lt;Yield&gt;
| PropertyGetterDeclaration&lt;Yield&gt;
| PropertySetterDeclaration&lt;Yield&gt;
| PropertyMethodDeclaration&lt;Yield&gt;
| PropertyNameValuePairSingleName&lt;Yield&gt;
| PropertySpread&lt;Yield&gt;
;
PropertyMethodDeclaration &lt;Yield&gt;:
=&gt; ({PropertyMethodDeclaration}
annotations+=Annotation*
TypeVariables? returnTypeRef=TypeRef?
(
generator?='*' LiteralOrComputedPropertyName&lt;Yield&gt; -&gt;MethodParamsAndBody&lt;Generator=true&gt;
| LiteralOrComputedPropertyName&lt;Yield&gt; -&gt;MethodParamsAndBody &lt;Generator=false&gt;
)
)
';'?
;
PropertyNameValuePair &lt;Yield&gt;:
=&gt; (
{PropertyNameValuePair}
annotations+=Annotation*
declaredTypeRef=TypeRef? LiteralOrComputedPropertyName&lt;Yield&gt; ':'
)
expression=AssignmentExpression&lt;In=true,Yield&gt;
;
/*
* Support for single name syntax in ObjectLiteral (but disallowed in actual object literals by ASTStructureValidator
* except in assignment destructuring patterns)
*/
PropertyNameValuePairSingleName &lt;Yield&gt;:
declaredTypeRef=TypeRef?
identifierRef=IdentifierRef&lt;Yield&gt;
('=' expression=AssignmentExpression&lt;In=true,Yield&gt;)?
;
PropertyGetterDeclaration &lt;Yield&gt;:
=&gt;(
{PropertyGetterDeclaration}
annotations+=Annotation*
GetterHeader&lt;Yield&gt;
)
body=Block&lt;Yield=false&gt;
;
PropertySetterDeclaration &lt;Yield&gt;:
=&gt;(
{PropertySetterDeclaration}
annotations+=Annotation*
'set'
-&gt;LiteralOrComputedPropertyName &lt;Yield&gt;
)
'(' fpar=FormalParameter&lt;Yield&gt; ')' body=Block&lt;Yield=false&gt;
;
PropertySpread &lt;Yield&gt;:
'...' expression=AssignmentExpression&lt;In=true,Yield&gt;
;</programlisting>
<programlisting language="n4js" linenumbering="unnumbered">import Address from "my/Address";
var simple = {name: "Walter", age: 72, address: new Address()};</programlisting>
<section xml:id="_properties-6">
<title>Properties</title>
<simpara>PropertyAssignments have common properties of PropertyNameValuePair, PropertyGetterDeclaration, and PropertySetterDeclaration:</simpara>
<variablelist>
<varlistentry>
<term><literal>annotations</literal> </term>
<listitem>
<simpara>The annotations of the property assignment.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>name</literal> </term>
<listitem>
<simpara>The name of the property. This may be an identifier, a string or a numeric literal.
When comparing names, we implicitly assume the name to be converted to an identifier, even if this identifier is not a valid ECMAScript identifier.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>declaredType</literal> </term>
<listitem>
<simpara>The declared type of the property which may be null.
This property is a pseudo property for PropertySetterDeclaration, in this case it is derived from the declared type of the setter’s formal parameter.</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>Additionally, we introduce the following pseudo properties to simplify constraints:</simpara>
<variablelist>
<varlistentry>
<term><literal>isAccessor</literal> </term>
<listitem>
<simpara>The read-only boolean property. This is true if the property assignment is a setter or getter declaration.
This is comparable to ECMAScript’s spec function <literal>IsAccessoprDescriptor</literal>.
For a given property assignment <emphasis>p</emphasis> this is semantically equivalent to <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>p</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>PropertyGetterDeclaration</mtext></mstyle><mo></mo><mi>μ</mi><mfenced close=")" open="("><mi>p</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>PropertySetterDeclaration</mtext></mstyle></math>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>isData</literal> </term>
<listitem>
<simpara>The read-only boolean property.
This is true if the property assignment is a name value pair.
For a given property assignment <emphasis>p</emphasis> this is semantically equivalent to <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>p</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>PropertyNameValuePair</mtext></mstyle></math>.
It is comparable to ECMAScript’s spec function <literal>isDataDescriptor</literal>.
The equation <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi><mi>s</mi><mi>A</mi><mi>c</mi><mi>c</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>o</mi><mi>r</mi><mo>=</mo><mo>¬</mo><mi>i</mi><mi>s</mi><mi>D</mi><mi>a</mi><mi>t</mi><mi>a</mi></math> is always true.</simpara>
</listitem>
</varlistentry>
</variablelist>
<bridgehead xml:id="properties-semantics" renderas="sect4">Semantics</bridgehead>
<requirement xml:id="IDE-96">
<title>Object literal</title>
<simpara>
<anchor xml:id="Req-IDE-96" xreflabel="[Req-IDE-96]"/>
<emphasis role="strong">Requirement: IDE-96:</emphasis>
<link linkend="Req-IDE-96">Object literal</link> (ver. 1)</simpara>
<simpara>
For a given object literal <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>o</mi><mi>l</mi></math> the following constraints must hold (cf. [<link linkend="ECMA11a">ECMA11a(p.p.66)</link>]:</simpara>
<itemizedlist>
<listitem>
<simpara>the name of each property is given as an identifier, a string literal, a numeric literal, or as a computed property name with a compile-time expression (see <xref linkend="compile-time-expressions"/>). In particular, string literals, e.g. <literal>['myProp']</literal>, built-in symbols, e.g. <literal>[Symbol.iterator]</literal>, and literals of <literal>@StringBased</literal> enums are all valid computed property names.</simpara>
</listitem>
<listitem>
<simpara>Object literal may not have two PropertyNameValuePairs with the same name in strict mode (cf. 4.a [<link linkend="ECMA11a">ECMA11a(p.p.66)</link>]):</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>m</mi><mi>o</mi><mi>d</mi><mi>e</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>strict</mtext></mstyle><mo></mo><mspace width="3.0mm"/><mo></mo><mi>p</mi><mi>a</mi><mo></mo><mi>o</mi><mi>l</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>y</mi><mi>A</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi>s</mi><mo>,</mo><mi>p</mi><mi>a</mi><mo>.</mo><mi>i</mi><mi>s</mi><mi>D</mi><mi>a</mi><mi>t</mi><mi>a</mi><mi>:</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo></mo><mi>p</mi><msup><mi>a</mi><mi>'</mi></msup><mo></mo><mi>o</mi><mi>l</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>y</mi><mi>A</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi>s</mi><mi>:</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mi>p</mi><msup><mi>a</mi><mi>'</mi></msup><mo>.</mo><mi>i</mi><mi>s</mi><mi>A</mi><mi>c</mi><mi>c</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>o</mi><mi>r</mi><mo></mo><mi>p</mi><msup><mi>a</mi><mi>'</mi></msup><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>p</mi><mi>a</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math></simpara>
</listitem>
<listitem>
<simpara>Object literal may not have PropertyNameValuePair and <literal>PropertyGetterDeclaration</literal>/<literal>PropertySetterDeclaration</literal> with the same name (cf. 4.b/c [<link linkend="ECMA11a">ECMA11a(p.p.66)</link>]):</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mspace width="3.0mm"/><mi>p</mi><mi>a</mi><mo></mo><mi>o</mi><mi>l</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>y</mi><mi>A</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi>s</mi><mo>,</mo><mi>p</mi><mi>a</mi><mo>.</mo><mi>i</mi><mi>s</mi><mi>D</mi><mi>a</mi><mi>t</mi><mi>a</mi><mi>:</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mspace width="3.0mm"/><mi>p</mi><mi>g</mi><mi>s</mi><mi>d</mi><mo></mo><mi>o</mi><mi>l</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>y</mi><mi>A</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi>s</mi><mi>:</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mrow><mi>p</mi><mi>g</mi><mi>s</mi><mi>d</mi></mrow></mfenced><mo></mo><mstyle mathvariant="monospace"><mtext>PropertyNameValuePair</mtext></mstyle><mo></mo><mi>p</mi><mi>g</mi><mi>s</mi><mi>d</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>p</mi><mi>a</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math></simpara>
</listitem>
<listitem>
<simpara>Object literal may not have multiple <literal>PropertyGetterDeclaration</literal> or <literal>PropertySetterDeclaration</literal> with the same name (cf. 4.d [<link linkend="ECMA11a">ECMA11a(p.p.66)</link>]):</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mspace width="3.0mm"/><mi>p</mi><mi>g</mi><mo></mo><mi>o</mi><mi>l</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>y</mi><mi>A</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi>s</mi><mo>,</mo><mi>p</mi><mi>g</mi><mo>.</mo><mi>i</mi><mi>s</mi><mi>A</mi><mi>c</mi><mi>c</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>o</mi><mi>r</mi><mi>:</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mspace width="3.0mm"/><mi>p</mi><msup><mi>g</mi><mi>'</mi></msup><mo></mo><mi>o</mi><mi>l</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>y</mi><mi>A</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi>s</mi><mfenced close="}" open="{"><mrow><mi>p</mi><mi>g</mi></mrow></mfenced><mi>:</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mi>μ</mi><mfenced close=")" open="("><mrow><mi>p</mi><msup><mi>g</mi><mi>'</mi></msup></mrow></mfenced><mo>=</mo><mi>μ</mi><mfenced close=")" open="("><mrow><mi>p</mi><mi>g</mi></mrow></mfenced><mo></mo><mi>p</mi><msup><mi>g</mi><mi>'</mi></msup><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>p</mi><mi>g</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math></simpara>
</listitem>
</itemizedlist>
<blockquote>
<simpara>It is a SyntaxError if the Identifier <literal>eval</literal> or the Identifier <literal>arguments</literal> occurs as the Identifier in a <literal>PropertySetParameterList</literal> of a <literal>PropertyAssignment</literal> that is contained in strict code or if its
<literal>FunctionBody</literal> is strict code. [<link linkend="ECMA11a">ECMA11a(p.p.66)</link>]</simpara>
</blockquote>
<itemizedlist>
<listitem>
<simpara>If two or more property assignments have the same name (and the previous conditions hold), then the types of these assignments must <emphasis>conform</emphasis>.
That is to say that the inferred (but not declared) type of all assignments must be type of probably declared types and if the types are explicitly declared, they must be equal.</simpara>
</listitem>
<listitem>
<simpara>In N4JS mode, the name of a property must be a valid N4JSIdentifier:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>m</mi><mi>o</mi><mi>d</mi><mi>e</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>n4js</mtext></mstyle><mo></mo><mspace width="3.0mm"/><mo></mo><mi>p</mi><mi>a</mi><mo></mo><mi>o</mi><mi>l</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>y</mi><mi>A</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi>s</mi><mi>:</mi></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mi>μ</mi><mfenced close=")" open="("><mrow><mi>p</mi><mi>a</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mrow></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>N4JSIdentifier</mtext></mstyle></math></simpara>
</listitem>
</itemizedlist>
</requirement>
<requirement xml:id="IDE-22501">
<title>Superfluous properties of an object literal</title>
<simpara>
<anchor xml:id="Req-IDE-22501" xreflabel="[Req-IDE-22501]"/>
<emphasis role="strong">Requirement: IDE-22501:</emphasis>
<link linkend="Req-IDE-22501">Superfluous properties of an object literal</link> (ver. 1)</simpara>
<simpara>
Let <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>E</mi></math> be the expected type of an object literal <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi></math> as defined by the context in which <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi></math> is used.
If <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>E</mi></math> is not type <literal>Object</literal> and not dynamic, then the compiler creates a warning <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi></math> contains properties not found in <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>E</mi></math>.</simpara>
<simpara>This is true in particular for object literals passed in as arguments of a spec-constructor.</simpara>
</requirement>
</section>
<section xml:id="_scoping-and-linking">
<title>Scoping and linking</title>
<example>
<title>Scoping and linking</title>
<programlisting language="n4js" linenumbering="unnumbered">var p = {
f: function() {
console.log("p´s f");
},
b: function() {
this.f();
},
o: {
nested: "Hello"
}
};
p.b();
p.o.nested;</programlisting>
<itemizedlist>
<listitem>
<simpara>Other properties within an object literal property can be accessed using this.
In the expression of property name value pairs, however, <literal>this</literal> is not be bound to the containing object literal, but usually to undefined or global.</simpara>
</listitem>
<listitem>
<simpara>The properties of an object literal are accessible from outside.</simpara>
</listitem>
<listitem>
<simpara>Nested properties of an object literal are also accessible from outside.</simpara>
</listitem>
</itemizedlist>
</example>
<bridgehead xml:id="type-inference-3" renderas="sect4">Type Inference</bridgehead>
<simpara>An object literal implicitly extends <literal>~Object</literal>, therefore, object literal types use structural typing.
For details see <xref linkend="_structural-typing"/>.
From a type systems point of view, the two variables <literal>ol</literal> and <literal>st</literal> below have the same type.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">var ol = {
s: "hello",
n: 42
}
var st: ~Object with { s: string; n: number;};</programlisting>
</section>
</section>
<section xml:id="_parenthesized-expression-and-grouping-operator">
<title>Parenthesized Expression and Grouping Operator</title>
<simpara>The grouping operator is defined here as a parenthesized expression.</simpara>
<bridgehead xml:id="parenthesized-expression-grouping-syntax" renderas="sect4">Syntax</bridgehead>
<simpara>cf. [<link linkend="ECMA11a">ECMA11a(p.S11.1.6, p.p.67)</link>]</simpara>
<programlisting language="xtext" linenumbering="unnumbered">ParenExpression &lt;Yield&gt;: '(' expression=Expression&lt;In=true,Yield&gt; ')';</programlisting>
<bridgehead xml:id="Grouping-Operator-type-inference" renderas="sect4">Type Inference</bridgehead>
<simpara>The type of the grouping operator simply is the type of its nested expression.
The type if a parenthesized expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>p</mi><mi>e</mi></math> is inferred as follows:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>Γ</mi><mo></mo><mi>e</mi><mi>:</mi><mi>T</mi></mrow><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>’(’</mtext></mstyle><mi>e</mi><mstyle mathvariant="monospace"><mtext>’)’</mtext></mstyle><mi>:</mi><mi>T</mi></mrow></mfrac></math>
<example>
<title>Parenthesized Expression Type Examples</title>
<simpara>In the following listing, the type of the plain expressions is equivalent to the parenthesized versions:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class A{} class B extends A{}
var f: boolean; var a: A a; var b: B;
/* simple &lt;-&gt; parenthesized */
10; (10);
"hello"; ("hello");
true; (true);
a; (a);
10-5; (10-5);
f?a:b (f?a:b);</programlisting>
</example>
</section>
<section xml:id="_property-accessors">
<title>Property Accessors</title>
<bridgehead xml:id="property-accessor-syntax" renderas="sect4">Syntax</bridgehead>
<simpara>Property accessors in N4JS are based on [<link linkend="ECMA11a">ECMA11a(p.S11.2.1, p.p.67ff)</link>].
They cannot only be used for accessing properties of an object, but also for accessing members of a class instance.
In order to support parameterized calls, the syntax is extended to optionally allow type arguments.</simpara>
<programlisting language="xtext" linenumbering="unnumbered">ParameterizedPropertyAccessExpression:
target=PrimaryExpression&lt;Yield&gt; ParameterizedPropertyAccessExpressionTail&lt;Yield&gt;
;
IndexedAccessExpression:
target=PrimaryExpression&lt;Yield&gt; IndexedAccessExpressionTail&lt;Yield&gt;
;
fragment IndexedAccessExpressionTail &lt;Yield&gt;*:
'[' index=Expression&lt;In=true,Yield&gt; ']'
;
fragment ParameterizedPropertyAccessExpressionTail &lt;Yield&gt;*:
'.' TypeArguments? property=[types::IdentifiableElement|IdentifierName]
;</programlisting>
<simpara>Note that in [<link linkend="ECMA11a">ECMA11a</link>], the <literal>index access</literal> is called <literal><emphasis>bracket notation</emphasis></literal>.</simpara>
<bridgehead xml:id="property-access-direct" renderas="sect4">Direct Property Access</bridgehead>
<simpara>We define a special case of property access as follows:</simpara>
<definition>
<title>Direct Property Access</title>
<simpara>
<anchor xml:id="direct_property_access" xreflabel="[direct_property_access]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="direct_property_access">Direct Property Access</link></simpara>
<simpara>
A property access expression is called <emphasis>direct</emphasis>, iff</simpara>
<itemizedlist>
<listitem>
<simpara>its target is an identifier reference to a class, interface, enum, or the built-in object <literal>Symbol</literal>, and</simpara>
</listitem>
<listitem>
<simpara>its property name denotes an <emphasis>owned</emphasis> member of the target classifier (not an inherited, consumed, or polyfilled member) or a literal if the target is an enum.</simpara>
</listitem>
</itemizedlist>
</definition>
<simpara>As a consequence, a direct property access can only refer to static members.</simpara>
<simpara>The first requirement of the above definition rules out property access expressions that do not directly point to their target classifier or enum, as shown in the following example:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class C {
const field = 'hello';
}
C.field; // direct property access to 'field'
let ctor = C;
ctor.field; // *not* a direct property access to 'field'</programlisting>
<simpara>Direct property access is the only form of property access allowed in compile-time expressions, cf. <xref linkend="compile-time-expressions"/>.</simpara>
<section xml:id="properties-1">
<title>Properties</title>
<simpara>We define the following properties:</simpara>
<variablelist>
<varlistentry>
<term><literal>target</literal> </term>
<listitem>
<simpara>The receiver of the property access.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>index</literal> </term>
<listitem>
<simpara>The index expression in case of an IndexedAccessExpression (returns <math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>null</mtext></mstyle></math> otherwise).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>property</literal> </term>
<listitem>
<simpara>The name of the property in case of non-indexed-access expressions (returns <math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>null</mtext></mstyle></math> otherwise, although the index may be interpreted as property name).</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>We define the following pseudo properties:</simpara>
<variablelist>
<varlistentry>
<term><literal>isDotAccess</literal> </term>
<listitem>
<simpara>Read-only boolean property, returns true for non-index access expression (similar to <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>p</mi></mfenced><mo></mo><mstyle mathvariant="monospace"><mtext>IndexedAccessExpression</mtext></mstyle></math>).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>isIndexAccess</literal> </term>
<listitem>
<simpara>Read-only boolean property, returns true for index access expression (similar to <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>p</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>IndexedAccessExpression</mtext></mstyle></math>.<?asciidoc-br?>
The equation <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>p</mi><mo>.</mo><mi>i</mi><mi>s</mi><mi>D</mi><mi>o</mi><mi>t</mi><mi>A</mi><mi>c</mi><mi>c</mi><mi>e</mi><mi>s</mi><mi>s</mi><mo>=</mo><mo>¬</mo><mi>p</mi><mo>.</mo><mi>i</mi><mi>s</mi><mi>I</mi><mi>n</mi><mi>d</mi><mi>e</mi><mi>x</mi><mi>A</mi><mi>c</mi><mi>c</mi><mi>e</mi><mi>s</mi><mi>s</mi></math> is always true.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>name</literal> </term>
<listitem>
<simpara>Returns the name of the property.
This is either the <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>p</mi><mi>r</mi><mi>o</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>y</mi></math> converted to a simple name or the index converted to a name (where possible) if it is an indexed-accessed expression.</simpara>
</listitem>
</varlistentry>
</variablelist>
<bridgehead xml:id="property-acessors-semantics" renderas="sect4">Semantics</bridgehead>
<simpara>The parameterization is part of the property access in case of generic methods.
For generic functions, a parameterized function call is introduced (cf. <xref linkend="_function-calls"/>).
The constraints are basically similar.</simpara>
<requirement xml:id="IDE-97">
<title>Property Access and Dot Notation</title>
<simpara>
<anchor xml:id="Req-IDE-97" xreflabel="[Req-IDE-97]"/>
<emphasis role="strong">Requirement: IDE-97:</emphasis>
<link linkend="Req-IDE-97">Property Access and Dot Notation</link> (ver. 1)</simpara>
<simpara>
</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>If dot notation is used in N4JS mode, the referenced property must exist unless receiver is a dynamic type:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mspace width="3.0mm"/><mi>p</mi><mi>a</mi><mi>e</mi><mo>.</mo><mi>i</mi><mi>s</mi><mi>D</mi><mi>o</mi><mi>t</mi><mi>A</mi><mi>c</mi><mi>c</mi><mi>e</mi><mi>s</mi><mi>s</mi><mo></mo><mo>¬</mo><mi>R</mi><mo>.</mo><mi>d</mi><mi>y</mi><mi>n</mi><mo></mo><mtext>
</mtext><mspace width="3.0mm"/><mo></mo><mi>m</mi><mo></mo><mi>p</mi><mi>a</mi><mi>e</mi><mo>.</mo><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>i</mi><mi>e</mi><mi>s</mi><mi>:</mi><mi>m</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>p</mi><mi>a</mi><mi>e</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math>
</listitem>
<listitem>
<simpara>If dot notation is used and the referenced property exists, then the property must be accessible:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mspace width="3.0mm"/><mi>p</mi><mi>a</mi><mi>e</mi><mo>.</mo><mi>i</mi><mi>s</mi><mi>D</mi><mi>o</mi><mi>t</mi><mi>A</mi><mi>c</mi><mi>c</mi><mi>e</mi><mi>s</mi><mi>s</mi><mo></mo><mo>¬</mo><mi>R</mi><mo>.</mo><mi>d</mi><mi>y</mi><mi>n</mi><mo></mo><mtext>
</mtext><mspace width="3.0mm"/><mfenced close=")" open="("><mrow><mo></mo><mi>m</mi><mo></mo><mi>p</mi><mi>a</mi><mi>e</mi><mo>.</mo><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>i</mi><mi>e</mi><mi>s</mi><mi>:</mi><mi>m</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>p</mi><mi>a</mi><mi>e</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mrow></mfenced><mo></mo><mi>α</mi><mfenced close=")" open="("><mrow><mi>p</mi><mi>a</mi><mi>e</mi></mrow><mi>m</mi></mfenced></math>
</listitem>
<listitem>
<simpara>If dot notation is used and the referenced property exists and this property is a member with a declared <literal>@This</literal> type (only possible for methods or field accessors),
then the receiver must be a subtype of the declared <literal>@This</literal> type.</simpara>
</listitem>
</orderedlist>
</requirement>
<requirement xml:id="IDE-98">
<title>Index Access</title>
<simpara>
<anchor xml:id="Req-IDE-98" xreflabel="[Req-IDE-98]"/>
<emphasis role="strong">Requirement: IDE-98:</emphasis>
<link linkend="Req-IDE-98">Index Access</link> (ver. 1)</simpara>
<simpara>
</simpara>
<simpara>An index access expression is valid iff one of the following cases applies:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>the receiver is of a dynamic type. In this case, the index may be any expression (need not be a compile-time expression).</simpara>
</listitem>
<listitem>
<simpara>the receiver is an immediate instance of <literal>Object</literal>, i.e. it is a subtype of <literal>Object</literal> and its super types but <emphasis role="strong">not</emphasis> of any other type including <literal>~Object</literal> and <literal>~~Object</literal>.</simpara>
</listitem>
<listitem>
<simpara>the receiver is of type Array, ArgumentType, string, or String (including their subtypes) <emphasis role="strong">and</emphasis> the index is an expression of type <literal>number</literal>.</simpara>
</listitem>
<listitem>
<simpara>the index expression is a compile-time expression</simpara>
<itemizedlist>
<listitem>
<simpara><emphasis role="strong">and</emphasis> the receiver type defines a member with a name equal to the string representation of the index expression&#8217;s compile-time value<?asciidoc-br?></simpara>
</listitem>
<listitem>
<simpara><emphasis role="strong">and</emphasis> the receiver is not an enum.</simpara>
</listitem>
</itemizedlist>
</listitem>
</orderedlist>
</requirement>
<simpara>Although index access is very limited, it is still possible to use immediate instances of <literal>Object</literal> in terms of a map (but this applies only to index access, not the dot notation):</simpara>
<example>
<title>Object as Map</title>
<programlisting language="n4js" linenumbering="unnumbered">var map: Object = new Object();
map["Kant"] = "Imperative";
map["Hegel"] = "Dialectic";
map.spinoza = "Am I?"; // error: Couldn't resolve reference to IdentifiableElement 'spinoza'.</programlisting>
</example>
<requirement xml:id="IDE-99">
<title>Parameterized Property Access</title>
<simpara>
<anchor xml:id="Req-IDE-99" xreflabel="[Req-IDE-99]"/>
<emphasis role="strong">Requirement: IDE-99:</emphasis>
<link linkend="Req-IDE-99">Parameterized Property Access</link> (ver. 1)</simpara>
<simpara>
For a parameterized property access expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>p</mi><mi>a</mi><mi>e</mi></math>, the following constraints must hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>The receiver or target must be a function or method:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>p</mi><mi>a</mi><mi>e</mi><mo>.</mo><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>Function</mtext></mstyle></math></simpara>
</listitem>
<listitem>
<simpara>The number of type arguments must match the number of type parameters of the generic function or method:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>|</mo><mi>p</mi><mi>a</mi><mi>e</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>A</mi><mi>r</mi><mi>g</mi><mi>s</mi><mo>|</mo><mo>=</mo><mo>|</mo><mi>p</mi><mi>a</mi><mi>e</mi><mo>.</mo><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>V</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>|</mo></math></simpara>
</listitem>
<listitem>
<simpara>The type arguments of a parameterized property access expression must be subtypes of the boundaries of the parameters of the called generic method.</simpara>
</listitem>
</orderedlist>
<simpara>Also see constraints on read (<xref linkend="Req-IDE-93"/>) and write (<xref linkend="Req-IDE-121"/>) access.</simpara>
</requirement>
<bridgehead xml:id="type-inference-5" renderas="sect4">Type Inference</bridgehead>
<simpara>Cf. [<link linkend="ECMA11a">ECMA11a(p.S11.2.1, p.p.67ff)</link>]</simpara>
<simpara>We define the following type inferencing rules for property accessors:</simpara>
<itemizedlist>
<listitem>
<simpara>The type of an indexed-access expression <emphasis>p</emphasis> is inferred as follows:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mfrac><mrow><mo>¬</mo><mi>p</mi><mo>.</mo><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi><mo>.</mo><mi>d</mi><mi>y</mi><mi>n</mi><mo></mo><mi>p</mi><mo>.</mo><mi>i</mi><mi>n</mi><mi>d</mi><mi>e</mi><mi>x</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mfenced close="]" open="["><mrow><mi>n</mi><mi>u</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi></mrow></mfenced><mspace width="3.0mm"/><mi>Γ</mi><mo></mo><mi>p</mi><mo>.</mo><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>Array</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mo>&gt;</mo></mrow><mrow><mi>Γ</mi><mo></mo><mi>p</mi><mi>:</mi><mi>T</mi></mrow></mfrac></mtd></mtr><mtr><mtd><mfrac><mrow><mi>e</mi><mi>l</mi><mi>s</mi><mi>e</mi></mrow><mrow><mi>Γ</mi><mo></mo><mi>p</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>any</mtext></mstyle></mrow></mfrac></mtd></mtr></mtable></math>
</listitem>
<listitem>
<simpara>The type of a property access expression is inferred as follows:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mfrac><mrow><mi>Γ</mi><mo></mo><mi>θ</mi><mfenced close=")" open="("><mi>R</mi></mfenced><mo></mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo>.</mo><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi><mi>:</mi><mi>R</mi><mspace width="3.0mm"/><mi>Γ</mi><mo></mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>y</mi><mi>:</mi><mi>T</mi></mrow><mrow><mstyle mathvariant="monospace"><mtext>PropertyAccessExpression</mtext></mstyle><mi> </mi><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>:</mi><mi>T</mi></mrow></mfrac></mtd></mtr></mtable></math>
</listitem>
<listitem>
<simpara>The type of a parameterized access expression <emphasis>p</emphasis> is inferred as follows:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mfrac><mrow><mo></mo><mi>m</mi><mo></mo><mi>p</mi><mo>.</mo><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi><mi>:</mi><mi>m</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>p</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mspace width="3.0mm"/><mi>Γ</mi><mo></mo><mi>m</mi><mi>:</mi><mi>T</mi></mrow><mrow><mi>Γ</mi><mo></mo><mi>p</mi><mi>:</mi><mi>T</mi></mrow></mfrac></mtd></mtr><mtr><mtd><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mi>p</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>any</mtext></mstyle></mrow></mfrac></mtd></mtr></mtable></math>
</listitem>
</itemizedlist>
</section>
</section>
<section xml:id="_new-expression">
<title>New Expression</title>
<simpara>cf. [<link linkend="ECMA11a">ECMA11a(p.S11.2.2, p.p.68)</link>]</simpara>
<bridgehead xml:id="new-expression-syntax" renderas="sect4">Syntax</bridgehead>
<programlisting language="xtext" linenumbering="unnumbered">NewExpression: 'new' callee=MemberExpression&lt;Yield&gt; (-&gt; TypeArguments)?
(=&gt; withArgs?='(' Arguments&lt;Yield&gt;? ')' )?</programlisting>
<programlisting language="n4js" linenumbering="unnumbered">import Address from "my/Address";
var a = new Address();
// a.type := my/Address
class C&lt;T&gt; {
constructor(param: T) {}
}
var c = new C&lt;string&gt;("hello");</programlisting>
<bridgehead xml:id="new-expression-semantics" renderas="sect4">Semantics</bridgehead>
<requirement xml:id="IDE-100">
<title>New expression</title>
<simpara>
<anchor xml:id="Req-IDE-100" xreflabel="[Req-IDE-100]"/>
<emphasis role="strong">Requirement: IDE-100:</emphasis>
<link linkend="Req-IDE-100">New expression</link> (ver. 1)</simpara>
<simpara>
Let <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi><mi>e</mi></math> be a new expression, with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>Γ</mi><mo></mo><mi>n</mi><mi>e</mi><mo>.</mo><mi>c</mi><mi>a</mi><mi>l</mi><mi>l</mi><mi>e</mi><mi>e</mi><mi>:</mi><mi>C</mi></math>.
The following constraints must hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara><anchor xml:id="new-expression-1" xreflabel="[new-expression-1]"/> The callee must be a constructor type: <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi><mo>&lt;</mo><mi>:</mi><mstyle mathvariant="monospace"><mtext>constructor</mtext></mstyle><mfenced close="}" open="{"><mi>?</mi></mfenced></math> or a constructable type.</simpara>
</listitem>
<listitem>
<simpara><anchor xml:id="new-expression-2" xreflabel="[new-expression-2]"/> Let <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi></math> be the type argument of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi></math>, that is <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi><mo>=</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mfenced close="}" open="{"><mi>O</mi></mfenced></math>. In that case,</simpara>
<orderedlist numeration="loweralpha">
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi></math> must not be an interface or enum: <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>C</mi></mfenced><mo></mo><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>Interface</mtext></mstyle><mstyle mathvariant="monospace"><mtext>Enum</mtext></mstyle></mfenced></math></simpara>
</listitem>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi></math> must not contain any wildcards.</simpara>
</listitem>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi></math> must not be a type variable.</simpara>
</listitem>
</orderedlist>
</listitem>
<listitem>
<simpara><anchor xml:id="new-expression-3" xreflabel="[new-expression-3]"/> If <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi></math> is not a constructor type, it must be a constructable type, that is one of the following:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfenced close="}" open="{"><mtable><mtr><mtd><mstyle mathvariant="monospace"><mtext>Object, Function, String, Boolean,</mtext></mstyle></mtd></mtr><mtr><mtd><mstyle mathvariant="monospace"><mtext>Number, Array, Date, RegExp, Error</mtext></mstyle></mtd></mtr></mtable></mfenced></math>
<simpara>In particular, it must not refer to a primitive type or a defined
functions (i.e., subtypes of <literal>Function</literal>) cannot be used in new-expressions in
N4JS.</simpara>
</listitem>
</orderedlist>
</requirement>
<simpara>Remarks:</simpara>
<simpara>to <link linkend="new-expression-1">1</link> The type of an abstract class <literal>A</literal> is <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mfenced close="}" open="{"><mi>A</mi></mfenced></math>.
Or in other words: Only instantiable classes have an inferred type of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mfenced close="}" open="{"><mrow><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></mrow></mfenced></math>.</simpara>
<simpara>to <link linkend="new-expression-2">2</link> Even though it is possible to use the constructor type of an abstract class – concrete subclasses with override compatible constructor signature will be subclasses of this constructor.</simpara>
<simpara>to <link linkend="new-expression-3">3</link> It is not possible to refer to union or intersection at that location. So this is not explicitly denied here since it is not possible anyway.</simpara>
<example>
<title>Abstract classes and construction</title>
<simpara>The following examples demonstrates the usage of abstract classes and constructor types, to make the first two constraints more clearer:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">/* XPECT_SETUP org.eclipse.n4js.spec.tests.N4JSSpecTest END_SETUP */
abstract class A {}
class B extends A {}
// XPECT errors --&gt; "Cannot instantiate abstract class A." at "A"
var x = new A();
// XPECT noerrors --&gt;
var y = new B();
function foo(ctor : constructor{A}) {
// XPECT noerrors --&gt;
return new ctor();
}
// XPECT errors --&gt; "type{A} is not a subtype of constructor{A}." at "A"
foo(A);
// XPECT noerrors --&gt;
foo(B);</programlisting>
</example>
<bridgehead xml:id="type-inference-6" renderas="sect4">Type Inference</bridgehead>
<simpara>The type of a new expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi><mi>e</mi></math> is inferred as follows:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>Γ</mi><mo></mo><mi>n</mi><mi>e</mi><mo>.</mo><mi>c</mi><mi>a</mi><mi>l</mi><mi>l</mi><mi>e</mi><mi>e</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>constructor</mtext><mtext>C</mtext></mstyle></mrow><mrow><mi>Γ</mi><mo></mo><mi>n</mi><mi>e</mi><mi>:</mi><mi>C</mi></mrow></mfrac></math>
<simpara>For classes, constructors are described in <xref linkend="_constructor-and-classifier-type"/>.</simpara>
<simpara>In N4JS it is not allowed to call new on a plain function.
For example:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">function foo() {}
var x = new foo();</programlisting>
<simpara>will issue an error.</simpara>
</section>
<section xml:id="_function-expression-2">
<title>Function Expression</title>
<simpara>See <xref linkend="_functions"/> for details.</simpara>
</section>
<section xml:id="_function-calls">
<title>Function Calls</title>
<simpara>In N4JS, a function call [<link linkend="ECMA11a">ECMA11a(p.S11.2.3)</link>] is similar to a method call.
Additionally to the ECMAScript’s CallExpression, a ParameterizedCallExpression is introduced to allow type arguments passed to plain functions.</simpara>
<bridgehead xml:id="_syntax-12" renderas="sect4">Syntax</bridgehead>
<literallayout class="monospaced">[[function-calls-syntax]]</literallayout>
<simpara>Similar to [<link linkend="ECMA11a">ECMA11a(p.S11.2.3, p.p.68ff)</link>], a function call is defined as follows:</simpara>
<programlisting language="xtext" linenumbering="unnumbered">CallExpression &lt;Yield&gt;:
target=IdentifierRef&lt;Yield&gt;
ArgumentsWithParentheses&lt;Yield&gt;
;
ParameterizedCallExpression &lt;Yield&gt;:
TypeArguments
target=IdentifierRef&lt;Yield&gt;
ArgumentsWithParentheses&lt;Yield&gt;
;
fragment ArgumentsWithParentheses &lt;Yield&gt;*:
'(' Arguments&lt;Yield&gt;? ')'
;
fragment Arguments &lt;Yield&gt;*:
arguments+=AssignmentExpression&lt;In=true,Yield&gt; (',' arguments+=AssignmentExpression&lt;In=true,Yield&gt;)* (',' spread?='...' arguments+=AssignmentExpression&lt;In=true,Yield&gt;)?
| spread?='...' arguments+=AssignmentExpression&lt;In=true,Yield&gt;
;</programlisting>
<bridgehead xml:id="function-calls-semantics" renderas="sect4">Semantics</bridgehead>
<requirement xml:id="IDE-101">
<title>Function Call Constraints</title>
<simpara>
<anchor xml:id="Req-IDE-101" xreflabel="[Req-IDE-101]"/>
<emphasis role="strong">Requirement: IDE-101:</emphasis>
<link linkend="Req-IDE-101">Function Call Constraints</link> (ver. 1)</simpara>
<simpara>
For a given call expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi></math> bound to a method or function declaration <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>F</mi></math>, the following constraints must hold:</simpara>
<itemizedlist>
<listitem>
<simpara>If less arguments are provided than formal parameters were declared, the missing formal parameters must have been declared optional:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>|</mo><mi>f</mi><mo>.</mo><mi>a</mi><mi>r</mi><mi>g</mi><mi>s</mi><mo>|</mo><mo>&lt;</mo><mo>|</mo><mi>F</mi><mo>.</mo><mi>p</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>|</mo><mo></mo><mo></mo><mo>|</mo><mi>f</mi><mo>.</mo><mi>a</mi><mi>r</mi><mi>g</mi><mi>s</mi><mo>|</mo><mo>&lt;</mo><mi>i</mi><mo></mo><mo>|</mo><mi>F</mi><mo>.</mo><mi>p</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>|</mo><mi>:</mi><msub><mi>F</mi><mi>p</mi></msub><mi>a</mi><mi>r</mi><msub><mi>s</mi><mi>i</mi></msub><mo>.</mo><mi>o</mi><mi>p</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mi>a</mi><mi>l</mi></math></simpara>
</listitem>
<listitem>
<simpara>If more arguments are provided than formal parameters were declared, the last formal parameter must have been declared variadic:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>|</mo><mi>f</mi><mo>.</mo><mi>a</mi><mi>r</mi><mi>g</mi><mi>s</mi><mo>|</mo><mo>&gt;</mo><mo>|</mo><mi>F</mi><mo>.</mo><mi>p</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>|</mo><mo></mo><mi>F</mi><mo>.</mo><mi>p</mi><mi>a</mi><mi>r</mi><msub><mi>s</mi><mrow><mo>|</mo><mi>F</mi><mo>.</mo><mi>p</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>|</mo><mo>-</mo><mn>1</mn></mrow></msub><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi><mi>i</mi><mi>a</mi><mi>d</mi><mi>i</mi><mi>c</mi></math></simpara>
</listitem>
<listitem>
<simpara>Types of provided arguments must be subtypes of the formal parameter types:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mn>0</mn><mo>&lt;</mo><mi>i</mi><mo>&lt;</mo><mi>m</mi><mi>i</mi><mi>n</mi><mfenced close=")" open="("><mrow><mo>|</mo><mi>f</mi><mo>.</mo><mi>a</mi><mi>r</mi><mi>g</mi><mi>s</mi><mo>|</mo></mrow><mrow><mo>|</mo><mi>F</mi><mo>.</mo><mi>p</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>|</mo></mrow></mfenced><mi>:</mi><mi>f</mi><mo>.</mo><mi>a</mi><mi>r</mi><mi>g</mi><msub><mi>s</mi><mi>i</mi></msub><mo>&lt;</mo><mi>:</mi><mi>F</mi><mo>.</mo><mi>p</mi><mi>a</mi><mi>r</mi><msub><mi>s</mi><mi>i</mi></msub></math></simpara>
</listitem>
<listitem>
<simpara>If more arguments are provided than formal parameters were declared, the type of the exceeding arguments must be a subtype of the last (variadic) formal parameter type:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mo>|</mo><mi>F</mi><mo>.</mo><mi>p</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>|</mo><mo>&lt;</mo><mi>i</mi><mo></mo><mo>|</mo><mi>f</mi><mo>.</mo><mi>a</mi><mi>r</mi><mi>g</mi><mi>s</mi><mo>|</mo><mi>:</mi><mi>f</mi><mo>.</mo><mi>a</mi><mi>r</mi><mi>g</mi><msub><mi>s</mi><mi>i</mi></msub><mo>&lt;</mo><mi>:</mi><mi>F</mi><mo>.</mo><mi>p</mi><mi>a</mi><mi>r</mi><msub><mi>s</mi><mrow><mo>|</mo><mi>F</mi><mo>.</mo><mi>p</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>|</mo><mo>-</mo><mn>1</mn></mrow></msub></math></simpara>
</listitem>
</itemizedlist>
</requirement>
<requirement xml:id="IDE-102">
<title>Parameterized Function Call Constraints</title>
<simpara>
<anchor xml:id="Req-IDE-102" xreflabel="[Req-IDE-102]"/>
<emphasis role="strong">Requirement: IDE-102:</emphasis>
<link linkend="Req-IDE-102">Parameterized Function Call Constraints</link> (ver. 1)</simpara>
<simpara>
* The number of type arguments in a parameterized call expression must be equal to the number of type parameters of the generic function / method and the
type arguments must be subtypes of the corresponding declared upper boundaries of the type parameters of the called generic function.</simpara>
<simpara>Note that (for a limited time), constraints <xref linkend="Req-IDE-101"/> and <xref linkend="Req-IDE-102"/> are not applied if the the type of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>F</mi></math> is <literal>Function</literal>.
See <xref linkend="_function-object-type"/>.</simpara>
</requirement>
<bridgehead xml:id="type-inference-7" renderas="sect4">Type Inference</bridgehead>
<simpara>A call expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi></math> is bound to a method (<xref linkend="_methods"/>) or function declaration (which may be part of a function definition
(<xref linkend="_function-declaration"/> or specified via a function type <xref linkend="_function-type"/>) <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>F</mi></math> (via evaluation of <literal>MemberExpression</literal>.
The type of the call is inferred from the function declaration or type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>F</mi></math> as follows:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>b</mi><mi>i</mi><mi>n</mi><mi>d</mi><mfenced close=")" open="("><mrow><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo>.</mo><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi></mrow><mi>F</mi></mfenced><mspace width="3.0mm"/><mi>F</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>n</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>:</mi><mi>T</mi></mrow><mrow><mi>Γ</mi><mo></mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>:</mi><mi>T</mi></mrow></mfrac></math>
<simpara>A generic method invocation may be parameterized as well.
This is rarely required as the function argument types are usually inferred from the given arguments.
In some cases, for instance with pathSelectors, this is useful.
In that case, the type variable defined in the generic method declaration is explicitly bound to types by using type arguments.
See <xref linkend="_property-accessors"/> for semantics and type inference.</simpara>
<example>
<title>Generic Method Invocation</title>
<simpara>This examples demonstrate how to explicitly
define the type argument in a method call in case it cannot be inferred
automatically.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class C {
static &lt;T&gt; foo(p: pathSelector&lt;T&gt;): void {..}
};
C.&lt;my.Address&gt;foo("street.number");</programlisting>
<simpara>Note that in many cases, the type inferencer should be able to infer the type automatically.
For example, for a method</simpara>
<programlisting language="n4js" linenumbering="unnumbered">function &lt;T&gt; bar(c: T, p: pathSelector&lt;T&gt;): void {..};</programlisting>
<simpara>and a function call</simpara>
<programlisting language="n4js" linenumbering="unnumbered">bar(context, "some.path.selector");
[source,n4js]</programlisting>
<simpara>the type variable <literal>T</literal> can be automatically bound to the type of variable <literal>context</literal>.</simpara>
</example>
</section>
<section xml:id="_postfix-expression">
<title>Postfix Expression</title>
<bridgehead xml:id="postfix-expression-syntax" renderas="sect4">Syntax</bridgehead>
<programlisting language="xtext" linenumbering="unnumbered">PostfixExpression returns Expression: LeftHandSideExpression
(=&gt;({PostfixExpression.expression=current} /* no line terminator here */ op=PostfixOperator))?
;
enum PostfixOperator: inc='++' | dec='--';</programlisting>
<bridgehead xml:id="semantics-and-type-inference" renderas="sect4">Semantics and Type Inference</bridgehead>
<simpara>The type inference and constraints for postfix operators <literal>++</literal> and <literal>--</literal>, cf. [<link linkend="ECMA11a">ECMA11a(p.S11.3.1, p.p.70)</link>], [<link linkend="ECMA11a">ECMA11a(p.S11.3.1, p.p.70)</link>],
are defined similarly to their prefix variants (unary expressions), see <xref linkend="_unary-expression"/>.</simpara>
<requirement xml:id="IDE-103">
<title>Postfix Expression Constraints</title>
<simpara>
<anchor xml:id="Req-IDE-103" xreflabel="[Req-IDE-103]"/>
<emphasis role="strong">Requirement: IDE-103:</emphasis>
<link linkend="Req-IDE-103">Postfix Expression Constraints</link> (ver. 1)</simpara>
<simpara>
</simpara>
<simpara>For a given postfix expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi></math> <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi></math> with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi><mo>.</mo><mi>o</mi><mi>p</mi><mo></mo><mfenced close="}" open="{"><mrow><mo>+</mo><mo>+</mo></mrow><mrow><mo>-</mo><mo>-</mo></mrow></mfenced></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>:</mi><mi>T</mi></math>, the following constraints must hold:</simpara>
<itemizedlist>
<listitem>
<simpara>In N4JS mode, the type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math> of the expression must be a number.</simpara>
</listitem>
<listitem>
<simpara>If <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo>=</mo><mi>P</mi><mi>r</mi><mi>o</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>y</mi><mi>A</mi><mi>c</mi><mi>c</mi><mi>e</mi><mi>s</mi><mi>s</mi><mspace width="0.278em"/><mi>p</mi><mi>a</mi><mfenced close=")" open="("><mi>p</mi></mfenced><mo></mo><mi>p</mi><mi>a</mi><mo>.</mo><mi>i</mi><mi>s</mi><mi>D</mi><mi>o</mi><mi>t</mi><mi>A</mi><mi>c</mi><mi>c</mi><mi>e</mi><mi>s</mi><mi>s</mi><mo></mo></math> both <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>g</mi><mi>e</mi><mi>t</mi></math> <emphasis>p</emphasis> and <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>e</mi><mi>t</mi></math> <emphasis>p</emphasis> must be defined.</simpara>
</listitem>
</itemizedlist>
</requirement>
</section>
<section xml:id="_unary-expression">
<title>Unary Expression</title>
<bridgehead xml:id="unary-expression-syntax" renderas="sect4">Syntax</bridgehead>
<simpara>We define the following unary operators and expression, similar to [<link linkend="ECMA11a">ECMA11a(p.p.70ff)</link>]:</simpara>
<programlisting language="xtext" linenumbering="unnumbered">UnaryExpression returns Expression:
PostfixExpression
| ({UnaryExpression} op=UnaryOperator expression=UnaryExpression);
enum UnaryOperator: delete | void | typeof | inc='++' | dec='--' | pos='+' | neg='-' | inv='$\sim$' | not='!';</programlisting>
<bridgehead xml:id="unary-expression-semantics" renderas="sect4">Semantics</bridgehead>
<simpara>For semantics of the delete operator, see also [<link linkend="MozillaJSRef">MozillaJSRef</link>]</simpara>
<requirement xml:id="IDE-104">
<title>Delete Operator Constraints</title>
<simpara>
<anchor xml:id="Req-IDE-104" xreflabel="[Req-IDE-104]"/>
<emphasis role="strong">Requirement: IDE-104:</emphasis>
<link linkend="Req-IDE-104">Delete Operator Constraints</link> (ver. 1)</simpara>
<simpara>
For a given unary expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi></math> with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi><mo>.</mo><mi>o</mi><mi>p</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>delete</mtext></mstyle></math>, the following constraints must hold:</simpara>
<itemizedlist>
<listitem>
<simpara>In strict mode, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi></math> must be a reference to a property of an object literal, a member of a class type, or to a property of the global type
(i.e., the reference must be bound, and the bound target must not be a variable).</simpara>
</listitem>
<listitem>
<simpara>In N4JS mode, the referenced property or member must not be declared in the containing type and the containing type reference must be declared dynamic.</simpara>
</listitem>
</itemizedlist>
</requirement>
<requirement xml:id="IDE-105">
<title>Void Operator Constraints</title>
<simpara>
<anchor xml:id="Req-IDE-105" xreflabel="[Req-IDE-105]"/>
<emphasis role="strong">Requirement: IDE-105:</emphasis>
<link linkend="Req-IDE-105">Void Operator Constraints</link> (ver. 1)</simpara>
<simpara>
There are no specific constraints defined for with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi><mo>.</mo><mi>o</mi><mi>p</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>void</mtext></mstyle></math></simpara>
</requirement>
<requirement xml:id="IDE-106">
<title>Typeof Operator Constraints</title>
<simpara>
<anchor xml:id="Req-IDE-106" xreflabel="[Req-IDE-106]"/>
<emphasis role="strong">Requirement: IDE-106:</emphasis>
<link linkend="Req-IDE-106">Typeof Operator Constraints</link> (ver. 1)</simpara>
<simpara>
There are no specific constraints defined for unary expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi></math> with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi><mo>.</mo><mi>o</mi><mi>p</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>typeof</mtext></mstyle></math>.</simpara>
</requirement>
<requirement xml:id="IDE-107">
<title>Increment/Decrement Constraints</title>
<simpara>
<anchor xml:id="Req-IDE-107" xreflabel="[Req-IDE-107]"/>
<emphasis role="strong">Requirement: IDE-107:</emphasis>
<link linkend="Req-IDE-107">Increment/Decrement Constraints</link> (ver. 1)</simpara>
<simpara>
For a given unary expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi></math> <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi></math> with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi><mo>.</mo><mi>o</mi><mi>p</mi><mo></mo><mfenced close="}" open="{"><mrow><mo>+</mo><mo>+</mo></mrow><mrow><mo>-</mo><mo>-</mo></mrow></mfenced></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>:</mi><mi>T</mi></math>, the following constraints must hold:</simpara>
<itemizedlist>
<listitem>
<simpara>If mode is N4JS, the type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math> of the expression must be a number</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>UnaryExpression</mtext></mstyle><mo></mo><mstyle mathvariant="monospace"><mtext>Expression</mtext></mstyle><mi>:</mi><mstyle mathvariant="monospace"><mtext>number</mtext></mstyle></mrow></mfrac></math>
</listitem>
<listitem>
<simpara>If <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo>=</mo><mi>P</mi><mi>r</mi><mi>o</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>y</mi><mi>A</mi><mi>c</mi><mi>c</mi><mi>e</mi><mi>s</mi><mi>s</mi><mspace width="0.278em"/><mi>p</mi><mi>a</mi><mfenced close=")" open="("><mi>p</mi></mfenced><mo></mo><mi>p</mi><mi>a</mi><mo>.</mo><mi>i</mi><mi>s</mi><mi>D</mi><mi>o</mi><mi>t</mi><mi>A</mi><mi>c</mi><mi>c</mi><mi>e</mi><mi>s</mi><mi>s</mi></math> <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo></math> both <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>g</mi><mi>e</mi><mi>t</mi></math> <emphasis>p</emphasis> and <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>e</mi><mi>t</mi></math> <emphasis>p</emphasis> must be defined.</simpara>
</listitem>
</itemizedlist>
</requirement>
<requirement xml:id="IDE-108">
<title>Unary Plus/Minus/Bitwise Not Operator Constraints</title>
<simpara>
<anchor xml:id="Req-IDE-108" xreflabel="[Req-IDE-108]"/>
<emphasis role="strong">Requirement: IDE-108:</emphasis>
<link linkend="Req-IDE-108">Unary Plus/Minus/Bitwise Not Operator Constraints</link> (ver. 1)</simpara>
<simpara>
For a given unary expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi></math> <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi></math> with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi><mo>.</mo><mi>o</mi><mi>p</mi><mo></mo><mfenced close="}" open="{"><mo>+</mo><mo>-</mo><mo></mo></mfenced></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>:</mi><mi>T</mi></math>, the following constraints must hold:</simpara>
<itemizedlist>
<listitem>
<simpara>In N4JS mode, the type T of the expression must be a number:</simpara>
</listitem>
</itemizedlist>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>UnaryExpression</mtext></mstyle><mo></mo><mstyle mathvariant="monospace"><mtext>Expression</mtext></mstyle><mi>:</mi><mstyle mathvariant="monospace"><mtext>number</mtext></mstyle></mrow></mfrac></math>
</requirement>
<requirement xml:id="IDE-109">
<title>Logical Not Operator Constraints</title>
<simpara>
<anchor xml:id="Req-IDE-109" xreflabel="[Req-IDE-109]"/>
<emphasis role="strong">Requirement: IDE-109:</emphasis>
<link linkend="Req-IDE-109">Logical Not Operator Constraints</link> (ver. 1)</simpara>
<simpara>
There are no specific constraints defined for with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi><mo>.</mo><mi>o</mi><mi>p</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>!</mtext></mstyle></math>.</simpara>
</requirement>
<bridgehead xml:id="type-inference-8" renderas="sect4">Type Inference</bridgehead>
<simpara>The following operators have fixed types independent of their operand types:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>’delete’</mtext></mstyle><mi> </mi><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>boolean</mtext></mstyle></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>’void’</mtext></mstyle><mi> </mi><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>undefined</mtext></mstyle></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>’typeof’</mtext></mstyle><mi> </mi><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>string</mtext></mstyle></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>(’++’—’–’—’+’—’-’—’ ’)</mtext></mstyle><mi> </mi><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>number</mtext></mstyle></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>’!’</mtext></mstyle><mi> </mi><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>boolean</mtext></mstyle></mrow></mfrac></math></simpara>
</section>
<section xml:id="_multiplicative-expression">
<title>Multiplicative Expression</title>
<bridgehead xml:id="multiplicative-expression-syntax" renderas="sect4">Syntax</bridgehead>
<simpara>Cf. [<link linkend="ECMA11a">ECMA11a(p.p.73ff)</link>]</simpara>
<programlisting language="xtext" linenumbering="unnumbered">MultiplicativeExpression returns Expression: UnaryExpression
(=&gt;({MultiplicativeExpression.lhs=current} op=MultiplicativeOperator) rhs=UnaryExpression)*;
enum MultiplicativeOperator: times='*' | div='/' | mod='%';</programlisting>
<bridgehead xml:id="multiplicative-expression-semantics" renderas="sect4">Semantics</bridgehead>
<requirement xml:id="IDE-110">
<title>Multiplicative Expression Constraints</title>
<simpara>
<anchor xml:id="Req-IDE-110" xreflabel="[Req-IDE-110]"/>
<emphasis role="strong">Requirement: IDE-110:</emphasis>
<link linkend="Req-IDE-110">Multiplicative Expression Constraints</link> (ver. 1)</simpara>
<simpara>
For a given multiplicative expression the following constraints must hold in N4JS mode :</simpara>
<itemizedlist>
<listitem>
<simpara>The types of the operands may be any type:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>MultiplicativeExpression</mtext></mstyle><mo></mo><mstyle mathvariant="monospace"><mtext>Expression</mtext></mstyle><mi>:</mi><mstyle mathvariant="monospace"><mtext>any</mtext></mstyle></mrow></mfrac><mrow/></math>
</listitem>
</itemizedlist>
</requirement>
<simpara>If a non-numeric operand is used, the result may be <literal>NaN</literal> which actually is a number as well.</simpara>
<bridgehead xml:id="_type-inference-6" renderas="sect4">Type Inference</bridgehead>
<literallayout class="monospaced">[[type-inference-9]]</literallayout>
<simpara>The inferred type of a multiplicative expression always is number:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>MultiplicativeExpression</mtext></mstyle><mi>:</mi><mstyle mathvariant="monospace"><mtext>number</mtext></mstyle></mrow></mfrac></math>
</section>
<section xml:id="_additive-expression">
<title>Additive Expression</title>
<bridgehead xml:id="additive-expression-syntax" renderas="sect4">Syntax</bridgehead>
<simpara>Cf. [<link linkend="ECMA11a">ECMA11a(p.p.75ff)</link>]</simpara>
<programlisting language="xtext" linenumbering="unnumbered">AdditiveExpression returns Expression: MultiplicativeExpression
(=&gt;({AdditiveExpression.lhs=current} op=AdditiveOperator) rhs=MultiplicativeExpression)*;
enum AdditiveOperator: add='+' | sub='-';</programlisting>
<bridgehead xml:id="additive-expression-semantics" renderas="sect4">Semantics</bridgehead>
<requirement xml:id="IDE-111">
<title>Additive Expression Constraints</title>
<simpara>
<anchor xml:id="Req-IDE-111" xreflabel="[Req-IDE-111]"/>
<emphasis role="strong">Requirement: IDE-111:</emphasis>
<link linkend="Req-IDE-111">Additive Expression Constraints</link> (ver. 1)</simpara>
<simpara>
</simpara>
<simpara>For a given additive expression the following constraints must hold in N4JS mode:</simpara>
<itemizedlist>
<listitem>
<simpara>The type of the operand can be any type:</simpara>
</listitem>
</itemizedlist>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>AdditiveExpression</mtext></mstyle><mi> </mi><mi>e</mi><mo></mo><mstyle mathvariant="monospace"><mtext>Expression</mtext></mstyle><mi>:</mi><mstyle mathvariant="monospace"><mtext>any</mtext></mstyle></mrow></mfrac></mtd></mtr></mtable></math>
</requirement>
<simpara>In JavaScript it is possible to subtract two non-numerics, leading to <literal>NaN</literal>. Also <literal>undefined</literal> or <literal>null</literal> may be used. The real difference is what type is to be returned (string or number, see below).</simpara>
<section xml:id="type-inference-10">
<title>Type Inference</title>
<simpara role="language-n4js">The type of an additive expression is usually inferred to <literal>number</literal>, except for addition which may lead to string as well.
The result for the addition operator is only be a number if both operands are numbers, booleans, null, or undefined.
Using <literal>undefined</literal> in an additive expression leads to <literal>NaN</literal> which actually is a number from the type system&#8217;s point of view. Additional analysis may create errors in the latter case though.</simpara>
<simpara>We first define two helper rules to simplify the addition operator condition:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mfrac><mrow><mo></mo><mi>N</mi><mi>i</mi><mi>n</mi><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>number, int, boolean, null, undefined</mtext></mstyle></mfenced><mi>:</mi><mi>T</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mo>=</mo><mi>N</mi></mrow><mrow><mi>n</mi><mi>b</mi><mfenced close=")" open="("><mi>T</mi></mfenced></mrow></mfrac><mstyle mathvariant="monospace"><mtext>nb</mtext></mstyle></mtd></mtr><mtr><mtd><mfrac><mrow><mi>n</mi><mi>b</mi><mfenced close=")" open="("><mi>T</mi></mfenced><mo></mo><mi> </mi><mrow><mo>(</mo><mi>μ</mi><mfenced close=")" open="("><mi>T</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Union</mtext></mstyle><mi> </mi><mo></mo><mi> </mi><mo></mo><mi> </mi><mi>E</mi><mo></mo><mi>T</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>s</mi><mi>:</mi><mi>m</mi><mi>n</mi><mi>b</mi><mfenced close=")" open="("><mi>E</mi></mfenced></mrow></mrow><mrow><mi>m</mi><mi>n</mi><mi>b</mi><mfenced close=")" open="("><mi>T</mi></mfenced></mrow></mfrac><mstyle mathvariant="monospace"><mtext>mnb</mtext></mstyle></mtd></mtr><mtr><mtd><mfrac><mrow><mi>Γ</mi><mo></mo><mi>e</mi><mo>.</mo><mi>l</mi><mi>h</mi><mi>s</mi><mi>:</mi><mi>L</mi><mspace width="3.0mm"/><mi>Γ</mi><mo></mo><mi>e</mi><mo>.</mo><mi>r</mi><mi>h</mi><mi>s</mi><mi>:</mi><mi>R</mi><mspace width="3.0mm"/><mi>n</mi><mi>b</mi><mfenced close=")" open="("><mi>L</mi></mfenced><mspace width="3.0mm"/><mi>n</mi><mi>b</mi><mfenced close=")" open="("><mi>R</mi></mfenced></mrow><mrow><mi>t</mi><mi>o</mi><mi>N</mi><mi>u</mi><mi>m</mi><mfenced close=")" open="("><mrow><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi></mrow></mfenced></mrow></mfrac><mstyle mathvariant="monospace"><mtext>toNum</mtext></mstyle></mtd></mtr><mtr><mtd><mfrac><mrow><mi>Γ</mi><mo></mo><mi>e</mi><mo>.</mo><mi>l</mi><mi>h</mi><mi>s</mi><mi>:</mi><mi>L</mi><mspace width="3.0mm"/><mi>Γ</mi><mo></mo><mi>e</mi><mo>.</mo><mi>r</mi><mi>h</mi><mi>s</mi><mi>:</mi><mi>R</mi><mspace width="3.0mm"/><mi>m</mi><mi>n</mi><mi>b</mi><mfenced close=")" open="("><mi>L</mi></mfenced><mspace width="3.0mm"/><mi>m</mi><mi>n</mi><mi>b</mi><mfenced close=")" open="("><mi>R</mi></mfenced></mrow><mrow><mi>m</mi><mi>a</mi><mi>y</mi><mi>N</mi><mi>u</mi><mi>m</mi><mfenced close=")" open="("><mrow><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi></mrow></mfenced></mrow></mfrac><mstyle mathvariant="monospace"><mtext>mayNum</mtext></mstyle></mtd></mtr></mtable></math>
<simpara>The type of an additive expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi></math> is inferred as follows:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mfrac><mrow><mi>e</mi><mo>.</mo><mi>o</mi><mi>p</mi><msup><mo>=</mo><mi>'</mi></msup><msup><mo>+</mo><mi>'</mi></msup><mspace width="3.0mm"/><mo>¬</mo><mi>t</mi><mi>o</mi><mi>N</mi><mi>u</mi><mi>m</mi><mfenced close=")" open="("><mi>e</mi></mfenced><mspace width="3.0mm"/><mo>¬</mo><mi>m</mi><mi>a</mi><mi>y</mi><mi>N</mi><mi>u</mi><mi>m</mi><mfenced close=")" open="("><mi>e</mi></mfenced></mrow><mrow><mi>Γ</mi><mo></mo><mi>e</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>string</mtext></mstyle></mrow></mfrac></mtd></mtr><mtr><mtd><mfrac><mrow><mi>e</mi><mo>.</mo><mi>o</mi><mi>p</mi><msup><mo>=</mo><mi>'</mi></msup><msup><mo>+</mo><mi>'</mi></msup><mspace width="3.0mm"/><mo>¬</mo><mi>t</mi><mi>o</mi><mi>N</mi><mi>u</mi><mi>m</mi><mfenced close=")" open="("><mi>e</mi></mfenced><mspace width="3.0mm"/><mi>m</mi><mi>a</mi><mi>y</mi><mi>N</mi><mi>u</mi><mi>m</mi><mfenced close=")" open="("><mi>e</mi></mfenced></mrow><mrow><mi>Γ</mi><mo></mo><mi>e</mi><mi>:</mi><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><mi>n</mi><mi>u</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mo>,</mo><mi>s</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>g</mi></mrow></mfenced></mrow></mfrac></mtd></mtr><mtr><mtd><mfrac><mrow><mi>e</mi><mo>.</mo><mi>o</mi><mi>p</mi><msup><mo>=</mo><mi>'</mi></msup><msup><mo>+</mo><mi>'</mi></msup><mspace width="3.0mm"/><mi>t</mi><mi>o</mi><mi>N</mi><mi>u</mi><mi>m</mi><mfenced close=")" open="("><mi>e</mi></mfenced></mrow><mrow><mi>Γ</mi><mo></mo><mi>e</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>number</mtext></mstyle></mrow></mfrac></mtd></mtr><mtr><mtd><mfrac><mrow><mi>e</mi><mo>.</mo><mi>o</mi><mi>p</mi><msup><mo></mo><mi>'</mi></msup><msup><mo>+</mo><mi>'</mi></msup></mrow><mrow><mi>Γ</mi><mo></mo><mi>e</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>number</mtext></mstyle></mrow></mfrac></mtd></mtr></mtable></math>
<simpara>That is, if both operands are number, int, boolean, null, or even undefined, then the 'plus' is interpreted as
mathematical addition and the result is a number. In other cases the 'plus' is interpreted as string concatenation and the result is a string. In case of union types, the result may be a union of number and string.</simpara>
<simpara>Adding two integers (int) leads to a number, since the result may not be represented as an (JavaScript) int anymore.</simpara>
<example>
<title>Type of addition expression</title>
<programlisting language="xtext" linenumbering="unnumbered">1+2; // number 3
"1"+"2"; // string "12"
"1"+2; // string "12"
1+true; // number 2
false+1; // number 1
"1"+true; // string "1true"
"1"+null; // string "1null"
1+null; // number 1
1+undefined; // number NaN
"1"+undefined; // string "1undefined"</programlisting>
</example>
<simpara>Support new <literal>Symbol.toPrimitive</literal>.</simpara>
</section>
</section>
<section xml:id="_bitwise-shift-expression">
<title>Bitwise Shift Expression</title>
<bridgehead xml:id="bitwise-shift-expression-syntax" renderas="sect4">Syntax</bridgehead>
<literallayout class="monospaced">Cf. +[+&lt;&lt;ECMA11a,ECMA11a(p.p.76f)&gt;&gt;+]+</literallayout>
<programlisting language="xtext" linenumbering="unnumbered">ShiftExpression returns Expression: AdditiveExpression
(=&gt;({ShiftExpression.lhs=current} op=ShiftOperator rhs=AdditiveExpression))*
;
ShiftOperator returns ShiftOperator:
'&gt;' '&gt;' '&gt;'? // SHR, USHR
| '&lt;' '&lt;' // SHL
;</programlisting>
<bridgehead xml:id="bitwise-shift-expression-semantics" renderas="sect4">Semantics</bridgehead>
<requirement xml:id="IDE-112">
<title>Bitwise Shift Expression Constraints</title>
<simpara>
<anchor xml:id="Req-IDE-112" xreflabel="[Req-IDE-112]"/>
<emphasis role="strong">Requirement: IDE-112:</emphasis>
<link linkend="Req-IDE-112">Bitwise Shift Expression Constraints</link> (ver. 1)</simpara>
<simpara>
For a given bitwise shift expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi></math> the following constraints must hold in N4JS mode:
* The types of the operands can be any.</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>BitwiseShiftExpression</mtext></mstyle><mi> </mi><mo></mo><mi> </mi><mstyle mathvariant="monospace"><mtext>Expression</mtext></mstyle><mi>:</mi><mstyle mathvariant="monospace"><mtext>any</mtext></mstyle></mrow></mfrac></math>
</requirement>
<bridgehead xml:id="type-inference-11" renderas="sect4">Type Inference</bridgehead>
<simpara>The type returned by a bitwise shift expression is always <literal>number</literal>:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mspace width="3.0mm"/><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mi> </mi><mrow><mo>(</mo><mrow><mstyle mathvariant="monospace"><mtext>Expression (’</mtext></mstyle><mo>&lt;</mo><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>’—’</mtext></mstyle><mo>&gt;</mo><mo>&gt;</mo><mstyle mathvariant="monospace"><mtext>’—’</mtext></mstyle><mo>&gt;</mo><mo>&gt;</mo><mo>&gt;</mo><mstyle mathvariant="monospace"><mtext>’)</mtext> <mtext>Expression</mtext></mstyle><mo>)</mo></mrow><mi>:</mi><mstyle mathvariant="monospace"><mtext>number</mtext></mstyle></mrow></mrow></mfrac><mtext>
</mtext></math>
<simpara>A non-numeric operand is interpreted as 0, except for <literal>true</literal> which is interpreted as <literal>1</literal>; or objects implementing the symbol <literal>toPrimitive</literal>.</simpara>
</section>
<section xml:id="_relational-expression">
<title>Relational Expression</title>
<bridgehead xml:id="relational-expression-syntax" renderas="sect4">Syntax</bridgehead>
<simpara>Cf. [<link linkend="ECMA11a">ECMA11a(p.p.77ff)</link>]</simpara>
<programlisting language="xtext" linenumbering="unnumbered">RelationalExpression returns Expression: ShiftExpression
(=&gt;({RelationalExpression.lhs=current} op=RelationalOperator) rhs=ShiftExpression)*;
RelationalExpressionNoIn returns Expression: ShiftExpression
(=&gt;({RelationalExpression.lhs=current} op=RelationalOperatorNoIn) rhs=ShiftExpression)*;
enum RelationalOperator:
lt='&lt;' | gt='&gt;' | lte='&lt;=' | gte='&gt;=' | instanceof | in;
RelationalOperatorNoIn returns RelationalOperator:
'&lt;' | '&gt;' | '&lt;=' | '&gt;=' | 'instanceof';</programlisting>
<bridgehead xml:id="relational-expression-semantics" renderas="sect4">Semantics</bridgehead>
<requirement xml:id="IDE-113">
<title>Greater/Less (Equals) Operator Constraints</title>
<simpara>
<anchor xml:id="Req-IDE-113" xreflabel="[Req-IDE-113]"/>
<emphasis role="strong">Requirement: IDE-113:</emphasis>
<link linkend="Req-IDE-113">Greater/Less (Equals) Operator Constraints</link> (ver. 1)</simpara>
<simpara>
</simpara>
<simpara>For a given relational expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi></math> with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><mo>.</mo><mi>o</mi><mi>p</mi><mo></mo><mrow><mo>{</mo><mrow><mo>&lt;</mo><mo>,</mo><mo>&gt;</mo><mo>,</mo><mo>&lt;</mo><mo>=</mo><mo>,</mo><mo>&gt;</mo><mo>=</mo><mo>}</mo></mrow></mrow></math> in N4JS mode,
the following constraints must hold:</simpara>
<itemizedlist>
<listitem>
<simpara>The operands must have the same type and the type must be either a number, string, or boolean:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mfrac><mrow><mi>Γ</mi><mo></mo><mi>r</mi><mi>h</mi><mi>s</mi><mi>:</mi><mi>T</mi><mspace width="3.0mm"/><mi>T</mi><mo></mo><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>number,string,boolean</mtext></mstyle></mfenced></mrow><mrow><mi>Γ</mi><mo></mo><mi>l</mi><mi>h</mi><mi>s</mi><mi> </mi><mrow><mo>(</mo><mrow><mi>'</mi><msup><mo>&lt;</mo><mi>'</mi></msup><msup><mo>|</mo><mi>'</mi></msup><mo>&lt;</mo><msup><mo>=</mo><mi>'</mi></msup><msup><mo>|</mo><mi>'</mi></msup><msup><mo>&gt;</mo><mi>'</mi></msup><msup><mo>|</mo><mi>'</mi></msup><mo>&gt;</mo><msup><mo>=</mo><mi>'</mi></msup><mo>)</mo></mrow><mi>r</mi><mi>h</mi><mi>s</mi><mi> </mi><mo></mo><mi> </mi><mi>l</mi><mi>h</mi><mi>s</mi><mi>:</mi><mi>T</mi></mrow></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mfrac><mrow><mi>Γ</mi><mo></mo><mi>r</mi><mi>h</mi><mi>s</mi><mi>:</mi><mi>O</mi><mspace width="3.0mm"/><mi>O</mi><mo></mo><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>number,string,boolean</mtext></mstyle></mfenced><mspace width="3.0mm"/><mi>T</mi><mo>=</mo><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>number,string,boolean</mtext></mstyle></mfenced></mrow><mrow><mi>Γ</mi><mo></mo><mi>l</mi><mi>h</mi><mi>s</mi><mi> </mi><mrow><mo>(</mo><mrow><mi>'</mi><msup><mo>&lt;</mo><mi>'</mi></msup><msup><mo>|</mo><mi>'</mi></msup><mo>&lt;</mo><msup><mo>=</mo><mi>'</mi></msup><msup><mo>|</mo><mi>'</mi></msup><msup><mo>&gt;</mo><mi>'</mi></msup><msup><mo>|</mo><mi>'</mi></msup><mo>&gt;</mo><msup><mo>=</mo><mi>'</mi></msup><mo>)</mo></mrow><mi>r</mi><mi>h</mi><mi>s</mi><mi> </mi><mo></mo><mi> </mi><mi>l</mi><mi>h</mi><mi>s</mi><mi>:</mi><mi>T</mi></mrow></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mfrac><mrow><mi>Γ</mi><mo></mo><mi>l</mi><mi>h</mi><mi>s</mi><mi>:</mi><mi>T</mi><mspace width="3.0mm"/><mi>T</mi><mo></mo><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>number,string,boolean</mtext></mstyle></mfenced></mrow><mrow><mi>Γ</mi><mo></mo><mi>l</mi><mi>h</mi><mi>s</mi><mi> </mi><mrow><mo>(</mo><mrow><mi>'</mi><msup><mo>&lt;</mo><mi>'</mi></msup><msup><mo>|</mo><mi>'</mi></msup><mo>&lt;</mo><msup><mo>=</mo><mi>'</mi></msup><msup><mo>|</mo><mi>'</mi></msup><msup><mo>&gt;</mo><mi>'</mi></msup><msup><mo>|</mo><mi>'</mi></msup><mo>&gt;</mo><msup><mo>=</mo><mi>'</mi></msup><mo>)</mo></mrow><mi>r</mi><mi>h</mi><mi>s</mi><mi> </mi><mo></mo><mi> </mi><mi>r</mi><mi>h</mi><mi>s</mi><mi>:</mi><mi>T</mi></mrow></mrow></mfrac></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mfrac><mrow><mi>Γ</mi><mo></mo><mi>l</mi><mi>h</mi><mi>s</mi><mi>:</mi><mi>O</mi><mspace width="3.0mm"/><mi>O</mi><mo></mo><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>number,string,boolean</mtext></mstyle></mfenced><mspace width="3.0mm"/><mi>T</mi><mo>=</mo><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>number,string,boolean</mtext></mstyle></mfenced></mrow><mrow><mi>Γ</mi><mo></mo><mi>l</mi><mi>h</mi><mi>s</mi><mi> </mi><mrow><mo>(</mo><mrow><mi>'</mi><msup><mo>&lt;</mo><mi>'</mi></msup><msup><mo>|</mo><mi>'</mi></msup><mo>&lt;</mo><msup><mo>=</mo><mi>'</mi></msup><msup><mo>|</mo><mi>'</mi></msup><msup><mo>&gt;</mo><mi>'</mi></msup><msup><mo>|</mo><mi>'</mi></msup><mo>&gt;</mo><msup><mo>=</mo><mi>'</mi></msup><mo>)</mo></mrow><mi>r</mi><mi>h</mi><mi>s</mi><mi> </mi><mo></mo><mi> </mi><mi>r</mi><mi>h</mi><mi>s</mi><mi>:</mi><mi>T</mi></mrow></mrow></mfrac></math></simpara>
</listitem>
</itemizedlist>
</requirement>
<requirement xml:id="IDE-114">
<title>Instanceof Operator Constraints</title>
<simpara>
<anchor xml:id="Req-IDE-114" xreflabel="[Req-IDE-114]"/>
<emphasis role="strong">Requirement: IDE-114:</emphasis>
<link linkend="Req-IDE-114">Instanceof Operator Constraints</link> (ver. 1)</simpara>
<simpara>
For a given relational expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi></math> with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><mo>.</mo><mi>o</mi><mi>p</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>instanceof</mtext></mstyle></math>, the following constraints must hold:</simpara>
<itemizedlist>
<listitem>
<simpara>The right operand of the instanceof operator must be a <literal>Function</literal> <footnote><simpara>Only <literal role="language-n4js">Function</literal> implements the ECMAScript specification property <literal role="language-n4js">hasInstance</literal>. Thus instanceof expressions are rewritten by the compiler for other types. Note that a reference to a class returns the constructor type, which actually is a function itself.</simpara></footnote></simpara>
</listitem>
</itemizedlist>
<simpara>In other words,</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mi>l</mi><mi>h</mi><mi>s</mi><mi> </mi><mstyle mathvariant="monospace"><mtext>’instanceof’</mtext></mstyle><mi> </mi><mi>r</mi><mi>h</mi><mi>s</mi><mi> </mi><mo></mo><mi> </mi><mi>r</mi><mi>h</mi><mi>s</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>type</mtext></mstyle><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>Class</mtext></mstyle></mfenced></mrow></mfrac><mrow/></math>
<simpara>is contained in the the first type rule, an object type reference <footnote><simpara>Includes interfaces, since an interface type reference is a subtype of object type reference: <math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>type</mtext></mstyle><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>Interface</mtext></mstyle></mfenced><mo>&lt;</mo><mi>:</mi><mstyle mathvariant="monospace"><mtext>type</mtext></mstyle><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>Object</mtext></mstyle></mfenced></math></simpara></footnote>
or an enum type reference.</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mspace width="3.0mm"/><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mi>l</mi><mi>h</mi><mi>s</mi><mi> </mi><mstyle mathvariant="monospace"><mtext>’instanceof’</mtext></mstyle><mi> </mi><mi>r</mi><mi>h</mi><mi>s</mi><mi> </mi><mo></mo><mi> </mi><mi>r</mi><mi>h</mi><mi>s</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>Function</mtext></mstyle></mrow></mfrac></mtd></mtr><mtr><mtd><mspace width="3.0mm"/><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mi>l</mi><mi>h</mi><mi>s</mi><mi> </mi><mstyle mathvariant="monospace"><mtext>’instanceof’</mtext></mstyle><mi> </mi><mi>r</mi><mi>h</mi><mi>s</mi><mi> </mi><mo></mo><mi> </mi><mi>r</mi><mi>h</mi><mi>s</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>type</mtext></mstyle><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>Object</mtext></mstyle></mfenced></mrow></mfrac></mtd></mtr><mtr><mtd><mspace width="3.0mm"/><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mi>l</mi><mi>h</mi><mi>s</mi><mi> </mi><mstyle mathvariant="monospace"><mtext>’instanceof’</mtext></mstyle><mi> </mi><mi>r</mi><mi>h</mi><mi>s</mi><mi> </mi><mo></mo><mi> </mi><mi>r</mi><mi>h</mi><mi>s</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>type</mtext></mstyle><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>N4Enum</mtext></mstyle></mfenced></mrow></mfrac></mtd></mtr></mtable></math>
<simpara>The type of a definition site structural classifier <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi></math> is not of type <literal>C</literal>.
Thus, the <literal>instanceof</literal> operator cannot be used for structural types.
Use-site structural typing is also not possible since <literal>~</literal> would be interpreted (by the parser) as a binary operator.</simpara>
</requirement>
<requirement xml:id="IDE-115">
<title>Operator Constraints</title>
<simpara>
<anchor xml:id="Req-IDE-115" xreflabel="[Req-IDE-115]"/>
<emphasis role="strong">Requirement: IDE-115:</emphasis>
<link linkend="Req-IDE-115">Operator Constraints</link> (ver. 1)</simpara>
<simpara>
</simpara>
<simpara>For a given relational expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi></math> with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><mo>.</mo><mi>o</mi><mi>p</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>in</mtext></mstyle></math>, the following constraints must hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>The right operand of the in operator must be an <literal>Object</literal>:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mspace width="3.0mm"/><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mi>l</mi><mi>h</mi><mi>s</mi><mi> </mi><mstyle mathvariant="monospace"><mtext>’in’</mtext></mstyle><mi> </mi><mi>r</mi><mi>h</mi><mi>s</mi><mi> </mi><mo></mo><mi> </mi><mi>r</mi><mi>h</mi><mi>s</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>Object</mtext></mstyle></mrow></mfrac></math>
</listitem>
<listitem>
<simpara>In N4JS mode, the left operand is restricted to be of type <literal>string</literal> or <literal>number</literal>:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mspace width="3.0mm"/><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mi>l</mi><mi>h</mi><mi>s</mi><mi> </mi><mstyle mathvariant="monospace"><mtext>’in’</mtext></mstyle><mi> </mi><mi>r</mi><mi>h</mi><mi>s</mi><mi> </mi><mo></mo><mi> </mi><mi>l</mi><mi>h</mi><mi>s</mi><mi>:</mi><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>string,number</mtext></mstyle></mfenced></mrow></mfrac></math>
</listitem>
</orderedlist>
</requirement>
<simpara>A special feature of N4JS is support for interface type references in combination with the <literal>instance of</literal> operator.
The compiler rewrites the code to make this work.</simpara>
<example>
<title><literal>instanceof</literal> with Interface</title>
<simpara>The following example demonstrates the use of the operator with an interface.
This is, of course, not working in pure ECMAScript.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">interface I {}
class A implements I {}
class B extends A {}
class C {}
function f(name: string, p: any) {
if (p instanceof I) {
console.log(name + " is instance of I");
}
}
f("A", new A())
f("B", new B())
f("C", new C())</programlisting>
<simpara>This will print out</simpara>
<programlisting language="n4js" linenumbering="unnumbered">A is instance of I
B is instance of I</programlisting>
</example>
<bridgehead xml:id="type-inference-12" renderas="sect4">Type Inference</bridgehead>
<simpara>The type of a relational expression always is <literal>boolean</literal>;</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mspace width="3.0mm"/><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mi>l</mi><mi>h</mi><mi>s</mi><mi> </mi><mfenced close=")" open="("><mrow><mstyle mathvariant="monospace"><mtext>’¡’</mtext></mstyle><mo>|</mo><mstyle mathvariant="monospace"><mtext>’¡=’</mtext></mstyle><mo>|</mo><mstyle mathvariant="monospace"><mtext>’¿’</mtext></mstyle><mo>|</mo><mstyle mathvariant="monospace"><mtext>’¿=’</mtext></mstyle><mo>|</mo><mstyle mathvariant="monospace"><mtext>’instanceof’</mtext></mstyle><mo>|</mo><mstyle mathvariant="monospace"><mtext>’in’</mtext></mstyle></mrow></mfenced><mi> </mi><mi>r</mi><mi>h</mi><mi>s</mi><mi> </mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>boolean</mtext></mstyle></mrow></mfrac></math>
</section>
<section xml:id="_equality-expression">
<title>Equality Expression</title>
<bridgehead xml:id="equality-expression-syntax" renderas="sect4">Syntax</bridgehead>
<simpara>Cf. [<link linkend="ECMA11a">ECMA11a(p.p.80ff)</link>]</simpara>
<programlisting language="xtext" linenumbering="unnumbered">EqualityExpression returns Expression: RelationalExpression
(=&gt;({EqualityExpression.lhs=current} op=EqualityOperator) rhs=RelationalExpression)*;
EqualityExpressionNoIn returns Expression: RelationalExpressionNoIn
(=&gt;({EqualityExpression.lhs=current} op=EqualityOperator) rhs=RelationalExpressionNoIn)*;
enum EqualityOperator: same='===' | nsame='!==' | eq='==' | neq='!=';</programlisting>
<bridgehead xml:id="equality-expression-semantics" renderas="sect4">Semantics</bridgehead>
<simpara>There are no hard constraints defined for equality expressions.</simpara>
<simpara>In N4JSmode, a warning is created if for a given expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>l</mi><mi>h</mi><mi>s</mi><mstyle mathvariant="monospace"><mtext>(’===’—’!==’)</mtext></mstyle><mi>r</mi><mi>h</mi><mi>s</mi></math>, neither <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>Γ</mi><mo></mo><mi>l</mi><mi>h</mi><mi>s</mi><mo>.</mo><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mo>&lt;</mo><mi>:</mi><mi>r</mi><mi>h</mi><mi>s</mi><mo>.</mo><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi></math> nor <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>Γ</mi><mo></mo><mi>r</mi><mi>h</mi><mi>s</mi><mo>.</mo><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mo>&lt;</mo><mi>:</mi><mi>l</mi><mi>h</mi><mi>s</mi><mo>.</mo><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi></math>
and no interface or composed type is involved as the result is constant in these cases.</simpara>
<simpara>Note that a warning is only created if the upper bounds do not match the described constraints.
This is necessary for wildcards. For example in</simpara>
<programlisting language="n4js" linenumbering="unnumbered">// with
class A{} class B extends A{}
function isFirst(ar: Array&lt;? extends A&gt;, b: B): boolean {
return b === ar[0]
}</programlisting>
<simpara>the type of array elements is <literal>? extends A</literal>.<?asciidoc-br?>
Neither <math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>? extends A</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>B</mtext></mstyle></math> nor <math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>B</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>? extends A</mtext></mstyle></math> is true.
This is why the upper bounds are to be used.</simpara>
<bridgehead xml:id="type-inference-13" renderas="sect4">Type Inference</bridgehead>
<simpara>The inferred type of an equality expression always is <literal>boolean</literal>.</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mspace width="3.0mm"/><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mi>l</mi><mi>h</mi><mi>s</mi><mi> </mi><mfenced close=")" open="("><mrow><mstyle mathvariant="monospace"><mtext>’==’</mtext></mstyle><mo>|</mo><mstyle mathvariant="monospace"><mtext>’!=’</mtext></mstyle><mo>|</mo><mstyle mathvariant="monospace"><mtext>’===’</mtext></mstyle><mo>|</mo><mstyle mathvariant="monospace"><mtext>’!==’</mtext></mstyle></mrow></mfenced><mi> </mi><mi>r</mi><mi>h</mi><mi>s</mi><mi> </mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>boolean</mtext></mstyle></mrow></mfrac></math>
</section>
<section xml:id="_binary-bitwise-expression">
<title>Binary Bitwise Expression</title>
<bridgehead xml:id="binary-bitwise-expression-syntax" renderas="sect4">Syntax</bridgehead>
<simpara>Cf. [<link linkend="ECMA11a">ECMA11a(p.p.82ff)</link>]</simpara>
<programlisting language="xtext" linenumbering="unnumbered">BitwiseANDExpression returns Expression: EqualityExpression
(=&gt; ({BitwiseANDExpression.lhs=current} '&amp;') rhs=EqualityExpression)*;
BitwiseANDExpressionNoIn returns Expression: EqualityExpressionNoIn
(=&gt; ({BitwiseANDExpression.lhs=current} '&amp;') rhs=EqualityExpressionNoIn)*;
BitwiseXORExpression returns Expression: BitwiseANDExpression
(=&gt; ({BitwiseXORExpression.lhs=current} '^') rhs=BitwiseANDExpression)*;
BitwiseXORExpressionNoIn returns Expression: BitwiseANDExpressionNoIn
(=&gt; ({BitwiseXORExpression.lhs=current} '^') rhs=BitwiseANDExpressionNoIn)*;
BitwiseORExpression returns Expression: BitwiseXORExpression
(=&gt; ({BitwiseORExpression.lhs=current} '|') rhs=BitwiseXORExpression)*;
BitwiseORExpressionNoIn returns Expression: BitwiseXORExpressionNoIn
(=&gt; ({BitwiseORExpression.lhs=current} '|') rhs=BitwiseXORExpressionNoIn)*;</programlisting>
<bridgehead xml:id="binary-bitwise-expression-semantics" renderas="sect4">Semantics</bridgehead>
<requirement xml:id="IDE-116">
<title>Bitwise Bitwise Expression Constraints</title>
<simpara>
<anchor xml:id="Req-IDE-116" xreflabel="[Req-IDE-116]"/>
<emphasis role="strong">Requirement: IDE-116:</emphasis>
<link linkend="Req-IDE-116">Bitwise Bitwise Expression Constraints</link> (ver. 1)</simpara>
<simpara>
For a given bitwise bitwise expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi></math> the following constraints must hold in N4JS mode:</simpara>
<itemizedlist>
<listitem>
<simpara>The types of the operands must be both number.</simpara>
</listitem>
</itemizedlist>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>BitwiseBitwiseExpression</mtext></mstyle><mi> </mi><mo></mo><mi> </mi><mstyle mathvariant="monospace"><mtext>Expression</mtext></mstyle><mi>:</mi><mstyle mathvariant="monospace"><mtext>number</mtext></mstyle></mrow></mfrac></math>
</requirement>
<bridgehead xml:id="type-inference-14" renderas="sect4">Type Inference</bridgehead>
<simpara>The type returned by a binary bitwise expression is always <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi><mi>u</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi></math>:</simpara>
</section>
<section xml:id="_binary-logical-expression">
<title>Binary Logical Expression</title>
<bridgehead xml:id="binary-logical-expression-syntax" renderas="sect4">Syntax</bridgehead>
<programlisting language="xtext" linenumbering="unnumbered">LogicalANDExpression returns Expression: BitwiseORExpression
(=&gt; ({LogicalANDExpression.lhs=current} '&amp;&amp;') rhs=BitwiseORExpression)*;
LogicalANDExpressionNoIn returns Expression: BitwiseORExpressionNoIn
(=&gt; ({LogicalANDExpression.lhs=current} '&amp;&amp;') rhs=BitwiseORExpressionNoIn)*;
LogicalORExpression returns Expression: LogicalANDExpression
(=&gt; ({LogicalORExpression.lhs=current} '||') rhs=LogicalANDExpression)*;
LogicalORExpressionNoIn returns Expression: LogicalANDExpressionNoIn
(=&gt; ({LogicalORExpression.lhs=current} '||') rhs=LogicalANDExpressionNoIn)*;</programlisting>
<bridgehead xml:id="binary-logical-expression-semantics" renderas="sect4">Semantics</bridgehead>
<requirement xml:id="IDE-117">
<title>Binary Logical Expression Constraints</title>
<simpara>
<anchor xml:id="Req-IDE-117" xreflabel="[Req-IDE-117]"/>
<emphasis role="strong">Requirement: IDE-117:</emphasis>
<link linkend="Req-IDE-117">Binary Logical Expression Constraints</link> (ver. 1)</simpara>
<simpara>
For a given binary logical expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi></math> with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><mo>.</mo><mi>l</mi><mi>h</mi><mi>s</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>:</mi><mi>L</mi></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><mo>.</mo><mi>r</mi><mi>h</mi><mi>s</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>:</mi><mi>R</mi></math> the following constraints must hold:</simpara>
<itemizedlist>
<listitem>
<simpara>In N4JS mode <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>L</mi></math> must not be <literal>undefined</literal> or <literal>null</literal>.</simpara>
</listitem>
</itemizedlist>
</requirement>
<bridgehead xml:id="type-inference-15" renderas="sect4">Type Inference</bridgehead>
<simpara>The evaluation relies on ECMAScript’s abstract operation <literal>ToBoolean</literal> [<link linkend="ECMA11a">ECMA11a(p.p.43)</link>].
A short-circuit evaluation strategy is used so that depending on the types of the operands, different result types may be inferred.
In particular, the inferred type usually is not <literal>boolean</literal> ((cf. [<link linkend="ECMA11a">ECMA11a(p.S11.11., p.p.83ff)</link>] ).
The type inference does not take this short-circuit evaluation strategy into account, as it will affect the result in case one of the types is <literal>null</literal>
either or <literal>undefined</literal>, which is not allowed in N4JS mode.</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mi>l</mi><mi>h</mi><mi>s</mi><mstyle mathvariant="monospace"><mtext></mtext></mstyle><mi>&amp;</mi><mi>&amp;</mi><mstyle mathvariant="monospace"><mtext>’—’——’</mtext></mstyle><mi>r</mi><mi>h</mi><mi>s</mi><mi>:</mi><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><mi>Γ</mi><mo></mo><mi>l</mi><mi>h</mi><mi>s</mi></mrow><mrow><mi>Γ</mi><mo></mo><mi>r</mi><mi>h</mi><mi>s</mi></mrow></mfenced></mrow></mfrac></math>
</section>
<section xml:id="_conditional-expression">
<title>Conditional Expression</title>
<bridgehead xml:id="conditional-expression-syntax" renderas="sect4">Syntax</bridgehead>
<simpara>Cf. [<link linkend="ECMA11a">ECMA11a(p.S11.12, p.p.84)</link>]</simpara>
<programlisting language="xtext" linenumbering="unnumbered">ConditionalExpression returns Expression: LogicalORExpression
(=&gt; ({ConditionalExpression.expression=current} '?') trueExpression=AssignmentExpression ':' falseExpression=AssignmentExpression)?;
ConditionalExpressionNoIn returns Expression: LogicalORExpressionNoIn
(=&gt; ({ConditionalExpression.expression=current} '?') trueExpression=AssignmentExpression ':' falseExpression=AssignmentExpressionNoIn)?;</programlisting>
<bridgehead xml:id="conditional-expression-semantics" renderas="sect4">Semantics</bridgehead>
<requirement xml:id="IDE-118">
<title>Conditional Expression Constraints</title>
<simpara>
<anchor xml:id="Req-IDE-118" xreflabel="[Req-IDE-118]"/>
<emphasis role="strong">Requirement: IDE-118:</emphasis>
<link linkend="Req-IDE-118">Conditional Expression Constraints</link> (ver. 1)</simpara>
<simpara>
For a given conditional expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi></math> with</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>:</mi><mi>C</mi><mo>,</mo></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><mo>.</mo><mi>t</mi><mi>r</mi><mi>u</mi><mi>e</mi><mi>E</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>:</mi><mi>T</mi><mo>,</mo></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><mo>.</mo><mi>f</mi><mi>a</mi><mi>l</mi><mi>s</mi><mi>e</mi><mo>-</mo><mi>E</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>:</mi><mi>F</mi></math></simpara>
<simpara>the following constraints must hold:</simpara>
<itemizedlist>
<listitem>
<simpara>A warning will be issued in N4JSmode if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi></math> evaluates to a constant value.
That is to say<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo></mo><mfenced close="}" open="{"><mrow><mi>f</mi><mi>a</mi><mi>l</mi><mi>s</mi><mi>e</mi></mrow><mrow><mi>t</mi><mi>r</mi><mi>u</mi><mi>e</mi></mrow><mrow><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi></mrow><mrow><mi>u</mi><mi>n</mi><mi>d</mi><mi>e</mi><mi>f</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>d</mi></mrow></mfenced></math> or
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi><mo></mo><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>void</mtext></mstyle><mstyle mathvariant="monospace"><mtext>undefined</mtext></mstyle></mfenced></math>.</simpara>
</listitem>
</itemizedlist>
<simpara>There are no specific constraints defined for the condition.
The ECMAScript operation <literal>ToBoolean</literal> [<link linkend="ECMA11a">ECMA11a(p.S9.2, p.p.43)</link>] is used to convert any type to boolean.</simpara>
</requirement>
<bridgehead xml:id="type-inference-16" renderas="sect4">Type Inference</bridgehead>
<simpara>The inferred type of a conditional expression is the union of the true and false expression (cf. [<link linkend="ECMA11a">ECMA11a(p.S11.12, p.p.84)</link>] ():</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>T</mi><mo>=</mo><mi>u</mi><mi>n</mi><mi>i</mi><mi>o</mi><mi>n</mi><mfenced close="}" open="{"><mrow><mi>Γ</mi><mo></mo><mi>e</mi><mi>t</mi><mo>,</mo><mi>Γ</mi><mo></mo><mi>e</mi><mi>f</mi></mrow></mfenced></mrow><mrow><mi>Γ</mi><mo></mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>d</mi><mi> </mi><mstyle mathvariant="monospace"><mtext>’?’</mtext></mstyle><mi>e</mi><mi>t</mi><mi> </mi><mstyle mathvariant="monospace"><mtext>’:’</mtext></mstyle><mi>e</mi><mi>f</mi><mi>:</mi><mi>T</mi></mrow></mfrac></math>
<example>
<title>Type of Conditional Expressions</title>
<simpara>Given the following declarations:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class A{} class B extends A{}
class C{} class D extends A{}
class G&lt;T&gt; { field: T; }
var ga: G&lt;A&gt;, gb: G&lt;B&gt;;
a: A, b: B, c: C, d: D;
var boolean cond;</programlisting>
<simpara>Then the type of the following conditional expression is inferred as noted in the comments:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">cond ? a : a; // A
cond ? a : b; // union{A,B}
cond ? a : c; // union{A,C}
cond ? b : d; // union{B,D}
cond ? (cond ? a : b) : (cond ? c : d); // union{A,B,C,D}
cond ? (cond ? a : b) : (cond ? b : d); // union{A,B,D}
cond ? ga : gb; // union{G&lt;A&gt;,G&lt;B&gt;}</programlisting>
</example>
</section>
<section xml:id="_assignment-expression">
<title>Assignment Expression</title>
<bridgehead xml:id="assignment-expression-syntax" renderas="sect4">Syntax</bridgehead>
<programlisting language="xtext" linenumbering="unnumbered">AssignmentExpression &lt;In, Yield&gt;:
lhs=Expression op=AssignmentOperator rhs=AssignmentExpression&lt;In,Yield&gt;
;
AssignmentOperator:
'='
| '*=' | '/=' | '%=' | '+=' | '-='
| '&lt;&lt;=' | '&gt;&gt;=' | '&gt;&gt;&gt;='
| '&amp;=' | '^=' | '|='
;</programlisting>
<bridgehead xml:id="assignment-expression-semantics" renderas="sect4">Semantics</bridgehead>
<requirement xml:id="IDE-119">
<title>Simple Assignment</title>
<simpara>
<anchor xml:id="Req-IDE-119" xreflabel="[Req-IDE-119]"/>
<emphasis role="strong">Requirement: IDE-119:</emphasis>
<link linkend="Req-IDE-119">Simple Assignment</link> (ver. 1)</simpara>
<simpara>
For a given assignment <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi></math> with</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mo>.</mo><mi>o</mi><mi>p</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>’=’</mtext></mstyle></math></simpara>
<simpara>the following constraints must hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>[</mo><mspace width="-0.167em"/><mrow><mo>[</mo></mrow></mrow><mrow><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mo>.</mo><mi>l</mi><mi>h</mi><mi>s</mi></mrow><mrow><mrow><mo>]</mo></mrow><mspace width="-0.167em"/><mo>]</mo></mrow><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mrow><mo>[</mo><mspace width="-0.167em"/><mrow><mo>[</mo></mrow></mrow><mrow><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mo>.</mo><mi>r</mi><mi>h</mi><mi>s</mi></mrow><mrow><mrow><mo>]</mo></mrow><mspace width="-0.167em"/><mo>]</mo></mrow></math></simpara>
<simpara>In the following inference rule and the constraint, ’@’ is to be replaced with the right part of one of the assignment operators listed above, that is,</simpara>
</listitem>
</orderedlist>
</requirement>
<requirement xml:id="IDE-120">
<title>Compound Assignment</title>
<simpara>
<anchor xml:id="Req-IDE-120" xreflabel="[Req-IDE-120]"/>
<emphasis role="strong">Requirement: IDE-120:</emphasis>
<link linkend="Req-IDE-120">Compound Assignment</link> (ver. 1)</simpara>
<simpara>
For a given assignment <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mi> </mi><mi>o</mi><mi>p</mi><mi> </mi><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></math>, with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>o</mi><mi>p</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>’@=’</mtext></mstyle></math> but not <literal>+=</literal>, both, left and right must be subtypes of <literal>number</literal>.<?asciidoc-br?>
For operator <literal>+=</literal>,</simpara>
<itemizedlist>
<listitem>
<simpara>if the left-hand side is a <literal>number</literal>, then <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mi> </mi><mstyle mathvariant="monospace"><mtext>’+’</mtext></mstyle><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></math> must return a number as well.
The right-hand side must, in fact, be a <literal>number</literal> (and not a <literal>boolean</literal>) here in order to avoid unexpected results.</simpara>
</listitem>
<listitem>
<simpara>if the left-hand side is a <literal>string</literal>, then <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mstyle mathvariant="monospace"><mtext>’+’</mtext></mstyle><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></math> must return a string as well.
That means that the right-hand side can be of <literal>any</literal> type.</simpara>
</listitem>
</itemizedlist>
<simpara>The expected type for the left-hand side is <literal>union{number,string}</literal>.</simpara>
<simpara>The basic idea behind these constraints is that the type of the left-hand side is not to be changed by the compound assignment.</simpara>
</requirement>
<requirement xml:id="IDE-121">
<title>Write Acccess</title>
<simpara>
<anchor xml:id="Req-IDE-121" xreflabel="[Req-IDE-121]"/>
<emphasis role="strong">Requirement: IDE-121:</emphasis>
<link linkend="Req-IDE-121">Write Acccess</link> (ver. 1)</simpara>
<simpara>
For a given assignment expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>E</mi><mi>x</mi><mi>p</mi><mi>r</mi></math>, the left-hand side must be writeable or a final data field and the assignment must be in the constructor.
Let <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>v</mi></math> be the bound variable (or field) with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>b</mi><mi>i</mi><mi>n</mi><mi>d</mi><mfenced close=")" open="("><mrow><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>E</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo>.</mo><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi></mrow><mi>v</mi></mfenced></math></simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mi>v</mi><mo>.</mo><mi>w</mi><mi>r</mi><mi>i</mi><mi>t</mi><mi>e</mi><mi>a</mi><mi>b</mi><mi>l</mi><mi>e</mi><mo></mo><mi>v</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi><mo></mo><mspace width="3.0mm"/><mspace width="2.0em"/><mi>v</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>null</mtext></mstyle></mtd></mtr><mtr><mtd><mspace width="3.0mm"/><mspace width="2.0em"/><mo></mo><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>E</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>i</mi><mi>n</mi><mi>g</mi><mi>F</mi><mi>u</mi><mi>n</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo>=</mo><mi>v</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></mtd></mtr><mtr><mtd><mspace width="3.0mm"/><mspace width="2.0em"/><mo></mo><mi>μ</mi><mfenced close=")" open="("><mrow><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>E</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo>.</mo><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi></mrow></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>PropertyAccess</mtext></mstyle></mtd></mtr><mtr><mtd><mspace width="3.0mm"/><mspace width="4.0em"/><mo></mo><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>E</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo>.</mo><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mo>.</mo><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>"this"</mtext></mstyle></mtd></mtr></mtable></math>
<simpara>The value of writeable is true for setters and usually for variables and data fields.
Assignability of variables and data fields can be restricted via <literal>const</literal> or the <literal>@Final</literal> annotation.
See <xref linkend="_assignment-modifiers"/>(data fields) and <xref linkend="_const"/> (const variables) for details.</simpara>
<simpara>Also see <xref linkend="Req-IDE-93"/> for read access constraint.</simpara>
<simpara>The left-hand side of an assignment expression may be an array or object literal and the assignment expression is then treated as a destructuring assignment.
See <xref linkend="_array-and-object-destructuring"/> for details.</simpara>
</requirement>
<bridgehead xml:id="type-inference-17" renderas="sect4">Type Inference</bridgehead>
<simpara>Similarly to [<link linkend="ECMA11a">ECMA11a(p.S11.1, p.p.84ff)</link>], we define type inference for simple assignment (<literal>=</literal>) and compound assignment (<literal>op=</literal>) individually.</simpara>
<simpara>The type of the assignment is simply the type of the right-hand side:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mspace width="3.0mm"/><mfrac><mrow><mi>Γ</mi><mo></mo><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi><mi>:</mi><mi>T</mi></mrow><mrow><mi>Γ</mi><mo></mo><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mi> </mi><mstyle mathvariant="monospace"><mtext>’=’</mtext></mstyle><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi><mi>:</mi><mi>T</mi></mrow></mfrac></math>
<simpara>Compound assignments are reduced to the former by splitting an operator <literal>@=</literal>, in which <literal>@</literal> is a simple operator,
into a simple operator expression with operator <literal>@</literal> and a simple assignment <literal>=</literal>.
Since the type of the latter is the right-hand side, we can define:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>Γ</mi><mo></mo><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mi> </mi><mstyle mathvariant="monospace"><mtext>’@’</mtext></mstyle><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi><mi>:</mi><mi>T</mi></mrow><mrow><mi>Γ</mi><mo></mo><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mi> </mi><mstyle mathvariant="monospace"><mtext>’@=’</mtext></mstyle><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi><mi>:</mi><mi>T</mi></mrow></mfrac></math>
</section>
<section xml:id="_comma-expression">
<title>Comma Expression</title>
<bridgehead xml:id="comma-expression-syntax" renderas="sect4">Syntax</bridgehead>
<simpara>Cf. [<link linkend="ECMA11a">ECMA11a(p.S11.14, p.p.85)</link>]</simpara>
<programlisting language="xtext" linenumbering="unnumbered">CommaExpression &lt;In, Yield&gt;:
exprs+=AssignmentExpression&lt;In,Yield&gt; ',' exprs+=AssignmentExpression&lt;In,Yield&gt;
(',' exprs+=AssignmentExpression&lt;In,Yield&gt;)*
;</programlisting>
<bridgehead xml:id="comma-expression-semantics" renderas="sect4">Semantics</bridgehead>
<simpara>All expressions will be evaluated even though only the value of the last expression will be the result.</simpara>
<example>
<title>Comma Expression</title>
<simpara>Assignment expressions preceed comma expressions:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">var b: boolean;
b = (12, 34, true); // ok, b=true
b = 12, 34, true ; // error, b=12 is invalid</programlisting>
</example>
<bridgehead xml:id="type-inference-18" renderas="sect4">Type Inference</bridgehead>
<simpara>Cf. [<link linkend="ECMA11a">ECMA11a(p.S11.14, p.p.85)</link>]</simpara>
<simpara>The type of a comma expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi></math> is inferred to the last expression:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>n</mi><mo>=</mo><mo>|</mo><mi>c</mi><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>s</mi><mo>|</mo><mo>,</mo><mi>Γ</mi><mo></mo><mi>c</mi><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><msub><mi>s</mi><mi>n</mi></msub><mi>:</mi><mi>T</mi></mrow><mrow><mi>Γ</mi><mo></mo><mi>c</mi><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>:</mi><mi>T</mi></mrow></mfrac></math>
</section>
</section>
<section xml:id="_ecmascript-6-expressions" role="language-n4js">
<title>ECMAScript 6 Expressions</title>
<section xml:id="_the-super-keyword">
<title>The super Keyword</title>
<programlisting language="n4js" linenumbering="unnumbered">SuperLiteral: {SuperLiteral} 'super';</programlisting>
<simpara>Apart from the use of keyword <literal>super</literal> in wildcards of type expressions (cf. <xref linkend="_type-expressions"/>),
there are two use cases for keyword <literal>super</literal>: super member access and super constructor calls.</simpara>
<example>
<title>Super Keyword</title>
<simpara>Two use cases for keyword super:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class B extends A {
constructor() {
// super call
super();
}
@Override
m();: void {
// super member access
super.m();
}
}</programlisting>
</example>
<bridgehead xml:id="super-keyword-semantics" renderas="sect4">Semantics</bridgehead>
<simpara><literal>super</literal> can be used to access the supertype’s constructor, methods, getters and setters.
The supertype is defined lexically, which is different from how <literal>this</literal> works.<footnote><simpara>See [<link linkend="ECMA15a">ECMA15a</link>], Chapter 12.3.5 "The Super Keyword"; note the use of <literal>HomeObject</literal> instead of <literal>thisValue</literal>; also see this blog - <link xl:href="http://www.2ality.com/2011/11/super-references.html">http://www.2ality.com/2011/11/super-references.html</link>.</simpara></footnote></simpara>
<simpara>Note that in [<link linkend="ECMA15a">ECMA15a</link>] Chapter 12.3.5 <literal>The Super Keyword</literal>, <literal>super</literal> is defined as a keyword but the syntax and semantics are defined in conjunction of member access.</simpara>
<requirement xml:id="IDE-122">
<title>Type of Super is Always Nominal</title>
<simpara>
<anchor xml:id="Req-IDE-122" xreflabel="[Req-IDE-122]"/>
<emphasis role="strong">Requirement: IDE-122:</emphasis>
<link linkend="Req-IDE-122">Type of Super is Always Nominal</link> (ver. 1)</simpara>
<simpara>
The type referenced with the super literal is always nominal.
This is a consequence of references to types in extend clauses to be nominal.</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>super</mtext></mstyle><mi>:</mi><mi>T</mi><mo></mo><mi>T</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>i</mi><mi>n</mi><mi>g</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>t</mi><mi>e</mi><mi>g</mi><mi>y</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>nominal</mtext></mstyle></math></simpara>
</requirement>
<requirement xml:id="IDE-123">
<title>Access Super Constructor with Super Literal</title>
<simpara>
<anchor xml:id="Req-IDE-123" xreflabel="[Req-IDE-123]"/>
<emphasis role="strong">Requirement: IDE-123:</emphasis>
<link linkend="Req-IDE-123">Access Super Constructor with Super Literal</link> (ver. 1)</simpara>
<simpara>
If the super literal <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi></math> is used to access the super constructor of a class, all of the following constraints must hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>The super constructor access must be a call expression:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>μ</mi><mfenced close=")" open="("><mrow><mi>c</mi><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi></mrow></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>CallExpression</mtext></mstyle><mo></mo><mi>c</mi><mo>.</mo><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi><mo>=</mo><mi>c</mi><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi></math>
</listitem>
<listitem>
<simpara>The super constructor call must be the expression of an expression statement <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>S</mi><mi>t</mi><mi>m</mi><mi>t</mi></math>:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>S</mi><mi>t</mi><mi>m</mi><mi>t</mi><mo>=</mo><mi>c</mi><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo></mo><mi>μ</mi><mfenced close=")" open="("><mrow><mi>c</mi><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>r</mi></mrow></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>ExpressionStatement</mtext></mstyle></math>
</listitem>
<listitem>
<simpara>The containing statement <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>t</mi><mi>m</mi><mi>t</mi><mi>E</mi><mi>x</mi><mi>p</mi><mi>r</mi></math> must be directly contained in a constructor body:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>μ</mi><mfenced close=")" open="("><mrow><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>S</mi><mi>t</mi><mi>m</mi><mi>t</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>i</mi><mi>n</mi><mi>g</mi><mi>F</mi><mi>u</mi><mi>n</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi></mrow></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Constructor</mtext></mstyle><mo>)</mo></mrow></math>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo></mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>S</mi><mi>t</mi><mi>m</mi><mi>t</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo>=</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>S</mi><mi>t</mi><mi>m</mi><mi>t</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>i</mi><mi>n</mi><mi>g</mi><mi>F</mi><mi>u</mi><mi>n</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo>.</mo><mi>b</mi><mi>o</mi><mi>d</mi><mi>y</mi></math></simpara>
</listitem>
<listitem>
<simpara>There must be no access to and not return statement before the containing statement <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>S</mi><mi>t</mi><mi>m</mi><mi>t</mi></math>.</simpara>
<simpara>Let <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>i</mi></math> be the index of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>S</mi><mi>t</mi><mi>m</mi><mi>t</mi></math> in the constructor body:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>S</mi><mi>t</mi><mi>m</mi><mi>t</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>m</mi><mi>t</mi><msub><mi>s</mi><mrow><mi>s</mi><mi>i</mi></mrow></msub><mo>=</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>S</mi><mi>t</mi><mi>m</mi><mi>t</mi></math>
<simpara>Then, the following constraint must hold: <footnote><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><msup><mo></mo><mo>*</mo></msup><mi>c</mi></math> is the transitive version of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><mo></mo><mi>c</mi></math>, that is, it <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi></math> directly or indirectly contained in <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi></math>.</simpara></footnote></simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mspace width="3.0mm"/><mo></mo><mi>i</mi><mo>&lt;</mo><mi>s</mi><mi>i</mi><mi>:</mi><mo></mo><mi>e</mi><mi>l</mi><mi>e</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><msup><mo></mo><mo>*</mo></msup><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>S</mi><mi>t</mi><mi>m</mi><mi>t</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>m</mi><mi>t</mi><msub><mi>s</mi><mi>i</mi></msub><mi>:</mi><mtext>
</mtext><mspace width="3.0mm"/><mspace width="3.0em"/><mi>μ</mi><mfenced close=")" open="("><mi>i</mi></mfenced><mo></mo><mstyle mathvariant="monospace"><mtext>ThisLiteral, ReturnStatement</mtext></mstyle></math>
</listitem>
</orderedlist>
<simpara>Further constraints with regard to super constructor calls are described in <xref linkend="_constructor-and-classifier-type"/>.</simpara>
</requirement>
<requirement xml:id="IDE-124">
<title>Access Super Member with Super Literal</title>
<simpara>
<anchor xml:id="Req-IDE-124" xreflabel="[Req-IDE-124]"/>
<emphasis role="strong">Requirement: IDE-124:</emphasis>
<link linkend="Req-IDE-124">Access Super Member with Super Literal</link> (ver. 1)</simpara>
<simpara>
If the super literal <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi></math> is used to access a member of the super class, all of the following constraints must hold, with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mo>=</mo><mi>s</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>r</mi></math></simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>The super literal must be the receiver of a method call (cf. remarks below):</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mspace width="3.0mm"/><mi>μ</mi><mfenced close=")" open="("><mi>c</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>CallExpression</mtext></mstyle></mtd></mtr><mtr><mtd><mo></mo><mspace width="3.0mm"/><mi>c</mi><mo>.</mo><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>PropertyAccessExpression</mtext></mstyle></mtd></mtr><mtr><mtd><mo></mo><mspace width="3.0mm"/><mi>c</mi><mo>.</mo><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi><mo>.</mo><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi><mo>=</mo><mi>s</mi></mtd></mtr></mtable></math>
</listitem>
<listitem>
<simpara>The super literal is used in a method or field accessor of a class:</simpara>
</listitem>
</orderedlist>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mrow><mi>s</mi><mo>.</mo><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>i</mi><mi>n</mi><mi>g</mi><mi>C</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi></mrow></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Class</mtext></mstyle></math>
3. The super literal must not be used in a nested function expression:</simpara>
<simpara>+
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mrow><mi>s</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>i</mi><mi>n</mi><mi>g</mi><mi>F</mi><mi>u</mi><mi>n</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi></mrow></mfenced><mo>=</mo><mi>s</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>i</mi><mi>n</mi><mi>g</mi><mi>M</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><mi>O</mi><mi>r</mi><mi>F</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mi>A</mi><mi>c</mi><mi>c</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>o</mi><mi>r</mi></math>
4. If the return type of the method access via super is this, the actually bound this type will be the type of the calling class (and not of the class defining the method).</simpara>
<simpara>+</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>s</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>i</mi><mi>n</mi><mi>g</mi><mi>C</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mo>=</mo><mi>T</mi><mspace width="3.0mm"/><mi>μ</mi><mfenced close=")" open="("><mi>m</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Method</mtext></mstyle><mspace width="3.0mm"/><mi>m</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>n</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>this</mtext></mstyle></mrow><mrow><mstyle mathvariant="monospace"><mtext>function():T</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>s</mi><mo>.</mo><mi>m</mi></mrow></mfrac></math>
</requirement>
<requirement xml:id="IDE-125">
<title>Super Literal Usage</title>
<simpara>
<anchor xml:id="Req-IDE-125" xreflabel="[Req-IDE-125]"/>
<emphasis role="strong">Requirement: IDE-125:</emphasis>
<link linkend="Req-IDE-125">Super Literal Usage</link> (ver. 1)</simpara>
<simpara>
For super literals, either <xref linkend="Req-IDE-123"/> or <xref linkend="Req-IDE-124"/> must hold, no other usage
is allowed.</simpara>
<simpara>Consequences:</simpara>
<itemizedlist>
<listitem>
<simpara>Since fields cannot be overridden (except for changing the access modifier), it is not possible nor allowed to access a field via <literal>super</literal>.</simpara>
</listitem>
<listitem>
<simpara>Super literals must not be used with index access (e.g., <literal>super["foo"]</literal>)</simpara>
</listitem>
<listitem>
<simpara>It is not possible to chain super keywords. That is, it is not possible to call <literal>super.super.m()</literal>.</simpara>
</listitem>
<listitem>
<simpara>It is not allowed to use the super literal in interfaces or non-methods/accessors.</simpara>
</listitem>
<listitem>
<simpara>Super cannot be used to call an overridden method of an implemented method from the overriding method in the implementing class.</simpara>
</listitem>
<listitem>
<simpara>In order to be able to access a super method of a method <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi></math> of a class <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi></math>, exactly one non-abstract super method <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>M</mi><mi>'</mi></msup></math> in a super class <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi></math> of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi></math> must exist.
This is assured by the standard rules for binding identifiers.</simpara>
</listitem>
</itemizedlist>
<simpara>If super is used to access a super member, the receiver type is not changed.
This is important in particular for static methods as demonstrated in the following example:</simpara>
<example>
<title>Super Call in Static Methods</title>
<programlisting language="n4js" linenumbering="unnumbered">class A {
static foo(): void { console.log("A") }
static bar(): void {
this.foo();
}
}
class B extends A {
@Override
static foo(): void { console.log("B") }
@Override
static bar(): void {
A.bar(); <co xml:id="CO5-1"/>
super.bar(); <co xml:id="CO5-2"/>
}
}
B.bar();</programlisting>
</example>
<calloutlist>
<callout arearefs="CO5-1">
<para>The receiver (which is similar to the this-binding in ECMAScript) is changed to <literal>A</literal>.</para>
</callout>
<callout arearefs="CO5-2">
<para>Using super, the receiver is preserved, i.e. <literal>B</literal>.</para>
</callout>
</calloutlist>
</requirement>
</section>
</section>
<section xml:id="_ecmascript-7-expressions" role="language-n4js">
<title>ECMAScript 7 Expressions</title>
<section xml:id="_await-expression">
<title>Await Expression</title>
<simpara>In N4JS, <literal>await</literal> is implemented as a unary operator with the same precedence as <literal>yield</literal> in ECMAScript 6.</simpara>
<simpara>Constraints governing the use of <literal>await</literal> are given together with those for <literal>async</literal> in <xref linkend="_asynchronous-functions"/>.</simpara>
</section>
</section>
<section xml:id="_n4js-specific-expressions" role="language-n4js">
<title>N4JS Specific Expressions</title>
<section xml:id="_class-expression">
<title>Class Expression</title>
<simpara>A class expression in N4JS is similar to a class expression in ECMAScript 6 [<link linkend="ECMA15a">ECMA15a(p.14.5)</link>].</simpara>
<note>
<simpara>Class expressions are not part of version 0.3</simpara>
</note>
<bridgehead xml:id="class-expression-syntax" renderas="sect4">Syntax</bridgehead>
<simpara>See <xref linkend="_classes"/>.</simpara>
<bridgehead xml:id="class-expression-semantics-type-inference" renderas="sect4">Semantics and Type Inference</bridgehead>
<simpara>The inferred type of a class expression simply is the class type as described in <xref linkend="_constructor-and-classifier-type"/>.</simpara>
</section>
<section xml:id="_cast-as-expression">
<title>Cast (As) Expression</title>
<bridgehead xml:id="cast-as-expression-syntax" renderas="sect4">Syntax</bridgehead>
<programlisting language="xtext" linenumbering="unnumbered">CastExpression &lt;Yield&gt; returns Expression: expression=Expression 'as' targetTypeRef=TypeRefForCast;
TypeRefForCast returns StaticBaseTypeRef:
ParameterizedTypeRef
| ThisTypeRef
| ConstructorTypeRef
| ClassifierTypeRef
| FunctionTypeExpression
| UnionTypeExpression
| IntersectionTypeExpression</programlisting>
<section xml:id="cast-as-expression-semantics-type-inference">
<title>Semantics and Type Inference</title>
<simpara>The inferred type of the type cast expression is the target type:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi> </mi><mstyle mathvariant="monospace"><mtext>"as"</mtext></mstyle><mi> </mi><mi>T</mi><mi>:</mi><mi>T</mi></mrow></mfrac></math>
<simpara>The type cast returns the expression without further modifications.
Type casts are simply removed during compilation so there will be no exceptions thrown at the cast until later when accessing properties which may not be present in case of a failed cast.</simpara>
<simpara>An error is issued if the cast is either unnecessary or cannot succeed.
See further details in <xref linkend="_type-cast"/>.</simpara>
</section>
</section>
<section xml:id="Import_Calls">
<title>Import Calls</title>
<simpara>Import calls as specified by the corresponding <link xl:href="https://github.com/tc39/proposal-dynamic-import">ECMA TC39 proposal</link> are
available in N4JS. Such an import call has the form</simpara>
<programlisting language="n4js" linenumbering="unnumbered">import(moduleSpecifier)</programlisting>
<simpara>and may appear in the source code wherever an expression may appear. It&#8217;s argument need not be a string literal, as is
the case with module specifiers of ordinary imports; instead, any expression that evaluates to a string at runtime is
accepted. Hence, it can be used to import from a target module that is not yet known at compile time.</simpara>
<simpara>A note on terminology: import calls covered in this section are sometimes referred to as "dynamic import". In N4JS
that term is already used for imports of the form <literal>import * as N+ from "&#8230;&#8203;"</literal>, i.e. compile-time imports that do not
require type information of the module imported from, see <xref linkend="Dynamic_Imports"/>, and stems from the term "dynamic type"
(see <xref linkend="Type_Modifiers_Dynamic"/>). To avoid confusion, we will usually avoid referring to import calls as a "dynamic
import".</simpara>
</section>
</section>
<section xml:id="compile-time-expressions" role="language-n4js">
<title>Compile-Time Expressions</title>
<simpara>A compile-time expression is an expression that can be fully evaluated at compile time. Not all expressions introduced
in the previous sections qualify as compile-time expressions. Some forms of expressions always qualify (e.g. a string
literal is always a compile-time expression), some never (e.g. call expressions), and for some expressions the operands
must be of a certain value. The latter applies, for example, to divison: <literal>5 / 0</literal> is a valid ECMAScript expression (evaluating
to <literal>NaN</literal>) but is not a compile-time expression. So it&#8217;s the actual compile-time value of the divisor that makes the difference,
here. In any case, if an expression has operands, it is a compile-time expression only if all operands are compile-time expressions.</simpara>
<simpara>The value a compile-time expression evaluates to at compile-time is called <emphasis>compile-time value</emphasis>. So, an expression has a compile-time
value if and only if it is a compile-time expression.</simpara>
<definition>
<title>Compile-Time Expression</title>
<simpara>
<anchor xml:id="compile-time_expression" xreflabel="[compile-time_expression]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="compile-time_expression">Compile-Time Expression</link></simpara>
<simpara>
The following expressions are called compile-time expressions:</simpara>
<itemizedlist>
<listitem>
<simpara><literal>undefined</literal> (but not <literal>NaN</literal> or <literal>Infinity</literal>).</simpara>
</listitem>
<listitem>
<simpara>the <literal>null</literal> literal.</simpara>
</listitem>
<listitem>
<simpara>all boolean, numeric, and string literals.</simpara>
</listitem>
<listitem>
<simpara>template string literals, iff all embedded expressions are compile-time expressions.</simpara>
</listitem>
<listitem>
<simpara>a parenthesis expression, iff its nested expression is a compile-time expression.</simpara>
</listitem>
<listitem>
<simpara>unary expressions in case of the following operators:</simpara>
<itemizedlist>
<listitem>
<simpara><literal>!</literal> iff the operand is a compile-time expression and evaluates to a boolean value.</simpara>
</listitem>
<listitem>
<simpara><literal>+</literal> iff the operand is a compile-time expression and evaluates to a numeric value.</simpara>
</listitem>
<listitem>
<simpara><literal>-</literal> iff the operand is a compile-time expression and evaluates to a numeric value.</simpara>
</listitem>
<listitem>
<simpara><literal>void</literal>.</simpara>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<simpara>binary expressions in case of the following operators:</simpara>
<itemizedlist>
<listitem>
<simpara><literal>+</literal> iff both operands are compile-time expressions and</simpara>
<itemizedlist>
<listitem>
<simpara>both evaluate to numeric values, or</simpara>
</listitem>
<listitem>
<simpara>at least one evaluates to a string value.</simpara>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<simpara><literal>-</literal>, <literal>*</literal> iff both operands are compile-time expressions and evaluate to numeric values.</simpara>
</listitem>
<listitem>
<simpara><literal>/</literal>, <literal>%</literal> iff both operands are compile-time expressions and evaluate to numeric values and the right-hand operand is non-zero (i.e. division by zero is disallowed in compile-time expression, because <literal>NaN</literal> is not a supported compile-time value).</simpara>
</listitem>
<listitem>
<simpara><literal>&amp;&amp;</literal>, <literal>||</literal> iff both operands are compile-time expressions and evaluate to boolean values.</simpara>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<simpara>a tertiary conditional expression, iff the first operand is a compile-time expression evaluating to a boolean value B and</simpara>
<itemizedlist>
<listitem>
<simpara>in case B is true, the second operand is a compile-time expression.</simpara>
</listitem>
<listitem>
<simpara>in case B is false, the third operand is a compile-time expression.</simpara>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<simpara>an identifier reference to a const variable, iff its initializer expression is a compile-time expression.</simpara>
</listitem>
<listitem>
<simpara>a property access expression, iff it is direct (see <xref linkend="property-access-direct"/>) and refers to</simpara>
<itemizedlist>
<listitem>
<simpara>a built-in symbol, e.g. <literal>Symbol.iterator</literal>,</simpara>
</listitem>
<listitem>
<simpara>a literal of a <literal>@StringBased</literal> enum, or</simpara>
</listitem>
<listitem>
<simpara>a const field with a compile-time initializer expression.</simpara>
</listitem>
</itemizedlist>
</listitem>
</itemizedlist>
<simpara>In all other cases, the expression is not a compile-time expression.</simpara>
</definition>
<simpara>Every expression in the code may be a compile-time expression, but in most places this has no particular
effect and is simply ignored. They are of significance only in computed property names, in index access
expressions, as initializers of const variables and fields (as stated above) and when nested as an operand
inside an expression at these locations.</simpara>
</section>
</chapter>
<chapter xml:id="_statements">
<title>Statements</title>
<simpara>For all statements, we define the following pseudo properties:</simpara>
<variablelist>
<varlistentry>
<term><literal>containingFunction</literal> </term>
<listitem>
<simpara>The function or method in which the statement is (indirectly) contained, this may be null.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>containingClass</literal> </term>
<listitem>
<simpara>The class in which the statement is (indirectly) contained, this may be null.</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>The expressions and statements are ordered, at first describing the constructs available in the 5th edition of ECMA-262
referred to as [<link linkend="ECMA11a">ECMA11a</link>] in the following.
The grammar snippets already use newer constructs in some cases.</simpara>
<section xml:id="_ecmascript-5-statements" role="language-n4js">
<title>ECMAScript 5 Statements</title>
<simpara>N4JS supports the same statements as
ECMAScript. Some of these statements are enhanced with annotations <xref linkend="_annotations"/> and type information.</simpara>
<simpara>Although some statements may return a value which can be used via certain constructs such as <literal>eval</literal>), no type is inferred for any statement.
The compiler will always create a warning if a statement is used instead of an expression.</simpara>
<simpara>The following sections, therefore, do not define how to infer types for statement but how types and type annotations
are used in these statements and the specific type constraints for a given statement.</simpara>
<simpara>All syntax definitions taken from [<link linkend="ECMA11a">ECMA11a</link>] are repeated here for convenience reasons and in order to define temporary variables for simplifying constraint definitions.
If non-terminals are not defined here, the definition specified in [<link linkend="ECMA11a">ECMA11a</link>] is to be used.</simpara>
<section xml:id="_function-or-field-accessor-bodies">
<title>Function or Field Accessor Bodies</title>
<requirement xml:id="IDE-126">
<title>Dead Code</title>
<simpara>
<anchor xml:id="Req-IDE-126" xreflabel="[Req-IDE-126]"/>
<emphasis role="strong">Requirement: IDE-126:</emphasis>
<link linkend="Req-IDE-126">Dead Code</link> (ver. 1)</simpara>
<simpara>
For all statements in a function or field accessor (getter/setter) body, the following constraints must hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>Statements appearing directly after return, throw, break, or continue statements (in the same block) are considered to be dead code and a warning is issued in these cases.</simpara>
</listitem>
</orderedlist>
</requirement>
</section>
<section xml:id="_variable-statement">
<title>Variable Statement</title>
<bridgehead xml:id="variable-statement-syntax" renderas="sect4">Syntax</bridgehead>
<simpara>A var statement can declare the type of the variable with a type
reference. This is described with the following grammar similar to
[<link linkend="ECMA11a">ECMA11a(p.S12.2, p.p.87)</link>]:</simpara>
<programlisting language="xtext" linenumbering="unnumbered">VariableStatement &lt;In, Yield&gt;:
=&gt;({VariableStatement}
'var'
)
varDeclsOrBindings+=VariableDeclarationOrBinding&lt;In,Yield,false&gt; (',' varDeclsOrBindings+=VariableDeclarationOrBinding&lt;In,Yield,false&gt;)* Semi
;
VariableDeclarationOrBinding &lt;In, Yield, OptionalInit&gt;:
VariableBinding&lt;In,Yield,OptionalInit&gt;
| VariableDeclaration&lt;In,Yield,true&gt;
;
VariableBinding &lt;In, Yield, OptionalInit&gt;:
=&gt; pattern=BindingPattern&lt;Yield&gt; (
&lt;OptionalInit&gt; ('=' expression=AssignmentExpression&lt;In,Yield&gt;)?
| &lt;!OptionalInit&gt; '=' expression=AssignmentExpression&lt;In,Yield&gt;
)
;
VariableDeclaration &lt;In, Yield, AllowType&gt;:
{VariableDeclaration} VariableDeclarationImpl&lt;In,Yield,AllowType&gt;;
fragment VariableDeclarationImpl &lt;In, Yield, AllowType&gt;*:
annotations+=Annotation*
(
&lt;AllowType&gt; =&gt;(
name=BindingIdentifier&lt;Yield&gt; ColonSepTypeRef?
) ('=' expression=AssignmentExpression&lt;In,Yield&gt;)?
| &lt;!AllowType&gt; =&gt;(
name=BindingIdentifier&lt;Yield&gt;
) ('=' expression=AssignmentExpression&lt;In,Yield&gt;)?
)
;</programlisting>
<example>
<title>Variable Statement</title>
<programlisting language="n4js" linenumbering="unnumbered">var any: any;
// any.type := any
var anyNull = null;
// anyNull.type := any
var s: string;
// s.type := string
var init = "Hi";
// init.type := string
const MESSAGE = "Hello World";
// MESSAGE.type := string</programlisting>
</example>
<bridgehead xml:id="variable-statement-semantics" renderas="sect4">Semantics</bridgehead>
<simpara>From a model and type inference point of view, variable and constant statements and declarations are similar except that the pseudo property <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi></math> is set to false for variables and true for constants.
Also see exported variable statement (<xref linkend="_export-statement"/>) and constant statement and declaration (<xref linkend="_const"/>).</simpara>
<requirement xml:id="IDE-127">
<title>Variable declaration</title>
<simpara>
<anchor xml:id="Req-IDE-127" xreflabel="[Req-IDE-127]"/>
<emphasis role="strong">Requirement: IDE-127:</emphasis>
<link linkend="Req-IDE-127">Variable declaration</link> (ver. 1)</simpara>
<simpara>
For a given variable declaration <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>d</mi></math>, the following constraints must hold:</simpara>
<itemizedlist>
<listitem>
<simpara>The type of the initializer expression must conform to the declared type:</simpara>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>d</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo></mo><mstyle mathvariant="monospace"><mtext>null</mtext></mstyle><mo></mo><mi>d</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mo></mo><mstyle mathvariant="monospace"><mtext>null</mtext></mstyle></math><?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>Γ</mi><mo></mo><mi>d</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>Γ</mi><mo></mo><mi>d</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi></math></simpara>
</listitem>
<listitem>
<simpara>The initializer expression should not contain a reference to <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>d</mi></math> except where the reference is contained in a class expression or function expression and
the class is not immediately initialized or the function is not immediately invoked.
In these cases, the code is executed later and the self-reference is not a problem.<?asciidoc-br?>
To clarify: <emphasis>should not</emphasis> means that only a warning will be produced.</simpara>
</listitem>
</itemizedlist>
<programlisting language="n4js" linenumbering="unnumbered">// not ok (simple case)
var n = n + 1;
// ok (class expression not fully supported)
// var cls1 = class { static sfield1 = "hello"; field2 = cls1.sfield1; };
// not ok, immediately instantiated (class expression not fully supported)
// var cls2 = new class { field1 = "hello"; field2 = cls2.field1; };
// ok
var fun1 = function() : number { var x = fun1; return -42; };
// not ok, immediately invoked
var fun2 = function() : number { var x = fun2; return -42; }();</programlisting>
<simpara>The variable statement may contain array or object destructuring patterns, see <xref linkend="_array-and-object-destructuring"/> for details.</simpara>
</requirement>
<bridgehead xml:id="variable-statement-type-inference" renderas="sect4">Type Inference</bridgehead>
<simpara>The type of a variable is the type of its declaration:</simpara>
<simpara>The type of a variable declaration is either the declared type or the inferred type of the initializer expression:</simpara>
</section>
<section xml:id="_if-statement" role="language-n4js">
<title>If Statement</title>
<bridgehead xml:id="if-statement-syntax" renderas="sect4">Syntax</bridgehead>
<simpara>Cf. [<link linkend="ECMA11a">ECMA11a(p.S12.5, p.p.89)</link>]</simpara>
<programlisting language="xtext" linenumbering="unnumbered">IfStatement &lt;Yield&gt;:
'if' '(' expression=Expression&lt;In=true,Yield&gt; ')'
ifStmt=Statement&lt;Yield&gt;
(=&gt; 'else' elseStmt=Statement&lt;Yield&gt;)?;</programlisting>
<bridgehead xml:id="if-statement-semantics" renderas="sect4">Semantics</bridgehead>
<simpara>There are no specific constraints defined for the condition, the ECMAScript operation <literal>ToBoolean</literal> [<link linkend="ECMA11a">ECMA11a(p.S9.2, p.p.43)</link>] is used to convert any type to boolean.</simpara>
<requirement xml:id="IDE-128">
<title>If Statement</title>
<simpara>
<anchor xml:id="Req-IDE-128" xreflabel="[Req-IDE-128]"/>
<emphasis role="strong">Requirement: IDE-128:</emphasis>
<link linkend="Req-IDE-128">If Statement</link> (ver. 1)</simpara>
<simpara>
In N4JS, the expression of an if statement must not evaluate to <literal>void</literal>.
If the expressions is a function call in particular, the called function must not be declared to return <literal>void</literal>.</simpara>
</requirement>
</section>
<section xml:id="_iteration-statements">
<title>Iteration Statements</title>
<bridgehead xml:id="iterations-statements-syntax" renderas="sect4">Syntax</bridgehead>
<simpara>Cf. [<link linkend="ECMA11a">ECMA11a(p.S12.6, p.p.90ff)</link>]</simpara>
<simpara>The syntax already considers the for-of style described in <xref linkend="_for-of-statement"/>.</simpara>
<programlisting language="xtext" linenumbering="unnumbered">IterationStatement &lt;Yield&gt;:
DoStatement&lt;Yield&gt;
| WhileStatement&lt;Yield&gt;
| ForStatement&lt;Yield&gt;
;
DoStatement &lt;Yield&gt;: 'do' statement=Statement&lt;Yield&gt; 'while' '(' expression=Expression&lt;In=true,Yield&gt; ')' =&gt; Semi?;
WhileStatement &lt;Yield&gt;: 'while' '(' expression=Expression&lt;In=true,Yield&gt; ')' statement=Statement&lt;Yield&gt;;
ForStatement &lt;Yield&gt;:
{ForStatement} 'for' '('
(
// this is not in the spec as far as I can tell, but there are tests that rely on this to be valid JS
=&gt;(initExpr=LetIdentifierRef forIn?='in' expression=Expression&lt;In=true,Yield&gt; ')')
| ( -&gt;varStmtKeyword=VariableStatementKeyword
(
=&gt;(varDeclsOrBindings+=BindingIdentifierAsVariableDeclaration&lt;In=false,Yield&gt; (forIn?='in' | forOf?='of') -&gt;expression=AssignmentExpression&lt;In=true,Yield&gt;?)
| varDeclsOrBindings+=VariableDeclarationOrBinding&lt;In=false,Yield,OptionalInit=true&gt;
(
(',' varDeclsOrBindings+=VariableDeclarationOrBinding&lt;In=false,Yield,false&gt;)* ';' expression=Expression&lt;In=true,Yield&gt;? ';' updateExpr=Expression&lt;In=true,Yield&gt;?
| forIn?='in' expression=Expression&lt;In=true,Yield&gt;?
| forOf?='of' expression=AssignmentExpression&lt;In=true,Yield&gt;?
)
)
| initExpr=Expression&lt;In=false,Yield&gt;
(
';' expression=Expression&lt;In=true,Yield&gt;? ';' updateExpr=Expression&lt;In=true,Yield&gt;?
| forIn?='in' expression=Expression&lt;In=true,Yield&gt;?
| forOf?='of' expression=AssignmentExpression&lt;In=true,Yield&gt;?
)
| ';' expression=Expression&lt;In=true,Yield&gt;? ';' updateExpr=Expression&lt;In=true,Yield&gt;?
)
')'
) statement=Statement&lt;Yield&gt;
;
ContinueStatement &lt;Yield&gt;: {ContinueStatement} 'continue' (label=[LabelledStatement|BindingIdentifier&lt;Yield&gt;])? Semi;
BreakStatement &lt;Yield&gt;: {BreakStatement} 'break' (label=[LabelledStatement|BindingIdentifier&lt;Yield&gt;])? Semi;</programlisting>
<simpara>Since <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>v</mi><mi>a</mi><mi>r</mi><mi>D</mi><mi>e</mi><mi>c</mi><mi>l</mi><mfenced close=")" open="("><mi>s</mi></mfenced></math> are <literal>VariableStatement</literal>s as described in <xref linkend="_variable-statement"/>, the declared variables can be type annotated.</simpara>
<tip>
<simpara>Using for-in is not recommended, instead <literal>_each</literal> should be used.</simpara>
</tip>
<bridgehead xml:id="iterations-statements-semantics" renderas="sect4">Semantics</bridgehead>
<simpara>There are no specific constraints defined for the condition, the
ECMAScript operation <literal>ToBoolean</literal> [<link linkend="ECMA11a">ECMA11a(p.S9.2, p.p.43)</link>] is used to convert any type to boolean.</simpara>
<requirement xml:id="IDE-129">
<title>For-In-Statement Constraints</title>
<simpara>
<anchor xml:id="Req-IDE-129" xreflabel="[Req-IDE-129]"/>
<emphasis role="strong">Requirement: IDE-129:</emphasis>
<link linkend="Req-IDE-129">For-In-Statement Constraints</link> (ver. 1)</simpara>
<simpara>
For a given <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi></math> the following conditions must hold:</simpara>
<itemizedlist>
<listitem>
<simpara>The type of the expression must be conform to object:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>Γ</mi><mo></mo><mi>f</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>union</mtext><mtext>Object,string,ArgumentType</mtext></mstyle></math></simpara>
</listitem>
<listitem>
<simpara>Either a new loop variable must be declared or an rvalue must be provided as init expression:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi><mi>D</mi><mi>e</mi><mi>c</mi><mi>l</mi><mo></mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi><mo></mo><mfenced close=")" open="("><mrow><mi>f</mi><mo>.</mo><mi>i</mi><mi>n</mi><mi>i</mi><mi>t</mi><mi>E</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo></mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi><mo></mo><mi>i</mi><mi>s</mi><mi>R</mi><mi>V</mi><mi>a</mi><mi>l</mi><mi>u</mi><mi>e</mi><mfenced close=")" open="("><mrow><mi>f</mi><mo>.</mo><mi>i</mi><mi>n</mi><mi>i</mi><mi>t</mi><mi>E</mi><mi>x</mi><mi>p</mi><mi>r</mi></mrow></mfenced></mrow></mfenced></math></simpara>
</listitem>
<listitem>
<simpara>The type of the loop variable must be a string (or a super type of string, i.e. any):</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd/><mtd><mrow><mo>(</mo><mrow><mi>f</mi><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi><mi>D</mi><mi>e</mi><mi>c</mi><mi>l</mi><mo></mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi><mo></mo><mi>Γ</mi><mo></mo><mi>f</mi><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi><mi>D</mi><mi>e</mi><mi>c</mi><mi>l</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>string</mtext></mstyle><mo>)</mo></mrow></mrow></mtd></mtr><mtr><mtd><mo></mo></mtd><mtd><mrow><mo>(</mo><mrow><mi>f</mi><mo>.</mo><mi>i</mi><mi>n</mi><mi>i</mi><mi>t</mi><mi>E</mi><mi>x</mi><mi>p</mi><mo></mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi><mo></mo><mi>Γ</mi><mo></mo><mstyle mathvariant="monospace"><mtext>string</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>f</mi><mo>.</mo><mi>i</mi><mi>n</mi><mi>i</mi><mi>t</mi><mi>E</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo>)</mo></mrow></mrow></mtd></mtr></mtable></math>
</listitem>
</itemizedlist>
</requirement>
</section>
<section xml:id="_return-statement" role="language-n4js">
<title>Return Statement</title>
<bridgehead xml:id="return-statement-syntax" renderas="sect4">Syntax</bridgehead>
<simpara>The returns statement is defined as in [<link linkend="ECMA11a">ECMA11a(p.S12.9, p.p.93)</link>] with</simpara>
<programlisting language="xtext" linenumbering="unnumbered">ReturnStatement &lt;Yield&gt;:
'return' (expression=Expression&lt;In=true,Yield&gt;)? Semi;</programlisting>
<bridgehead xml:id="return-statement-semantics" renderas="sect4">Semantics</bridgehead>
<requirement xml:id="IDE-130">
<title>Return statement</title>
<simpara>
<anchor xml:id="Req-IDE-130" xreflabel="[Req-IDE-130]"/>
<emphasis role="strong">Requirement: IDE-130:</emphasis>
<link linkend="Req-IDE-130">Return statement</link> (ver. 1)</simpara>
<simpara>
1. Expected type of expression in a return statement must be a sub type of the return type of the enclosing function:</simpara>
<simpara>+</simpara>
<simpara>Note that the expression may be evaluated to <literal>void</literal>.
2. If enclosing function is declared to return <literal>void</literal>, then either
* no return statement must be defined
* return statement has no expression
* type of expression of return statement is <literal>void</literal>
3. If enclosing function is declared to to return a type different from <literal>void</literal>, then
* all return statements must have a return expression
* all control flows must either end with a return or throw statement
4. Returns statements must be enclosed in a function.
A return statement, for example, must not be a top-level statement.</simpara>
</requirement>
</section>
<section xml:id="_with-statement">
<title>With Statement</title>
<bridgehead xml:id="with-statement-syntax" renderas="sect4">Syntax</bridgehead>
<simpara>The with statement is not allowed in N4JS, thus an error is issued.</simpara>
<programlisting language="xtext" linenumbering="unnumbered">WithStatement &lt;Yield&gt;:
'with' '(' expression=Expression&lt;In=true,Yield&gt; ')'
statement=Statement&lt;Yield&gt;;</programlisting>
<bridgehead xml:id="with-statement-semantics" renderas="sect4">Semantics</bridgehead>
<simpara>N4JS is based on strict mode and the with statement is not allowed in
strict mode, cf. [<link linkend="ECMA11a">ECMA11a(p.S12.10.1, p.p.94)</link>].</simpara>
<requirement xml:id="IDE-131">
<title>With Statement</title>
<simpara>
<anchor xml:id="Req-IDE-131" xreflabel="[Req-IDE-131]"/>
<emphasis role="strong">Requirement: IDE-131:</emphasis>
<link linkend="Req-IDE-131">With Statement</link> (ver. 1)</simpara>
<simpara>
With statements are not allowed in N4JS or strict mode.</simpara>
</requirement>
</section>
<section xml:id="_switch-statement">
<title>Switch Statement</title>
<bridgehead xml:id="switch-statement-syntax" renderas="sect4">Syntax</bridgehead>
<simpara>Cf. [<link linkend="ECMA11a">ECMA11a(p.S12.11, p.p.94ff)</link>]</simpara>
<programlisting language="xtext" linenumbering="unnumbered">SwitchStatement &lt;Yield&gt;:
'switch' '(' expression=Expression&lt;In=true,Yield&gt; ')' '{'
(cases+=CaseClause&lt;Yield&gt;)*
((cases+=DefaultClause&lt;Yield&gt;)
(cases+=CaseClause&lt;Yield&gt;)*)? '}'
;
CaseClause &lt;Yield&gt;: 'case' expression=Expression&lt;In=true,Yield&gt; ':' (statements+=Statement&lt;Yield&gt;)*;
DefaultClause &lt;Yield&gt;: {DefaultClause} 'default' ':' (statements+=Statement&lt;Yield&gt;)*;</programlisting>
<bridgehead xml:id="switch-statement-semantics" renderas="sect4">Semantics</bridgehead>
<requirement xml:id="IDE-132">
<title>Switch Constraints</title>
<simpara>
<anchor xml:id="Req-IDE-132" xreflabel="[Req-IDE-132]"/>
<emphasis role="strong">Requirement: IDE-132:</emphasis>
<link linkend="Req-IDE-132">Switch Constraints</link> (ver. 1)</simpara>
<simpara>
For a given switch statement <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi></math>, the following constraints must hold:</simpara>
<itemizedlist>
<listitem>
<simpara>For all cases <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mo></mo><mi>s</mi><mo>.</mo><mi>c</mi><mi>a</mi><mi>s</mi><mi>e</mi><mi>s</mi></math>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi></math>===<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi></math> must be valid according to the constraints defined in <xref linkend="_equality-expression"/>.</simpara>
</listitem>
</itemizedlist>
</requirement>
</section>
<section xml:id="_throw-try-and-catch-statements">
<title>Throw, Try, and Catch Statements</title>
<bridgehead xml:id="throw-try-catch-syntax" renderas="sect4">Syntax</bridgehead>
<simpara>Cf. [<link linkend="ECMA11a">ECMA11a(p.S12.13/14, p.p.96ff)</link>]</simpara>
<programlisting language="xtext" linenumbering="unnumbered">ThrowStatement &lt;Yield&gt;:
'throw' expression=Expression&lt;In=true,Yield&gt; Semi;
TryStatement &lt;Yield&gt;:
'try' block=Block&lt;Yield&gt;
((catch=CatchBlock&lt;Yield&gt; finally=FinallyBlock&lt;Yield&gt;?) | finally=FinallyBlock&lt;Yield&gt;)
;
CatchBlock &lt;Yield&gt;: {CatchBlock} 'catch' '(' catchVariable=CatchVariable&lt;Yield&gt; ')' block=Block&lt;Yield&gt;;
CatchVariable &lt;Yield&gt;:
=&gt;bindingPattern=BindingPattern&lt;Yield&gt;
| name=BindingIdentifier&lt;Yield&gt;
;
FinallyBlock &lt;Yield&gt;: {FinallyBlock} 'finally' block=Block&lt;Yield&gt;;</programlisting>
<simpara>There must be not type annotation for the catch variable, as this would lead to the wrong assumption that a type can be specified.</simpara>
<bridgehead xml:id="throw-try-catch-type-inference" renderas="sect4">Type Inference</bridgehead>
<simpara>The type of the catch variable is always assumed to be <literal>any</literal>.</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow/><mrow><mi>Γ</mi><mo></mo><mi>c</mi><mi>a</mi><mi>t</mi><mi>c</mi><mi>h</mi><mi>B</mi><mi>l</mi><mi>o</mi><mi>c</mi><mi>k</mi><mo>.</mo><mi>c</mi><mi>a</mi><mi>t</mi><mi>c</mi><mi>h</mi><mi>V</mi><mi>a</mi><mi>r</mi><mi>i</mi><mi>a</mi><mi>b</mi><mi>l</mi><mi>e</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>any</mtext></mstyle></mrow></mfrac></math>
</section>
<section xml:id="_debugger-statement">
<title>Debugger Statement</title>
<bridgehead xml:id="debugger-statement-syntax" renderas="sect4">Syntax</bridgehead>
<simpara>Cf. [<link linkend="ECMA11a">ECMA11a(p.S12.15, p.p.97ff)</link>])</simpara>
<programlisting language="xtext" linenumbering="unnumbered">DebuggerStatement: {DebuggerStatement} 'debugger' Semi;</programlisting>
<bridgehead xml:id="debugger-statement--semantics" renderas="sect4">Semantics</bridgehead>
<simpara>na</simpara>
</section>
</section>
<section xml:id="_ecmascript-6-statements" role="language-n4js">
<title>ECMAScript 6 Statements</title>
<simpara>N4JS export and import statements are similar to ES6 with some minor d ifferences which are elaborated on below.</simpara>
<section xml:id="_let">
<title>Let</title>
<simpara>Cf. [<link linkend="ECMA11a">ECMA11a(p.13.2.1)</link>], also <link xl:href="http://www.2ality.com/2015/02/es6-scoping.html">Rauschmayer, 2ality: <emphasis role="strong">Variables and scoping in ECMAScript 6</emphasis></link></simpara>
</section>
<section xml:id="_const">
<title>Const</title>
<simpara>Cf. [<link linkend="ECMA15a">ECMA15a(p.13.2.1)</link>], also <link xl:href="http://www.2ality.com/2015/02/es6-scoping.html">Rauschmayer, 2ality: <emphasis role="strong">Variables and scoping in ECMAScript 6</emphasis></link></simpara>
<simpara>Additionally to the <literal>var</literal> statement, the <literal>const</literal> statement is supported.
It allows for declaring variables which must be assigned to a value in the declaration and their value must not change.
That is to say that constants are not allowed to be on the left-hand side of other assignments.</simpara>
<programlisting language="xtext" linenumbering="unnumbered">ConstStatement returns VariableStatement: 'const' varDecl+=ConstDeclaration ( ',' varDecl+=ConstDeclaration )* Semi;
ConstDeclaration returns VariableDeclaration: typeRef=TypeRef? name=IDENTIFIER const?='=' expression=AssignmentExpression;</programlisting>
<bridgehead xml:id="const-semantics" renderas="sect4">Semantics</bridgehead>
<simpara>A const variable statement is more or less a normal variable statement (see <xref linkend="_variable-statement"/>), except that all variables declared by that
statement are not writable (cf. <xref linkend="Req-IDE-121"/>).
This is similar to constant data fields (cf. <xref linkend="_assignment-modifiers"/>).</simpara>
<requirement xml:id="IDE-133">
<title>Writability of const variables</title>
<simpara>
<anchor xml:id="Req-IDE-133" xreflabel="[Req-IDE-133]"/>
<emphasis role="strong">Requirement: IDE-133:</emphasis>
<link linkend="Req-IDE-133">Writability of const variables</link> (ver. 1)</simpara>
<simpara>
All variable declarations of a const variable statement
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>S</mi><mi>t</mi><mi>m</mi><mi>t</mi></math> are not writeable:
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>v</mi><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mo></mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>S</mi><mi>t</mi><mi>m</mi><mi>t</mi><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi><mi>D</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>:</mi><mo>¬</mo><mi>v</mi><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mo>.</mo><mi>w</mi><mi>r</mi><mi>i</mi><mi>t</mi><mi>a</mi><mi>b</mi><mi>l</mi><mi>e</mi></math></simpara>
</requirement>
</section>
<section xml:id="_for-of-statement">
<title><literal>for &#8230;&#8203; of</literal> statement</title>
<simpara>ES6 introduced a new form of <literal>for</literal> statement: <literal>for &#8230;&#8203; of</literal> to iterate over the elements of an <literal>Iterable</literal>, cf. <xref linkend="_iterablen"/>.</simpara>
<bridgehead xml:id="for-of-statement-syntax" renderas="sect4">Syntax</bridgehead>
<simpara>See <xref linkend="_iteration-statements"/></simpara>
<bridgehead xml:id="for-of-statement-semantics" renderas="sect4">Semantics</bridgehead>
<requirement xml:id="IDE-134">
<title>for &#8230;&#8203; of statement</title>
<simpara>
<anchor xml:id="Req-IDE-134" xreflabel="[Req-IDE-134]"/>
<emphasis role="strong">Requirement: IDE-134:</emphasis>
<link linkend="Req-IDE-134">for ... of statement</link> (ver. 1)</simpara>
<simpara>
For a given <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi></math> the following conditions must hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>The value provided after <literal>of</literal> in a <literal>for &#8230;&#8203; of</literal> statement must be a subtype of <literal>Iterable&lt;?&gt;</literal>.</simpara>
</listitem>
<listitem>
<simpara>Either a new loop variable must be declared or an rvalue must be provided as init expression:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo>.</mo><mi>v</mi><mi>a</mi><mi>r</mi><mi>D</mi><mi>e</mi><mi>c</mi><mi>l</mi><mo></mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi><mo></mo><mfenced close=")" open="("><mrow><mi>f</mi><mo>.</mo><mi>i</mi><mi>n</mi><mi>i</mi><mi>t</mi><mi>E</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo></mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi><mo></mo><mi>i</mi><mi>s</mi><mi>R</mi><mi>V</mi><mi>a</mi><mi>l</mi><mi>u</mi><mi>e</mi><mfenced close=")" open="("><mrow><mi>f</mi><mo>.</mo><mi>i</mi><mi>n</mi><mi>i</mi><mi>t</mi><mi>E</mi><mi>x</mi><mi>p</mi><mi>r</mi></mrow></mfenced></mrow></mfenced></math></simpara>
</listitem>
<listitem>
<simpara>If a new variable <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>v</mi></math> is declared before <literal>of</literal> and it has a declared type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math>, the value provided after must be a subtype of <literal>Iterable&lt;?extendsT&gt;</literal>.
If <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>v</mi></math> does not have a declared type, the type of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>v</mi></math> is inferred to the type of the first type argument of the actual type of the value provided after <literal>of</literal>.</simpara>
</listitem>
<listitem>
<simpara>If a previously-declared variable is referenced before with a declared or inferred type of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math>, the value provided after <literal>of</literal> must be a subtype of <literal>Iterable&lt;?extendsT&gt;</literal>.</simpara>
</listitem>
</orderedlist>
<note>
<simpara><literal>Iterable</literal> is structurally typed on definition-site so non-N4JS types can meet the above requirements by simply implementing the only method in interface <literal>Iterable</literal> (with a correct return type).</simpara>
</note>
<note>
<simpara>The first of the above constraints (the type required by the ’of’ part in a <literal>for &#8230;&#8203; of</literal> loop is <literal>Iterable</literal>) was changed during the definition of ECMAScript 6.
This is implemented differently in separate implementations and even in different versions of the same implementation (for instance in different versions of V8).
Older implementations require an <literal>Iterator</literal> or accept both <literal>Iterator</literal> an or <literal>Iterable</literal>.</simpara>
</note>
<simpara>Requiring an <literal>Iterable</literal> and not accepting a plain <literal>Iterator</literal> seems to be the final decision (as of Dec. 2014).
For reference, see abstract operations <literal>GetIterator</literal> in [<link linkend="ECMA15a">ECMA15a(p.S7.4.2)</link>] and "CheckIterable" [<link linkend="ECMA15a">ECMA15a(p.S7.4.1)</link>] and their
application in "ForIn/OfExpressionEvaluation" [<link linkend="ECMA15a">ECMA15a(p.S13.6.4.8)</link>] and <literal>CheckIterable</literal> and their application in <literal>ForIn/OfExpressionEvaluation</literal>.
See also a related blog post <footnote><simpara>available at: <link xl:href="http://www.2ality.com/2013/06/iterators-generators.html">http://www.2ality.com/2013/06/iterators-generators.html</link></simpara></footnote> that is kept up to date with changes to ECMAScript 6:</simpara>
<blockquote>
<simpara><emphasis>ECMAScript 6 has a new loop, for-of. That loop works with iterables. Before we can use it with createArrayIterator(), we need to turn the result into an iterable.</emphasis></simpara>
</blockquote>
<simpara>An array or object destructuring pattern may be used left of the <literal>of</literal>.
This is used to destructure the elements of the <literal>Iterable</literal> on the right-hand side (not the <literal>Iterable</literal> itself).
For detais, see <xref linkend="_array-and-object-destructuring"/>.</simpara>
</requirement>
</section>
<section xml:id="_import-statement">
<title>Import Statement</title>
<simpara>Cf. ES6 import [<link linkend="ECMA15a">ECMA15a(p.15.2.2)</link>], see also <link xl:href="https://babeljs.io/docs/usage/modules/">https://babeljs.io/docs/usage/modules/</link></simpara>
<bridgehead xml:id="import-statement-syntax" renderas="sect4">Syntax</bridgehead>
<simpara>The grammar of import declarations is defined as follows:</simpara>
<programlisting language="xtext" linenumbering="unnumbered">ImportDeclaration:
{ImportDeclaration}
ImportDeclarationImpl
;
fragment ImportDeclarationImpl*:
'import' (
ImportClause importFrom?='from'
)? module=[types::TModule|ModuleSpecifier] Semi
;
fragment ImportClause*:
importSpecifiers+=DefaultImportSpecifier (',' ImportSpecifiersExceptDefault)?
| ImportSpecifiersExceptDefault
;
fragment ImportSpecifiersExceptDefault*:
importSpecifiers+=NamespaceImportSpecifier
| '{' (importSpecifiers+=NamedImportSpecifier (',' importSpecifiers+=NamedImportSpecifier)* ','?)? '}'
;
NamedImportSpecifier:
importedElement=[types::TExportableElement|BindingIdentifier&lt;Yield=false&gt;]
| importedElement=[types::TExportableElement|IdentifierName] 'as' alias=BindingIdentifier&lt;Yield=false&gt;
;
DefaultImportSpecifier:
importedElement=[types::TExportableElement|BindingIdentifier&lt;Yield=false&gt;]
;
NamespaceImportSpecifier: {NamespaceImportSpecifier} '*' 'as' alias=BindingIdentifier&lt;false&gt; (declaredDynamic?='+')?;
ModuleSpecifier: STRING;</programlisting>
<simpara>These are the properties of import declaration which can be specified by the user:</simpara>
<variablelist>
<varlistentry>
<term><literal>annotations</literal> </term>
<listitem>
<simpara>Arbitrary annotations, see <xref linkend="_annotations"/> and below for details.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>importSpecifiers</literal> </term>
<listitem>
<simpara>The elements to be imported with their names.</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>Also see compilation as described in <xref linkend="_modules"/>, for semantics see <xref linkend="import-statement-semantics"/>.</simpara>
<example>
<title>Import</title>
<programlisting language="n4js" linenumbering="unnumbered">import A from "p/A"
import {C,D,E} from "p/E"
import * as F from "p/F"
import {A as G} from "p/G"
import {A as H, B as I} from "p/H"</programlisting>
</example>
<bridgehead xml:id="import-statement-semantics" xreflabel="Import Statement Semantics" renderas="sect4">Semantics</bridgehead>
<simpara>Import statements are used to import identifiable elements from another module.
Identifiable elements are</simpara>
<itemizedlist>
<listitem>
<simpara>types (via their type declaration), in particular</simpara>
<itemizedlist>
<listitem>
<simpara>classifiers (classes, interfaces)</simpara>
</listitem>
<listitem>
<simpara>functions</simpara>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<simpara>variables and constants.</simpara>
</listitem>
</itemizedlist>
<simpara>The module to import from is identified by the string literal following keyword <literal>from</literal>.
This string must be a valid</simpara>
<itemizedlist>
<listitem>
<simpara>complete module specifier <footnote><simpara>For more details on module specifiers, see <xref linkend="_qualified-names"/>.</simpara></footnote>:</simpara>
<programlisting language="n4js" linenumbering="unnumbered"> import {A} from "ProjectA/a/b/c/M"</programlisting>
</listitem>
<listitem>
<simpara>plain module specifier:</simpara>
<programlisting language="n4js" linenumbering="unnumbered"> import {A} from "a/b/c/M"</programlisting>
</listitem>
<listitem>
<simpara>or project name only, assuming the project defines a main module in its <literal>package.json</literal> file (using the <literal>mainModule</literal> package.json property, see <xref linkend="package-json-mainModule"/>):</simpara>
<programlisting language="n4js" linenumbering="unnumbered"> import {A} from "ProjectA"</programlisting>
</listitem>
</itemizedlist>
<simpara>For choosing the element to import, there are the exact same options as in ECMAScript6:</simpara>
<itemizedlist>
<listitem>
<simpara>named imports select one or more elements by name, optionally introducing a local alias:</simpara>
<programlisting language="n4js" linenumbering="unnumbered"> import {C} from "M"
import {D as MyD} from "M"
import {E, F as MyF, G, H} from "M"</programlisting>
</listitem>
<listitem>
<simpara>namespace imports select all elements of the remote module for import and define a namespace name; the imported elements are then accessed via the namespace name:</simpara>
<programlisting language="n4js" linenumbering="unnumbered"> import * as N from "M"
var c: N.C = new N.C();</programlisting>
</listitem>
<listitem>
<simpara>default imports select whatever element was exported by the remote module as the default (there can be at most one default export per module):</simpara>
<programlisting language="n4js" linenumbering="unnumbered"> import C from "M"</programlisting>
</listitem>
<listitem>
<simpara>namespace imports provide access to the default export:</simpara>
<programlisting language="n4js" linenumbering="unnumbered"> import * as N from "M"
var c: N.default = new N.default();</programlisting>
</listitem>
</itemizedlist>
<simpara>The following constraints are defined on a (non-dynamic) import statement <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi></math>:</simpara>
<itemizedlist>
<listitem>
<simpara>The imported module needs to be accessible from the current project.</simpara>
</listitem>
<listitem>
<simpara>The imported declarations need to be accessible from the current module.</simpara>
</listitem>
</itemizedlist>
<simpara>For named imports, the following constraints must hold:</simpara>
<itemizedlist>
<listitem>
<simpara>No declaration must be imported multiple times, even if aliases are used.</simpara>
</listitem>
<listitem>
<simpara>The names must be unique in the module. They must not conflict with each other or locally declared variables, types, or functions.</simpara>
</listitem>
<listitem>
<simpara>Declarations imported via named imports are accessible only via used name (or alias) and not via original name directly.</simpara>
</listitem>
</itemizedlist>
<simpara>For wildcard imports, the following constraints must hold:</simpara>
<itemizedlist>
<listitem>
<simpara>Only one namespace import can be used per (target) module, even if different namespace name is used.</simpara>
</listitem>
<listitem>
<simpara>The namespace name must be unique in the module. They must not conflict with each other or locally declared variables, types, or functions.</simpara>
</listitem>
<listitem>
<simpara>Declarations imported via namespace import are accessible via namespace only and not with original name directly.</simpara>
</listitem>
</itemizedlist>
<simpara>For namespace imports, the following constraints must hold:</simpara>
<itemizedlist>
<listitem>
<simpara>If the referenced module is a plain <literal>js</literal> file, a warning will be created to use the dynamic import instead.</simpara>
</listitem>
</itemizedlist>
<simpara>For default imports, the following constraints must hold:</simpara>
<itemizedlist>
<listitem>
<simpara>The referenced module must have a default export.</simpara>
</listitem>
</itemizedlist>
<simpara>Cross-cutting constraints:</simpara>
<itemizedlist>
<listitem>
<simpara>No declaration can be imported via named import and namespace import at the same time.</simpara>
</listitem>
</itemizedlist>
<example>
<title>Imports</title>
<simpara>Imports cannot be duplicated:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">import * as A from 'A';
import * as A from 'A';//error, duplicated import statement
import B from 'B';
import B from 'B';//error, duplicated import statement</programlisting>
<simpara>Given element cannot be imported multiple times:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">import * as A1 from 'A';
import * as A2 from 'A';//error, elements from A already imported in A1
import B from 'B';
import B as B1 from 'B';//error, B/B is already imported as B
import C as C1 from 'C';
import C from 'C';//error, C/C is already imported as C1
import D as D1 from 'D';
import D as D2 from 'D';//error, D/D is already imported as D1
import * as NE from 'E';
import E from 'E';//error, E/E is already imported as NE.E
import F from 'F';
import * as NF from 'F';//error, F/F is already imported as F</programlisting>
<simpara><?asciidoc-pagebreak?></simpara>
<simpara>Names used in imports must not not conflict with each other or local
declarations:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">import * as A from 'A1';
import * as A from 'A2';//A is already used as namespace for A1
import B from 'B1';
import B1 as B from 'B2';//B us already used as import B/B1
import C1 as C from 'C1';
import * as C from 'C2'; //C is already used as import C1/C1
import * as D from 'D1';
import D2 as D from 'D2';//D is already used as namespace for D1
import E from 'E';
var E: any; // conflict with named import E/E
import * as F from 'F';
var F: any; // conflict with namespace F</programlisting>
<simpara>Using named imports, aliases and namespaces allows to refer to mulitple
types of the same name such as <literal>A/A</literal>, <literal>B/A</literal> and <literal>C/A</literal>:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">import A from 'A';// local name A referencess to A/A
import A as B from 'B';//local name B referencess to B/A
import * as C from 'C';//local name C.A referencess to C/A</programlisting>
<simpara><?asciidoc-pagebreak?></simpara>
<simpara>If a declaration has been imported with an alias or namespace, it is not
accessible via its original name:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">import * as B from 'A1';
import A2 as C from 'A2';
var a1_bad: A1;//error, A1/A1 is not directly accessible with original name
var a1_correct: B.A1;// A1/A1 is accessible via namespace B
var a2_bad: A2;//error, A2/A2 is not directly accessible with original name
var a2_correct: C;// A2/A2 is accessible via alias C</programlisting>
</example>
<section xml:id="Dynamic_Imports">
<title>Dynamic Imports</title>
<simpara>N4JS extends the ES6 module import in order that modules without a <literal>n4jsd</literal> or <literal>n4js</literal> file (plain <literal>js</literal> modules) can be imported.
This is done by adding <literal>+</literal> to the name of the named import. This form of compile-time import without type information is not
to be confused with import calls as described in <xref linkend="Import_Calls"/>, which are sometimes referred to as "dynamic import" as well.</simpara>
<requirement xml:id="IDE-136">
<title>Dynamic Import</title>
<simpara>
<anchor xml:id="Req-IDE-136" xreflabel="[Req-IDE-136]"/>
<emphasis role="strong">Requirement: IDE-136:</emphasis>
<link linkend="Req-IDE-136">Dynamic Import</link> (ver. 1)</simpara>
<simpara>
Let <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi></math> be an import
statement <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi></math> with a dynamic namespace specifier. The
following constraints must hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi><mo>.</mo><mi>m</mi><mi>o</mi><mi>d</mi><mi>u</mi><mi>l</mi><mi>e</mi></math> must not reference an <literal>n4js</literal> file.</simpara>
</listitem>
<listitem>
<simpara>If <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi><mo>.</mo><mi>m</mi><mi>o</mi><mi>d</mi><mi>u</mi><mi>l</mi><mi>e</mi></math> references an <literal>n4jsd</literal> file, a warning is
to be created.</simpara>
</listitem>
<listitem>
<simpara>If the file referenced by <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi><mo>.</mo><mi>m</mi><mi>o</mi><mi>d</mi><mi>u</mi><mi>l</mi><mi>e</mi></math> is not found, an
error is created just as in the static case.</simpara>
</listitem>
</orderedlist>
<simpara>These constraints define the error level when using dynamic import: we receive no error for <literal>js</literal>, a warning for <literal>n4jsd</literal>, and an error for <literal>n4js</literal> files.
The idea behind these distinct error levels is as follows:<?asciidoc-br?>
If only a plain <literal>js</literal> file is available, using the dynamic import is the only way to access elements from the <literal>js</literal> module.
This might be an unsafe way, but it allows the access and simplifies the first steps.
An <literal>n4jsd</literal> file may then be made available either by the developer using the <literal>js</literal> module or by a third-party library.
In this case, we do not want to break existing code.
There is only a warning created in the case of an available <literal>n4jsd</literal> file and a <literal>js</literal> file still must be provided by the user.
Having an <literal>n4js</literal> file is a completely different story; no <literal>n4jsd</literal> file is required, no <literal>js</literal> file is needed
(since the transpiler creates one from the <literal>n4js</literal> file) and there is absolutely no reason to use the module dynamically.</simpara>
</requirement>
</section>
<section xml:id="_immutabilaty-of-imports">
<title>Immutabilaty of Imports</title>
<simpara>Imports create always immutable bindings, c.f.
[<link linkend="ECMA15a">ECMA15a(p.8.1.1.5)</link>]
<link xl:href="http://www.ecma-international.org/ecma-262/6.0/index.html#sec-createimportbinding">http://www.ecma-international.org/ecma-262/6.0/index.html#sec-createimportbinding</link></simpara>
<requirement xml:id="IDE-137">
<title>Immutable Import</title>
<simpara>
<anchor xml:id="Req-IDE-137" xreflabel="[Req-IDE-137]"/>
<emphasis role="strong">Requirement: IDE-137:</emphasis>
<link linkend="Req-IDE-137">Immutable Import</link> (ver. 1)</simpara>
<simpara>
Let <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi></math> be a binding to an imported element.
It is an error if</simpara>
<itemizedlist>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi></math> occurs on the left-hand side as the assignment-target of an assignment expression (this also includes any level in a destructuring pattern on the left-hand side),</simpara>
</listitem>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi></math> as a direct argument of a postfix operator (<literal>i++</literal>/<literal>i--</literal>),</simpara>
</listitem>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi></math> as a direct argument of a <literal>delete</literal> operator,</simpara>
</listitem>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi></math> as a direct argument of the <literal>increment</literal> or <literal>decrement</literal> unary operator (<literal>i++</literal>/<literal>i--</literal>)</simpara>
</listitem>
</itemizedlist>
</requirement>
</section>
</section>
<section xml:id="_export-statement">
<title>Export Statement</title>
<simpara>Cf. ES6 import [<link linkend="ECMA15a">ECMA15a(p.15.2.3)</link>]</simpara>
<bridgehead xml:id="export-statement-syntax" renderas="sect4">Syntax</bridgehead>
<simpara>Grammar of export declarations is defined as follows:</simpara>
<programlisting language="xtext" linenumbering="unnumbered">ExportDeclaration:
{ExportDeclaration}
ExportDeclarationImpl
;
fragment ExportDeclarationImpl*:
'export' (
wildcardExport?='*' ExportFromClause Semi
| ExportClause -&gt;ExportFromClause? Semi
| exportedElement=ExportableElement
| defaultExport?='default' (-&gt;exportedElement=ExportableElement | defaultExportedExpression=AssignmentExpression&lt;In=true,Yield=false&gt; Semi)
)
;
fragment ExportFromClause*:
'from' reexportedFrom=[types::TModule|ModuleSpecifier]
;
fragment ExportClause*:
'{'
(namedExports+=ExportSpecifier (',' namedExports+=ExportSpecifier)* ','?)?
'}'
;
ExportSpecifier:
element=IdentifierRef&lt;Yield=false&gt; ('as' alias=IdentifierName)?
;
ExportableElement:
N4ClassDeclaration&lt;Yield=false&gt;
| N4InterfaceDeclaration&lt;Yield=false&gt;
| N4EnumDeclaration&lt;Yield=false&gt;
| ExportedFunctionDeclaration&lt;Yield=false&gt;
| ExportedVariableStatement
;</programlisting>
<simpara>These are the properties of export declaration, which can be specified by the user:</simpara>
<variablelist>
<varlistentry>
<term><literal>exportedElement</literal> </term>
<listitem>
<simpara>The element to be exported, can be a declaration or a variable/const statement.</simpara>
</listitem>
</varlistentry>
</variablelist>
<example>
<title>Export</title>
<programlisting language="n4js" linenumbering="unnumbered">export public class A{}
export interface B{}
export function foo() {}
export var a;
export const c="Hello";</programlisting>
</example>
<bridgehead xml:id="export-statement-semantics" renderas="sect4">Semantics</bridgehead>
<simpara>With regard to type inference, export statements are not handled at all.
Only the exported element is inferred and the <literal>export</literal> keyword is ignored.</simpara>
<simpara>In order to use types defined in other compilation units, these types have to be explicitly imported with an import statement.</simpara>
<simpara>Imported namespaces cannot be exported.</simpara>
<simpara>Declared elements (types, variables, functions) are usually only visible outside the declaring module if the elements are exported and imported (by the using module, cf. <xref linkend="_import-statement"/>).</simpara>
<simpara>Some special components (runtime environment and libraries, cf. <xref linkend="_runtime-environment-and-runtime-libraries"/>, may export elements globally.
This is done by annotating the export (or the whole module) with <literal>@Global</literal>, see <xref linkend="_global-definitions"/> for details.</simpara>
<simpara>By adding <literal>default</literal> after the keyword <literal>export</literal>, the identifiable element can be exported as ’the default’.
This can then be imported from other modules via default imports (see <xref linkend="_import-statement"/>).</simpara>
</section>
</section>
</chapter>
<chapter xml:id="_annotations">
<title>Annotations</title>
<section xml:id="_introduction-2" role="language-n4js">
<title>Introduction</title>
<simpara>Annotations are used to further define meta properties of language elements such as types, variables and functions.
These annotations are used by the compiler and validator to prohibit the developer from introducing constructs which are either not allowed or are unnecessary in certain contexts.</simpara>
<simpara>Since annotations are to be processed by the compiler and the compilation cannot be extended by third-party users for security reasons, annotations cannot be defined by developers.
Instead, the compiler comes with a predefined set of annotations which are summarized here.</simpara>
<section xml:id="_syntax-13">
<title>Syntax</title>
<simpara>Annotations are used similarly as in Java (although new annotations cannot be defined by the user).
They are formally defined as follows:</simpara>
<programlisting language="xtext" linenumbering="unnumbered">Annotation:'@' AnnotationNoAtSign;
ScriptAnnotation returns Annotation: '@@' AnnotationNoAtSign;
AnnotationNoAtSign returns Annotation:
name=AnnotationName (=&gt; '(' (args+=AnnotationArgument (',' args+=AnnotationArgument)*)? ')')?;
AnnotationArgument:
LiteralAnnotationArgument | TypeRefAnnotationArgument
;
LiteralAnnotationArgument:
literal=Literal
;
TypeRefAnnotationArgument:
typeRef=TypeRef
;</programlisting>
</section>
<section xml:id="_properties-7">
<title>Properties</title>
<simpara>We use the map notation for retrieving annotation properties and values from a list of annotations,
for example <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>x</mi><mo>.</mo><mi>a</mi><mi>n</mi><mi>n</mi><mi>o</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mi>s</mi><mfenced close="]" open="["><mrow><mi>R</mi><mi>e</mi><mi>q</mi><mi>u</mi><mi>i</mi><mi>r</mi><mi>e</mi><mi>d</mi></mrow></mfenced></math>, or shorter <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>x</mi><mi>@</mi><mi>R</mi><mi>e</mi><mi>q</mi><mi>u</mi><mi>i</mi><mi>r</mi><mi>e</mi><mi>d</mi></math>.</simpara>
<simpara><?asciidoc-pagebreak?></simpara>
</section>
<section xml:id="_element-specific-annotations">
<title>Element-Specific Annotations</title>
<simpara>The following annotations are element-specific and are explained in the corresponding sections:</simpara>
<table frame="all" rowsep="1" colsep="1">
<title>Element-Specific Annotations</title>
<tgroup cols="3">
<colspec colname="col_1" colwidth="33.3333*"/>
<colspec colname="col_2" colwidth="33.3333*"/>
<colspec colname="col_3" colwidth="33.3334*"/>
<thead>
<row>
<entry align="left" valign="top">Annotation</entry>
<entry align="center" valign="top">Element Types</entry>
<entry align="left" valign="top">Section</entry>
</row>
</thead>
<tbody>
<row>
<entry align="left" valign="top"><simpara><literal>@Internal</literal></simpara></entry>
<entry align="left" valign="top"><simpara><literal>TypeDefiningElement, Member, Function, Export</literal></simpara></entry>
<entry align="left" valign="top"><simpara><xref linkend="_access-control"/></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>@Undefined</literal></simpara></entry>
<entry align="left" valign="top"><simpara><literal>Variable</literal></simpara></entry>
<entry align="left" valign="top"><simpara><xref linkend="_undefined-type"/></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>@StringBased</literal></simpara></entry>
<entry align="left" valign="top"><simpara><literal>Enum</literal></simpara></entry>
<entry align="left" valign="top"><simpara><xref linkend="_string-based-enums"/></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>@Final</literal></simpara></entry>
<entry align="left" valign="top"><simpara><literal>Class, Member</literal></simpara></entry>
<entry align="left" valign="top"><simpara><xref linkend="_final-methods"/></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>@Spec</literal></simpara></entry>
<entry align="left" valign="top"><simpara><literal>FPar</literal></simpara></entry>
<entry align="left" valign="top"><simpara><xref linkend="Req-IDE-59"/></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>@Override</literal></simpara></entry>
<entry align="left" valign="top"><simpara><literal>Method</literal></simpara></entry>
<entry align="left" valign="top"><simpara><xref linkend="_redefinition-of-members"/></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>@Promisifiable</literal></simpara></entry>
<entry align="left" valign="top"><simpara><literal>Function</literal></simpara></entry>
<entry align="left" valign="top"><simpara><xref linkend="_promisifiable-functions"/></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>@Promisify</literal></simpara></entry>
<entry align="left" valign="top"><simpara><literal>CallExpression</literal></simpara></entry>
<entry align="left" valign="top"><simpara><xref linkend="_promisifiable-functions"/></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>@This</literal></simpara></entry>
<entry align="left" valign="top"><simpara><literal>Function</literal></simpara></entry>
<entry align="left" valign="top"><simpara><xref linkend="_this-keyword"/></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>@N4JS</literal></simpara></entry>
<entry align="left" valign="top"><simpara><literal>Class, Export Statement</literal></simpara></entry>
<entry align="left" valign="top"><simpara><xref linkend="_external-declarations"/></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>@IgnoreImplementation</literal></simpara></entry>
<entry align="left" valign="top"><simpara><literal>Script, ExportDeclaration, ExportableElement</literal></simpara></entry>
<entry align="left" valign="top"><simpara><xref linkend="_external-declarations"/></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>@Global</literal></simpara></entry>
<entry align="left" valign="top"><simpara><literal>External Declaration</literal></simpara></entry>
<entry align="left" valign="top"><simpara><xref linkend="_global-definitions"/></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>@ProvidedByRuntime</literal></simpara></entry>
<entry align="left" valign="top"><simpara><literal>External Declaration</literal></simpara></entry>
<entry align="left" valign="top"><simpara><xref linkend="_runtime-definitions"/></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>@TestAPI</literal></simpara></entry>
<entry align="left" valign="top"><simpara><literal>TypeDefiningElement, Member</literal></simpara></entry>
<entry align="left" valign="top"><simpara><xref linkend="_test-support"/></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>@Polyfill</literal></simpara></entry>
<entry align="left" valign="top"><simpara><literal>Class</literal></simpara></entry>
<entry align="left" valign="top"><simpara><xref linkend="_polyfill-definitions"/></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>@StaticPolyfill</literal></simpara></entry>
<entry align="left" valign="top"><simpara><literal>Class</literal></simpara></entry>
<entry align="left" valign="top"><simpara><xref linkend="_static-polyfill-definitions"/></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>@StaticPolyfillAware</literal></simpara></entry>
<entry align="left" valign="top"><simpara><literal>Script</literal></simpara></entry>
<entry align="left" valign="top"><simpara><xref linkend="_static-polyfill-definitions"/></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>@StaticPolyfillModule</literal></simpara></entry>
<entry align="left" valign="top"><simpara><literal>Script</literal></simpara></entry>
<entry align="left" valign="top"><simpara><xref linkend="_static-polyfill-definitions"/></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara><literal>@Transient</literal></simpara></entry>
<entry align="left" valign="top"><simpara><literal>Field</literal></simpara></entry>
<entry align="left" valign="top"><simpara><footnote><simpara>intended for internal use only; will be removed</simpara></footnote></simpara></entry>
</row>
</tbody>
</tgroup>
</table>
</section>
<section xml:id="_general-annotations">
<title>General Annotations</title>
<section xml:id="_idebug">
<title>IDEBUG</title>
<simpara><literal>@IDEBUG</literal> is an annotation similar to Java’s <literal>@SuppressWarnings</literal>.
It changes the severity of an issue from an error to a warning so that code can be compiled regardless of validation errors.
This is to be used for known IDE bugs only.</simpara>
</section>
</section>
<section xml:id="idebug-syntax">
<title>Syntax</title>
<programlisting language="xtext" linenumbering="unnumbered">'@IDEBUG' '(' bugID = INT ',' errorMessage=STRING ')'</programlisting>
<simpara>The annotation is defined transitively and repeatable on script, type declaration, function and method level.</simpara>
<section xml:id="_semantics-11">
<title>Semantics</title>
<simpara>This annotation will cause errors issued in the scope of the annotation (in the defined script, type, or method) to be transformed to warnings if their message text is similar to the <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><mi>r</mi><mi>r</mi><mi>o</mi><mi>r</mi><mi>M</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>a</mi><mi>g</mi><mi>e</mi></math> text.
If <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><mi>r</mi><mi>r</mi><mi>o</mi><mi>r</mi><mi>M</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>a</mi><mi>g</mi><mi>e</mi></math> ends with <literal></literal> (three dots as a single character, created by Eclipse to abbreviate messages), then the error’s message text must start with the specified text.</simpara>
<simpara>If no matching error is found, the annotation itself will issue an error.</simpara>
<example xml:id="ex:IDEBUG">
<title>IDEBUG Example</title>
<simpara>In the following code snippet, two errors are to be transformed to warnings.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">export class TestDataBridge with IModuleTest {
@IDEBUG(166, "{function(number):void} is not a subtype of {function(T):void}.") <co xml:id="CO6-1"/>
@IDEBUG(91, "Incorrect number of arguments: expected 1, got 2.") <co xml:id="CO6-2"/>
@Override public run(): void {
var foo = new Foo(),
cb = function(val: number): void {},
db = DataBridge.&lt;number&gt;bind(foo, "bar");
db.add(cb); <co xml:id="CO6-3"/>
Assert.isTrue(called);
}
}</programlisting>
<calloutlist>
<callout arearefs="CO6-1">
<para>The annotation transforms the error <literal>{function(number):void} is not a subtype of {function(T):void}</literal> into a warning with the following text:</para>
<programlisting language="n4js" linenumbering="unnumbered">IDEBUG-166: {function(number):void} is not a subtype of {function(T):void}.</programlisting>
<simpara>where <literal>IDEBUG-166</literal> refers to the corresponding bug report, that is <link xl:href="https://github.com/NumberFour/n4js/issues/166">IDEBUG-166</link>.</simpara>
</callout>
<callout arearefs="CO6-2">
<para>This annotation was proposed as a workaround for <link xl:href="https://github.com/NumberFour/n4js/issues/91">IDEBUG-91</link> which has been fixed.<?asciidoc-br?>
No error message is produced and an error will be issued on this line instead:</para>
<programlisting language="n4js" linenumbering="unnumbered">No matching error found, apparently bug IDEBUG-91 has been fixed or does not occur here.</programlisting>
</callout>
<callout arearefs="CO6-3">
<para>The first error occurs since there is a bug in the IDE type system (as of writing this example) where type arguments are not correctly bound in the case of function expressions used as callback methods.</para>
</callout>
</calloutlist>
</example>
</section>
<section xml:id="_suppress-warnings">
<title>Suppress Warnings</title>
<tip>
<simpara>This is not part of the current version</simpara>
</tip>
</section>
</section>
</section>
<section xml:id="_declaration-of-annotations">
<title>Declaration of Annotations</title>
<tip>
<simpara>This is not part of the current version</simpara>
</tip>
</section>
</chapter>
<chapter xml:id="_extended-fetaures">
<title>Extended Fetaures</title>
<section xml:id="_array-and-object-destructuring" role="language-n4js">
<title>Array and Object Destructuring</title>
<simpara>N4JS supports array and object destructuring as provided in ES6.
This is used to conveniently assign selected elements of an array or object to a number of newly-declared or pre-existing variables or to further destructure them by using nested
destructuring patterns <footnote><simpara>Further reading on <link linkend="_acronyms">DI</link> Basics: [<link linkend="Fowler04b">Fowler04b</link>; <link linkend="Prasanna09a">Prasanna09a</link>], Verification [<link linkend="Zhu13a">Zhu13a</link>; <link linkend="Hudli13a">Hudli13a</link>], Frameworks [<link linkend="Lesiecki08a">Lesiecki08a</link>; <link linkend="Betts13a">Betts13a</link>; <link linkend="Knol13a">Knol13a</link>; <link linkend="Dagger">Dagger</link>]</simpara></footnote>.</simpara>
<section xml:id="_syntax-14">
<title>Syntax</title>
<programlisting language="ebnf" linenumbering="unnumbered">BindingPattern &lt;Yield&gt;:
ObjectBindingPattern&lt;Yield&gt;
| ArrayBindingPattern&lt;Yield&gt;
;
ObjectBindingPattern &lt;Yield&gt; returns BindingPattern:
{BindingPattern}
'{' (properties+=BindingProperty&lt;Yield,AllowType=false&gt; (',' properties+=BindingProperty&lt;Yield,AllowType=false&gt;)*)? '}'
;
ArrayBindingPattern &lt;Yield&gt; returns BindingPattern:
{BindingPattern}
'['
elements+=Elision* (
elements+=BindingRestElement&lt;Yield&gt;
(',' elements+=Elision* elements+=BindingRestElement&lt;Yield&gt;)*
(',' elements+=Elision*)?
)?
']'
;
BindingProperty &lt;Yield, AllowType&gt;:
=&gt;(LiteralBindingPropertyName&lt;Yield&gt; ':') value=BindingElement&lt;Yield&gt;
| value=SingleNameBinding&lt;Yield,AllowType&gt;
;
fragment LiteralBindingPropertyName &lt;Yield&gt;*:
declaredName=IdentifierName | declaredName=STRING | declaredName=NumericLiteralAsString
// this is added here due to special treatment for a known set of expressions
| '[' (declaredName=SymbolLiteralComputedName&lt;Yield&gt; | declaredName=STRING) ']'
;</programlisting>
</section>
<section xml:id="_semantics-12">
<title>Semantics</title>
<simpara>The following example declares four variables <literal>a</literal>, <literal>b</literal>, <literal>x</literal>, and <literal>prop2</literal>. Variables <literal>a</literal> and <literal>x</literal> will have the value <literal>hello</literal>, whereas <literal>b</literal> and <literal>prop2</literal> will have value 42.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">var [a,b] = ["hello", 42];
var {prop1:x, prop2} = {prop1:"hello", prop2:42};</programlisting>
<simpara>In the case of <literal>prop2</literal>, we do not provide a property name and variable name separately; this is useful in cases where the property name also makes for a
suitable variable name (called <literal>single name binding</literal>).</simpara>
<simpara>One of the most useful use cases of destructuring is in a <literal>for..of</literal> loop.
Take this example:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">var arr1 = [ ["hello",1,2,3], ["goodbye",4,5,6] ];
for(var [head,...tail] of arr1) {
console.log(head,'/',tail);
}
// will print:
// hello / [ 1, 2, 3 ]
// goodbye / [ 4, 5, 6 ]
var arr2 = [ {key:"hello", value:42}, {key:"goodbye", value:43} ];
for(var {key,value} of arr2) {
console.log(key,'/',value);
}
// will print:
// hello / 42
// goodbye / 43</programlisting>
<simpara>Array and object destructuring pattern can appear in many different places:</simpara>
<itemizedlist>
<listitem>
<simpara>In a variable declaration (not just in variable statements but also in other places where variable declarations are allowed, e.g. plain for loops; called <emphasis>destructuring binding</emphasis>; see <xref linkend="_variable-statement"/>).</simpara>
</listitem>
<listitem>
<simpara>On the left-hand side of an assignment expression (the assignment expression is then called <emphasis>destructuring assignment</emphasis>; see <xref linkend="_assignment-expression"/>).</simpara>
</listitem>
<listitem>
<simpara>In a <literal>for..in</literal> or <literal>for..of</literal> loop on the left side of the <literal>in</literal>/<literal>of</literal> (see <xref linkend="_for-of-statement"/>).</simpara>
<note>
<simpara>It can also be used in plain statements, but then we actually have one of the above two use cases.</simpara>
</note>
</listitem>
<listitem>
<simpara>With lists of formal parameters or function arguments (not supported yet).</simpara>
</listitem>
</itemizedlist>
<simpara>For further details on array and object destructuring please refer to the ECMAScript 6 specification - [<link linkend="ECMA15a">ECMA15a</link>].</simpara>
<simpara>Type annotations can only be added when a new variable name is introduced since the short version would be ambiguous with the long one.
For example:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">var {x: someTypeOrNewVar} = ol</programlisting>
<simpara>could either mean that a new variable <literal>someTypeOrNewVar</literal> is declared and <literal>ol.x</literal> is assigned to it, or that a new variable <literal>x</literal> is declared with type <literal>someTypeOrNewVar</literal>.
The longer form would look like this:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">var {x: x: someType} = ol</programlisting>
<simpara>We can make this more readable:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">var {propOfOl: newVar: typeOfNewVar} = ol</programlisting>
</section>
</section>
<section xml:id="_dependency-injection" role="language-n4js">
<title>Dependency Injection</title>
<simpara>This chapter describes <link linkend="_acronyms">DI</link> mechanisms for N4JS.
This includes compiler, validation and language extensions that allow to achieve DI mechanisms built in into the N4JS language and IDE.</simpara>
<simpara>N4JS <link linkend="_acronyms">DI</link> support specifies a means for obtaining objects in such a way as to maximize reusability, testability and maintainability,
especially compared to traditional approaches such as constructors, factories and service locators.
While this can be achieved manually (without tooling support) it is difficult for nontrivial applications.
The solutions that DI provides should empower N4JS users to achieve the above goals without the burden of maintaining so-called ’boilerplate’ code.</simpara>
<figure xml:id="fig-di-terms">
<title>DI Basic Terms</title>
<mediaobject>
<imageobject>
<imagedata fileref="chapters/11_extFeatures/fig/diBasicTerms.png" width="60%" scalefit="1" align="center"/>
</imageobject>
<textobject><phrase>diBasicTerms</phrase></textobject>
</mediaobject>
</figure>
<simpara><emphasis>key: pass the dependency instead of letting the client create or find it</emphasis></simpara>
<simpara>Core terms</simpara>
<itemizedlist>
<listitem>
<simpara><emphasis role="strong">Service</emphasis> - A set of APIs describing the functionality of the service.</simpara>
</listitem>
<listitem>
<simpara><emphasis role="strong">Service Implementation</emphasis>s - One or more implementations of given service API.</simpara>
</listitem>
<listitem>
<simpara><emphasis role="strong">Client</emphasis> - Consumer of a given functionality, uses the given <emphasis role="strong">Service Implementation</emphasis>.</simpara>
</listitem>
<listitem>
<simpara><emphasis role="strong">Injector</emphasis> - Object providing <emphasis role="strong">Service Implementation</emphasis> of a specific <emphasis role="strong">Service</emphasis>, according to configuration.</simpara>
</listitem>
<listitem>
<simpara><emphasis role="strong">Binding</emphasis> - Part of configuration describing which interface implementing a subtype will be injected, when a given interface is requested.</simpara>
</listitem>
<listitem>
<simpara><emphasis role="strong">Provider</emphasis> - Factory used to create instances of a given <emphasis role="strong">Service Implementation</emphasis> or its sub-components, can be a method.</simpara>
</listitem>
<listitem>
<simpara><emphasis role="strong">Injection Point</emphasis> - Part of the user’s code that will have the given dependency injected. This is usually fields, method parameters, constructor parameters etc.</simpara>
</listitem>
<listitem>
<simpara><emphasis role="strong">DI configuration</emphasis> - This describes which elements of the user’s code are used in mechanisms and how they are wired.
It is derived from user code elements being marked with appropriate annotations, bindings and providers.</simpara>
</listitem>
<listitem>
<simpara><emphasis role="strong">di wiring</emphasis> - The code responsible for creating user objects.
These are injectors, type factories/providers, fields initiators etc.</simpara>
</listitem>
</itemizedlist>
<section xml:id="_di-components-and-injectors">
<title>DI Components and Injectors</title>
<simpara>N4JS’ <xref linkend="_dependency-injection"/> systems is based on the notion of <link linkend="_acronyms">DIC</link>.</simpara>
<definition>
<title>DI Component</title>
<simpara>
<anchor xml:id="di_component" xreflabel="[di_component]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="di_component">DI Component</link></simpara>
<simpara>
A <link linkend="_acronyms">DIC</link> is a N4Class annotated with <literal>@GenerateInjector</literal>.</simpara>
</definition>
<simpara>This annotation causes an <emphasis>injector</emphasis> to be created for (and associated to) the <link linkend="_acronyms">DI</link>.
DIC can be composed; meaning that when requested to inject an instance of a type, a DIC’s injector can delegate this request to the injector of the containing DIC.
An injector always prioritizes its own configuration before delegating to the container’s injector.
For validation purposes, a child DI can be annotated with <literal>@WithParent</literal> to ensure that it is always used with a proper parent.</simpara>
<simpara><emphasis>Injector</emphasis> is the main object of DI mechanisms responsible for creating object graphs of the application.
At runtime, injectors are instances of <literal>N4Injector</literal>.</simpara>
<requirement xml:id="IDE-138">
<title>DI Component and Injector</title>
<simpara>
<anchor xml:id="Req-IDE-138" xreflabel="[Req-IDE-138]"/>
<emphasis role="strong">Requirement: IDE-138:</emphasis>
<link linkend="Req-IDE-138">DI Component and Injector</link> (ver. 1)</simpara>
<simpara>
</simpara>
<simpara>The following constraints must hold for a class <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi></math> marked as DIC:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>A subclass <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi></math> of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi></math> is a DIC as well and it must be marked with <literal>GenerateInjector</literal>.</simpara>
</listitem>
<listitem>
<simpara>If a parent <link linkend="_dicomponent-relations">DIC</link> <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi></math> is specified via <literal>WithParent</literal>, then <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi></math> must be a DIC as well.</simpara>
</listitem>
<listitem>
<simpara>The injector associated to a DIC is of type <literal>N4Injector</literal>. It can be retrieved via <literal>N4Injector.of(DIC)</literal> in which <literal>DIC</literal> is the <literal>DIC</literal>.</simpara>
</listitem>
<listitem>
<simpara>Injectors associated to DIC a are DI-singletons (cf. <xref linkend="_singleton-scope"/>).
Two calls to <literal>N4Injector.of(DIC)</literal> are different (as different DIC are assumed).</simpara>
</listitem>
</orderedlist>
</requirement>
<requirement xml:id="IDE-139">
<title>Injection Phase</title>
<simpara>
<anchor xml:id="Req-IDE-139" xreflabel="[Req-IDE-139]"/>
<emphasis role="strong">Requirement: IDE-139:</emphasis>
<link linkend="Req-IDE-139">Injection Phase</link> (ver. 1)</simpara>
<simpara>
</simpara>
<simpara>We call the (transitive) creation and setting of values by an injector <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>I</mi></math> caused by the creation of an root object <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>R</mi></math> the <emphasis>injection phase</emphasis>.
If an instance <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi></math> is newly created by the injector <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>I</mi></math> (regardless of the injection point being used), the injection is transitively applied on <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi></math>.
The following constraints have to hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>Root objects are created by one of the following mechanisms:</simpara>
<orderedlist numeration="loweralpha">
<listitem>
<simpara>Any class or interface can be created as root objects via an injector associated to a DIC:<?asciidoc-br?>
<literal>var x: X = N4Injector.of(DIC).create(X);</literal><?asciidoc-br?>
in which <literal>DIC</literal> is a DIC.</simpara>
<simpara>Of course, an appropriate binding must exist. <footnote><simpara>Usually, only the <literal>DIC</literal> itself is created like that, e.g., <literal role="language-n4js">var dic = N4Injector.of(DIC).create(DIC);</literal></simpara></footnote></simpara>
</listitem>
<listitem>
<simpara>If a type has the injector being injected, e.g. via field injection <literal>@Inject injector: N4Injector;</literal>, then this injector can be used anytime in the control flow to create
a new root object similar as above (using <literal>create</literal> method).</simpara>
</listitem>
<listitem>
<simpara>If a provider has been injected (i.e. an instance of <literal>{N4Provider}</literal>), then its <literal>get()</literal> method can be used to create a root object causing a new injection phase to take place.</simpara>
</listitem>
</orderedlist>
</listitem>
<listitem>
<simpara>If <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi><mo>.</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math> is marked as injection point, all its arguments are set by the injector.
This is also true for an inherited constructor marked as an injection point.
See <xref linkend="Req-IDE-143"/> . For all arguments the injection phase constraints have to hold as well.</simpara>
</listitem>
<listitem>
<simpara>All fields of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi></math>, including <emphasis>inherited</emphasis> once, marked as injection points are set by the injector.
For all fields the injection phase constraints have to hold as well.</simpara>
</listitem>
</orderedlist>
<simpara>The injector may use a provider method (of a binder) to create nested instances.</simpara>
<simpara>The injector is configured with <emphasis>Binders</emphasis> and it tracks <emphasis>Bindings</emphasis> between types (<xref linkend="_binders-and-bindings"/>).
An N4JS developer normally would not interact with this object directly except when defining an entry-point to his application.
<emphasis>Injector</emphasis>s are configured with <emphasis>Binder</emphasis>s which contain explicit <emphasis>Binding</emphasis>s defined by an N4JS developer.
A set of these combined with <emphasis>implicit bindings</emphasis> creates the <emphasis>di configuration</emphasis> used by a given injector.
To configure given <emphasis>Injector</emphasis>s with given <emphasis>Binder</emphasis>(s) use <literal>@UseBinder</literal> annotation.</simpara>
</requirement>
<section xml:id="_dicomponent-relations">
<title>DIComponent Relations</title>
<simpara>A Parent-Child relation can be established between two DIComponents.
Child DIComponents use the parent bindings but can also be configured with their own bindings or <emphasis>change</emphasis> targets used by a parent.
The final circumstance is local to the child and is referred to as <emphasis>rebinding</emphasis>.
For more information about bindings see <xref linkend="_binders-and-bindings"/>.
A Child-Parent relation is expressed by the <literal>@WithParentInjector</literal> annotation attached to a given DIComponent.
When this relation is defined between DIComponents, the user needs to take care to preserve the proper relation between injectors.
In other words, the user must provide an instance of the parent injector (the injector of the DIComponent passes as a parameter to <literal>@WithParentInjector</literal>) when creating the child injector
(injector of the DIComponent annotated with <literal>@WithParentInjector</literal>).</simpara>
<example>
<title>Simple DIComponents Relation</title>
<programlisting language="n4js" linenumbering="unnumbered">@GenerateInjector
class ParentDIComponent{}
@GenerateInjector
@WithParentInjector(ParentDIComponent)
class ChildDIComponent{}
var parentInejctor = N4Inejctor.of(ParentDiCompoennt);
var childInjector = N4Inejctor.of(ChildDIComponent, parentInjector);</programlisting>
</example>
<simpara>With complex DIComponent structures, injector instances can be created with a directly-declared parent and also with any of its children.
This is due to the fact that any child can rebind types, add new bindings, but not remove them.
Any child is, therefore, <emphasis>compatible</emphasis> with its parents.</simpara>
<definition>
<title>Compatible DIComponent</title>
<simpara>
<anchor xml:id="compatible_dicomponent" xreflabel="[compatible_dicomponent]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="compatible_dicomponent">Compatible DIComponent</link></simpara>
<simpara>
A given DIComponent is compatible with another DIComponent if it has bindings for all keys in other component bindings.</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mo></mo><mi>D</mi><mi>I</mi><mi>C</mi><mn>1</mn><mo>,</mo><mi>D</mi><mi>I</mi><mi>C</mi><mn>2</mn><mi>:</mi><mi>D</mi><mi>I</mi><mi>C</mi><mn>1.</mn><mover accent="true"><mrow><mi>b</mi><mi>i</mi><mi>n</mi><mi>d</mi><mi>i</mi><mi>n</mi><mi>g</mi></mrow><mo>¯</mo></mover><mo>.</mo><mover accent="true"><mrow><mi>k</mi><mi>e</mi><mi>y</mi></mrow><mo>¯</mo></mover><mo></mo><mi>D</mi><mi>I</mi><mi>C</mi><mn>2.</mn><mover accent="true"><mrow><mi>b</mi><mi>i</mi><mi>n</mi><mi>d</mi><mi>i</mi><mi>n</mi><mi>g</mi></mrow><mo>¯</mo></mover><mo>.</mo><mover accent="true"><mrow><mi>k</mi><mi>e</mi><mi>y</mi></mrow><mo>¯</mo></mover><mo></mo><mi>D</mi><mi>I</mi><mi>C</mi><mn>2</mn><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>D</mi><mi>I</mi><mi>C</mi><mn>1</mn></math>
<note>
<simpara>Although subtype notation <math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/></math> is used here it does <emphasis role="strong">not</emphasis> imply actual subtype relations.
It was used in this instance for of lack of formal notations for DI concepts and because this is similar to the Liskov Substitution principle.</simpara>
</note>
<simpara>A complex Child-Parent relation between components is depicted in <xref linkend="fig-complex-dicomponents-relations"/> and <xref linkend="ex:complex-dicomponents-relations"/> below.</simpara>
<figure xml:id="fig-complex-dicomponents-relations">
<title>Complex DIComponents Relations</title>
<mediaobject>
<imageobject>
<imagedata fileref="chapters/11_extFeatures/fig/diagDICParentChild.svg" width="50%" scalefit="1" align="center"/>
</imageobject>
<textobject><phrase>diagDICParentChild</phrase></textobject>
</mediaobject>
</figure>
</definition>
<example xml:id="ex:complex-dicomponents-relations">
<title>Complex DIComponents Relations</title>
<programlisting language="n4js" linenumbering="unnumbered">@GenerateInjector class A {}
@GenerateInjector @WithParentInjector(A) class B {}
@GenerateInjector @WithParentInjector(B) class C {}
@GenerateInjector @WithParentInjector(C) class D {}
@GenerateInjector @WithParentInjector(A) class B2 {}
@GenerateInjector @WithParentInjector(B2) class C2 {}
@GenerateInjector @WithParentInjector(C2) class D2 {}
@GenerateInjector @WithParentInjector(A) class X {}
@GenerateInjector @WithParentInjector(C) class Y {}
// creating injectors
var injectorA = N4Injector.of(A);
//following throws DIConfigurationError, expected parent is not provided
//var injectorB = N4Injector.of(B);
//correct declarations
var injectorB = N4Injector.of(B, injectorA);
var injectorC = N4Injector.of(C, injectorB);
var injectorD = N4Injector.of(D, injectorC);
var injectorB2 = N4Injector.of(B2, injectorA);
var injectorC2 = N4Injector.of(C2, injectorB2);
var injectorD2 = N4Injector.of(D2, injectorC2);
//Any injector of {A,B,C,D,b2,C2,D2} s valid parent for injector of X, e.g. D or D2
N4Injector.of(X, injectorD);//is ok as compatible parent is provided
N4Injector.of(X, injectorD2);//is ok as compatible parent is provided
N4Injector.of(Y, injectorC);//is ok as direct parent is provided
N4Injector.of(Y, injectorD);//is ok as compatible parent is provided
N4Injector.of(Y, injectorB2);//throws DIConfigurationError, incompatible parent is provided
N4Injector.of(Y, injectorC2);//throws DIConfigurationError, incompatible parent is provided
N4Injector.of(Y, injectorD2);//throws DIConfigurationError, incompatible parent is provided</programlisting>
</example>
</section>
</section>
<section xml:id="_binders-and-bindings">
<title>Binders and Bindings</title>
<simpara><emphasis>Binder</emphasis> allows an N4JS developer to (explicitly) define a set of <emphasis>Binding</emphasis>s that will be used by an <emphasis>Injector</emphasis> configured with a given <emphasis>Binder</emphasis>.
There are two ways for <emphasis>Binder</emphasis> to define <emphasis>Binding</emphasis>s: <literal>@Bind</literal> (<xref linkend="_n4js-di-bind"/>) annotations and a method annotated with <literal>@Provides</literal>.</simpara>
<simpara><emphasis>Binder</emphasis> is declared by annotating a class with the <literal>@Binder</literal> annotation.</simpara>
<simpara>A <emphasis>Binding</emphasis> is part of a configuration that defines which instance of
what type should be injected into an <emphasis>injection point</emphasis> (<xref linkend="_injection-points"/>) with an expected type.</simpara>
<simpara><emphasis>Provider Method</emphasis> is essentially a <emphasis>factory method</emphasis> that is used to create an instance of a type.
N4JS allows a developer to declare those methods (see <xref linkend="_n4js-di-provides"/>) which gives them a hook in instance creation process.
Those methods will be used when creating instances by the <emphasis>Injector</emphasis> configured with the corresponding <emphasis>Binder</emphasis>.
A provider method is a special kind of binding (<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>k</mi><mi>e</mi><mi>y</mi></math>) in which the return type of the method is the <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>k</mi><mi>e</mi><mi>y</mi></math>.
The <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi></math> type is unknown at compile time (although it may be inferred by examining the return statements of the provide method).</simpara>
<definition>
<title>Binding</title>
<simpara>
<anchor xml:id="binding" xreflabel="[binding]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="binding">Binding</link></simpara>
<simpara>
A <emphasis>binding</emphasis> is a pair <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>b</mi><mi>i</mi><mi>n</mi><mi>d</mi><mfenced close=")" open="("><mrow><mi>k</mi><mi>e</mi><mi>y</mi></mrow><mrow><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi></mrow></mfenced></math>.
It defines that for a dependency with a given key which usually is the expected type at the injection point.
An instance of type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi></math> is injected.</simpara>
<simpara>A <emphasis>binding</emphasis> is called <emphasis>explicit</emphasis> if it is declared in the code, i.e. via <literal>@Bind</literal>
annotation or <literal>@Provides</literal> annotation).</simpara>
<simpara>A <emphasis>binding</emphasis> is called <emphasis>implicit</emphasis> if it is not declared.
An implicit binding can only be used if the <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>k</mi><mi>e</mi><mi>y</mi></math> is a class and derived from the type at the injection point, i.e. the type of the field or parameter to be injected.
In that case, the <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi></math> equals the <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>k</mi><mi>e</mi><mi>y</mi></math>.</simpara>
<simpara>A provider method <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi></math> (in the binder) defines a binding</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>b</mi><mi>i</mi><mi>n</mi><mi>d</mi><mfenced close=")" open="("><mrow><mi>M</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>n</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi></mrow><mi>X</mi></mfenced></math>
<simpara>(in which <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math> is an existential type with <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>X</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>n</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi></math>).</simpara>
<simpara>For simplification, we define:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mi>k</mi><mi>e</mi><mi>y</mi><mo>*</mo><mo>=</mo><mfenced close="" open="{"><mtable><mtr><mtd><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>n</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mo>,</mo></mtd><mtd><mstyle mathvariant="bold"><mtext>if target is provider method</mtext></mstyle></mtd></mtr><mtr><mtd><mi>k</mi><mi>e</mi><mi>y</mi><mo>,</mo></mtd><mtd><mstyle mathvariant="bold"><mtext>otherwise (key is a type reference)</mtext></mstyle></mtd></mtr></mtable></mfenced></mtd></mtr></mtable></math>
<simpara>and</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi><mo>*</mo><mo>=</mo><mfenced close="" open="{"><mtable><mtr><mtd><mi>X</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>n</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mo>,</mo></mtd><mtd><mstyle mathvariant="bold"><mtext>if target is provider method</mtext></mstyle></mtd></mtr><mtr><mtd><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi><mo>,</mo></mtd><mtd><mstyle mathvariant="bold"><mtext>otherwise (target is a type reference)</mtext></mstyle></mtd></mtr></mtable></mfenced></mtd></mtr></mtable></math>
</definition>
<requirement xml:id="IDE-140">
<title>Bindings</title>
<simpara>
<anchor xml:id="Req-IDE-140" xreflabel="[Req-IDE-140]"/>
<emphasis role="strong">Requirement: IDE-140:</emphasis>
<link linkend="Req-IDE-140">Bindings</link> (ver. 1)</simpara>
<simpara>
For a given binding <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>b</mi><mo>=</mo><mfenced close=")" open="("><mrow><mi>k</mi><mi>e</mi><mi>y</mi></mrow><mrow><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi></mrow></mfenced></math>, the following constraints must hold: <footnote><simpara>Note that other frameworks may define other constraints, e.g., arbitrary keys.</simpara></footnote></simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>k</mi><mi>e</mi><mi>y</mi></math> must be either a class or an interface.</simpara>
</listitem>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi></math> must either be a class or a provider method.</simpara>
</listitem>
<listitem>
<simpara>If <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>b</mi></math> is implicit, then <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>k</mi><mi>e</mi><mi>y</mi></math> must be a class.
If <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>k</mi><mi>e</mi><mi>y</mi></math> references a type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math>, then <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi><mo>=</mo><mi>T</mi></math> – even if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>k</mi><mi>e</mi><mi>y</mi></math> is a use-site structural type.</simpara>
</listitem>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>k</mi><mi>e</mi><mi>y</mi></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi><mo>*</mo></math> can be nominal, structural or field-structural types, either definition-site or use-site.
The injector and binder needs to take the different structural reference into account at runtime!</simpara>
</listitem>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi><mo>*</mo><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>k</mi><mi>e</mi><mi>y</mi></math> must hold</simpara>
</listitem>
<listitem>
<simpara>If during injection phase no binding for a given key is found, an <literal>DIUnsatisfiedBindingError</literal> is thrown.</simpara>
</listitem>
</orderedlist>
</requirement>
<requirement xml:id="IDE-141">
<title>Transitive Bindings</title>
<simpara>
<anchor xml:id="Req-IDE-141" xreflabel="[Req-IDE-141]"/>
<emphasis role="strong">Requirement: IDE-141:</emphasis>
<link linkend="Req-IDE-141">Transitive Bindings</link> (ver. 1)</simpara>
<simpara>
If an injector contains two given bindings <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>b</mi><mn>1</mn></msub><mo>=</mo><mfenced close=")" open="("><mrow><mi>k</mi><mi>e</mi><msub><mi>y</mi><mn>1</mn></msub></mrow><mrow><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><msub><mi>t</mi><mn>1</mn></msub></mrow></mfenced></math> and
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>b</mi><mn>2</mn></msub><mo>=</mo><mfenced close=")" open="("><mrow><mi>k</mi><mi>e</mi><msub><mi>y</mi><mn>2</mn></msub></mrow><mrow><mi>k</mi><mi>e</mi><msub><mi>y</mi><mn>1</mn></msub></mrow></mfenced></math>, an effective binding
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>b</mi><mo>=</mo><mfenced close=")" open="("><mrow><mi>k</mi><mi>e</mi><msub><mi>y</mi><mn>2</mn></msub></mrow><mrow><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><msub><mi>t</mi><mn>1</mn></msub></mrow></mfenced></math> is derived (replacing
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>b</mi><mn>1</mn></msub></math>).</simpara>
<simpara>N4JS <link linkend="_acronyms">DI</link> mechanisms don’t allow for injection of primitives or built-in types.
Only user-defined N4Types can be used. In cases where a user needs to inject a primitive or a built-in type, the developer must wrap it into its own
class <footnote><simpara>Cf. a blog post about micro types - <link xl:href="http://www.markhneedham.com/blog/2009/03/10/oo-micro-types/">http://www.markhneedham.com/blog/2009/03/10/oo-micro-types/</link>, and tiny types - <link xl:href="http://darrenhobbs.com/2007/04/11/tiny-types/">http://darrenhobbs.com/2007/04/11/tiny-types/</link></simpara></footnote>.
This is to say that none of the following metatypes can be bound: primitive types, enumerations, functions, object types, union- or intersection types. It is possible to (implicitly) bind to built-in classes.</simpara>
<simpara>While direct binding overriding or rebinding is not allowed, <emphasis>Injector</emphasis> can be configured in a way where one type can be separately bound to different types with implicit binding,
<emphasis>explicit binding</emphasis> and in bindings of the child injectors.
<emphasis>Binding precedence</emphasis> is a mechanism of <emphasis>Injector</emphasis> selecting a binding use for a type.
It operates in the following order:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>Try to use explicit binding, if this is not available:</simpara>
</listitem>
<listitem>
<simpara>Try to delegate to parent injectors (order of lookup is not guaranteed, first found is selected). If this is not available then:</simpara>
</listitem>
<listitem>
<simpara>Try to use use implicit binding, which is simply to attempt to create the instance.</simpara>
</listitem>
</orderedlist>
<simpara>If no binding for a requested type is available an error will be thrown.</simpara>
</requirement>
</section>
<section xml:id="_injection-points">
<title>Injection Points</title>
<simpara>By <emphasis>injection point</emphasis> we mean a place in the source code which, at runtime, will be expected to hold a reference to a particular type instance.</simpara>
<section xml:id="_field-injection">
<title>Field Injection</title>
<simpara>In its simplest form, this is a class field annotated with <literal>@Inject</literal> annotation.
At runtime, an instance of the containing class will be expected to hold reference to an instance of the field declared type.
Usually that case
is called <emphasis>Field Injection</emphasis>.</simpara>
<requirement xml:id="IDE-142">
<title>Field Injection</title>
<simpara>
<anchor xml:id="Req-IDE-142" xreflabel="[Req-IDE-142]"/>
<emphasis role="strong">Requirement: IDE-142:</emphasis>
<link linkend="Req-IDE-142">Field Injection</link> (ver. 1)</simpara>
<simpara>
The injector will inject the
following fields:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>All directly contained fields annotated with <literal>@Inject</literal>.</simpara>
</listitem>
<listitem>
<simpara>All inherited fields annotated with <literal>@Inject</literal>.</simpara>
</listitem>
<listitem>
<simpara>The injected fields will be created by the injector and their fields will be injected as well.</simpara>
</listitem>
</orderedlist>
</requirement>
<example>
<title>Simple Field Injection</title>
<simpara><xref linkend="ex:field-injection"/> demonstrates simple field injection using default bindings.
Note that all inherited fields (i.e. <literal>A.xInA</literal>) are injected and also fields in injected fields (i.e. <literal>x.y</literal>)</simpara>
<formalpara xml:id="ex:field-injection">
<title>Simple Field Injection</title>
<para>
<programlisting language="n4js" linenumbering="unnumbered">class X {
@Inject y: Y;
}
class Y {}
class A {
@Inject xInA: X;
}
class B extends A {
@Inject xInB: X;
}
@GenerateInjector
export public class DIC {
@Inject a: B;
}
var dic = N4Injector.of(DIC).create(DIC);
console.log(dic); // --&gt; DIC
console.log(dic.a); // --&gt; B
console.log(dic.a.xInA); // --&gt; X
console.log(dic.a.xInA.y); // --&gt; Y
console.log(dic.a.xInB); // --&gt; X
console.log(dic.a.xInB.y); // --&gt; Y</programlisting>
</para>
</formalpara>
</example>
</section>
<section xml:id="_constructor-injection">
<title>Constructor Injection</title>
<simpara>Parameters of the constructor can also be injected, in which case this is usually referred to as <emphasis>Constructor Inejction</emphasis>.
This is similar to <emphasis>Method Injection</emphasis> and while constructor injection is supported in N4JS, method injection is not (see remarks below).</simpara>
<simpara>When a constructor is annotated with <literal>@Inject</literal> annotation, all user-defined, non-generic types given as the parameters will be injected into the instance’s constructor created by the dependency injection framework.
Currently, optional constructor parameters are always initialized and created by the framework, therefore, they are ensured to be available at the constructor invocation time.
Unlike optional parameters, variadic parameters cannot be injected into a type’s constructor.
In case of annotating a constructor with <literal>@Inject</literal> that has variadic parameters, a validation error will be reported.
When a class’s constructor is annotated with <literal>@Inject</literal> annotation, it is highly recommended to annotate all explicitly-defined constructors at the subclass level.
If this is not done, the injection chain can break and runtime errors might occur due to undefined constructor parameters.
In the case of a possible broken injection chain due to missing <literal>@Inject</literal> annotations for any subclasses, a validation warning will
be reported.</simpara>
<requirement xml:id="IDE-143">
<title>Constructor Injection</title>
<simpara>
<anchor xml:id="Req-IDE-143" xreflabel="[Req-IDE-143]"/>
<emphasis role="strong">Requirement: IDE-143:</emphasis>
<link linkend="Req-IDE-143">Constructor Injection</link> (ver. 1)</simpara>
<simpara>
If a class <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi></math> has a constructor marked as injection point, the
following applies:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>If <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi></math> is subclassed by <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi></math>, and if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi></math> has no explicit constructor, then <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi></math> inherits the constructor from <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi></math> and it will be an injection point handled by the injector during injection phase.</simpara>
</listitem>
<listitem>
<simpara>If <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi></math> provides its own injector, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi><mo>.</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math> is no longer recognized by the injector during the injection phase.
There will be a warning generated in <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mo>.</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math> to mark it as injection point as well in order to prevent inconsistent injection behavior.
Still, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi><mo>.</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math> must be called in <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mo>.</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math> similarly to other overridden constructors.</simpara>
</listitem>
</orderedlist>
</requirement>
</section>
<section xml:id="_method-injection">
<title>Method Injection</title>
<simpara>Other kinds of injector points are method parameters where (usually) all method parameters are injected when the method is called.
In a way, constructor injection is a special case of the method itself.</simpara>
<section xml:id="_provider">
<title>Provider</title>
<simpara><emphasis>Provider</emphasis> is essentially a <emphasis>factory</emphasis> for a given type.
By injecting an <literal>N4Provider</literal> into any injection point, one can acquire new instances of a given type provided by the injected provider.
The providers prove useful when one has to solve re-injection issues since the depended type can be wired and injected via the provider rather than the dependency itself and can therefore obtain
new instances from it if required.
Provider can be also used as a means of delaying the instantiation time of a given type.</simpara>
<simpara><literal>N4Provider</literal> is a public generic built-in interface that is used to support the re-injection.
The generic type represents the dependent type that has to be obtained.
The <literal>N4Provider</literal> interface has one single public method: <literal>public T get()</literal> which should be invoked from the client code when a new instance of the dependent type is required.
Unlike any other unbound interfaces, the <literal>N4Provider</literal> can be injected without any explicit binding.</simpara>
<simpara>The following snippet demonstrates the usage of <literal>N4Provider</literal>:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class SomeService { }
@Singleton
class SomeSingletonService { }
class SomeClass {
@Inject serviceProvider: N4Provider&lt;SomeService&gt;;
@Inject singletonServiceProvider: N4Provider&lt;SomeSingletonService&gt;;
void foo() {
console.log(serviceProvider.get() ===
serviceProvider.get()); //false
console.log(singletonServiceProvider.get() ===
singletonServiceProvider.get()); //true
}
}</programlisting>
<simpara>It is important to note that the <literal>N4Provider</literal> interface can be extended by any user-defined interfaces and/or can be implemented by any user-defined classes.
For those user-defined providers, consider all binding-related rules; the extended interface, for example, must be explicitly bound via a binder to be injected.
The binding can be omitted only for the built-in <literal>N4Provider</literal>s.</simpara>
</section>
</section>
</section>
<section xml:id="_n4js-di-life-cycle-and-scopes">
<title>N4JS DI Life Cycle and Scopes</title>
<simpara><link linkend="_acronyms">DI</link> Life Cycle defines when a new instance is created by the injector as its destruction is handled by JavaScript.
The creation depends on the scope of the type.
Aside from the scopes, note that it is also possible to implement custom scopes and life cycle management via <literal>N4JSProvider</literal> and <literal>Binder@Provides</literal> methods.</simpara>
<section xml:id="_injection-cylces">
<title>Injection Cylces</title>
<definition>
<title>Injection Cycle</title>
<simpara>
<anchor xml:id="injection_cycle" xreflabel="[injection_cycle]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="injection_cycle">Injection Cycle</link></simpara>
<simpara>
We define an injection graph <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>G</mi><mfenced close=")" open="("><mi>V</mi><mi>E</mi></mfenced></math> as a directed graph as follows: <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>V</mi></math> (the vertices) is the set types of which instances are created during the injection phase and which use .
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>E</mi></math> (the edges) is a set of directed and labeled edges <math xmlns="http://www.w3.org/1998/Math/MathML"><mfenced close=")" open="("><msub><mi>v</mi><mn>1</mn></msub><msub><mi>v</mi><mn>2</mn></msub><mrow><mi>l</mi><mi>a</mi><mi>b</mi><mi>e</mi><mi>l</mi></mrow></mfenced></math>, where label indicates the injection point:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mfenced close=")" open="("><msub><mi>T</mi><mi>o</mi></msub><msub><mi>T</mi><mi>f</mi></msub><mrow><mi>"</mi><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mi>"</mi></mrow></mfenced></math>, if <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>T</mi><mi>f</mi></msub></math> is the actualy type of an an injected field of an instance of type <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>T</mi><mi>o</mi></msub></math></simpara>
</listitem>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mfenced close=")" open="("><msub><mi>T</mi><mi>c</mi></msub><msub><mi>T</mi><mi>p</mi></msub><mrow><mi>"</mi><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mi>"</mi></mrow></mfenced></math>, if <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>T</mi><mi>p</mi></msub></math> is the type of a parameter used in a constructor injection of type <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>T</mi><mi>c</mi></msub></math></simpara>
</listitem>
</orderedlist>
<simpara>One cycle in this graph is an injection cycle.</simpara>
</definition>
<simpara>When injecting instances into an object, cycles have to be detected and handled independently from the scope.
If this is not done, the following examples would result in an infinite loop causing the entire script to freeze until the engine reports an error:</simpara>
<informaltable frame="none" rowsep="1" colsep="0">
<tgroup cols="2">
<colspec colname="col_1" colwidth="57.1428*"/>
<colspec colname="col_2" colwidth="42.8572*"/>
<tbody>
<row>
<entry align="left" valign="bottom"><programlisting language="n4js" linenumbering="unnumbered">class A { @Inject b: B; }
class B { @Inject a: A; }</programlisting></entry>
<entry align="center" valign="top"><figure xml:id="fig-field-cycle">
<title>Field Cycle</title>
<mediaobject>
<imageobject>
<imagedata fileref="chapters/11_extFeatures/fig/injectionGraph_cycleField.svg" width="40%" scalefit="1" align="center"/>
</imageobject>
<textobject><phrase>injectionGraph cycleField</phrase></textobject>
</mediaobject>
</figure></entry>
</row>
<row>
<entry align="left" valign="bottom"><programlisting language="n4js" linenumbering="unnumbered">class C { @Inject constructor(d: D) {} }
class D { @Inject c: C; }</programlisting></entry>
<entry align="center" valign="top"><figure xml:id="fig-ctor-field">
<title>Ctor Field Cycle</title>
<mediaobject>
<imageobject>
<imagedata fileref="chapters/11_extFeatures/fig/injectionGraph_cycleCtorField.svg" width="40%" scalefit="1" align="center"/>
</imageobject>
<textobject><phrase>injectionGraph cycleCtorField</phrase></textobject>
</mediaobject>
</figure></entry>
</row>
<row>
<entry align="left" valign="bottom"><programlisting language="n4js" linenumbering="unnumbered">class E { @Inject constructor(f: F) {} }
class F { @Inject constructor(e: E) {} }</programlisting></entry>
<entry align="center" valign="top"><figure xml:id="fig-ctor-cycle">
<title>Ctor Cycle</title>
<mediaobject>
<imageobject>
<imagedata fileref="chapters/11_extFeatures/fig/injectionGraph_cycleCtor.svg" width="40%" scalefit="1" align="center"/>
</imageobject>
<textobject><phrase>injectionGraph cycleCtor</phrase></textobject>
</mediaobject>
</figure></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<simpara>The injector needs to detect these cycles and resolve them.</simpara>
<requirement xml:id="IDE-144">
<title>Resolution of Injection Cycles</title>
<simpara>
<anchor xml:id="Req-IDE-144" xreflabel="[Req-IDE-144]"/>
<emphasis role="strong">Requirement: IDE-144:</emphasis>
<link linkend="Req-IDE-144">Resolution of Injection Cycles</link> (ver. 1)</simpara>
<simpara>
A cycle <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mo></mo><mi>G</mi></math>, with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>G</mi></math> being an injection graph, is resolved as follows:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>If <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi></math> contains no edge with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>l</mi><mi>a</mi><mi>b</mi><mi>e</mi><mi>l</mi><mo>=</mo><mi>"</mi><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mi>"</mi></math>, the cycle is resolved using the algorithm described below.</simpara>
</listitem>
<listitem>
<simpara>If <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi></math> contains at least one edge with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>l</mi><mi>a</mi><mi>b</mi><mi>e</mi><mi>l</mi><mo>=</mo><mi>"</mi><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mi>"</mi></math>, a runtime exception is thrown.</simpara>
</listitem>
</orderedlist>
</requirement>
<simpara>Cycles stemming from field injection are resolved by halting the creation of new instances of types which have been already created by a containing instance.
The previously-created instance is then reused.
This makes injecting the instance of a (transitive) container less complicated and without the need to pass the container instance down the entire chain.
The following pseudo code describes the algorithm to create new instances which are injected into a newly created object:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">function injectDependencies(object) {
doInjectionWithCylceAwareness(object, {(typeof object -&gt; object)})
}
function doInjectionWithCylceAwareness(object, createdInstancesPerType) {
forall v $\in$ injectedVars of object {
var type = retrieveBoundType(v)
var instance = createdInstancesPerType.get(type)
if (not exists instance) {
instance = createInstance(type, createdInstancesPerType)
doInjectionWithCylceAwareness(instance,
createdInstancesPerType $\cap$ {(type-&gt;instance)})
}
v.value = instance;
}
}</programlisting>
<simpara>The actual instance is created in line 10 via <literal>createInstance</literal>.
This function then takes scopes into account.
The <literal>createdInstancesPerType</literal> map is passed to that function in order to enable cycle detection for constructor injection.
The following scopes are supported by the N4JS DI, other scopes, cf. <link xl:href="https://jersey.java.net/documentation/latest/ioc.html">Jersey custom scopes</link> and <link xl:href="https://github.com/google/guice/wiki/CustomScopes">Guice custom scopes</link>, may be added in the future.</simpara>
<simpara>This algorithm is not working for constructor injection because it is possible to already access all fields of the arguments passed to the constructor.
In the algorithm, however, the instances may not be completely initialized.</simpara>
</section>
<section xml:id="_default-scope">
<title>Default Scope</title>
<simpara>The default scope always creates a new instance.</simpara>
</section>
<section xml:id="_singleton-scope">
<title>Singleton Scope</title>
<simpara>The singleton scope (per injector) creates one instance (of the type with <literal>@Singleton</literal> scope) per injector, which is then shared between clients.</simpara>
<simpara>The injector will preserve a single instance of the type of <literal>S</literal> and will provide it to all injection points where type of <literal>S</literal> is used.
Assuming nested injectors without any declared binding where the second parameter is <literal>S</literal>, the same preserved singleton instance will be available for all nested injectors at all injection points as well.</simpara>
<simpara>The singleton preservation behavior changes when explicit bindings are declared for type <literal>S</literal> on the nested injector level.
Let&#8217;s assume that the type <literal>S</literal> exists and the type is annotated with <literal>@Singleton</literal>.
Furthermore, there is a declared binding where the binding&#8217;s second argument is <literal>S</literal>.
In that case, unlike in other dependency injection frameworks, nested injectors may preserve a singleton for itself and all descendant injectors with <literal>@Bind</literal> annotation.
In this case, the preserved singleton at the child injector level will be a different instance than the one at the parent injectors.</simpara>
<simpara>The tables below depict the expected runtime behavior of singletons used at different injector levels.
Assume the following are injectors: <literal>C</literal>, <literal>D</literal>, <literal>E</literal>, <literal>F</literal> and <literal>G</literal>. Injector <literal>C</literal> is the top most injector and its nesting injector <literal>D</literal>, hence injector <literal>C</literal> is the parent of the injector <literal>D</literal>.
Injector <literal>D</literal> is nesting <literal>E</literal> and so on.
The most nested injector is <literal>G</literal>. Let&#8217;s assume <literal>J</literal> is an interface, class <literal>U</literal> implements interface <literal>J</literal> and class <literal>V</literal> extends class <literal>U</literal>.
Finally assume both <literal>U</literal> and <literal>V</literal> are annotated with <literal>@Singleton</literal> at definition-site.</simpara>
<simpara><xref linkend="tab:diNoBindings"/> depicts the singleton preservation for nested injectors without any bindings.
All injectors use the same instance from a type.
Type <literal>J</literal> is not available at all since it is not bound to any concrete implementation:</simpara>
<table xml:id="tab:diNoBindings" frame="all" rowsep="1" colsep="1">
<title>DI No Bindings</title>
<tgroup cols="6">
<colspec colname="col_1" colwidth="28.5714*"/>
<colspec colname="col_2" colwidth="14.2857*"/>
<colspec colname="col_3" colwidth="14.2857*"/>
<colspec colname="col_4" colwidth="14.2857*"/>
<colspec colname="col_5" colwidth="14.2857*"/>
<colspec colname="col_6" colwidth="14.2858*"/>
<tbody>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">Binding</emphasis></simpara></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">Injector nesting (<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>&gt;</mo></math>)</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara>C</simpara></entry>
<entry align="center" valign="top"><simpara>D</simpara></entry>
<entry align="center" valign="top"><simpara>E</simpara></entry>
<entry align="center" valign="top"><simpara>F</simpara></entry>
<entry align="center" valign="top"><simpara>G</simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">J</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>N</mi><mi>a</mi><mi>N</mi></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>N</mi><mi>a</mi><mi>N</mi></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>N</mi><mi>a</mi><mi>N</mi></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>N</mi><mi>a</mi><mi>N</mi></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>N</mi><mi>a</mi><mi>N</mi></math></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">U</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">V</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></simpara></entry>
</row>
</tbody>
</tgroup>
</table>
<simpara><xref linkend="tab:diTransitiveBindings"/> is configured by explicit bindings. At the root injector level, type <literal>J</literal> is bound to type <literal>U</literal>.
Since the second argument of the binding is declared as a singleton at the definition-site,
this explicit binding implicitly ensures that the injector and all of its descendants preserve a singleton of the bound type <literal>U</literal>.
At injector level <literal>C</literal>, <literal>D</literal> and <literal>E</literal>, the same instance is used for type <literal>J</literal> which is type <literal>U</literal> at runtime.
At injector level <literal>E</literal> there is an additional binding from type <literal>U</literal> to type <literal>V</literal> that overrules the binding declared at the root injector level.
With this binding, each places where <literal>J</literal> is declared, type <literal>U</literal> is used at runtime.</simpara>
<simpara>Furthermore, since <literal>V</literal> is declared as a singleton, both injector <literal>F</literal> and <literal>G</literal> are using a shared singleton instance of type <literal>V</literal>.
Finally, for type <literal>V</literal>, injector <literal>C</literal>, <literal>D</literal> and <literal>E</literal> should use a separate instance of <literal>V</literal> other than injector level <literal>F</literal> and <literal>G</literal> because <literal>V</literal> is preserved at injector level <literal>F</literal> with the <literal>U</literal> <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo></math> <literal>V</literal> binding.</simpara>
<table xml:id="tab:diTransitiveBindings" frame="all" rowsep="1" colsep="1">
<title>DI Transitive Bindings</title>
<tgroup cols="6">
<colspec colname="col_1" colwidth="28.5714*"/>
<colspec colname="col_2" colwidth="14.2857*"/>
<colspec colname="col_3" colwidth="14.2857*"/>
<colspec colname="col_4" colwidth="14.2857*"/>
<colspec colname="col_5" colwidth="14.2857*"/>
<colspec colname="col_6" colwidth="14.2858*"/>
<thead>
<row>
<entry align="center" valign="top">Binding</entry>
<entry align="center" valign="top">J &#8594; U</entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top">U &#8594; V</entry>
<entry align="center" valign="top"></entry>
</row>
</thead>
<tbody>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">Injector nesting (&gt;)</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara>C</simpara></entry>
<entry align="center" valign="top"><simpara>D</simpara></entry>
<entry align="center" valign="top"><simpara>E</simpara></entry>
<entry align="center" valign="top"><simpara>F</simpara></entry>
<entry align="center" valign="top"><simpara>G</simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">J</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">U</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">V</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>1</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>1</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>1</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></simpara></entry>
</row>
</tbody>
</tgroup>
</table>
<simpara><xref linkend="tab:diReBinding"/> depicts the singleton behaviour but unlike the above
table, the bindings are declared for the interface <literal>J</literal>.</simpara>
<table xml:id="tab:diReBinding" frame="all" rowsep="1" colsep="1">
<title>DI Re - Binding</title>
<tgroup cols="6">
<colspec colname="col_1" colwidth="28.5714*"/>
<colspec colname="col_2" colwidth="14.2857*"/>
<colspec colname="col_3" colwidth="14.2857*"/>
<colspec colname="col_4" colwidth="14.2857*"/>
<colspec colname="col_5" colwidth="14.2857*"/>
<colspec colname="col_6" colwidth="14.2858*"/>
<thead>
<row>
<entry align="center" valign="top">Binding</entry>
<entry align="center" valign="top">J &#8594; U</entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top">J &#8594; V</entry>
<entry align="center" valign="top"></entry>
</row>
</thead>
<tbody>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">Injector nesting (<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>&gt;</mo></math>)</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara>C</simpara></entry>
<entry align="center" valign="top"><simpara>D</simpara></entry>
<entry align="center" valign="top"><simpara>E</simpara></entry>
<entry align="center" valign="top"><simpara>F</simpara></entry>
<entry align="center" valign="top"><simpara>G</simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">J</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">U</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">V</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>1</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>1</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>1</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></simpara></entry>
</row>
</tbody>
</tgroup>
</table>
<simpara><xref linkend="tab:diChildBinding"/> describes the singleton behavior when both bindings are configured at child injector levels but not the root injector level.</simpara>
<table xml:id="tab:diChildBinding" frame="all" rowsep="1" colsep="1">
<title>DI Child Binding</title>
<tgroup cols="6">
<colspec colname="col_1" colwidth="28.5714*"/>
<colspec colname="col_2" colwidth="14.2857*"/>
<colspec colname="col_3" colwidth="14.2857*"/>
<colspec colname="col_4" colwidth="14.2857*"/>
<colspec colname="col_5" colwidth="14.2857*"/>
<colspec colname="col_6" colwidth="14.2858*"/>
<thead>
<row>
<entry align="center" valign="top">Binding</entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top">U <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo></math> V</entry>
<entry align="center" valign="top"></entry>
<entry align="center" valign="top">J <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo></math> U</entry>
<entry align="center" valign="top"></entry>
</row>
</thead>
<tbody>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">Injector nesting (<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>&gt;</mo></math>)</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara>C</simpara></entry>
<entry align="center" valign="top"><simpara>D</simpara></entry>
<entry align="center" valign="top"><simpara>E</simpara></entry>
<entry align="center" valign="top"><simpara>F</simpara></entry>
<entry align="center" valign="top"><simpara>G</simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">J</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>N</mi><mi>a</mi><mi>N</mi></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>N</mi><mi>a</mi><mi>N</mi></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>N</mi><mi>a</mi><mi>N</mi></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">U</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>1</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><emphasis role="strong">V</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>1</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></simpara></entry>
<entry align="center" valign="top"><simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></simpara></entry>
</row>
</tbody>
</tgroup>
</table>
</section>
<section xml:id="_per-injection-chain-singleton">
<title>Per Injection Chain Singleton</title>
<simpara>The per injection chain singleton is ’between’ the default and singleton scope.
It can be used in order to explicitly describe the situation which happens when a simple cycle is resolved automatically.
It has more effects that lead to a more deterministic behavior.</simpara>
<simpara>Assume a provider declared as</simpara>
<programlisting language="n4js" linenumbering="unnumbered">var pb: Provider&lt;B&gt;;</programlisting>
<simpara>to be available:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">@PerInjectionSingleton
class A { }
class B { @Inject a: A; @Inject a1: A;}
b1=pb.get();
b2=pb.get();
b1.a != b2.a
b1.a == b1.a1
b2.a == b2.a1</programlisting>
<programlisting language="n4js" linenumbering="unnumbered">@Singleton
class A { }
class B { @Inject a: A; @Inject a1: A;}
b1=pb.get();
b2=pb.get();
b1.a == b2.a
b1.a == b1.a1
b2.a == b2.a1</programlisting>
<programlisting language="n4js" linenumbering="unnumbered">// no annotation
class A { }
class B { @Inject a A; @Inject a1: A;}
b1=pb.get();
b2=pb.get();
b1.a != b2.a
b1.a != b1.a1
b2.a != b2.a1</programlisting>
</section>
</section>
<section xml:id="_validation-of-callsites-targeting-n4injector-methods">
<title>Validation of callsites targeting N4Injector methods</title>
<simpara>Terminology for this section:</simpara>
<itemizedlist>
<listitem>
<simpara>a value is <emphasis role="strong">injectable</emphasis> if it</simpara>
<itemizedlist>
<listitem>
<simpara>either conforms to a user-defined class or interface (a non-parameterized one, that is),</simpara>
</listitem>
<listitem>
<simpara>or conforms to Provider-of-T where T is injectable itself.</simpara>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<simpara>a classifier declaring injected members is said to <emphasis role="strong">require injection</emphasis></simpara>
</listitem>
</itemizedlist>
<simpara>To better understand the validations in effect for callsites targeting</simpara>
<programlisting language="n4js" linenumbering="unnumbered">N4Injector.of(ctorOfDIC: constructor{N4Object}, parentDIC: N4Injector?, ...providedBinders: N4Object)</programlisting>
<simpara>we can recap that at runtime:</simpara>
<itemizedlist>
<listitem>
<simpara>The first argument denotes a DIC constructor.</simpara>
</listitem>
<listitem>
<simpara>The second (optional) argument is an injector.</simpara>
</listitem>
<listitem>
<simpara>Lastly, the purpose of <literal>providedBinders</literal> is as follows:</simpara>
<itemizedlist>
<listitem>
<simpara>The DIC above is marked with one or more <literal>@UseBinder</literal>.</simpara>
</listitem>
<listitem>
<simpara>Some of those binders may require injection.</simpara>
</listitem>
<listitem>
<simpara>Some of those binders may have constructor(s) taking parameters.</simpara>
</listitem>
<listitem>
<simpara>The set of binders described above should match the providedBinders.</simpara>
</listitem>
</itemizedlist>
</listitem>
</itemizedlist>
<simpara>Validations in effect for <literal>N4Injector.create(type{T} ctor)</literal> callsites:</simpara>
<itemizedlist>
<listitem>
<simpara><literal>type{T}</literal> should be injectable (in particular, it may be an <literal>N4Provider</literal>).</simpara>
</listitem>
</itemizedlist>
</section>
<section xml:id="_n4js-di-annotations">
<title>N4JS DI Annotations</title>
<simpara>Following annotations describe API used to configure N4JSDI.</simpara>
<section xml:id="_n4js-di-generateinjector">
<title>N4JS DI @GenerateInjector</title>
<informaltable frame="none" rowsep="0" colsep="0">
<tgroup cols="3">
<colspec colname="col_1" colwidth="33.3333*"/>
<colspec colname="col_2" colwidth="33.3333*"/>
<colspec colname="col_3" colwidth="33.3334*"/>
<tbody>
<row>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>name</term>
<listitem>
<simpara>@GenerateInjector</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>targets</term>
<listitem>
<simpara>N4Class</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>retention policy</term>
<listitem>
<simpara>RUNTIME</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
</row>
<row>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>transitive</term>
<listitem>
<simpara>NO</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>repeatable</term>
<listitem>
<simpara>NO</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>arguments</term>
<listitem>
<simpara>NO</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<simpara><literal>@GenerateInjector</literal> marks a given class as DIComponent of the graph.
The generated injector will be responsible for creating an instance of that class and all of its dependencies.</simpara>
</section>
<section xml:id="_n4js-di-withparentinjector">
<title>N4JS DI @WithParentInjector</title>
<informaltable frame="none" rowsep="0" colsep="0">
<tgroup cols="3">
<colspec colname="col_1" colwidth="33.3333*"/>
<colspec colname="col_2" colwidth="33.3333*"/>
<colspec colname="col_3" colwidth="33.3334*"/>
<tbody>
<row>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>name</term>
<listitem>
<simpara>@WithParentInjector</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>targets</term>
<listitem>
<simpara>N4Class</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>retention policy</term>
<listitem>
<simpara>RUNTIME</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
</row>
<row>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>transitive</term>
<listitem>
<simpara>NO</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>repeatable</term>
<listitem>
<simpara>NO</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>arguments</term>
<listitem>
<simpara>TypeRef</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<simpara><literal>@WithParentInjector</literal> marks given <emphasis>injector</emphasis> as depended on other <emphasis>injector</emphasis>.
The depended <emphasis>injector</emphasis> may use provided <emphasis>injector</emphasis> to create instances of objects required in its object graph.</simpara>
<simpara>Additional <emphasis>WithParentInjector</emphasis> constraints:</simpara>
<requirement xml:id="IDE-145">
<title>DI WithParentInjector</title>
<simpara>
<anchor xml:id="Req-IDE-145" xreflabel="[Req-IDE-145]"/>
<emphasis role="strong">Requirement: IDE-145:</emphasis>
<link linkend="Req-IDE-145">DI WithParentInjector</link> (ver. 1)</simpara>
<simpara>
1. Allowed only on <literal>N4ClassDeclarations</literal> annotated with <literal>@GenerateInjector</literal>.
2. Its parameter can only be <literal>N4ClassDeclarations</literal> annotated with .</simpara>
</requirement>
</section>
<section xml:id="_n4js-di-usebinder">
<title>N4JS DI @UseBinder</title>
<informaltable frame="none" rowsep="0" colsep="0">
<tgroup cols="3">
<colspec colname="col_1" colwidth="33.3333*"/>
<colspec colname="col_2" colwidth="33.3333*"/>
<colspec colname="col_3" colwidth="33.3334*"/>
<tbody>
<row>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>name</term>
<listitem>
<simpara>@UseBinder</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>targets</term>
<listitem>
<simpara>N4Class</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>retention policy</term>
<listitem>
<simpara>RUNTIME</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
</row>
<row>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>transitive</term>
<listitem>
<simpara>NO</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>arguments</term>
<listitem>
<simpara>TypeRef</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>arguments are optional</term>
<listitem>
<simpara>NO</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<simpara><literal>@UseBinder</literal> describes <emphasis>Binder</emphasis> to be used (configure) target <emphasis>Injector</emphasis>.</simpara>
<requirement xml:id="IDE-146">
<title>DI UseInjector</title>
<simpara>
<anchor xml:id="Req-IDE-146" xreflabel="[Req-IDE-146]"/>
<emphasis role="strong">Requirement: IDE-146:</emphasis>
<link linkend="Req-IDE-146">DI UseInjector</link> (ver. 1)</simpara>
<simpara>
1. Allowed only on <literal>N4ClassDeclarations</literal> annotated with <literal>@GenerateInjector</literal>.
2. Its parameter can only be <literal>N4ClassDeclarations</literal> annotated with <literal>@Binder</literal>.</simpara>
</requirement>
</section>
<section xml:id="_n4js-di-binder">
<title>N4JS DI @Binder</title>
<informaltable frame="none" rowsep="0" colsep="0">
<tgroup cols="3">
<colspec colname="col_1" colwidth="33.3333*"/>
<colspec colname="col_2" colwidth="33.3333*"/>
<colspec colname="col_3" colwidth="33.3334*"/>
<tbody>
<row>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>name</term>
<listitem>
<simpara>@Binder</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>targets</term>
<listitem>
<simpara>N4Class</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>retention policy</term>
<listitem>
<simpara>RUNTIME</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
</row>
<row>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>transitive</term>
<listitem>
<simpara>NO</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>repeatable</term>
<listitem>
<simpara>NO</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>arguments</term>
<listitem>
<simpara>NONE</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<simpara><literal>@Binder</literal> defines a list of bind configurations.
That can be either <literal>@Bind</literal> annotations on <literal>@Binder</literal> itself or its factory methods annotated with <literal>@Provides</literal>.</simpara>
<requirement xml:id="IDE-147">
<title>DI binder</title>
<simpara>
<anchor xml:id="Req-IDE-147" xreflabel="[Req-IDE-147]"/>
<emphasis role="strong">Requirement: IDE-147:</emphasis>
<link linkend="Req-IDE-147">DI binder</link> (ver. 1)</simpara>
<simpara>
1. Target <literal>N4ClassDeclaration</literal> must not be <emphasis>abstract</emphasis>.
2. Target <literal>N4ClassDeclaration</literal> must not be annotated with <literal>@GenerateInjector</literal>.
3. Target class cannot have <emphasis>injection points</emphasis>.</simpara>
</requirement>
</section>
<section xml:id="_n4js-di-bind">
<title>N4JS DI @Bind</title>
<informaltable frame="none" rowsep="0" colsep="0">
<tgroup cols="3">
<colspec colname="col_1" colwidth="33.3333*"/>
<colspec colname="col_2" colwidth="33.3333*"/>
<colspec colname="col_3" colwidth="33.3334*"/>
<tbody>
<row>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>name</term>
<listitem>
<simpara>@Bind</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>targets</term>
<listitem>
<simpara>N4ClassDeclaration</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>retention policy</term>
<listitem>
<simpara>RUNTIME</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
</row>
<row>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>transitive</term>
<listitem>
<simpara>NO</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>arguments</term>
<listitem>
<simpara>TypeRef key, TypeRef target</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>arguments are optional</term>
<listitem>
<simpara>NO</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<simpara>Defines <emphasis>binding</emphasis> between type and subtype that will be used by injector when configured with target <xref linkend="_n4js-di-binder"/>.
See also <xref linkend="_validation-of-callsites-targeting-n4injector-methods"/> for description of injectable types.</simpara>
<requirement xml:id="IDE-148">
<title>DI Bind</title>
<simpara>
<anchor xml:id="Req-IDE-148" xreflabel="[Req-IDE-148]"/>
<emphasis role="strong">Requirement: IDE-148:</emphasis>
<link linkend="Req-IDE-148">DI Bind</link> (ver. 1)</simpara>
<simpara>
1. Allowed only on <literal>N4ClassDeclarations</literal> that are annotated with <literal>@Binder</literal>(<xref linkend="_n4js-di-binder"/>).
2. Parameters are instances of one of the values described in <xref linkend="_validation-of-callsites-targeting-n4injector-methods"/>.
3. The second parameter must be a subtype of the first one.</simpara>
</requirement>
</section>
<section xml:id="_n4js-di-provides">
<title>N4JS DI @Provides</title>
<informaltable frame="none" rowsep="0" colsep="0">
<tgroup cols="3">
<colspec colname="col_1" colwidth="33.3333*"/>
<colspec colname="col_2" colwidth="33.3333*"/>
<colspec colname="col_3" colwidth="33.3334*"/>
<tbody>
<row>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>name</term>
<listitem>
<simpara>@Provides</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>targets</term>
<listitem>
<simpara>N4MethodDeclaration</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>retention policy</term>
<listitem>
<simpara>RUNTIME</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
</row>
<row>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>transitive</term>
<listitem>
<simpara>NO</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>repeatable</term>
<listitem>
<simpara>NO</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>arguments</term>
<listitem>
<simpara>NONE</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<simpara><literal>@Provides</literal> marks <emphasis>factory method</emphasis> to be used as part <link linkend="AC">DI</link>.
This is treated as <emphasis>explicit binding</emphasis> between declared return type and actual return type.
This method is expected to be part of the <literal>@Binder</literal>.
Can be used to implement custom scopes.</simpara>
<requirement xml:id="IDE-149">
<title>DI Provides</title>
<simpara>
<anchor xml:id="Req-IDE-149" xreflabel="[Req-IDE-149]"/>
<emphasis role="strong">Requirement: IDE-149:</emphasis>
<link linkend="Req-IDE-149">DI Provides</link> (ver. 1)</simpara>
<simpara>
</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>Allowed only on <literal>N4MethodDeclarations</literal> that are part of a classifier annotated with <literal>@Binder</literal>.</simpara>
</listitem>
<listitem>
<simpara>Annotated method declared type returns instance of one of the types described in <emphasis>injectable values</emphasis> <xref linkend="_validation-of-callsites-targeting-n4injector-methods"/>.</simpara>
</listitem>
</orderedlist>
</requirement>
</section>
<section xml:id="_n4js-di-inject">
<title>N4JS DI @Inject</title>
<informaltable frame="none" rowsep="0" colsep="0">
<tgroup cols="3">
<colspec colname="col_1" colwidth="33.3333*"/>
<colspec colname="col_2" colwidth="33.3333*"/>
<colspec colname="col_3" colwidth="33.3334*"/>
<tbody>
<row>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>name</term>
<listitem>
<simpara>@Inject</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>targets</term>
<listitem>
<simpara>N4Field, N4Method, constructor</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>retention policy</term>
<listitem>
<simpara>RUNTIME</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
</row>
<row>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>transitive</term>
<listitem>
<simpara>NO</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>repeatable</term>
<listitem>
<simpara>NO</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>arguments</term>
<listitem>
<simpara>NO</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<simpara><literal>@Inject</literal> defines the injection point into which an instance object will be injected.
The specific instance depends on the injector configuration (bindings) used.
Class fields, methods and constructors can be annotated. See <xref linkend="_injection-points"/> for more information.</simpara>
<requirement xml:id="IDE-150">
<title>DI Inject</title>
<simpara>
<anchor xml:id="Req-IDE-150" xreflabel="[Req-IDE-150]"/>
<emphasis role="strong">Requirement: IDE-150:</emphasis>
<link linkend="Req-IDE-150">DI Inject</link> (ver. 1)</simpara>
<simpara>
1. Injection point bindings need to be resolvable.
2. Binding for given type must not be duplicated.
3. Annotated types must be instances of one of the types described in <xref linkend="_validation-of-callsites-targeting-n4injector-methods"/>.</simpara>
</requirement>
</section>
<section xml:id="_n4js-di-singleton">
<title>N4JS DI @Singleton</title>
<informaltable frame="none" rowsep="0" colsep="0">
<tgroup cols="3">
<colspec colname="col_1" colwidth="33.3333*"/>
<colspec colname="col_2" colwidth="33.3333*"/>
<colspec colname="col_3" colwidth="33.3334*"/>
<tbody>
<row>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>name</term>
<listitem>
<simpara>@Singleton</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>targets</term>
<listitem>
<simpara>N4Class</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>retention policy</term>
<listitem>
<simpara>RUNTIME</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
</row>
<row>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>transitive</term>
<listitem>
<simpara>NO</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>repeatable</term>
<listitem>
<simpara>NO</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
<entry align="left" valign="top"><variablelist>
<varlistentry>
<term>arguments</term>
<listitem>
<simpara>NO</simpara>
</listitem>
</varlistentry>
</variablelist></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<simpara>In the case of annotating a class <literal>S</literal> with <literal>@Singleton</literal> on the definition-site, the singleton scope will be used as described in <xref linkend="_singleton-scope"/>.</simpara>
</section>
</section>
</section>
<section xml:id="_test-support" role="language-n4js">
<title>Test Support</title>
<simpara>N4JS provides some annotations for testing. Most of these annotations are similar to annotations found in JUnit 4.
For details see our Mangelhaft test framework (stdlib specification) and the N4JS-IDE specification.</simpara>
<simpara>In order to enable tests for private methods, test projects may define which project they are testing.</simpara>
<requirement xml:id="IDE-151">
<title>Test API methods and types</title>
<simpara>
<anchor xml:id="Req-IDE-151" xreflabel="[Req-IDE-151]"/>
<emphasis role="strong">Requirement: IDE-151:</emphasis>
<link linkend="Req-IDE-151">Test API methods and types</link> (ver. 1)</simpara>
<simpara>
</simpara>
<simpara>In some cases, types or methods are only provided for testing purposes.
In order to improve usability, e.g. content assist, these types and methods can be annotated with <literal>@TestAPI</literal>.
There are no constraints defined for that annotation at the moment.</simpara>
</requirement>
</section>
<section xml:id="_polyfill-definitions" role="language-n4js">
<title>Polyfill Definitions</title>
<simpara>In plain JavaScript, so called <emphasis>polyfill</emphasis> (or sometimes called <emphasis>shim</emphasis>) libraries are provided in order to modify existing classes which are only prototypes in plain JavaScript.
In N4JS, this can be defined for declarations via the annotation <literal>@Polyfill</literal> or <literal>@StaticPolyfill</literal>.
One of these annotations can be added to class declarations which do not look that much different from normal classes.
In the case of polyfill classes, the extended class is modified (or filled) instead of being subclassed. It is therefore valid to polyfill a class even if it is declared <literal>@Final</literal>.</simpara>
<simpara>We distinguish two flavours of polyfill classes: runtime and static.</simpara>
<itemizedlist>
<listitem>
<simpara>Runtime polyfilling covers type enrichment for runtime libraries.
For type modifications the annotation <literal>@Polyfill</literal> is used.</simpara>
</listitem>
<listitem>
<simpara>Static polyfilling covers code modifications for adapting generated code.
The annotation <literal>@StaticPolyfill</literal> denotes a polyfill in ordinary code, which usually provides executable implementations.</simpara>
</listitem>
</itemizedlist>
<definition>
<title>Polyfill Class</title>
<simpara>
<anchor xml:id="polyfill_class" xreflabel="[polyfill_class]"/>
<emphasis role="strong">Definition:</emphasis>
<link linkend="polyfill_class">Polyfill Class</link></simpara>
<simpara>
A <emphasis>polyfill class</emphasis> (or simply <emphasis>polyfill</emphasis>) is
a class modifying an existing one. The polyfill is not a new class (or type) on its own.
Instead, new members defined in the polyfill are added to the modified class and existing members can be modified similarly to overriding.
We call the modified class the <emphasis>filled</emphasis> class and the modification <emphasis>filling</emphasis>.</simpara>
<simpara>We add a new pseudo property <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>p</mi><mi>o</mi><mi>l</mi><mi>y</mi><mi>f</mi><mi>i</mi><mi>l</mi><mi>l</mi></math> to classes in order to distinguish between normal (sub-) classes and polyfill classes.</simpara>
</definition>
<requirement xml:id="IDE-152">
<title>Polyfill Class</title>
<simpara>
<anchor xml:id="Req-IDE-152" xreflabel="[Req-IDE-152]"/>
<emphasis role="strong">Requirement: IDE-152:</emphasis>
<link linkend="Req-IDE-152">Polyfill Class</link> (ver. 1)</simpara>
<simpara>
For a polyfill class <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi></math> annotated with <literal>@Polyfill</literal> or <literal>@StaticPolyfill</literal>, that is <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi><mo>.</mo><mi>p</mi><mi>o</mi><mi>l</mi><mi>y</mi><mi>f</mi><mi>i</mi><mi>l</mi><mi>l</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>true</mtext></mstyle></math>, all the following constraints must hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi></math> must extend a class <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>F</mi></math>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>F</mi></math> is called the filled class:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>P</mi><mo>.</mo><mi>s</mi><mi>u</mi><mi>p</mi><mi>e</mi><mi>r</mi><mo>=</mo><mi>F</mi></math>
</listitem>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi></math>’s name equals the name of the filled class and is contained in a module with same qualified name (specifier or global):<?asciidoc-br?></simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mspace width="3.0mm"/><mi>P</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>F</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mtd></mtr><mtr><mtd><mspace width="3.0mm"/><mo></mo><mi>P</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>o</mi><mi>d</mi><mi>u</mi><mi>l</mi><mi>e</mi><mo>.</mo><mi>g</mi><mi>l</mi><mi>o</mi><mi>b</mi><mi>a</mi><mi>l</mi><mo>=</mo><mi>F</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>o</mi><mi>d</mi><mi>u</mi><mi>l</mi><mi>e</mi><mo>.</mo><mi>g</mi><mi>l</mi><mi>o</mi><mi>b</mi><mi>a</mi><mi>l</mi></mtd></mtr><mtr><mtd><mspace width="3.0mm"/><mo></mo><mrow><mo>(</mo><mi>P</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>o</mi><mi>d</mi><mi>u</mi><mi>l</mi><mi>e</mi><mo>.</mo><mi>g</mi><mi>l</mi><mi>o</mi><mi>b</mi><mi>a</mi><mi>l</mi></mrow></mtd></mtr><mtr><mtd><mrow><mspace width="3.0mm"/><mspace width="3.0em"/><mo></mo><mi>P</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>o</mi><mi>d</mi><mi>u</mi><mi>l</mi><mi>e</mi><mo>.</mo><mi>s</mi><mi>p</mi><mi>e</mi><mi>c</mi><mi>i</mi><mi>f</mi><mi>i</mi><mi>e</mi><mi>r</mi><mo>=</mo><mi>F</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>o</mi><mi>d</mi><mi>u</mi><mi>l</mi><mi>e</mi><mo>.</mo><mi>s</mi><mi>p</mi><mi>e</mi><mi>c</mi><mi>i</mi><mi>f</mi><mi>i</mi><mi>e</mi><mi>r</mi><mo>)</mo></mrow></mtd></mtr></mtable></math>
</listitem>
<listitem>
<simpara>Both the polyfill and filled class must be top-level declarations (i.e., no class expression):<?asciidoc-br?></simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>P</mi><mo>.</mo><mi>t</mi><mi>o</mi><mi>p</mi><mi>L</mi><mi>e</mi><mi>v</mi><mi>e</mi><mi>l</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>true</mtext></mstyle><mo></mo><mi>F</mi><mo>.</mo><mi>t</mi><mi>o</mi><mi>p</mi><mi>L</mi><mi>e</mi><mi>v</mi><mi>e</mi><mi>l</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>true</mtext></mstyle></math>
</listitem>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi></math> must not implement any interfaces:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>P</mi><mo>.</mo><mi>i</mi><mi>m</mi><mi>p</mi><mi>l</mi><mi>e</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>d</mi><mi>I</mi><mi>n</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>f</mi><mi>a</mi><mi>c</mi><mi>e</mi><mi>s</mi><mo>=</mo><mi></mi></math>
</listitem>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi></math> must have the same access modifier (access, abstract, final) as the filled class:<?asciidoc-br?></simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mi>P</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>M</mi><mi>o</mi><mi>d</mi><mi>i</mi><mi>f</mi><mi>i</mi><mi>e</mi><mi>r</mi><mo>=</mo><mi>F</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>M</mi><mi>o</mi><mi>d</mi><mi>i</mi><mi>f</mi><mi>i</mi><mi>e</mi><mi>r</mi></mtd></mtr><mtr><mtd><mi>P</mi><mo>.</mo><mi>a</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi><mo>=</mo><mi>F</mi><mo>.</mo><mi>a</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi></mtd></mtr><mtr><mtd><mi>P</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi><mo>=</mo><mi>F</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi></mtd></mtr></mtable></math>
</listitem>
<listitem>
<simpara>If <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi></math> declares a constructor, it must be override compatible with the constructor of the filled class:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mo></mo><mi>P</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>C</mi><mi>t</mi><mi>o</mi><mi>r</mi><mi>:</mi><mi>P</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>C</mi><mi>t</mi><mi>o</mi><mi>r</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>F</mi><mo>.</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math>
</listitem>
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi></math> must define the same type variables as the filled class <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>F</mi></math> and the arguments must be in the same order as the parameters (with no further modifications):</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mo></mo><mi>i</mi><mo>,</mo><mn>0</mn><mo></mo><mi>i</mi><mo>&lt;</mo><mo>|</mo><mi>P</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>P</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>|</mo><mi>:</mi></mtd></mtr><mtr><mtd><mspace width="3.0em"/><mi>P</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>P</mi><mi>a</mi><mi>r</mi><msub><mi>s</mi><mi>i</mi></msub><mo>=</mo><mi>F</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>P</mi><mi>a</mi><mi>r</mi><msub><mi>s</mi><mi>i</mi></msub></mtd></mtr><mtr><mtd><mspace width="3.0em"/><mo></mo><mi>P</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>P</mi><mi>a</mi><mi>r</mi><msub><mi>s</mi><mi>i</mi></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>P</mi><mo>.</mo><mi>s</mi><mi>u</mi><mi>p</mi><mi>e</mi><mi>r</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>A</mi><mi>r</mi><mi>g</mi><msub><mi>s</mi><mi>i</mi></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mtd></mtr></mtable></math>
</listitem>
<listitem>
<simpara>All constraints related to member redefinition (cf. <xref linkend="_redefinition-of-members"/>) have to hold.
In the case of polyfills, this is true for constructors (cf. <xref linkend="Req-IDE-72"/>) and private members.</simpara>
</listitem>
</orderedlist>
</requirement>
<section xml:id="_runtime-polyfill-definitions">
<title>Runtime Polyfill Definitions</title>
<simpara>(Runtime) Libraries often do not provide completely new types but modify existing types.
The ECMA-402 Internationalization Standard [<link linkend="ECMA12a">ECMA12a</link>], for example, changes methods of the built-in class <literal>Date</literal> to be timezone aware.
Other scenarios include new functionality provided by browsers which are not part of an official standard yet.
Even ECMAScript 6 [<link linkend="ECMA15a">ECMA15a</link>] extends the predecessor [<link linkend="ECMA11a">ECMA11a</link>] in terms of new methods (or new method parameters) added to existing types (it also adds completely new classes and features, of course).</simpara>
<simpara>Runtime polyfills are only applicable to runtime libraries or environments and thus are limited to n4jsd files.</simpara>
<requirement xml:id="IDE-153">
<title>Runtime Polyfill Class</title>
<simpara>
<anchor xml:id="Req-IDE-153" xreflabel="[Req-IDE-153]"/>
<emphasis role="strong">Requirement: IDE-153:</emphasis>
<link linkend="Req-IDE-153">Runtime Polyfill Class</link> (ver. 1)</simpara>
<simpara>
For a runtime-polyfill class <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi></math> annotated with <literal>@Polyfill</literal>, that is <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mi>p</mi><mi>o</mi><mi>l</mi><mi>y</mi><mi>f</mi><mi>i</mi><mi>l</mi><mi>l</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>false</mtext></mstyle></math>, all the following constraints must hold in addition to <xref linkend="Req-IDE-152"/>:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>Both the polyfill and filled class are provided by the runtime
(annotated with <literal>@ProvidedByRuntime</literal>): <footnote><simpara>This restriction has two reasons: Firstly, user-defined types with implementations would require to ’bootstrap’ the polyfill, which is impossible to do automatically without serious constraints on bootstrap code in general. Secondly, instead of filling user-defined types, they can be subclasses. Mechanisms such as dependency injection could then solve almost all remaining problems.</simpara></footnote></simpara>
</listitem>
</orderedlist>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>P</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>v</mi><mi>i</mi><mi>d</mi><mi>e</mi><mi>d</mi><mi>B</mi><mi>y</mi><mi>R</mi><mi>u</mi><mi>n</mi><mi>t</mi><mi>i</mi><mi>m</mi><mi>e</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>true</mtext></mstyle><mo></mo><mi>F</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>v</mi><mi>i</mi><mi>d</mi><mi>e</mi><mi>d</mi><mi>B</mi><mi>y</mi><mi>R</mi><mi>u</mi><mi>n</mi><mi>t</mi><mi>i</mi><mi>m</mi><mi>e</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>true</mtext></mstyle></math>
</requirement>
<requirement xml:id="IDE-154">
<title>Applying Polyfills</title>
<simpara>
<anchor xml:id="Req-IDE-154" xreflabel="[Req-IDE-154]"/>
<emphasis role="strong">Requirement: IDE-154:</emphasis>
<link linkend="Req-IDE-154">Applying Polyfills</link> (ver. 1)</simpara>
<simpara>
</simpara>
<simpara>A polyfill is automatically applied if a runtime library or environment required by the current project provides it. In this case, the following constraints must hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>No member must be filled by more than one polyfill.</simpara>
</listitem>
</orderedlist>
</requirement>
</section>
<section xml:id="_static-polyfill-definitions">
<title>Static Polyfill Definitions</title>
<simpara>Static polyfilling is a compile time feature to enrich the definition and usually also the implementation of generated code in N4JS. It is related to runtime polyfilling described in <xref linkend="_runtime-polyfill-definitions"/> in a sense that both fillings enrich the types they address. Despite this, static polyfilling and runtime polyfilling differ in the way they are handled.</simpara>
<simpara>Static polyfills usually provide executable implementations and are thus usually found in n4js files. However, they are allowed in n4jsd files, as well, for example to enrich generated code in an API project.</simpara>
<simpara>The motivation for static polyfills is to support automatic code generation.
In many cases, automatically generated code is missing some information to make it sufficiently usable in the desired environment.
Manual enhancements usually need to be applied.
If we think of a toolchain, the question may arise how to preserve the manual work when a
regeneration is triggered. Static polyfilling allows the separation of generated code and manual adjustments in separate files.
The transpiler merges the two files into a single transpiled file.
To enable this behaviour, the statically fillable types must be contained in a module annotated with <literal>@StaticPolyfillAware</literal>.
The filling types must also be annotated with <literal>@StaticPolyfill</literal> and be contained in a different module with same specifier but annotated with <literal>@StaticPolyfillModule</literal>.
Static polyfilling is restricted to a project, thus the module to be filled as well as the filling module must be contained in the same project.</simpara>
<simpara>We add a new pseudo property <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mi>P</mi><mi>o</mi><mi>l</mi><mi>y</mi><mi>f</mi><mi>i</mi><mi>l</mi><mi>l</mi></math> to classes in order to distinguish between normal (sub-) classes and static polyfill classes.
We add two new pseudo properties to modules in order to modify the transpilation process.
The mutually-exclusive properties <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mi>P</mi><mi>o</mi><mi>l</mi><mi>y</mi><mi>f</mi><mi>i</mi><mi>l</mi><mi>l</mi><mi>A</mi><mi>w</mi><mi>a</mi><mi>r</mi><mi>e</mi></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mi>P</mi><mi>o</mi><mi>l</mi><mi>y</mi><mi>f</mi><mi>i</mi><mi>l</mi><mi>l</mi></math> signal the way these files are processed.</simpara>
<simpara>In order to support efficient transpilation, the following constraint must hold in addition to constraints:</simpara>
<requirement xml:id="IDE-155">
<title>Static Polyfill Layout</title>
<simpara>
<anchor xml:id="Req-IDE-155" xreflabel="[Req-IDE-155]"/>
<emphasis role="strong">Requirement: IDE-155:</emphasis>
<link linkend="Req-IDE-155">Static Polyfill Layout</link> (ver. 1)</simpara>
<simpara>
For a static polyfill class <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi></math> annotated with <literal>@StaticPolyfill</literal>, that is <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mi>p</mi><mi>o</mi><mi>l</mi><mi>y</mi><mi>f</mi><mi>i</mi><mi>l</mi><mi>l</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>true</mtext></mstyle></math>, all the following constraints must hold in addition to <xref linkend="Req-IDE-152"/>:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi></math>’s name equals the name of the filled class and is contained in a module with the same qualified name:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mi>P</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>F</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mtd></mtr><mtr><mtd><mo></mo><mi>P</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>o</mi><mi>d</mi><mi>u</mi><mi>l</mi><mi>e</mi><mo>.</mo><mi>s</mi><mi>p</mi><mi>e</mi><mi>c</mi><mi>i</mi><mi>f</mi><mi>i</mi><mi>e</mi><mi>r</mi><mo>=</mo><mi>F</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>o</mi><mi>d</mi><mi>u</mi><mi>l</mi><mi>e</mi><mo>.</mo><mi>s</mi><mi>p</mi><mi>e</mi><mi>c</mi><mi>i</mi><mi>f</mi><mi>i</mi><mi>e</mi><mi>r</mi></mtd></mtr></mtable></math>
</listitem>
<listitem>
<simpara>Both the static polyfill and the filled class are part of the same project:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>P</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>j</mi><mi>e</mi><mi>c</mi><mi>t</mi><mo>=</mo><mi>F</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>j</mi><mi>e</mi><mi>c</mi><mi>t</mi></math>
</listitem>
<listitem>
<simpara>The filled class must be contained in a module annotated with <literal>@StaticPolyfillAware</literal>:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>F</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>o</mi><mi>d</mi><mi>u</mi><mi>l</mi><mi>e</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mi>P</mi><mi>o</mi><mi>l</mi><mi>y</mi><mi>f</mi><mi>i</mi><mi>l</mi><mi>l</mi><mi>A</mi><mi>w</mi><mi>a</mi><mi>r</mi><mi>e</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>true</mtext></mstyle></math>
</listitem>
<listitem>
<simpara>The static polyfill and the filled type must both be declared in an n4js file or both in an n4jsd file.</simpara>
</listitem>
<listitem>
<simpara>The filling class must be contained in a module annotated with <literal>@StaticPolyfillModule</literal>:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>P</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>o</mi><mi>d</mi><mi>u</mi><mi>l</mi><mi>e</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mi>P</mi><mi>o</mi><mi>l</mi><mi>y</mi><mi>f</mi><mi>i</mi><mi>l</mi><mi>l</mi><mi>M</mi><mi>o</mi><mi>d</mi><mi>u</mi><mi>l</mi><mi>e</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>true</mtext></mstyle></math>
</listitem>
<listitem>
<simpara>For a statically-filled class <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>F</mi></math> there is at most one static polyfill:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfenced close=")" open="("><mrow><msub><mi>P</mi><mn>1</mn></msub><mstyle mathvariant="monospace"><mspace width="1ex"/><mtext>is static polyfill of</mtext><mspace width="1ex"/></mstyle><mi>F</mi><mo></mo><msub><mi>P</mi><mn>2</mn></msub><mstyle mathvariant="monospace"><mspace width="1ex"/><mtext>is static polyfill of</mtext><mspace width="1ex"/></mstyle><mi>F</mi></mrow></mfenced><mo></mo><msub><mi>P</mi><mn>1</mn></msub><mo>=</mo><msub><mi>P</mi><mn>2</mn></msub></math>
</listitem>
</orderedlist>
</requirement>
<requirement xml:id="IDE-156">
<title>Restrictions on static polyfilling</title>
<simpara>
<anchor xml:id="Req-IDE-156" xreflabel="[Req-IDE-156]"/>
<emphasis role="strong">Requirement: IDE-156:</emphasis>
<link linkend="Req-IDE-156">Restrictions on static polyfilling</link> (ver. 1)</simpara>
<simpara>
For a static polyfilling module <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>M</mi><mi>P</mi></msub></math> the following must hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>All top-level elements are static polyfills:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mo></mo><mi>T</mi><mo></mo><msub><mi>M</mi><mi>P</mi></msub><mo></mo><mi>T</mi><mo>.</mo><mi>t</mi><mi>o</mi><mi>p</mi><mi>L</mi><mi>e</mi><mi>v</mi><mi>e</mi><mi>l</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>true</mtext></mstyle></mrow><mrow><mi>T</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mi>P</mi><mi>o</mi><mi>l</mi><mi>y</mi><mi>f</mi><mi>i</mi><mi>l</mi><mi>l</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>true</mtext></mstyle></mrow></mfrac></math>
</listitem>
<listitem>
<simpara>It exists exactly one filled module <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>M</mi><mi>F</mi></msub></math> annotated with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mi>P</mi><mi>o</mi><mi>l</mi><mi>y</mi><mi>f</mi><mi>i</mi><mi>l</mi><mi>l</mi><mi>A</mi><mi>w</mi><mi>a</mi><mi>r</mi><mi>e</mi></math> in the same project.</simpara>
</listitem>
<listitem>
<simpara>It is an error if two static polyfill modules for the same filled module exist in the same project:</simpara>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><msub><mi>M</mi><mn>1.</mn></msub><mi>s</mi><mi>p</mi><mi>e</mi><mi>c</mi><mi>i</mi><mi>f</mi><mi>i</mi><mi>e</mi><mi>r</mi><mo>=</mo><msub><mi>M</mi><mn>2.</mn></msub><mi>s</mi><mi>p</mi><mi>e</mi><mi>c</mi><mi>i</mi><mi>f</mi><mi>i</mi><mi>e</mi><mi>r</mi><mo></mo><msub><mi>M</mi><mn>1.</mn></msub><mi>p</mi><mi>r</mi><mi>o</mi><mi>j</mi><mi>e</mi><mi>c</mi><mi>t</mi><mo>=</mo><msub><mi>M</mi><mn>2.</mn></msub><mi>p</mi><mi>r</mi><mi>o</mi><mi>j</mi><mi>e</mi><mi>c</mi><mi>t</mi><mtext>
</mtext><mo></mo><msub><mi>M</mi><mn>1.</mn></msub><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mi>P</mi><mi>o</mi><mi>l</mi><mi>y</mi><mi>f</mi><mi>i</mi><mi>l</mi><mi>l</mi><mi>M</mi><mi>o</mi><mi>d</mi><mi>u</mi><mi>l</mi><mo>=</mo><msub><mi>M</mi><mn>2.</mn></msub><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mi>P</mi><mi>o</mi><mi>l</mi><mi>y</mi><mi>f</mi><mi>i</mi><mi>l</mi><mi>l</mi><mi>M</mi><mi>o</mi><mi>d</mi><mi>u</mi><mi>l</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>true</mtext></mstyle></mrow><mrow><msub><mi>M</mi><mn>1</mn></msub><mo>=</mo><msub><mi>M</mi><mn>2</mn></msub></mrow></mfrac></math>
</listitem>
</orderedlist>
</requirement>
<example>
<title>Static polyfill</title>
<simpara><xref linkend="ex:staticpolyfill-genmod"/> shows an example of generated code.
<xref linkend="ex:staticpolyfill-polyfillmod"/> demonstrates the static polyfill.</simpara>
<simpara>Note that the containing project has two source folders configured:<?asciidoc-br?>
<literal>Project/src/n4js</literal> and <literal>Project/src/n4jsgen</literal>.</simpara>
<formalpara xml:id="ex:staticpolyfill-polyfillmod">
<title>Static Polyfill, Polyfillmod</title>
<para>
<programlisting language="n4js" linenumbering="unnumbered">@@StaticPolyfillAware
export public class A {
constructor() {...}
m1(): void{...}
}
export public class B {
constructor() {...}
m2(): void{...}
}</programlisting>
</para>
</formalpara>
<formalpara xml:id="ex:staticpolyfill-genmod">
<title>Static Polyfill, Genmod</title>
<para>
<programlisting language="n4js" linenumbering="unnumbered">@@StaticPolyfillModule
@StaticPolyfill
export public class B extends B {
@Override
constructor(){ ... } // replaces generated ctor of B
@Override
m1(): void {...} // adds overridden method m1 to B
@Override
m2(): void {...} // replaces method m2 in B
m3(): void {...} // adds new method m3 to B
}</programlisting>
</para>
</formalpara>
</example>
</section>
<section xml:id="_transpiling-static-polyfilled-classes">
<title>Transpiling static polyfilled classes</title>
<simpara>Transpiling static polyfilled classes encounters the special case that two different <literal>n4js</literal> source files with the same qualified name are part of the project.
Since the current transpiler is file-based, both files would be transpiled to the same output destination and would therefore overwrite each other.
The following pre-transpilation steps handle this situation:</simpara>
<itemizedlist>
<listitem>
<simpara>Current file to transpile is <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi></math></simpara>
</listitem>
<listitem>
<simpara>If <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mi>P</mi><mi>o</mi><mi>l</mi><mi>y</mi><mi>f</mi><mi>i</mi><mi>l</mi><mi>l</mi><mi>A</mi><mi>w</mi><mi>a</mi><mi>r</mi><mi>e</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>true</mtext></mstyle></math>, then</simpara>
<itemizedlist>
<listitem>
<simpara>search for a second file <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>G</mi></math> with same qualified name:<?asciidoc-br?>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>G</mi><mo>.</mo><mi>s</mi><mi>p</mi><mi>e</mi><mi>c</mi><mi>i</mi><mi>f</mi><mi>i</mi><mi>e</mi><mi>r</mi><mo>=</mo><mi>M</mi><mo>.</mo><mi>s</mi><mi>p</mi><mi>e</mi><mi>c</mi><mi>i</mi><mi>f</mi><mi>i</mi><mi>e</mi><mi>r</mi><mo></mo><mi>G</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>j</mi><mi>e</mi><mi>c</mi><mi>t</mi><mo>=</mo><mi>M</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>j</mi><mi>e</mi><mi>c</mi><mi>t</mi></math></simpara>
</listitem>
<listitem>
<simpara>If <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>G</mi></math>, then</simpara>
<itemizedlist>
<listitem>
<simpara>merge <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>G</mi></math> into current file <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi><mo></mo><msup><mi>M</mi><mi>'</mi></msup></math></simpara>
</listitem>
<listitem>
<simpara>conventionally transpile <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>M</mi><mi>'</mi></msup></math></simpara>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<simpara>else conventionally transpile <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi></math></simpara>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<simpara>else, if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mi>P</mi><mi>o</mi><mi>l</mi><mi>y</mi><mi>f</mi><mi>i</mi><mi>l</mi><mi>l</mi><mi>M</mi><mi>o</mi><mi>d</mi><mi>u</mi><mi>l</mi><mi>e</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>true</mtext></mstyle></math>,</simpara>
<itemizedlist>
<listitem>
<simpara>then <emphasis>do nothing</emphasis>. (Transpilation will be triggered for filled type separately.)</simpara>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<simpara>else, conventionally transpile <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi></math></simpara>
</listitem>
</itemizedlist>
</section>
</section>
</chapter>
<chapter xml:id="_components">
<title>Components</title>
<section xml:id="_overview-2" role="language-n4js">
<title>Overview</title>
<simpara>For modularization purposes, N4JS code is managed in so-called <emphasis>components</emphasis>. These components correspond to what
node and npm call <emphasis>packages</emphasis>, what OSGi calls <emphasis>bundle</emphasis>, and what Eclipse calls <emphasis>project</emphasis>.</simpara>
<simpara>N4JS distinguishes several types of such components. <xref linkend="fig-cmpd_components_in_n4js"/> shows the N4JS component types
described in detail in this chapter. <xref linkend="fig-cmp_components"/> shows a recently updated diagram, which is not yet fully
reflected in the implementation.</simpara>
<figure xml:id="fig-cmpd_components_in_n4js">
<title>Overview of N4JS Components (OLD)</title>
<mediaobject>
<imageobject>
<imagedata fileref="chapters/12_components/fig/cmpd_components_in_n4js.svg" align="center"/>
</imageobject>
<textobject><phrase>cmpd components in n4js</phrase></textobject>
</mediaobject>
</figure>
<figure xml:id="fig-cmp_components">
<title>Overview of N4JS Components and Dependencies (NEW)</title>
<mediaobject>
<imageobject>
<imagedata fileref="chapters/12_components/fig/cmp_components.svg" align="center"/>
</imageobject>
<textobject><phrase>cmp components</phrase></textobject>
</mediaobject>
</figure>
<simpara>The following types of components can only be created by internal developers:</simpara>
<variablelist>
<varlistentry>
<term>Runtime Environment</term>
<listitem>
<simpara>Definition of a runtime environment such as ECMAScript 5, node.js, or Chrome.
A Runtime Environment provides a number of base types which are usually globally available.
Other components do not directly rely on runtime environments, but on runtime libraries.
NOTE: runtime environments are not fully implemented at this time (see GH-1291).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>Runtime Library</term>
<listitem>
<simpara>Contains type definitions for the base types provided by one or more runtime environments.
These types may be extensions to certain language specifications.
E.g., the ECMAScript 6 collection classes are already provided by some environments otherwise only supporting ECMAScript 5.
The collections are defined in terms of a runtime library which can then be provided by these environments.
Runtime libraries may also contain polyfills to alter types predefined in the environment.</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>The following components can be created by external developers:</simpara>
<variablelist xml:id="App">
<varlistentry>
<term>App</term>
<listitem>
<simpara>User-written N4JS applications.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>Processor</term>
<listitem>
<simpara>User-written N4JS code running server-side on the N4 platform. Not implemented yet.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>Library</term>
<listitem>
<simpara>User-written libraries used by apps, processors, or other libraries.</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>The available component types are described in more detail in <xref linkend="Component_Types"/>.</simpara>
<simpara>A component corresponds to a single project in the N4JS IDE. Generally, it contains the following:</simpara>
<variablelist>
<varlistentry>
<term>Package.json File</term>
<listitem>
<simpara>The <literal>package.json</literal> file describing the contents, dependencies and metadata.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>Resources</term>
<listitem>
<simpara>Resources such as images, data files etc.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>Sources</term>
<listitem>
<simpara>Source files containing either N4JS code or plain Javascript. The actual code used in a project.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>Output Code</term>
<listitem>
<simpara>Compiled and possibly adjusted (e.g. minified, concatenated) versions of the N4JS source files and
plain Javascript files.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>Tests</term>
<listitem>
<simpara>Optional test sources and compiled output code.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>Source Maps</term>
<listitem>
<simpara>Optional source maps.</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>Components contain modules. <xref linkend="fig-component_content"/> describes what can be contained in a component.</simpara>
<figure xml:id="fig-component_content">
<title>Content of a Component</title>
<mediaobject>
<imageobject>
<imagedata fileref="chapters/12_components/fig/cmpd_component_content.svg" width="70%" scalefit="1" align="center"/>
</imageobject>
<textobject><phrase>cmpd component content</phrase></textobject>
</mediaobject>
</figure>
<simpara>At both compile time and runtime, all components defined as dependency have to be available. Since dependencies
are defined in <literal>package.json</literal> files in a form compliant to node/npm, this can be fully managed by npm (or yarn).</simpara>
</section>
<section xml:id="Component_Types" role="language-n4js">
<title>Component Types</title>
<simpara>Different N4JS component types are described in this section.</simpara>
<section xml:id="_libraries">
<title>Libraries</title>
<simpara>A library is a user project providing modules with declaration.</simpara>
</section>
<section xml:id="_runtime-environment-and-runtime-libraries">
<title>Runtime Environment and Runtime Libraries</title>
<note>
<simpara>runtime environments are not fully implemented at this time (see GH-1291).</simpara>
</note>
<simpara>Runtime environments and libraries define globally available elements (types, variables, functions) provided by the JavaScript engine.
Both must contain <emphasis>only</emphasis> definition files (n4jsd) of which all elements are marked as <literal>@ProvidedByRuntime</literal> (<xref linkend="_runtime-definitions"/>) and <literal>@Global</literal> (<xref linkend="_global-definitions"/>).</simpara>
<simpara>Other projects may refer to <emphasis>multiple</emphasis> runtime libraries in their <literal>package.json</literal> file via the property <xref linkend="package-json-requiredRuntimeLibraries"/>.</simpara>
<simpara>The concrete runtime environment and library are selected by the JavaScript engine.
Deployment and execution scripts must ensure that a component can run on the given engine; the required environments and libraries must all be compatible with the provided environment.
If no runtime environment is specified, a default an ECMAScript 5 runtime is assumed to be present.</simpara>
<simpara>Typical runtime environments are ES5 or ES6, typical runtime libraries are DOM or HTML.</simpara>
<simpara>In JavaScript, browsers and other execution environments provide built-in objects.
In browsers, for example, the whole DOM is made available via built-in object types.
In this case, even the global object also becomes a different type (in N4JS terms).
Besides execution environments such as browsers or Node.js, libraries also provide functionality by exposing globally available functions.
This is often used to bridge execution environment inconsistencies.
When browser API differences are adapted by a library, this is called a <emphasis>polyfil</emphasis>.
Other adaptations, such as enabling ECMSScript 6 object types in ECMAScript 5 environments, are known as <emphasis>shim</emphasis>.
Instead of directly supporting these kind of 'hacks', other components specify which runtime environment and libraries they depend on by specifying unique runtime ids.
Possible shims (in case of environments) or polyfils (in case of libraries) are transparently provided by the execution environment and the bootstrap code.</simpara>
</section>
<section xml:id="_tests">
<title>Tests</title>
<simpara>Tests are special projects which contain tests for other projects.</simpara>
<requirement xml:id="IDE-157">
<title>Test Project</title>
<simpara>
<anchor xml:id="Req-IDE-157" xreflabel="[Req-IDE-157]"/>
<emphasis role="strong">Requirement: IDE-157:</emphasis>
<link linkend="Req-IDE-157">Test Project</link> (ver. 1)</simpara>
<simpara>
</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>Tests have full access to the tested project including elements with <literal>project</literal> visibility.</simpara>
</listitem>
<listitem>
<simpara>Only other test projects can depend on tests project.
In other words, other components must not depend on test components.</simpara>
</listitem>
</orderedlist>
<simpara>In a test project, the tested projects can be specified via <literal>testee</literal>.</simpara>
</requirement>
</section>
<section xml:id="_type-definitions">
<title>Type Definitions</title>
<simpara>Projects of type "definition" are special projects which only provide type information for another so-called <emphasis>implementation project</emphasis>, which only provides executable JS files.</simpara>
<simpara>Generally, client projects that depend on a given <emphasis>implementation project</emphasis> may additionally declare a dependency on a corresponding type definitions project, in order to integrate type information on the implementation project.
This is implemented by means of module-level shadowing.
More specifically, given a client imports a module with module specifier <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi></math> from the implementation project.
When resolving the module specifier, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi></math> will first be resolved against the implementation project&#8217;s type definitions and only secondarily against the implementation project.
As a consequence, type definition projects may only provide partial type information, while the remaining modules of the implementation project remain accessible through dynamic namespace imports.</simpara>
<requirement xml:id="GH-821002">
<title>Type Definition Project Configuration</title>
<simpara>
<anchor xml:id="Req-GH-821002" xreflabel="[Req-GH-821002]"/>
<emphasis role="strong">Requirement: GH-821002:</emphasis>
<link linkend="Req-GH-821002">Type Definition Project Configuration</link> (ver. 1)</simpara>
<simpara>
</simpara>
<simpara>For type definition projects, the following constraints must hold true with regard to their project configuration:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>They must declare their <emphasis>implementation project</emphasis> via the <xref linkend="package-json-definesPackage"/> property in their <literal>package.json</literal> file.</simpara>
</listitem>
<listitem>
<simpara>They must not declare an output folder.</simpara>
</listitem>
</orderedlist>
</requirement>
</section>
</section>
<section xml:id="package-json" role="language-n4js" xreflabel="Package.json File">
<title>Package.json File</title>
<simpara>A folder is a "component" if and only if it contains a <literal>package.json</literal> file. Being a component means
that this folder is recognized by all N4JS-related tools but does not necessarily mean the component
contains N4JS code (it could just contain plain Javascript). The main benefit of being a component
in this sense is that this unit of code can be used by other N4JS components as a dependency.</simpara>
<simpara>For example, a plain npm project containing only plain Javascript can be a component and
can therefore be used as a project dependency of a full-blown N4JS component.</simpara>
<section xml:id="_basic-properties">
<title>Basic Properties</title>
<simpara>The following standard <literal>package.json</literal> properties are used by N4JS tooling. Unless otherwise
noted, all these properties have the exact same format and meaning as usual in <literal>package.json</literal>
files.</simpara>
<variablelist>
<varlistentry>
<term>name </term>
<listitem>
<simpara>Used as the globally unique identifier of the component.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>version </term>
<listitem>
<simpara>The component&#8217;s version.</simpara>
</listitem>
</varlistentry>
</variablelist>
<variablelist xml:id="package-json-dependencies" xreflabel="dependencies">
<varlistentry>
<term>dependencies </term>
<listitem>
<simpara>List of components required at runtime and compile time.</simpara>
</listitem>
</varlistentry>
</variablelist>
<variablelist xml:id="package-json-devDependencies" xreflabel="devDependencies">
<varlistentry>
<term>devDependencies </term>
<listitem>
<simpara>List of components required at compile time only.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>main </term>
<listitem>
<simpara>Path relative to the component&#8217;s root folder, pointing to a <literal>.js</literal> file
located in a source container (the <literal>.js</literal> file extension is optional,
i.e. may be omitted). This file then serves as the component&#8217;s
default entry point, i.e. project imports pointing to this component from
other components will import from the file denoted by this property. In
addition, this property may denote a folder and is then assumed to point
to a file <literal>index.js</literal> located in that folder. If this property denotes a file
other than a <literal>.js</literal> file, it will be ignored. In particular, it cannot be
used for <literal>.n4js</literal> files; in that case, property "mainModule" has to be used
(see below).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>workspaces </term>
<listitem>
<simpara>(array of strings) Property used by package management tool <literal>yarn</literal> to denote
that a project serves as a "yarn workspace" and to denote the other projects
that form the members of this yarn workspace. For details, see
<link xl:href="https://yarnpkg.com/lang/en/docs/workspaces">here</link>.
In N4JS, a project is called a "yarn workspace root" if and only if its
<literal>package.json</literal> file contains top-level property "workspaces", no matter the property&#8217;s
value (i.e. it will be called "yarn workspace root" even if the value of property
"workspaces" is the empty array or an invalid value such as a number). The nested
projects referred to via the strings in this property&#8217;s array value are called
"member projects" of the yarn workspace.</simpara>
</listitem>
</varlistentry>
</variablelist>
</section>
<section xml:id="_n4js-properties">
<title>N4JS Properties</title>
<simpara>In addition to the standard properties above, there is a single N4JS-specific
top-level property called "n4js". The value of this property must always be
an object that may have any combination of the following properties:</simpara>
<variablelist>
<varlistentry>
<term>projectType</term>
<listitem>
<simpara>(string) Must be one of the following strings:</simpara>
<variablelist>
<varlistentry>
<term>application</term>
<listitem>
<simpara>An application. See <xref linkend="App"/>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>library</term>
<listitem>
<simpara>A library. See <xref linkend="_libraries"/>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>processor</term>
<listitem>
<simpara>For processors running server-side on the N4 platform. Not implemented yet.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>test</term>
<listitem>
<simpara>An N4JS project containing tests for one or more other N4JS projects specified
via property "testedProjects".</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>api</term>
<listitem>
<simpara>For N4JS projects that contain only API (in <literal>.n4jsd</literal> files) to be implemented by other,
so-called implementation projects. See properties "implementationId", "implementedProjects".
NOTE: the API/Implementation concept is not fully implemented at this time (see GH-1291).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>runtimeEnvironment</term>
<listitem>
<simpara>Runtime environments. See <xref linkend="Runtime Environment Resolution"/>.
NOTE: runtime environments are not fully implemented at this time (see GH-1291).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>runtimeLibrary</term>
<listitem>
<simpara>Runtime libraries. See <xref linkend="_runtime-environment-and-runtime-libraries"/>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>validation</term>
<listitem>
<simpara>A project in which <literal>.n4js</literal> files are only being validated, not transpiled. This is used for projects
that are implemented in terms of <literal>.js</literal> files but that also provide type information in terms of <literal>.n4jsd</literal> files.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>plainjs</term>
<listitem>
<simpara>A project which only contains <literal>.js</literal> files and no N4JS resources. The contained JS files are only indexed to allow
for dynamic imports of specific JavaScript modules. Projects of this type are not being transpiled.</simpara>
</listitem>
</varlistentry>
</variablelist>
</listitem>
</varlistentry>
<varlistentry>
<term>vendorId</term>
<listitem>
<simpara>(string) Globally unique identifier for the component&#8217;s vendor.
Used for the <literal>@Internal</literal> accessibility modifier.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>vendorName</term>
<listitem>
<simpara>(string) Human-readable name of the component&#8217;s vendor. Used only for informational
purposes.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>output</term>
<listitem>
<simpara>(string) Path relative to the component&#8217;s root folder, pointing to a folder where
all output files will be placed. In particular, this is where the N4JS transpiler
will put the <literal>.js</literal> files created for each <literal>.n4js</literal> file.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>sources</term>
<listitem>
<simpara>(object) Defines various sub-folders where sources, etc. are located. All properties
of the given object must have the following format: the name must be "source", "external",
or "test"; the value must be an array of strings, with each string defining a
path relative to the component&#8217;s root folder, pointing to a folder where
source files of the corresponding type are located. For example, paths given via name
"source" tell the N4JS transpiler where to look for <literal>.n4js</literal> source files to be compiled.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>moduleFilters</term>
<listitem>
<simpara>(object) Filters for fine-tuning the validator and compiler. A filter is applied to modules
matching the given module specifier which may contain wildcards, optionally restricted to
modules defined in a specific source path.</simpara>
<simpara>All properties of the given object must have the following format: the name must be a valid
module filter type (see below); the value must be an array of strings, with each string
defining a pattern of files inside one of the source containers for which validation or
module wrapping is to be turned off. Instead of a plain string, the inner array may
contain an object with properties "module" and "sourceContainer" to make this filter apply
to only one of the source containers (instead of all source containers, which is the default).</simpara>
<variablelist>
<varlistentry>
<term>noValidate</term>
<listitem>
<simpara>Modules matching this filter are not semantically validated.
That is, they are still syntactically validated.
If they are contained in source or test source fragments, it must be possible to bind references
to declarations inside these modules.
Note that switching off validation for n4js files is disallowed.</simpara>
</listitem>
</varlistentry>
</variablelist>
</listitem>
</varlistentry>
</variablelist>
<example>
<title>Module Filters</title>
<simpara>A simple and a source-container-specific module filter in the <literal>n4js</literal> section of a package.json file.</simpara>
<programlisting language="json" linenumbering="unnumbered">"moduleFilters": {
"noValidate": [
"abc*",
{
"module": "xyz*",
"sourceContainer": "src/n4js"
}
]
}</programlisting>
</example>
<variablelist xml:id="package-json-mainModule" xreflabel="mainModule">
<varlistentry>
<term>mainModule</term>
<listitem>
<simpara>(string) A plain module specifier defining the project’s 'main module'.
If this property is defined, other projects can import from this project using imports where the string following
keyword <literal>from</literal> states only the project name and not the complete module specifier (see <xref linkend="import-statement-semantics"/>).
If this property is defined, top-level property <literal>main</literal> will be ignored.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>testedProjects</term>
<listitem>
<simpara> (array) List of N4JS components being tested by this project.
<?asciidoc-br?>
Only components of project type "test" may declare this property. Furthermore, the referenced
projects must all be of the same project type and must not be of type "test" themselves.</simpara>
</listitem>
</varlistentry>
</variablelist>
<variablelist xml:id="package-json-implementationId" xreflabel="implementationId">
<varlistentry>
<term>implementationId</term>
<listitem>
<simpara>(string) If this property is defined, this component is called an "implementation project" and the string value
provides a unique identifier for the implementation provided in this component. If this is defined, property
"implementedProjects" must be defined as well. For details, see <xref linkend="API and Implementation Components"/>.</simpara>
<simpara>Only projects of type "application", "processor", "library", "api" or "validation" may declare this property.</simpara>
</listitem>
</varlistentry>
</variablelist>
<variablelist xml:id="package-json-implementedProjects" xreflabel="implementedProjects">
<varlistentry>
<term>implementedProjects</term>
<listitem>
<simpara>(array) A list of API components (components of type "api") that are implemented by this component. If this
is defined, property "implementationId" must be defined as well. For details, see
<xref linkend="API and Implementation Components"/>. Only components of type "application", "processor", "library", "api"
or "validation" may declare this property.</simpara>
</listitem>
</varlistentry>
</variablelist>
<variablelist xml:id="package-json-requiredRuntimeLibraries" xreflabel="requiredRuntimeLibraries">
<varlistentry>
<term>requiredRuntimeLibraries</term>
<listitem>
<simpara>(array) The list of required runtime library components that are required for the execution of this
component. All components but components of type "runtime environment" may declare this property. Each
required runtime library must also be specified as a dependency using one of the top-level
properties <literal>dependencies</literal> or <literal>devDependencies</literal>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>extendedRuntimeEnvironment</term>
<listitem>
<simpara>(string) The name of the runtime environment project that is extended by this component. Only components of
type "runtime environment" may declare this property.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>providedRuntimeLibraries</term>
<listitem>
<simpara>(array) The list of runtime library components that are provided by this component. Only components of
type "runtime environment" may declare this property.</simpara>
</listitem>
</varlistentry>
</variablelist>
<variablelist xml:id="package-json-definesPackage" xreflabel="definesPackage">
<varlistentry>
<term>definesPackage</term>
<listitem>
<simpara>(string) The name of the package this component provides type definitions for. Only components of project type "definition" may declare this property.</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>All properties described above are optional. The following default values apply:</simpara>
<informaltable frame="all" rowsep="1" colsep="1">
<tgroup cols="2">
<colspec colname="col_1" colwidth="50*"/>
<colspec colname="col_2" colwidth="50*"/>
<tbody>
<row>
<entry align="left" valign="top"><simpara><emphasis role="strong">Property</emphasis></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Default Value</emphasis></simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>name</simpara></entry>
<entry align="left" valign="top"><simpara>name of the folder containing the <literal>package.json</literal> file</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>version</simpara></entry>
<entry align="left" valign="top"><simpara>"0.0.1"</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>projectType</simpara></entry>
<entry align="left" valign="top"><simpara>"plainjs"</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>vendorId</simpara></entry>
<entry align="left" valign="top"><simpara>"vendor.default"</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>mainModule</simpara></entry>
<entry align="left" valign="top"><simpara>"index"</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>output</simpara></entry>
<entry align="left" valign="top"><simpara>"."</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><simpara>sources</simpara></entry>
<entry align="left" valign="top"><simpara>a single source-container of type "source" with path "." (except for yarn workspace roots, see below)</simpara></entry>
</row>
</tbody>
</tgroup>
</informaltable>
<simpara>All other properties are undefined if not given in the <literal>package.json</literal> file. The default source folder of "." does not
apply to projects that represent the root folder of a yarn workspace; those projects do not have any source folder,
by default.</simpara>
<example>
<title>A package.json file with N4JS-specific properties.</title>
<simpara>The following example illustrates how to use the N4JS-related package.json properties.</simpara>
<screen>{
"name": "SampleProject",
"version": "0.0.1",
"author": "Enfore AG",
"main": "./src/js/main.js",
"dependencies": {
"OtherProject": "&gt;=1.2.3 &lt;2.0.0",
"n4js-runtime-es2015": "latest"
},
"devDependencies": {
"org.eclipse.n4js.mangelhaft": "latest"
},
"n4js": {
"projectType": "library",
"vendorId": "org.eclipse.n4js",
"vendorName": "Eclipse N4JS Project",
"output": "src-gen",
"mainModule": "a/b/Main",
"sources": {
"source": [
"src/n4js",
"src/n4js-gen"
],
"external": [
"src-ext"
],
"test": [
"src-test"
]
},
"moduleFilters": {
"noValidate": [
"abc*",
{
"module": "xyz*",
"sourceContainer": "src/n4js"
}
]
},
"requiredRuntimeLibraries": [
"n4js-runtime-es2015"
]
}
}</screen>
</example>
</section>
<section xml:id="_constraints">
<title>Constraints</title>
<simpara>The following constraints apply.</simpara>
<requirement xml:id="IDE-158">
<title>GeneralConstraints</title>
<simpara>
<anchor xml:id="Req-IDE-158" xreflabel="[Req-IDE-158]"/>
<emphasis role="strong">Requirement: IDE-158:</emphasis>
<link linkend="Req-IDE-158">GeneralConstraints</link> (ver. 1)</simpara>
<simpara>
1. There must be an output directory specified so the compiler(s) can run.</simpara>
</requirement>
<requirement xml:id="IDE-159">
<title>Paths</title>
<simpara>
<anchor xml:id="Req-IDE-159" xreflabel="[Req-IDE-159]"/>
<emphasis role="strong">Requirement: IDE-159:</emphasis>
<link linkend="Req-IDE-159">Paths</link> (ver. 1)</simpara>
<simpara>
Paths Paths are constrained in the following way:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>A path cannot appear more than one time within a source fragment type (same applies to paths in the resources section).</simpara>
</listitem>
<listitem>
<simpara>A path cannot be used in different source fragment types at same times.</simpara>
</listitem>
<listitem>
<simpara>A path can only be declared exclusively in one of the sections Output, Libraries, Resources or Sources.</simpara>
</listitem>
<listitem>
<simpara>A path must not contain wild cards.</simpara>
</listitem>
<listitem>
<simpara>A path has to be relative to the project path.</simpara>
</listitem>
<listitem>
<simpara>A path has to point to folder.</simpara>
</listitem>
<listitem>
<simpara>The folder a defined path points to must exist in the project (but in case of non-existent folders of source fragments, only a warning is shown).</simpara>
</listitem>
</orderedlist>
</requirement>
<requirement xml:id="IDE-160">
<title>Module Specifiers</title>
<simpara>
<anchor xml:id="Req-IDE-160" xreflabel="[Req-IDE-160]"/>
<emphasis role="strong">Requirement: IDE-160:</emphasis>
<link linkend="Req-IDE-160">Module Specifiers</link> (ver. 1)</simpara>
<simpara>
Module Specifiers are constrained in the following
way:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>Within a module filter type no duplicate specifiers are allowed.</simpara>
</listitem>
<listitem>
<simpara>A module specifier is by default applied relatively to all defined source containers, i.e. if there src and src2 defined as source containers in both folders files are looked up that matches the given module specifier</simpara>
</listitem>
<listitem>
<simpara>A module specifier can be constrained to be applied only to a certain source container.</simpara>
</listitem>
<listitem>
<simpara>A module specifier is allowed to contain wildcards but it must resolve to some existing files in the project</simpara>
</listitem>
</orderedlist>
</requirement>
<requirement xml:id="IDE-161">
<title>Module Specifier Wildcard Constraints</title>
<simpara>
<anchor xml:id="Req-IDE-161" xreflabel="[Req-IDE-161]"/>
<emphasis role="strong">Requirement: IDE-161:</emphasis>
<link linkend="Req-IDE-161">Module Specifier Wildcard Constraints</link> (ver. 1)</simpara>
<simpara>
. All path patterns are case sensitive.
. <literal>**</literal> all module specifiers will be matched.
. <literal>**/*</literal> all module specifiers will be matched.
. <literal>test/A??</literal> matches all module specifiers whose qualified name consists of two segments where the first part matches test and the second part starts with an <literal>A</literal> and then two more characters.
. <literal>**/test/**/XYZ</literal> - matches all module specifiers whose qualified name contains a segment that matches test and the last segment ends with an ’XYZ’.
. A module specifier wild card isn’t allowed to contain <literal>***</literal>.
. A module specifier wild card isn’t allowed to contain relative navigation.
. A module specifier wild card shouldn’t contain the file extension (only state the file name (pattern) without extension, valid file extensions will then be used to match the file).</simpara>
</requirement>
<simpara>Examples of using external source fragments and filters are given in <xref linkend="_implementation-of-external-declarations"/>, see <xref linkend="external-definitions-and-implementations"/>.</simpara>
<requirement xml:id="GH-821001">
<title>Dependencies to Definition Projects</title>
<simpara>
<anchor xml:id="Req-GH-821001" xreflabel="[Req-GH-821001]"/>
<emphasis role="strong">Requirement: GH-821001:</emphasis>
<link linkend="Req-GH-821001">Dependencies to Definition Projects</link> (ver. 1)</simpara>
<simpara>
. For each listed project dependency of type "definition", a corresponding dependency (in the (dev)dependencies section) must be declared, whose "name" matches the "definesPackage" property value of the definition project.</simpara>
</requirement>
</section>
</section>
<section xml:id="_support-for-npm-scopes" role="language-n4js">
<title>Support for NPM Scopes</title>
<simpara>NPM supports a namespace concept for npm packages. Such namespaces are called "scopes". For details see
<link xl:href="https://docs.npmjs.com/misc/scope">https://docs.npmjs.com/misc/scope</link> and <link xl:href="https://docs.npmjs.com/getting-started/scoped-packages">https://docs.npmjs.com/getting-started/scoped-packages</link>.
In N4JS, this is supported too.</simpara>
<simpara>Terminology:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>A project&#8217;s <emphasis>plain project name</emphasis> is its name without mentioning the project&#8217;s scope (if any),
e.g. <literal>myProject</literal>.</simpara>
</listitem>
<listitem>
<simpara>A project&#8217;s <emphasis>scope name</emphasis> is the name of the npm scope a project resides in, including a leading <literal>@</literal>.
E.g. <literal>@myScope</literal>.</simpara>
</listitem>
<listitem>
<simpara>A project&#8217;s <emphasis>N4JS project name</emphasis> is its plain project name, prefixed by its scope name (if any),
separated by a <literal>/</literal>. For unscoped projects, this is identical to the plain project name.
E.g., <literal>myProject</literal> (if unscoped), <literal>@myScope/myProject</literal> (if scoped).</simpara>
</listitem>
<listitem>
<simpara>A project&#8217;s <emphasis>Eclipse project name</emphasis> is an ancillary name used only within the Eclipse UI for
the project in the workspace. It is equal to the N4JS project name, except that <literal>:</literal> instead of <literal>/</literal> is
used as separator between the scope and plain project name.
E.g., <literal>myProject</literal> (if unscoped), <literal>@myScope:myProject</literal> (if scoped).</simpara>
</listitem>
</orderedlist>
<simpara>In case the intended meaning is apparent from the context, the "N4JS project name" can simply be referred to
as "project name" (as is common practice in the context of npm).</simpara>
<simpara>In N4JS, when importing from a module <literal>M</literal> contained in a scoped project <literal>@myScope/myProject</literal>, the import statement&#8217;s
module specifier should have one of the following forms:</simpara>
<itemizedlist>
<listitem>
<simpara><literal>import * as N from "a/b/c/M";</literal></simpara>
</listitem>
<listitem>
<simpara><literal>import * as N from "@myScope/myProject/a/b/c/M";</literal></simpara>
</listitem>
<listitem>
<simpara><literal>import * as N from "@myScope/myProject";</literal> (if <literal>M</literal> is specified as main module in `myProject&#8217;s package.json)</simpara>
</listitem>
</itemizedlist>
<simpara>Thus, the N4JS project name, which includes the scope name, is simply used in place of an ordinary, non-scoped
project&#8217;s name. This is in line with conventions in Javascript.</simpara>
<requirement xml:id="GH-1026">
<title>General Constraints</title>
<simpara>
<anchor xml:id="Req-GH-1026" xreflabel="[Req-GH-1026]"/>
<emphasis role="strong">Requirement: GH-1026:</emphasis>
<link linkend="Req-GH-1026">General Constraints</link> (ver. 1)</simpara>
<simpara>
1. The name given in the package.json file (i.e. value of top-level property "name") must be equal to
the project&#8217;s "N4JS project name", as defined above.
2. The name of the project folder on disk (i.e. folder containing the package.json file) must be equal to
the project&#8217;s "plain project name", as defined above.
3. Iff the project is scoped, this project folder must have a parent folder with a name equal to
the project&#8217;s "scope name", including the leading <literal>@</literal>.
4. Within Eclipse, the name of of an N4JS project in the workspace UI must be equal to the project&#8217;s
"Eclipse project name", as defined above.</simpara>
</requirement>
</section>
<section xml:id="sec:N4JS-Type-Definitions">
<title>N4JS Type Definitions</title>
<simpara>N4JS projects can depend on ordinary JavaScript projects by including them in the package.json file.
From there on, modules of those JavaScript projects can be imported when writing N4JS code.
However, since JavaScript is untyped there will not be any type information for e.g. classes, functions
of ordinary JavaScript projects unless this type information is provided by a type definition project.
Type definition projects do only contain n4jsd files that reflect the classes and functions of a specific npm.
To refer to a JavaScript npm, the term <emphasis>plain-JS project</emphasis> will be used.</simpara>
<section xml:id="_specify-type-definition">
<title>Specify Type Definition</title>
<simpara>A type definition project is structured like a normal npm.
The major difference is that it provides n4jsd files instead of js files.
These n4jsd files are named like and located at the exact position in the file tree as their js-counterparts.
This ensures the type definition module and the corresponding plain-JS module to have the same fully-qualified name.
Besides the usual properties the package.json file usually needs to specify the following properties in the n4js section.</simpara>
<formalpara>
<title>Package.json: Important properties for type definition projects</title>
<para>
<screen>{
"n4js": {
"projectType": "definition"
"definesPackage": "..."
"mainModule": "..."
}
}</screen>
</para>
</formalpara>
<simpara>The project type declares this project to be a type definition projects.
Consequently, it has to also declare the name for which plain-JS project its type definitions are provided (using <literal>definesPackage</literal>).
Lastly, the main module has to be specified since this information will not be taken from the package.json of the plain-JS project.</simpara>
<simpara>A type definition project may only define a main module if the corresponding plain-JS project defines a main module. In this case,
the main module of the type definition project may have a different fully-qualified name / module specifier (but should, of course,
define the types provided by the plain-JS project&#8217;s main module). This is possible, because in client code the import of a main
module will always look the same, no matter the main module&#8217;s fully-qualified name / module specifier.</simpara>
</section>
<section xml:id="_name-conventions">
<title>Name Conventions</title>
<simpara>A type definition package can have an arbitrary name and define an arbitrary npm package.
This can be handy for testing purposes or just creating some temporary type definitions for a local package.
However, we chose to use a convention to simplify finding the right type definition package for a specific plain-JS project.
First, the scope <literal>@n4jsd</literal> and second the exact name of the plain-JS project is used.
For instance, when a user wants to install type definitions for the plain-JS project <literal>express</literal>,
our related type definitions are called <literal>@n4jsd/express</literal>.</simpara>
</section>
<section xml:id="_version-conventions">
<title>Version Conventions</title>
<simpara>Since the plain-JS project will evolve over time and publish different versions, the need arises to also
publish the type definition project in different versions that reflect this evolution.
In addition to the evolution of the plain-JS project, a new version of the type definition project can
also become necessary in case a bug in the type definitions was found or in case the language of N4JS changes
and the type definitions have to be adjusted accordingly.
Effectively, this will lead to a situation where both the implementation and the type definition project have
a version that are technically unrelated from an npm point of view, but still are somehow related to each other
from a semantical point of view.
To keep the distinct versioning of both of the projects manageable,
we propose the following conventions to partially align the type definition project&#8217;s version to that of the plain-JS project.</simpara>
<section xml:id="_define-a-new-type-definition-package">
<title>Define a New Type Definition Package</title>
<simpara>We use the following convention to compute the version of type definition packages.</simpara>
<simpara>&#160;&#160;&#160;&#160; Major<subscript>types</subscript>.Minor<subscript>types</subscript>.Patch<subscript>types</subscript> = Major<subscript>impl</subscript>.Minor<subscript>impl</subscript>.0</simpara>
<simpara>&#160;&#160;&#160;&#160; Major<subscript>types</subscript>.Minor<subscript>types</subscript>.Patch<subscript>types</subscript> = 3.3.0</simpara>
<simpara>Let&#8217;s say that a new version of a type definition package called <emphasis>types</emphasis> should be created
that defines types for an npm called <emphasis>impl</emphasis> of version Major<subscript>impl</subscript>.Minor<subscript>impl</subscript>.Patch<subscript>impl</subscript>.
According to our convention, the major and minor version numbers of the type definition package
should just be copied from the version of the <emphasis>impl</emphasis> package.
However, the version patch number of <emphasis>types</emphasis> should not be taken from <emphasis>impl</emphasis>.
Instead, the patch number of <emphasis>types</emphasis> starts with <emphasis>0</emphasis> and increases with every update of this type definition version.
For instance when a bug was found in version Major<subscript>types</subscript>.Minor<subscript>types</subscript>.0, the definitions have been extended, or
adjusted to new language features, only the patch number increases to e.g. Major<subscript>types</subscript>.Minor<subscript>types</subscript>.1.</simpara>
</section>
<section xml:id="_using-a-type-definition-package">
<title>Using a Type Definition Package</title>
<simpara>On the client side, a type definition package is listed among the dependency section.
Here we use the following convention to specify the required version of a type definition package.</simpara>
<simpara>"dependencies": {<?asciidoc-br?>
&#160;&#160;&#160;&#160; "@n4jsd/Types": "&lt;=Major<subscript>impl</subscript>.Minor<subscript>impl</subscript>.*"<?asciidoc-br?>
}</simpara>
<simpara>"dependencies": {<?asciidoc-br?>
&#160;&#160;&#160;&#160; "express": "^3.3.3",<?asciidoc-br?>
&#160;&#160;&#160;&#160; "@n4jsd/express": "&lt;=3.3.*"<?asciidoc-br?>
}</simpara>
<simpara>According to this convention, the major and minor version numbers of the implementation package are used,
prepended with a smaller-equals and appended with an asterisk for the patch number.
This also applies when the implementation version contains a tilde, a caret, etc., or is omitting a minor or patch number.
In case a non SemVer version is given (e.g. <literal>latest</literal>, empty string, url, etc.)
it is recommended to plain copy the non SemVer version when possible.</simpara>
</section>
<section xml:id="_rational">
<title>Rational</title>
<simpara>The rational behind this convention reflects the idea of semantic versioning:</simpara>
<blockquote>
<attribution>
<link xl:href="https://www.semver.org">semver.org</link>
</attribution>
<simpara>Given a version number MAJOR.MINOR.PATCH, increment the:</simpara>
<itemizedlist>
<listitem>
<simpara>MAJOR version when you make incompatible API changes,</simpara>
</listitem>
<listitem>
<simpara>MINOR version when you add functionality in a backwards-compatible manner, and</simpara>
</listitem>
<listitem>
<simpara>PATCH version when you make backwards-compatible bug fixes.</simpara>
</listitem>
</itemizedlist>
</blockquote>
<simpara>Patch version increments are always backwards compatible according to SemVer.
In addition also no further functionality is added since this would imply at least an increment of the minor version.
Consequently, patch versions do not affect the interface or type information of an plain-JS project.
This is why patch version number fully suffices to reflect bug fixes and language changes for a given major.minor version.</simpara>
<simpara>On client side, we recommend to use a smaller-equals qualifier because most probably there will not be the exact version
of a requested type definition project.
Instead, only some major.minor versions will have a type definition counterpart.
Using a smaller-equals qualifier will make sure that a client will always get the latest version of a requested plain-JS project version.
In case a newer version of the plain-JS project was already published,
this convention guarantees that a compatible version of the type definition project is installed.</simpara>
</section>
</section>
</section>
<section xml:id="_modules" role="language-n4js">
<title>Modules</title>
<simpara>Each N4JS source file defines a module in the sense of ECMAScript2015, [<link linkend="ECMA15a">ECMA15a(p.S14)</link>].
This is the overall structure of a module, based on [<link linkend="ECMA15a">ECMA15a(p.S14)</link>].</simpara>
<programlisting language="xtext" linenumbering="unnumbered">Script: {Script}
annotations+=ScriptAnnotation*
scriptElements+=ScriptElement*;
/*
* The top level elements in a script are type declarations, exports, imports or statements
*/
ScriptElement:
AnnotatedScriptElement
| N4ClassDeclaration&lt;Yield=false&gt;
| N4InterfaceDeclaration&lt;Yield=false&gt;
| N4EnumDeclaration&lt;Yield=false&gt;
| ImportDeclaration
| ExportDeclaration
| RootStatement&lt;Yield=false&gt;
;</programlisting>
<simpara>Grammar and semantics of import statement is described in <xref linkend="_import-statement"/>; of export statement described in <xref linkend="_export-statement"/>.</simpara>
<simpara>An import statement imports a variable declaration, function declaration, or N4 type declaration defined and exported by another module into the current
module under the given alias (which is similar to the original name if no alias is defined).
The name of the module is its project’s source folder’s relative path without any extension, see <xref linkend="_qualified-names"/> for details.</simpara>
</section>
<section xml:id="_api-and-implementation-component" role="language-n4js">
<title>API and Implementation Component</title>
<note>
<simpara>the API/Implementation concept is not fully implemented at this time (see GH-1291).</simpara>
</note>
<simpara>Instead of providing an implementation, N4JS components may only define an API by way of one or more n4jsd files which is then implemented by separate implementation projects.
For one such API project, several implementation projects may be provided.
Client code using the API will always be bound to the API project only, i.e. only the API project will appear in the client project’s <literal>package.json</literal> file under <literal>dependencies</literal>.
When launching the client code, the launcher will choose an appropriate implementation for each API project in the client code’s direct or indirect dependencies
and transparently replace the API project by the implementation project.
In other words, instead of the API project’s output folder, the implementation project’s output folder will be put on the class path.
Static compile time validations ensure that the implementation projects comply to their corresponding API project.</simpara>
<simpara>Note how this concept can be seen as an alternative way of providing the implementation for an n4jsd file: usually n4jsd files are used to define types
that are implemented in plain JavaScript code or provided by the runtime; this concept allows for providing the implementation of an n4jsd file in form of ordinary N4JS code.</simpara>
<simpara>At this time, the concept of API and implementation components is in a prototype phase and the tool support is limited.
The goal is to gain experience from using the early prototype support and then refine the concept over time.</simpara>
<simpara>Here is a summary of the most important details of this concept (they
are all subject to discussion and change):</simpara>
<itemizedlist>
<listitem>
<simpara>Support for this concept, esp. validations, should not be built into the core language but rather implemented as a separate validation/analysis tool.</simpara>
</listitem>
<listitem>
<simpara>Validation is currently provided in the form of a separate view: the API / Implementation compare view.</simpara>
</listitem>
<listitem>
<simpara>A project that defines one or more other projects in its <literal>package.json</literal> file under <literal>implementedProjects</literal> (cf. <xref linkend="package-json-implementedProjects"/>) is called <emphasis>implementation project</emphasis>.
A project that has another project pointing to itself via <literal>ImplementedProjects</literal> is called <emphasis>API project</emphasis>.
Note that, at the moment, there is no explicit definition making a project an API project.</simpara>
</listitem>
<listitem>
<simpara>An implementation project must define an implementation ID in its <literal>package.json</literal> file using the <literal>implementationId</literal> property in the <literal>n4js</literal> section (cf. <xref linkend="package-json-implementationId"/>).</simpara>
</listitem>
<listitem>
<simpara>For each public or public@Internal classifier or enum in an API project, there must be a corresponding type with the same fully-qualified name of the same or higher visibility in the implementation project.
For each member of such a type in the API, there must exist a corresponding, owned <emphasis>or</emphasis> inherited type-compatible member in the implementation type.</simpara>
</listitem>
<listitem>
<simpara>Beyond type compatibility, formal parameters should have the same name on API and implementation side; however, different names are legal but should be highlighted by API / Implementation tool support as a (legal) change.</simpara>
</listitem>
<listitem>
<simpara>Comments regarding the state of the API or implementation may be added to the JSDoc in the source code using the special tag @apiNote.
API / Implementation tool support should extract and present this information to the user in an appropriate form.</simpara>
</listitem>
<listitem>
<simpara>If an API class <literal>C</literal> implements an interface <literal>I</literal>, it has to explicitly (re-) declare all members of <literal>I</literal> similar to the implementation.
This is necessary for abstract classes anyway in order to distinguish the implemented methods from the non-implemented ones.
For concrete classes, we want all members in <literal>C</literal> in order to be complete and avoid problems when the interface is changed or <literal>C</literal> is made abstract.</simpara>
</listitem>
</itemizedlist>
<section xml:id="_execution-of-api-and-implementation-components">
<title>Execution of API and Implementation Components</title>
<simpara>When launching an N4JS component <emphasis>C</emphasis> under runtime environment <link linkend="AC">RE</link>, the user may(!) provide an implementation ID <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>I</mi><mspace width="-0.167em"/><mi>I</mi><mspace width="-0.167em"/><mi>D</mi></math> to run.
Then, for each API project <emphasis>A</emphasis> in the direct or indirect dependencies of <emphasis>C</emphasis> an implementation project is chosen as follows:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>Collect all implementation projects for <emphasis>A</emphasis> (i.e. projects that specify <emphasis>A</emphasis> in their <literal>package.json</literal> file under <literal>implementedProjects</literal>).</simpara>
</listitem>
<listitem>
<simpara>Remove implementation projects that cannot be run under runtime environment <link linkend="AC">RE</link>, using the same logic as for running ordinary N4JS components (this step is not implemented yet!).</simpara>
</listitem>
<listitem>
<simpara>If there are no implementation projects left, show an error.</simpara>
</listitem>
<listitem>
<simpara>If <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>I</mi><mspace width="-0.167em"/><mi>I</mi><mspace width="-0.167em"/><mi>D</mi></math> is defined (i.e. user specified an implementation ID to run), then:</simpara>
<orderedlist numeration="loweralpha">
<listitem>
<simpara>If there is an implementation project left with implementation ID <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>I</mi><mspace width="-0.167em"/><mi>I</mi><mspace width="-0.167em"/><mi>D</mi></math>, use that.</simpara>
</listitem>
<listitem>
<simpara>Otherwise, show an error.</simpara>
</listitem>
</orderedlist>
</listitem>
<listitem>
<simpara>If <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>I</mi><mspace width="-0.167em"/><mi>I</mi><mspace width="-0.167em"/><mi>D</mi></math> is undefined, then</simpara>
<orderedlist numeration="loweralpha">
<listitem>
<simpara>If there is exactly 1 implementation project left, use it.</simpara>
</listitem>
<listitem>
<simpara>Otherwise, in UI mode prompt the user for a choice, in headless mode how an error.</simpara>
</listitem>
</orderedlist>
</listitem>
</orderedlist>
<simpara>Having found an implementation project <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>I</mi><mi>n</mi></msub></math> for each API project <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>A</mi><mi>n</mi></msub></math>, launch as usual except that whenever <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>A</mi><mi>n</mi></msub></math>’s output folder would be used, use <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>I</mi><mi>n</mi></msub></math>’s
output folder (esp. when constructing a <literal>class path</literal>) and when loading or importing a type from <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>A</mi><mi>n</mi></msub></math> return the corresponding type with the same fully-qualified name from <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>I</mi><mi>n</mi></msub></math>.</simpara>
</section>
<section xml:id="_api-and-implementation-with-di" role="language-n4js">
<title>API and Implementation With DI</title>
<simpara>API projects may use N4JS DI (<xref linkend="_dependency-injection"/>) language features which require Implementation projects to provide DI-compatible behaviour
in order to allow a Client (implemented against an API project) to be executed with a given Implementation project.
This is essential for normal execution and for test execution.</simpara>
<simpara><xref linkend="diag_APITestsDI_Overview"/> shows some of those considerations from test client point of view.</simpara>
<figure xml:id="diag_APITestsDI_Overview">
<title>Overview of API tests with DI</title>
<mediaobject>
<imageobject>
<imagedata fileref="chapters/12_components/fig/diag_ApiTestsDI_Overview.svg"/>
</imageobject>
<textobject><phrase>diag ApiTestsDI Overview</phrase></textobject>
</mediaobject>
</figure>
<simpara>Static DI mechanisms in N4JS allow an API project to enforce Implementation projects to provide all necessary information.
This allows clients to work seamlessly with various implementations without specific knowledge about them or without relying on extra tools for proper project wiring.</simpara>
<simpara><xref linkend="diag_ApiTestsDI_StaticDI"/> shows how API project defines project wiring and enforces certain level of testability.</simpara>
<figure xml:id="diag_ApiTestsDI_StaticDI">
<title>API tests with static DI</title>
<mediaobject>
<imageobject>
<imagedata fileref="chapters/12_components/fig/diag_ApiTestsDI_StaticDI.svg"/>
</imageobject>
<textobject><phrase>diag ApiTestsDI StaticDI</phrase></textobject>
</mediaobject>
</figure>
<simpara>During Client execution, weather it is test execution or not, N4JS mechanisms will replace the API project with a proper Implementation project.
During runtime DI mechanisms will take care of providing proper instances of implantation types.</simpara>
<simpara><xref linkend="diag_ApiTestsDI_Views"/> shows Types View perspective of the client, and Instances View perspective of the client.</simpara>
<figure xml:id="diag_ApiTestsDI_Views">
<title>Types view and Instances view</title>
<mediaobject>
<imageobject>
<imagedata fileref="chapters/12_components/fig/diag_ApiTestsDI_Views.svg" width="80%" scalefit="1" align="center"/>
</imageobject>
<textobject><phrase>diag ApiTestsDI Views</phrase></textobject>
</mediaobject>
</figure>
</section>
</section>
</chapter>
<chapter xml:id="_plainjs">
<title>PlainJS</title>
<simpara>Since N4JS is a super set of JavaScript, is it both possible to use plain JavaScript in N4JS and vice versa.
There may be some obstacles due to concepts introduced by N4JS to make code more maintainable and robust:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>N4JS’ static type system may complain about some older JavaScript hacks. Declared types, in particular, are assumed to be implicitly frozen.</simpara>
</listitem>
<listitem>
<simpara>In N4JS, modules are used as namespaces with explicit export and import statements.
The notion of globals is not directly supported by N4JS as this leads to unexpected side effects (several components providing and thus overriding global definitions, for example).</simpara>
</listitem>
<listitem>
<simpara>N4JS defines a (ECMAScript 6 compatible) concept of object-oriented programming which may conflict with other plain JavaScript solutions.</simpara>
</listitem>
</orderedlist>
<simpara>To overcome these problems, N4JS provides a couple of techniques summarized in this chapter.</simpara>
<section xml:id="_type-inference-and-validation-for-plain-js" role="language-n4js">
<title>Type Inference and Validation for Plain JS</title>
<simpara>In plain JavaScript mode:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>All declared variables are inferred to <literal>any+</literal>.</simpara>
</listitem>
<listitem>
<simpara>All declared functions return and accept a variadic number of arguments of type <literal>any+</literal>.</simpara>
</listitem>
<listitem>
<simpara>It is allowed to use the <literal>return</literal> statement with or without an expression.</simpara>
</listitem>
<listitem>
<simpara>New expressions with a receiver of <literal>any+</literal> is allowed.</simpara>
</listitem>
<listitem>
<simpara>No type arguments are required for generic built-in types.</simpara>
</listitem>
<listitem>
<simpara>Assigning a value to a read-only variable is not checked.</simpara>
</listitem>
<listitem>
<simpara>Undeclared variables are treated as <literal>any+</literal> as well.</simpara>
</listitem>
</orderedlist>
<simpara>Note that this essentially disables all validation particularly since methods such as the ’import’-like function <literal>require</literal> are unknown.</simpara>
</section>
<section xml:id="_external-declarations" role="language-n4js">
<title>External Declarations</title>
<simpara>N4JS supports declaring external classes as a means to declare classes whose implementation is not N4JS so they can be used from N4JS.
Together with structural typing, this allows N4JS to seamlessly integrate frameworks and libraries which have not been implemented in N4JS but in plain ECMAScript or another language.</simpara>
<requirement xml:id="IDE-163">
<title>External allowed occurrences</title>
<simpara>
<anchor xml:id="Req-IDE-163" xreflabel="[Req-IDE-163]"/>
<emphasis role="strong">Requirement: IDE-163:</emphasis>
<link linkend="Req-IDE-163">External allowed occurrences</link> (ver. 1)</simpara>
<simpara>
</simpara>
<itemizedlist>
<listitem>
<simpara>Declarations with external flags are only allowed in files with the extension <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi><mn>4</mn><mi>j</mi><mi>s</mi><mi>d</mi></math> (so called N4JS definition files).</simpara>
</listitem>
<listitem>
<simpara>Only external classes, external interfaces marked with <literal>@N4JS</literal>, external enums, external function declarations and structurally typed interfaces are allowed in a <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi><mn>4</mn><mi>j</mi><mi>s</mi><mi>d</mi></math> file.</simpara>
</listitem>
<listitem>
<simpara>Declarations with external flags are allowed to subclass built-in type <literal>Error</literal> type and all of its descendants such as
<literal>EvalError</literal>, <literal>RangeError</literal>, <literal>ReferenceError</literal>, <literal>SyntaxError</literal>, <literal>TypeError</literal> and <literal>URIError</literal>, although any of the error types are annotated with <literal>@N4JS</literal>.</simpara>
</listitem>
</itemizedlist>
<simpara>The following explanations apply to all external declarations except
where stated otherwise.</simpara>
<simpara>In general, an external declaration uses the same syntax as the declaration of a normal N4JS declaration with the addition of the modifier <literal>external</literal>.</simpara>
<simpara>External classifiers are always ’entirely external’ in that it is not possible to combine defined methods and external methods within a single class or interface.</simpara>
</requirement>
<requirement xml:id="IDE-164">
<title>External classes inheritance</title>
<simpara>
<anchor xml:id="Req-IDE-164" xreflabel="[Req-IDE-164]"/>
<emphasis role="strong">Requirement: IDE-164:</emphasis>
<link linkend="Req-IDE-164">External classes inheritance</link> (ver. 1)</simpara>
<simpara>
1. An external class <emphasis>without</emphasis> the <literal>@N4JS</literal> annotation can only inherit from another external class or from one of the built-in ECMAScript types (e.g. Object).
That is, by default external classes are derived from <literal>Object</literal>.
2. An external class <emphasis>with</emphasis> the annotation <literal>@N4JS</literal> can only inherit from another external class annotated with <literal>@N4JS</literal> or from non-external N4JS classes.</simpara>
</requirement>
<requirement xml:id="IDE-165">
<title>Structurally typed interface implementation</title>
<simpara>
<anchor xml:id="Req-IDE-165" xreflabel="[Req-IDE-165]"/>
<emphasis role="strong">Requirement: IDE-165:</emphasis>
<link linkend="Req-IDE-165">Structurally typed interface implementation</link> (ver. 1)</simpara>
<simpara>
1. An external class <emphasis>without</emphasis> the annotation <literal>@N4JS</literal> can only be implemented by structurally typed interfaces.
2. An external class <emphasis>with</emphasis> the annotation <literal>@N4JS</literal> can only be implemented by structurally typed interfaces annotated with <literal>@N4JS</literal>.
3. An external interface <emphasis>without</emphasis> the annotation <literal>@N4JS</literal> must be defined structurally.</simpara>
</requirement>
<requirement xml:id="IDE-166">
<title>External interface inheritance</title>
<simpara>
<anchor xml:id="Req-IDE-166" xreflabel="[Req-IDE-166]"/>
<emphasis role="strong">Requirement: IDE-166:</emphasis>
<link linkend="Req-IDE-166">External interface inheritance</link> (ver. 1)</simpara>
<simpara>
</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>An interface in a n4jsd file <emphasis>without</emphasis> the annotation <literal>@N4JS</literal> can only inherit from another interface within a n4jsd file.</simpara>
</listitem>
<listitem>
<simpara>An interface <emphasis>with</emphasis> the <literal>@N4JS</literal> annotation can only inherit from another interface annotated with <literal>@N4JS</literal>.</simpara>
</listitem>
</orderedlist>
</requirement>
<requirement xml:id="IDE-167">
<title>External class/interface members</title>
<simpara>
<anchor xml:id="Req-IDE-167" xreflabel="[Req-IDE-167]"/>
<emphasis role="strong">Requirement: IDE-167:</emphasis>
<link linkend="Req-IDE-167">External class/interface members</link> (ver. 1)</simpara>
<simpara>
1. The static and instance methods, getters and setters of an external class must not have a method body.
2. The static and instance fields of an external class must not have an initializer.
3. The constructor of an external class without the annotation <literal>@N4JS</literal> must not be declared private.
4. Methods in interfaces with default implementation which cannot be expressed in the definition file must be annotated with <literal>@ProvidesDefaultImplementation</literal>.
This is only allowed in interfaces annotated with <literal>@N4JS</literal>.
5. Fields in interfaces or classes with initializers which cannot be expressed in the definition file, must be annotated with <literal>@ProvidesInitializer</literal>.
This is only allowed in classes or interfaces annotated with <literal>@N4JS</literal>.</simpara>
<simpara>This means that in external classes, all members except constructors may be declared private even if the class is not annotated with <literal>@N4JS</literal>. In interfaces, however, private members are disallowed anyway,
cf. <xref linkend="Req-IDE-48"/>.</simpara>
</requirement>
<requirement xml:id="IDE-168">
<title>Other external declarations</title>
<simpara>
<anchor xml:id="Req-IDE-168" xreflabel="[Req-IDE-168]"/>
<emphasis role="strong">Requirement: IDE-168:</emphasis>
<link linkend="Req-IDE-168">Other external declarations</link> (ver. 1)</simpara>
<simpara>
</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>The literals of an external enum must not have a value.</simpara>
</listitem>
<listitem>
<simpara>An external function declaration must not have a body.</simpara>
</listitem>
</orderedlist>
</requirement>
<section xml:id="_declaring-externals">
<title>Declaring externals</title>
<simpara>By default, the implicit supertype of an external class is Object.
If the <literal>@N4JS</literal> annotation is provided it is N4Object.
If a superclass is explicitly given, the constraints from the previous section apply.</simpara>
</section>
<section xml:id="_instantiating-external-classes">
<title>Instantiating external classes</title>
<simpara>In most cases, it is desirable to instantiate external classes from external projects.
Publicly exporting the class definition and providing a public constructor is good practice.</simpara>
<simpara>In some cases, the instantiation from an outer scope is not wanted.
A possible approach is to use a structurally typed interface instead of a class to link to the implementation.</simpara>
<simpara>In case of API-definitions (see <xref linkend="_api-and-implementation-components"/>), it might be useful to limit the visibility of classes to narrower scopes such as package or private.</simpara>
<simpara>External declarations can be instantiated if the following three requirements are fulfilled (not a constraint!):</simpara>
<itemizedlist>
<listitem>
<simpara>External declarations have to be exported and be marked as public so they are accessible from outside.</simpara>
</listitem>
<listitem>
<simpara>The contained or inherited constructor of an external class must be public.</simpara>
</listitem>
<listitem>
<simpara>The external class must be linked to an implementation module (see below <xref linkend="_implementation-of-external-declarations"/>).</simpara>
</listitem>
</itemizedlist>
</section>
<section xml:id="_implementation-of-external-declarations">
<title>Implementation of External Declarations</title>
<simpara>All external declarations must be associated with an external implementation module in one way or another.
Any time the external declaration is imported, the compiler generates code that imports the corresponding implementation module at runtime.</simpara>
<simpara>There are two possible ways of linking an external declaration to its corresponding implementation:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>By naming convention defined in the <literal>package.json</literal> file.</simpara>
</listitem>
<listitem>
<simpara>By declaring that the implementation is provided by the JavaScript runtime, see <xref linkend="_runtime-definitions"/> for details.</simpara>
</listitem>
</orderedlist>
<simpara>The naming convention is based on the <literal>external</literal> source fragments defined in the <literal>package.json</literal> file (<xref linkend="package-json"/>).
If the implementation is provided by the runtime directly, then this can be also specified in the <literal>package.json</literal> file by a module filter.</simpara>
<simpara>The implicit link via the naming convention is used to link an external class declaration to its non-N4JS implementation module.
It does not effect validation, but only compilation and runtime.
Essentially, this makes the compiler generate code so that at runtime, the linked implementation module is imported instead of the declaration module.</simpara>
<simpara>In most use cases of external declarations you also want to disable validation and module wrapping by specifying appropriate filters in the <literal>package.json</literal> file.</simpara>
<simpara>Occasionally it is not possible for the validation to correctly detect a corresponding implementation element.
For that reason, it is possible to disable validation of implementations completely via <literal>@@IgnoreImplementation</literal>.</simpara>
<requirement xml:id="IDE-169">
<title>Implementation of External Declarations</title>
<simpara>
<anchor xml:id="Req-IDE-169" xreflabel="[Req-IDE-169]"/>
<emphasis role="strong">Requirement: IDE-169:</emphasis>
<link linkend="Req-IDE-169">Implementation of External Declarations</link> (ver. 1)</simpara>
<simpara>
</simpara>
<simpara>For a given external declaration <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>D</mi></math> but not for
API-definitions <footnote><simpara><xref linkend="_api-and-implementation-components"/></simpara></footnote>, the
following constraints must hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>If the declaration is neither provided by runtime nor validation of implementation is disabled,
a corresponding implementation must be found by the naming convention.
If no such implementation is found, a warning is generated.</simpara>
</listitem>
</orderedlist>
</requirement>
<example xml:id="external-definitions-and-implementations">
<title>External Definitions and Their Implementations</title>
<simpara>If, in addition to standard <literal>source</literal>, the <literal>source-external</literal> fragment is provided in <literal>Sources</literal>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi><mn>4</mn><mi>j</mi><mi>s</mi><mi>d</mi></math> files in the folder tree below source folders
will be related to modules of the same name in the external folders. This is shown in <xref linkend="fig-external-class-naming"/>.</simpara>
<figure xml:id="fig-external-class-naming">
<title>External Class Implementation, Naming Convention</title>
<mediaobject>
<imageobject>
<imagedata fileref="chapters/13_plainJS/fig/externalClassImplementation_naming.svg" width="60%" scalefit="1" align="center"/>
</imageobject>
<textobject><phrase>externalClassImplementation naming</phrase></textobject>
</mediaobject>
</figure>
</example>
</section>
<section xml:id="_example">
<title>Example</title>
<simpara>Assume the following non-N4JS module:</simpara>
<example xml:id="ex:External_Classes_Example">
<title>External Classes</title>
<programlisting language="javascript" linenumbering="unnumbered">module.exports = {
"Point": function Point(x, y) {
this.x = x;
this.y = y;
},
"Circle": function Circle(center, radius) {
this.center = center;
this.radius = radius;
this.scaleX = function(x){ this.x = x; }
this.scaleY= function(y){ this.y = y; }
}
}</programlisting>
<simpara>Assuming</simpara>
<itemizedlist>
<listitem>
<simpara><literal>shapes.js</literal> is placed in project folder <emphasis role="strong">/external/a/b/shapes.js</emphasis></simpara>
</listitem>
<listitem>
<simpara><literal>shapes.n4jsd</literal> is placed in project folder <emphasis role="strong">/src/a/b/shapes.n4jsd</emphasis></simpara>
</listitem>
<listitem>
<simpara><literal>package.json</literal> defines <emphasis role="strong">src</emphasis> as source folder and <emphasis role="strong">external</emphasis> as external source folder</simpara>
</listitem>
</itemizedlist>
<simpara>the following N4JS external class declarations in <emphasis role="strong">shapes.n4jsd</emphasis> are sufficient:</simpara>
<programlisting language="javascript" linenumbering="unnumbered">export external public class Point {
x: number; y: number;
constructor(x: number, y: number);
}
export external public class Circle {
center: Point; radius: number;
constructor(center: Point, radius: number);
}</programlisting>
<simpara>Note that the class and interface names in n4jsd files must match those in the js files, respectively.</simpara>
</example>
<example>
<title>Structurally-typed external interfaces</title>
<programlisting language="javascript" linenumbering="unnumbered">export external public interface ~Scalable {
scaleX(factor: number);
scaleY(factor: number);
}
export external public class Circle implements Scalable {
center: Point;
radius: number; x: number; y: number;
@Override public scaleX(factor: number);
@Override public scaleY(factor: number);
constructor(center: Point, radius: number);
}</programlisting>
</example>
</section>
</section>
<section xml:id="_global-definitions" role="language-n4js">
<title>Global Definitions</title>
<simpara>Existing JavaScript libraries and built-in objects provided by certain JavaScript environments often globally define variables.
Although it is not recommended to use global definitions, this cannot always be avoided.</simpara>
<simpara>N4JS supports global definitions via the annotation <literal>Global</literal>.
This annotation can only be defined on modules (via <literal>@@Global</literal>) – this means that all declarations in the module are
globally defined.<footnote><simpara>Global basically means that the module defines no namespace on its own. Thus the annotation is a script/module related annotation.</simpara></footnote></simpara>
<simpara>We introduce a new pseudo property <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>g</mi><mi>l</mi><mi>o</mi><mi>b</mi><mi>a</mi><mi>l</mi></math> on all declared elements accordingly:</simpara>
<variablelist>
<varlistentry>
<term><literal>@Global</literal> </term>
<listitem>
<simpara>Boolean flag set to true if annotation <literal>@Global</literal> is set in containing module. The flag indicates that the exported element is globally available and must not be imported.</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>Since definition of global elements is not supported by N4JS directly, this can be only used in external definitions.
A declaration with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>g</mi><mi>l</mi><mi>o</mi><mi>b</mi><mi>a</mi><mi>l</mi></math> can be used without explicit import statement. It is not possible to import these declarations.</simpara>
<requirement xml:id="IDE-170">
<title>Global Definitions</title>
<simpara>
<anchor xml:id="Req-IDE-170" xreflabel="[Req-IDE-170]"/>
<emphasis role="strong">Requirement: IDE-170:</emphasis>
<link linkend="Req-IDE-170">Global Definitions</link> (ver. 1)</simpara>
<simpara>
Global Definitions</simpara>
<simpara>For a declaration <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>D</mi></math> with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>D</mi><mo>.</mo><mi>g</mi><mi>l</mi><mi>o</mi><mi>b</mi><mi>a</mi><mi>l</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>true</mtext></mstyle></math>, not a polyfill (<math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>D</mi><mo>.</mo><mi>p</mi><mi>o</mi><mi>l</mi><mi>y</mi><mi>f</mi><mi>i</mi><mi>l</mi><mi>l</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>false</mtext></mstyle><mo>)</mo></mrow></math>, the following constraints must hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>The name of the definition must not be equal to any primitive type (<literal>string</literal>, <literal>number</literal> etc.), <literal>any</literal>, or an built-in N4 type (<literal>N4Object</literal> etc.).</simpara>
</listitem>
<listitem>
<simpara>If the name of the definition equals a basic runtime time Object Type then the project must be a runtime environment:</simpara>
</listitem>
</orderedlist>
<simpara><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>D</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo></mo><mrow><mo>{</mo></mrow></math>
<math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mspace width="3.0em"/><mi>'</mi></msup><mi>O</mi><mi>b</mi><mi>j</mi><mi>e</mi><mi>c</mi><msup><mi>t</mi><mi>'</mi></msup><msup><mo>,</mo><mi>'</mi></msup><mi>F</mi><mi>u</mi><mi>n</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><msup><mi>n</mi><mi>'</mi></msup><msup><mo>,</mo><mi>'</mi></msup><mi>A</mi><mi>r</mi><mi>r</mi><mi>a</mi><msup><mi>y</mi><mi>'</mi></msup><msup><mo>,</mo><mi>'</mi></msup><mi>S</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>n</mi><msup><mi>g</mi><mi>'</mi></msup><msup><mo>,</mo><mi>'</mi></msup><mi>B</mi><mi>o</mi><mi>o</mi><mi>l</mi><mi>e</mi><mi>a</mi><msup><mi>n</mi><mi>'</mi></msup></math>
<math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mspace width="3.0em"/><mi>'</mi></msup><mi>N</mi><mi>u</mi><mi>m</mi><mi>b</mi><mi>e</mi><msup><mi>r</mi><mi>'</mi></msup><msup><mo>,</mo><mi>'</mi></msup><mi>M</mi><mi>a</mi><mi>t</mi><msup><mi>h</mi><mi>'</mi></msup><msup><mo>,</mo><mi>'</mi></msup><mi>D</mi><mi>a</mi><mi>t</mi><msup><mi>e</mi><mi>'</mi></msup><msup><mo>,</mo><mi>'</mi></msup><mi>R</mi><mi>e</mi><mi>g</mi><mi>E</mi><mi>x</mi><msup><mi>p</mi><mi>'</mi></msup><msup><mo>,</mo><mi>'</mi></msup><mi>E</mi><mi>r</mi><mi>r</mi><mi>o</mi><msup><mi>r</mi><mi>'</mi></msup><msup><mo>,</mo><mi>'</mi></msup><mi>J</mi><mi>S</mi><mi>O</mi><msup><mi>N</mi><mi>'</mi></msup></math>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>}</mo></mrow></math>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>D</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>i</mi><mi>n</mi><mi>g</mi><mi>P</mi><mi>r</mi><mi>o</mi><mi>j</mi><mi>e</mi><mi>c</mi><mi>t</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>runtimeEnvironment</mtext></mstyle></math></simpara>
</requirement>
</section>
<section xml:id="_runtime-definitions" role="language-n4js">
<title>Runtime Definitions</title>
<simpara>Some elements are predefined by the JavaScript runtime such as DOM elements by the browser or built-in ECMAScript or non-standard objects.
These elements can be defined by means of external definitions; however, no actual implementation can be provided as these elements are actually provided by the runtime itself.</simpara>
<simpara>Since these cases are rather rare and in order to enable additional checks such as verification that a given runtime actually provides the elements,
this kind of element can only be defined in components of type runtime environment or runtime library (cf <xref linkend="_runtime-environment-and-runtime-libraries"/>).</simpara>
<simpara>N4JS supports runtime definitions via the annotation <literal>@ProvidedByRuntime</literal>.
This annotation can be defined</simpara>
<itemizedlist>
<listitem>
<simpara>on modules (via <literal>@@ProvidedByRuntime</literal>)– this means that all declarations in the module are provided by the runtime</simpara>
</listitem>
<listitem>
<simpara>on export statements or declarations.</simpara>
</listitem>
</itemizedlist>
<simpara>We introduce a new pseudo property <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>p</mi><mi>r</mi><mi>o</mi><mi>v</mi><mi>i</mi><mi>d</mi><mi>e</mi><mi>d</mi><mi>B</mi><mi>y</mi><mi>R</mi><mi>u</mi><mi>n</mi><mi>t</mi><mi>i</mi><mi>m</mi><mi>e</mi></math> accordingly:</simpara>
<variablelist>
<varlistentry>
<term><literal>@ProvidedByRuntime</literal> </term>
<listitem>
<simpara>Boolean flag set to true if the annotation <literal>@ProvidedByRuntime</literal> is set.
Flag indicates that the element is only declared in the module but its implementation is provided by the runtime.</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>Since built-in types are usually defined globally, the annotation <literal>@ProvidedByRuntime</literal> is usually used in combination with <literal>@Global</literal>.</simpara>
<requirement xml:id="IDE-171">
<title>Provided By Runtime</title>
<simpara>
<anchor xml:id="Req-IDE-171" xreflabel="[Req-IDE-171]"/>
<emphasis role="strong">Requirement: IDE-171:</emphasis>
<link linkend="Req-IDE-171">Provided By Runtime</link> (ver. 1)</simpara>
<simpara>
</simpara>
<simpara>For a declaration <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>D</mi></math> with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>D</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>v</mi><mi>i</mi><mi>d</mi><mi>e</mi><mi>d</mi><mi>B</mi><mi>y</mi><mi>R</mi><mi>u</mi><mi>n</mi><mi>t</mi><mi>i</mi><mi>m</mi><mi>e</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>true</mtext></mstyle></math>, the following constraints must hold:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>The declaration must either be an export declaration itself or an exportable declaration.</simpara>
</listitem>
<listitem>
<simpara>The declaration must be contained in a definition module.</simpara>
</listitem>
<listitem>
<simpara>The declaration must be (indirectly) contained in a component of type <math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>runtimeEnvironment</mtext></mstyle></math> or <math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>runtimeLibrary</mtext></mstyle></math>.</simpara>
</listitem>
<listitem>
<simpara>There must be no implementation file with the same name as the definition module if annotation is defined for a whole module.</simpara>
</listitem>
</orderedlist>
</requirement>
</section>
<section xml:id="_applying-polyfills" role="language-n4js">
<title>Applying Polyfills</title>
<simpara>(Runtime) Libraries often do not provide completely new types but modify existing types.
The ECMA-402 Internationalization Standard [<link linkend="ECMA12a">ECMA12a</link>], for example, changes methods of the built-in class <literal>Date</literal> to be timezone-aware.
Other scenarios include new functionality provided by browsers which are not part of an official standard yet.
Even ECMAScript 6 [<link linkend="ECMA15a">ECMA15a</link>] extends the predecessor [<link linkend="ECMA11a">ECMA11a</link>] in terms of new methods or new method parameters added to existing types.
It also adds completely new classes and features, of course.</simpara>
<simpara>The syntax of runtime polyfills is described in section <xref linkend="_polyfill-definitions"/>.
Here, an example of applying a runtime polyfill is detailed.</simpara>
<example>
<title>Object.observe with Polyfill</title>
<simpara>The following snippet demonstrates how to define a polyfill of the built-in class <literal>Object</literal> to add the new ECMAScript 7 observer functionality.
This snippet has to be defined in a runtime library or environment.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">@@ProvidedByRuntime
@@Global
@Polyfill
export external public class Object extends Object {
public static Object observe(Object object, Function callback, Array&lt;string&gt;? accept);
}</programlisting>
<simpara>A client referring to this runtime library (or environment) can now access the observer methods as if it were defined directly in the original declaration of <literal>Object</literal>.</simpara>
</example>
</section>
</chapter>
<chapter xml:id="_jsdoc-2">
<title>JSDoc</title>
<simpara role="language-n4js">In N4JS, comments starting with two asterisks (in <literal>/** .. */</literal>) are interpreted as
documentation comments. The format is similar to JavaDoc or Google
Closure annotations.</simpara>
<section xml:id="_general-n4jsdoc-features" role="language-n4js">
<title>General N4JSDoc Features</title>
<simpara>We distinguish between line and inline tags.
The format of the content of a tag is specific to the tag.
Most line tags, however, contain a description which is simply multiline text with nested inline tags.
Every comment may start with a description.</simpara>
<section xml:id="_provided-inline-tags">
<title>Provided Inline Tags</title>
<section xml:id="jsdoc_tag__code">
<title>@code</title>
<simpara>Small code snippet, not validated yet.</simpara>
</section>
<section xml:id="jsdoc_tag__link">
<title>@link</title>
<simpara>Link to a type of element, not validated or supported in navigation yet.</simpara>
</section>
</section>
</section>
<section xml:id="_n4jsdoc-for-user-projects" role="language-n4js">
<title>N4JSdoc for User Projects</title>
<section xml:id="_standard-tags">
<title>Standard Tags</title>
<section xml:id="_-author">
<title>@author</title>
<simpara>Name of author, repeat for multiple authors. Name of author is not
validated.</simpara>
</section>
<section xml:id="jsdoc_tag_param">
<title>@param</title>
<simpara>Parameter description, not validated at the moment.</simpara>
</section>
<section xml:id="jsdoc_tag_return">
<title>@return</title>
<simpara>Return description, not validated at the moment.</simpara>
</section>
</section>
<section xml:id="_test-related-tags">
<title>Test Related Tags</title>
<simpara>The following tags are supposed to be used only in tests.</simpara>
<section xml:id="jsdoc_tag__testee">
<title>@testee</title>
<simpara>Link to type (maybe a function) or member tested by the test.</simpara>
<requirement xml:id="IDE-172">
<title>@testee Semantics</title>
<simpara>
<anchor xml:id="Req-IDE-172" xreflabel="[Req-IDE-172]"/>
<emphasis role="strong">Requirement: IDE-172:</emphasis>
<link linkend="Req-IDE-172">@testee Semantics</link> (ver. 1)</simpara>
<simpara>
. Tag can be only used on either
.. methods annotated with <literal>@Test</literal>
.. classes in test projects or folders, cf. <xref linkend="jsdoc_tag__testeeFromType"/>.
. Tag requires a single argument, which is a fully qualified name to a type, including the module specifier.
The format is as follows: <literal>moduleSpecifier '.' typeName ( ('.'|'#') memberName)?</literal>
. Tag is <emphasis>not</emphasis> repeatable, that is a single test method (or class) can refer to only one testee.
. Tag precedes the <literal>reqid</literal> tag, i.e., if a <literal>@testee</literal> is specified, the <literal>reqid</literal> will be ignored.</simpara>
</requirement>
<example>
<title>@testee</title>
<simpara>The target element is to be fully qualified including the module specifier. The module specifier is simply
the source folder relative path name with forward slashes. Type and
element are added to that using dot as a separator. For example:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">/**
* @testee some/package/Module.Type.member
*/</programlisting>
</example>
</section>
<section xml:id="jsdoc_tag__testeeFromType">
<title>@testeeFromType</title>
<simpara>Instead of directly linking a test method to a testee, the testee is to
be derived from the linked testee of the test class. This is useful if a
base test class is defined with generic tests, e.g., for testing methods
defined in an interface and implemented by some classes. This base test
class is then extended by concrete test classes, correctly setting up
the test fixture, e.g., creating instances of some classes implementing
the interfaces tested in the base class.</simpara>
<example>
<title>Usage of testeeFromType</title>
<simpara>In the following example, the is used. This tag will lead to a test documentation for <literal>B.foo</literal> and <literal>C.foo</literal>.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">abstract class Base {
/**
* @testeeFromType
*/
@Test testFoo() {..}
}
/**
* @testee B.foo
*/
class TestB extends Base {}
/**
* @testee C.foo
*/
class TestC extends Base {}</programlisting>
</example>
<note>
<simpara>The resulting spec has to be double-checked for consistency
since it is easily possible that too many constraints are generated.</simpara>
</note>
</section>
<section xml:id="_testeeType_and__testeeMember">
<title>@testeeType and @testeeMember</title>
<simpara>Specifying the testee at the test method directly should be sufficient
for most cases. The <literal>@testeeFromType</literal> tag already provides support for some cases in which a base test class is reused by subtypes. This case usually only works if
the base test class tests a single method only. If the base test class
tests several methods and if a sub test class only provides a different
fixture, this mechanism is not sufficient. For that purpose, the two
tags <literal>@testeeFromType</literal> and <literal>@@testeeMember</literal> are to be used.
They enable the separation of a test related to a specific member and the concrete receiver type of the tested member.</simpara>
<simpara>The <literal>@testeeType</literal> is to defined in the test class JSDoc (actually, it is not
recognized when defined in a member JSDoc). The <literal>@testeeMember</literal> is specified in the test method JSDoc. The "real" testee is then computed from the testee type and the testee method.</simpara>
<note>
<simpara>1. This only works for instance members, so far!<?asciidoc-br?>
2. There is no validation for invalid combinations!</simpara>
</note>
<example xml:id="ex:testeetype-and-testeemethod">
<title>testeeType and testeeMethod</title>
<simpara>Assume the following testees:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">class A {
foo(): void {..}
bar(): void { .. this.foo(); ..}
}
class B extends A {
@Override foo() { .. }
}</programlisting>
</example>
<simpara>Assume that the tests have to ensure the same semantics for <literal>bar</literal>, which is
maybe changed by a wrong implementation of <literal>foo</literal>. That is, <literal>bar</literal> is to be tested in
case of the receiver type <literal>A</literal> and <literal>B</literal>. This can be achieved by the following
tests:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">/**
* @testeeType A.A
*/
class ATest {
fixture(): A { return new A(); }
/**
* @testeeMember bar
*/
@Test testBar(): void { assertBehavior( fixture().bar() ); }
}
/**
* @testeeType B.B
*/
class BTest extends ATest {
@Override fixture(): B { return new B(); }
}</programlisting>
<simpara>This actually defines two tests, which is also recognized by the spec
exporter:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara><literal>testBar</literal> for a receiver of type <literal>A</literal>:</simpara>
<simpara><literal>ATest</literal>'s JSDoc <literal>@testeeType</literal> + <literal>ATest.testBar</literal>'s JSDoc <literal>@testeeMember</literal> = testee <literal>A.A.bar</literal></simpara>
</listitem>
<listitem>
<simpara><literal>testBar</literal> for a receiver of type <literal>B</literal>:</simpara>
<simpara><literal>BTest</literal>'s JSDoc <literal>@testeeType</literal> + <literal>ATest.testBar</literal>'s JSDoc <literal>@testeeMember</literal> = testee <literal>B.B.bar</literal></simpara>
</listitem>
</orderedlist>
<note>
<simpara>In all cases when <literal>@testeeFromType</literal> or <literal>@testeeType</literal>/<literal>@testeeMember</literal> is used, the resulting spec has to be
double-checked for consistency. Consider if the multiplication of spec
constraints is truly required, in particular if the original semantics
of a method is not changed. Remember: It is possible to write API tests
and omit the spec constraint generation simply by not adding the testee
links.</simpara>
</note>
<example>
<title>testeeType and testeeMethod with omitted constraints</title>
<simpara>Assume testees similar as in <xref linkend="ex:testeetype-and-testeemethod"/>. Since the semantics of <literal>bar</literal> is not changed in <literal>B</literal>, it is probably not necessary to generate the same constraint in the documentation for <literal>bar</literal> twice (one in the section for class <literal>A</literal> and another one in the section of class <literal>B</literal>).
Still, we want the test to be executed for both receivers. This is how it is achieved:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">abstract class BaseTest {
abstract fixture(): A;
/**
* @testeeMember bar
*/
@Test testBar(): void { assertBehavior( fixture().bar() ); }
}
/**
* @testeeType A.A
*/
class ATest extends BaseTest {
fixture(): A { return new A(); }
}
class BTest extends BaseTest {
@Override fixture(): B { return new B(); }
}</programlisting>
<simpara>This actually defines two tests as in the previous example. Only one
constraint is created in the spec by the spec exporter:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara><literal>testBar</literal> for a receiver of type <literal>A</literal>:<?asciidoc-br?>
<literal>ATest</literal>'s JSDoc <literal>@testeeType</literal> + <literal>BaseTest.testBar</literal>'s JSDoc <literal>@testeeMember</literal> = testee <literal>A.A.bar</literal></simpara>
</listitem>
</orderedlist>
<simpara>Although a test for receiver of type <literal>B</literal> is run, no additional constraint is
created since there is no <literal>@testeeType</literal> available neither in <literal>BTest</literal> nor in <literal>BaseTest</literal>.</simpara>
</example>
</section>
<section xml:id="jsdoc_tag_reqid_in_Tests">
<title>@reqid in Tests</title>
<simpara>ID of feature used in <literal>JSDoc</literal> for the requirements section. If no
testee (via one of the tags above) is given, then the test is linked to
the requirement with given id.</simpara>
</section>
</section>
</section>
<section xml:id="_n4jsdoc-for-api-and-implementation-projects" role="language-n4js">
<title>N4JSDoc for API and Implementation Projects</title>
<simpara>The following tags are supposed to be used in API and implementation
projects.</simpara>
<section xml:id="jsdoc_tag__apiNote">
<title>@apiNote</title>
<simpara>Simple note that is shown in the API compare view.</simpara>
</section>
<section xml:id="API_Project_Tags">
<title>API Project Tags</title>
<simpara>The following tags are supposed to be used in API projects only.</simpara>
<section xml:id="jsdoc_tag_apiState">
<title>@apiState</title>
<simpara>State of type or member definition, e.g., stable or draft. This can be
used to define a history. In this case, the tag has to be repeated. For
example:</simpara>
<programlisting language="n4js" linenumbering="unnumbered">/**
* @apiState stable (WK)
* @apiState reviewed (JvP)
*/</programlisting>
</section>
</section>
</section>
</chapter>
<chapter xml:id="_jsx">
<title>JSX</title>
<section xml:id="_jsx-support" role="language-jsx">
<title>JSX Support</title>
<simpara>N4JS scripts with file extension "n4jsx" support the special <link xl:href="https://facebook.github.io/jsx/">JSX syntax</link>. Files ending with "jsx" are considered as plain Javascript.</simpara>
<requirement xml:id="IDE-241101">
<title>React Component</title>
<simpara>
<anchor xml:id="Req-IDE-241101" xreflabel="[Req-IDE-241101]"/>
<emphasis role="strong">Requirement: IDE-241101:</emphasis>
<link linkend="Req-IDE-241101">React Component</link> (ver. 1)</simpara>
<simpara>
There are two ways of defining a React component:</simpara>
<itemizedlist>
<listitem>
<simpara>functional component: A function is a React component, iff</simpara>
<itemizedlist>
<listitem>
<simpara>its return type is React.Element</simpara>
</listitem>
<listitem>
<simpara>the function has at least one parameter. This first parameter is considered as the <literal>props</literal> property.
The type of the first parameter only has public fields (or methods defined by Object).
The fields may be defined as optional.</simpara>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<simpara>class component: A class is a React component, iff</simpara>
<itemizedlist>
<listitem>
<simpara>it extends <literal>React.Component</literal>.</simpara>
</listitem>
<listitem>
<simpara>the type parameters must be structural types.</simpara>
<itemizedlist>
<listitem>
<simpara>The first parameter defines the <literal>props</literal> type, it must only define public fields (or methods defined by Object)</simpara>
</listitem>
<listitem>
<simpara>The second parameter defines the <literal>state</literal> type, it must only define public fields (or methods defined by Object)</simpara>
</listitem>
</itemizedlist>
</listitem>
</itemizedlist>
</listitem>
</itemizedlist>
<simpara>In both cases, the name must be capitalized (i.e., start with an upper case letter).</simpara>
</requirement>
<simpara>Component must be written capitalized (i.e. first letter upper case), html (or other xml elements) must be written lower case.</simpara>
<requirement xml:id="IDE-241102">
<title>JSX Basic Syntax</title>
<simpara>
<anchor xml:id="Req-IDE-241102" xreflabel="[Req-IDE-241102]"/>
<emphasis role="strong">Requirement: IDE-241102:</emphasis>
<link linkend="Req-IDE-241102">JSX Basic Syntax</link> (ver. 1)</simpara>
<simpara>
</simpara>
<simpara>The following JSX syntax is supported (EBNF with model element assignments as used in Xtext). All other ECMAScript 2015 and N4JS syntax constructs are unaffected by this addition.</simpara>
<programlisting language="xtext" linenumbering="unnumbered">PrimaryExpression &lt;Yield&gt; returns n4js::Expression:
... // as in N4JS
| JSXElement
;
JSXElement:
'&lt;' jsxElementName=JSXElementName JSXAttributes
(('&gt;' jsxChildren+=JSXChild* JSXClosingElement) | ('/' '&gt;'));
fragment JSXClosingElement *:
'&lt;' '/' jsxClosingName=JSXElementName '&gt;';
JSXChild:
JSXElement | JSXExpression
;
JSXExpression: '{' expression=AssignmentExpression&lt;false,false&gt; '}';
JSXElementName:
expression=JSXElementNameExpression
;
JSXElementNameExpression returns n4js::Expression:
IdentifierRef&lt;false&gt;
({n4js::ParameterizedPropertyAccessExpression.target=current} ParameterizedPropertyAccessExpressionTail&lt;false&gt;)*
;
fragment JSXAttributes *:
jsxAttributes+=JSXAttribute*;
JSXAttribute:
JSXSpreadAttribute
|
JSXPropertyAttribute;
JSXSpreadAttribute:
'{' '...' expression=AssignmentExpression&lt;false,false&gt; '}';
JSXPropertyAttribute:
property=[types::IdentifiableElement|IdentifierName] '=' (jsxAttributeValue=StringLiteral | ('{'
jsxAttributeValue=AssignmentExpression&lt;false,false&gt; '}'));</programlisting>
<simpara>This syntax is similar to the syntax defined by <link xl:href="https://facebook.github.io/jsx/">JSX</link>, except that</simpara>
<itemizedlist>
<listitem>
<simpara>JSXNamedspaceName is not supported</simpara>
</listitem>
<listitem>
<simpara>JSXText is not supported, instead, string template literals are to be used</simpara>
</listitem>
</itemizedlist>
</requirement>
<simpara>Remarks on differences between the syntax defined in <xref linkend="Req-IDE-241102"/> and <link xl:href="https://facebook.github.io/jsx/">JSX</link>:</simpara>
<itemizedlist>
<listitem>
<simpara>JSXSelfClosingElement, JSXOpeningElement and JSXClosingElement are merged into one single rule JSXElement</simpara>
</listitem>
<listitem>
<simpara>JSXAttributes is defined as fragment, that is, the attributes become fields of the JSXElement</simpara>
</listitem>
<listitem>
<simpara>The different types of JSXElementName are defined by means of JSXElementNameExpression which is a simple expression, reusing the existing rules IdentifierRef and ParameterizedPropertyAccessExpression to model JSXIdentifer and JSXMemberExpression respectively</simpara>
</listitem>
<listitem>
<simpara>JSXPropertyAttribute models the JSX non-spread JSXAttribute definition; again existing rules (IdentifierName, StringLiteral) are used to model JSX specific ones (JSXAttributeName, JSXDoubleStringCharacters, JSXSingleStringCharacters)</simpara>
</listitem>
</itemizedlist>
<requirement xml:id="IDE-241401">
<title>JSX Syntax With Free Text</title>
<simpara>
<anchor xml:id="Req-IDE-241401" xreflabel="[Req-IDE-241401]"/>
<emphasis role="strong">Requirement: IDE-241401:</emphasis>
<link linkend="Req-IDE-241401">JSX Syntax With Free Text</link> (ver. 1)</simpara>
<simpara>
</simpara>
<programlisting language="xtext" linenumbering="unnumbered">JSXChild:
JSXElement | JSXExpression | JSXText
;</programlisting>
</requirement>
<requirement xml:id="IDE-241103">
<title>JSX Extended Syntax Check</title>
<simpara>
<anchor xml:id="Req-IDE-241103" xreflabel="[Req-IDE-241103]"/>
<emphasis role="strong">Requirement: IDE-241103:</emphasis>
<link linkend="Req-IDE-241103">JSX Extended Syntax Check</link> (ver. 1)</simpara>
<simpara>
* It is an error, if corresponding opening and closing JSXElements have different names.</simpara>
</requirement>
<requirement xml:id="IDE-241113">
<title>JSX Expression Type</title>
<simpara>
<anchor xml:id="Req-IDE-241113" xreflabel="[Req-IDE-241113]"/>
<emphasis role="strong">Requirement: IDE-241113:</emphasis>
<link linkend="Req-IDE-241113">JSX Expression Type</link> (ver. 1)</simpara>
<simpara>
The type of a JSX expression is React.Element.</simpara>
</requirement>
<requirement xml:id="IDE-241114">
<title>React Symbol</title>
<simpara>
<anchor xml:id="Req-IDE-241114" xreflabel="[Req-IDE-241114]"/>
<emphasis role="strong">Requirement: IDE-241114:</emphasis>
<link linkend="Req-IDE-241114">React Symbol</link> (ver. 1)</simpara>
<simpara>
If a JSX literal is used, it is an error if the React symbol is not imported via</simpara>
<programlisting language="jsx" linenumbering="unnumbered">import React from "react"</programlisting>
</requirement>
<requirement xml:id="IDE-241115">
<title>JSXElement names (tags)</title>
<simpara>
<anchor xml:id="Req-IDE-241115" xreflabel="[Req-IDE-241115]"/>
<emphasis role="strong">Requirement: IDE-241115:</emphasis>
<link linkend="Req-IDE-241115">JSXElement names (tags)</link> (ver. 1)</simpara>
<simpara>
* It is an error if a capitalized tag cannot be bound to a function or class declaration.</simpara>
</requirement>
<requirement xml:id="IDE-241116">
<title>JSXElements referring to React components</title>
<simpara>
<anchor xml:id="Req-IDE-241116" xreflabel="[Req-IDE-241116]"/>
<emphasis role="strong">Requirement: IDE-241116:</emphasis>
<link linkend="Req-IDE-241116">JSXElements referring to React components</link> (ver. 1)</simpara>
<simpara>
* It is an error, if a tag binds to a function declaration, which is not conform to the functional component definition.
* It is an error, if a tag binds to a class declaration, which is not conform to the class component definition.</simpara>
</requirement>
<requirement xml:id="IDE-241117">
<title>JSXAttributes and React component properties</title>
<simpara>
<anchor xml:id="Req-IDE-241117" xreflabel="[Req-IDE-241117]"/>
<emphasis role="strong">Requirement: IDE-241117:</emphasis>
<link linkend="Req-IDE-241117">JSXAttributes and React component properties</link> (ver. 1)</simpara>
<simpara>
If the tag binds to a component, the following constraints must hold:</simpara>
<itemizedlist>
<listitem>
<simpara>The attribute must be a non-private field of the properties type.</simpara>
</listitem>
<listitem>
<simpara>The tag should define attributes for all non-optional fields of the properties type. If no attribute is defined for a non-optional field, a warning is issued.</simpara>
</listitem>
<listitem>
<simpara>The type of the attribute expression must be conform to the type of the corresponding <literal>props</literal>'s property</simpara>
</listitem>
</itemizedlist>
</requirement>
<requirement xml:id="IDE-241118">
<title>JSXElements referring to XML elements</title>
<simpara>
<anchor xml:id="Req-IDE-241118" xreflabel="[Req-IDE-241118]"/>
<emphasis role="strong">Requirement: IDE-241118:</emphasis>
<link linkend="Req-IDE-241118">JSXElements referring to XML elements</link> (ver. 1)</simpara>
<simpara>
If the lower-case tag does not bind to a function or class declaration, the following constraints must be hold:</simpara>
<itemizedlist>
<listitem>
<simpara>If the tag is neither a pre-defined HTML tag nor an SVG tag, a warning is issued.</simpara>
</listitem>
<listitem>
<simpara>If an attribute of the tag is not a pre-defined property of the html tag or react specific attributes, a warning is issued. This requirement is currently NOT supported.</simpara>
</listitem>
</itemizedlist>
</requirement>
<requirement xml:id="IDE-241119">
<title>JSXSpreadAttribute behavior</title>
<simpara>
<anchor xml:id="Req-IDE-241119" xreflabel="[Req-IDE-241119]"/>
<emphasis role="strong">Requirement: IDE-241119:</emphasis>
<link linkend="Req-IDE-241119">JSXSpreadAttribute behavior</link> (ver. 1)</simpara>
<simpara>
The use of spread operators within an JSX element for specifying multiple attributes should be allowed. In this case, all constraints regarding type conformity checking and non-optional properties mentioned in <xref linkend="Req-IDE-241117"/> apply to the attributes specified in the spread operator. In particular,</simpara>
<itemizedlist>
<listitem>
<simpara>The type of each attribute specified in spread operator must be conform to the type of the corresponding property of <literal>props</literal>.</simpara>
</listitem>
<listitem>
<simpara>If a non-optional property of <literal>props</literal> is specified neither as attribute nor in a spread operator, a warning is issued.</simpara>
</listitem>
</itemizedlist>
</requirement>
</section>
<section xml:id="_jsx-backend">
<title>JSX Backend</title>
<simpara>The support for JSX in N4JS aims for an implementation that adheres to the idea of <link xl:href="https://reactjs.org/docs/jsx-in-depth.html">React JSX</link>. This means that JSX elements are transpiled to React Element factory calls (e.g. <literal>&lt;div prop="c"&gt;content&lt;/div&gt;</literal> transpiles to <literal>React.createElement('div', {prop: "c"}, null)</literal>). For that, the transpiler must be aware of a specific implementation of React and the corresponding <literal>createElement</literal> function.</simpara>
<requirement xml:id="GH-687">
<title>React Implementation</title>
<simpara>
<anchor xml:id="Req-GH-687" xreflabel="[Req-GH-687]"/>
<emphasis role="strong">Requirement: GH-687:</emphasis>
<link linkend="Req-GH-687">React Implementation</link> (ver. 1)</simpara>
<simpara>
A react implementation is given in terms of a module that fulfils the following properties:</simpara>
<itemizedlist>
<listitem>
<simpara>The FQN of the module is <literal>react</literal>.</simpara>
</listitem>
<listitem>
<simpara>Type definitions are available for the module.</simpara>
</listitem>
<listitem>
<simpara>The module exports a function of name <literal>createElement</literal>.</simpara>
</listitem>
</itemizedlist>
</requirement>
<simpara>If a react implementation is declared as project dependency, the N4JS transpiler automatically imports it to the module using JSX and generates the corresponding factory calls.</simpara>
</section>
</chapter>
<chapter xml:id="_grammars">
<title>Grammars</title>
<simpara>N4JS extends the ECMAScript 2015 language grammar and combines it with type expression.</simpara>
<note>
<simpara>These grammars are slightly simplified versions of the "real" Xtext grammars used in the implementation.
These grammars are post-processed and combined with additional validators so not all constructs are necessarily available in N4JS.</simpara>
</note>
<section xml:id="_type-expressions-grammar">
<title>Type Expressions Grammar</title>
<formalpara xml:id="lst:EBNFTypeExpression">
<title>EBNF Type Expression Grammar</title>
<para>
<programlisting language="xtext" linenumbering="unnumbered">TypeRef:
TypeRefWithoutModifiers =&gt;undefModifier=UndefModifierToken?
| undefModifier=UndefModifierToken;
TypeRefWithoutModifiers:
((ParameterizedTypeRef | ThisTypeRef) =&gt; dynamic?='+'?)
| ConstructorTypeRef
| ClassifierTypeRef
| FunctionTypeExpression
| UnionTypeExpression
| IntersectionTypeExpression;
TypeRefFunctionTypeExpression:
ParameterizedTypeRef
| ConstructorTypeRef
| ClassifierTypeRef
| UnionTypeExpression
| IntersectionTypeExpression
;
TypeRefForCast:
ParameterizedTypeRef
| ThisTypeRef
| ConstructorTypeRef
| ClassifierTypeRef
| FunctionTypeExpression;
TypeRefInClassifierType:
ParameterizedTypeRefNominal
| ThisTypeRefNominal;
ThisTypeRef:
ThisTypeRefNominal | ThisTypeRefStructural;
ThisTypeRefNominal:
'this';
ThisTypeRefStructural:
definedTypingStrategy=TypingStrategyUseSiteOperator
'this'
('with' TStructMemberList)?;
FunctionTypeExpression:
'{'
('@' 'This' '(' declaredThisType=TypeRefFunctionTypeExpression ')')?
'function'
('&lt;' ownedTypeVars+=TypeVariable (',' ownedTypeVars+=TypeVariable)* '&gt;')?
'(' TAnonymousFormalParameterList ')'
(':' returnTypeRef=TypeRef)?
'}';
fragment TAnonymousFormalParameterList*:
(fpars+=TAnonymousFormalParameter (',' fpars+=TAnonymousFormalParameter)*)?
;
TAnonymousFormalParameter:
variadic?='...'? (=&gt; name=TIdentifier ':')? typeRef=TypeRef
;
UnionTypeExpression:
'union' '{' typeRefs+=TypeRefWithoutModifiers (',' typeRefs+=TypeRefWithoutModifiers)* '}';
IntersectionTypeExpression:
'intersection' '{' typeRefs+=TypeRefWithoutModifiers (',' typeRefs+=TypeRefWithoutModifiers)* '}';
ParameterizedTypeRef:
ParameterizedTypeRefNominal | ParameterizedTypeRefStructural;
ParameterizedTypeRefStructural:
definedTypingStrategy=TypingStrategyUseSiteOperator
declaredType=[Type|TypeReferenceName]
(=&gt;'&lt;' typeArgs+=TypeArgument (',' typeArgs+=TypeArgument)* '&gt;')?
('with' TStructMemberList)?;
fragment TStructMemberList*: '{' (astStructuralMembers+=TStructMember (';'|',')?)* '}';
TStructMember:
TStructGetter
| TStructSetter
| TStructMethod
| TStructField;
TStructMethod:
=&gt;
(('&lt;' typeVars+=TypeVariable (',' typeVars+=TypeVariable)* '&gt;')?
name=TypesIdentifier '('
) TAnonymousFormalParameterList ')'
(':' returnTypeRef=TypeRef)?
;
TStructField:
name=TypesIdentifier (':' typeRef=TypeRef)?
;
TStructGetter:
=&gt; ('get'
name=TypesIdentifier)
'(' ')' (':' declaredTypeRef=TypeRef)?
;
TStructSetter:
=&gt; ('set'
name=TypesIdentifier)
'(' fpar=TAnonymousFormalParameter ')'
;
ParameterizedTypeRefNominal:
declaredType=[Type|TypeReferenceName]
(=&gt; '&lt;' typeArgs+=TypeArgument (',' typeArgs+=TypeArgument)* '&gt;')?;
TypingStrategyUseSiteOperator:
'~' ('~' | STRUCTMODSUFFIX)?;
TypingStrategyDefSiteOperator:
'~';
terminal STRUCTMODSUFFIX:
('r' | 'i' | 'w') '~'
;
ConstructorTypeRef:
'constructor' '{' staticTypeRef=TypeRefInClassifierType '}';
ClassifierTypeRef:
'type' '{' staticTypeRef=TypeRefInClassifierType '}';
TypeReferenceName:
IDENTIFIER ('.' IDENTIFIER)*;
TypeArgument:
Wildcard | TypeRef;
Wildcard:
=&gt; ('?') (('extends' declaredUpperBound=TypeRef) | ('super'
declaredLowerBound=TypeRef))?;
UndefModifierToken:
'?';
TypeVariable:
name=IDENTIFIER ('extends' declaredUpperBounds+=ParameterizedTypeRef ('&amp;'
declaredUpperBounds+=ParameterizedTypeRef)*)?;
TypesIdentifier:
IDENTIFIER
| 'get' | 'set' | 'abstract' | 'project'
| 'union' | 'intersection'
| 'as' | 'from' | 'type' | 'void' | 'null';
TIdentifier:
TypesIdentifier
| 'implements' | 'interface'
| 'private' | 'protected' | 'public'
| 'static'
;
terminal IDENTIFIER:
IDENTIFIER_START IDENTIFIER_PART*;
terminal INT:
DECIMAL_INTEGER_LITERAL_FRAGMENT;
terminal ML_COMMENT:
ML_COMMENT_FRAGMENT;
terminal SL_COMMENT:
'//' (!LINE_TERMINATOR_FRAGMENT)*;
terminal EOL:
LINE_TERMINATOR_SEQUENCE_FRAGMENT;
terminal WS:
WHITESPACE_FRAGMENT+;
terminal fragment UNICODE_ESCAPE_FRAGMENT:
'\\' ('u' (
HEX_DIGIT (HEX_DIGIT (HEX_DIGIT HEX_DIGIT?)?)?
| '{' HEX_DIGIT* '}'?
)?)?;
terminal fragment IDENTIFIER_START:
UNICODE_LETTER_FRAGMENT
| '$'
| '_'
| UNICODE_ESCAPE_FRAGMENT;
terminal fragment IDENTIFIER_PART:
UNICODE_LETTER_FRAGMENT
| UNICODE_ESCAPE_FRAGMENT
| '$'
| UNICODE_COMBINING_MARK_FRAGMENT
| UNICODE_DIGIT_FRAGMENT
| UNICODE_CONNECTOR_PUNCTUATION_FRAGMENT
| ZWNJ
| ZWJ;
terminal DOT_DOT:
'..'
;</programlisting>
</para>
</formalpara>
</section>
<section xml:id="_n4js-language-grammar">
<title>N4JS Language Grammar</title>
<programlisting language="xtext" linenumbering="unnumbered">Script: annotations+=ScriptAnnotation*
scriptElements+=ScriptElement*;
ScriptElement:
AnnotatedScriptElement
| N4ClassDeclaration&lt;Yield=false&gt;
| N4InterfaceDeclaration&lt;Yield=false&gt;
| N4EnumDeclaration&lt;Yield=false&gt;
| ImportDeclaration
| ExportDeclaration
| RootStatement&lt;Yield=false&gt;
;
AnnotatedScriptElement:
AnnotationList (
{ExportDeclaration.annotationList=current} ExportDeclarationImpl
| {ImportDeclaration.annotationList=current} ImportDeclarationImpl
| {FunctionDeclaration.annotationList=current}
=&gt;((declaredModifiers+=N4Modifier)* AsyncNoTrailingLineBreak
-&gt;FunctionImpl&lt;Yield=false,YieldIfGenerator=false,Expression=false&gt;)
| (
(
{N4ClassDeclaration.annotationList=current}
(declaredModifiers+=N4Modifier)*
'class' typingStrategy=TypingStrategyDefSiteOperator?
name=BindingIdentifier&lt;Yield=false&gt;
TypeVariables?
ClassExtendsClause&lt;Yield=false&gt;?
| {N4InterfaceDeclaration.annotationList=current}
(declaredModifiers+=N4Modifier)*
'interface' typingStrategy=TypingStrategyDefSiteOperator? name=BindingIdentifier&lt;Yield=false&gt;
TypeVariables?
InterfaceImplementsList?
)
Members&lt;Yield=false&gt;
)
| {N4EnumDeclaration.annotationList=current}
(declaredModifiers+=N4Modifier)*
'enum' name=BindingIdentifier&lt;Yield=false&gt;
'{'
literals+=N4EnumLiteral (',' literals+= N4EnumLiteral)*
'}'
)
;
fragment TypeVariables*:
'&lt;' typeVars+=TypeVariable (',' typeVars+=TypeVariable)* '&gt;'
;
ExportDeclaration:
ExportDeclarationImpl
;
fragment ExportDeclarationImpl*:
'export' (
wildcardExport?='*' ExportFromClause Semi
| ExportClause -&gt;ExportFromClause? Semi
| exportedElement=ExportableElement
| defaultExport?='default' (-&gt;exportedElement=ExportableElement | defaultExportedExpression=AssignmentExpression&lt;In=true,Yield=false&gt; Semi)
)
;
fragment ExportFromClause*:
'from' reexportedFrom=[types::TModule|ModuleSpecifier]
;
fragment ExportClause*:
'{'
(namedExports+=ExportSpecifier (',' namedExports+=ExportSpecifier)* ','?)?
'}'
;
ExportSpecifier:
element=IdentifierRef&lt;Yield=false&gt; ('as' alias=IdentifierName)?
;
ExportableElement:
AnnotatedExportableElement&lt;Yield=false&gt;
| N4ClassDeclaration&lt;Yield=false&gt;
| N4InterfaceDeclaration&lt;Yield=false&gt;
| N4EnumDeclaration&lt;Yield=false&gt;
| ExportedFunctionDeclaration&lt;Yield=false&gt;
| ExportedVariableStatement
;
AnnotatedExportableElement &lt;Yield&gt;:
AnnotationList (
{FunctionDeclaration.annotationList=current}
(declaredModifiers+=N4Modifier)* AsyncNoTrailingLineBreak
FunctionImpl&lt;Yield, Yield, Expression=false&gt;
| {ExportedVariableStatement.annotationList=current}
(declaredModifiers+=N4Modifier)*
varStmtKeyword=VariableStatementKeyword
varDeclsOrBindings+=ExportedVariableDeclarationOrBinding&lt;Yield&gt; ( ',' varDeclsOrBindings+=ExportedVariableDeclarationOrBinding&lt;Yield&gt; )* Semi
| (
(
{N4ClassDeclaration.annotationList=current}
(declaredModifiers+=N4Modifier)*
'class' typingStrategy=TypingStrategyDefSiteOperator?
name=BindingIdentifier&lt;Yield&gt;
TypeVariables?
ClassExtendsClause&lt;Yield&gt;?
| {N4InterfaceDeclaration.annotationList=current}
(declaredModifiers+=N4Modifier)*
('interface') typingStrategy=TypingStrategyDefSiteOperator? name=BindingIdentifier&lt;Yield&gt;
TypeVariables?
InterfaceImplementsList?
)
Members&lt;Yield&gt;
)
| {N4EnumDeclaration.annotationList=current}
(declaredModifiers+=N4Modifier)*
'enum' name=BindingIdentifier&lt;Yield&gt;
'{'
literals+=N4EnumLiteral (',' literals+= N4EnumLiteral)*
'}'
)
;
ImportDeclaration:
ImportDeclarationImpl
;
fragment ImportDeclarationImpl*:
'import' (
ImportClause importFrom?='from'
)? module=[types::TModule|ModuleSpecifier] Semi
;
fragment ImportClause*:
importSpecifiers+=DefaultImportSpecifier (',' ImportSpecifiersExceptDefault)?
| ImportSpecifiersExceptDefault
;
fragment ImportSpecifiersExceptDefault*:
importSpecifiers+=NamespaceImportSpecifier
| '{' (importSpecifiers+=NamedImportSpecifier (',' importSpecifiers+=NamedImportSpecifier)* ','?)? '}'
;
NamedImportSpecifier:
importedElement=[types::TExportableElement|BindingIdentifier&lt;Yield=false&gt;]
| importedElement=[types::TExportableElement|IdentifierName] 'as' alias=BindingIdentifier&lt;Yield=false&gt;
;
DefaultImportSpecifier:
importedElement=[types::TExportableElement|BindingIdentifier&lt;Yield=false&gt;]
;
NamespaceImportSpecifier: '*' 'as' alias=BindingIdentifier&lt;false&gt; (declaredDynamic?='+')?;
ModuleSpecifier: STRING;
FunctionDeclaration &lt;Yield&gt;:
=&gt; ((declaredModifiers+=N4Modifier)* AsyncNoTrailingLineBreak
-&gt; FunctionImpl &lt;Yield,Yield,Expression=false&gt;
) =&gt; Semi?
;
fragment AsyncNoTrailingLineBreak *: (declaredAsync?='async' NoLineTerminator)?;
fragment FunctionImpl&lt;Yield, YieldIfGenerator, Expression&gt;*:
'function'
(
generator?='*' FunctionHeader&lt;YieldIfGenerator,Generator=true&gt; FunctionBody&lt;Yield=true,Expression&gt;
| FunctionHeader&lt;Yield,Generator=false&gt; FunctionBody&lt;Yield=false,Expression&gt;
)
;
fragment FunctionHeader&lt;Yield, Generator&gt;*:
TypeVariables?
name=BindingIdentifier&lt;Yield&gt;?
StrictFormalParameters&lt;Yield=Generator&gt;
(-&gt; ':' returnTypeRef=TypeRef)?
;
fragment FunctionBody &lt;Yield, Expression&gt;*:
&lt;Expression&gt; body=Block&lt;Yield&gt;
| &lt;!Expression&gt; body=Block&lt;Yield&gt;?
;
ExportedFunctionDeclaration&lt;Yield&gt;:
FunctionDeclaration&lt;Yield&gt;
;
FunctionTypeExpression:
{types::FunctionTypeExpression}
'{'
('@' 'This' '(' declaredThisType=TypeRefFunctionTypeExpression ')')?
'function'
('&lt;' ownedTypeVars+=TypeVariable (',' ownedTypeVars+=TypeVariable)* '&gt;')?
'('
(fpars+=TAnonymousFormalParameter (',' fpars+=TAnonymousFormalParameter)*)?
')'
(':' returnTypeRef=TypeRef)?
'}';
AnnotatedFunctionDeclaration &lt;Yield, Default&gt;:
annotationList=AnnotationList
(declaredModifiers+=N4Modifier)* AsyncNoTrailingLineBreak
FunctionImpl&lt;Yield,Yield,Expression=false&gt;
;
FunctionExpression:
(FunctionImpl&lt;Yield=false,YieldIfGenerator=true,Expression=true&gt;
)
;
AsyncFunctionExpression:
=&gt;(declaredAsync?='async' NoLineTerminator 'function')
FunctionHeader&lt;Yield=false,Generator=false&gt; FunctionBody&lt;Yield=false,Expression=true&gt;
;
ArrowExpression &lt;In, Yield&gt;:
=&gt; (
(
'(' (fpars+=FormalParameter&lt;Yield&gt;
(',' fpars+=FormalParameter&lt;Yield&gt;)*)?
')' (':' returnTypeRef=TypeRef)?
| =&gt;(declaredAsync?='async' NoLineTerminator '(')
(fpars+=FormalParameter&lt;Yield&gt; (',' fpars+=FormalParameter&lt;Yield&gt;)*)?
')' (':' returnTypeRef=TypeRef)?
| fpars+=BindingIdentifierAsFormalParameter&lt;Yield&gt;
)
'=&gt;'
)
(-&gt; hasBracesAroundBody?='{' body=BlockMinusBraces&lt;Yield&gt; '}'
| body=ExpressionDisguisedAsBlock&lt;In&gt;)
;
fragment StrictFormalParameters &lt;Yield&gt;*:
'(' (fpars+=FormalParameter&lt;Yield&gt; (',' fpars+=FormalParameter&lt;Yield&gt;)*)? ')'
;
BindingIdentifierAsFormalParameter &lt;Yield&gt;: name=BindingIdentifier&lt;Yield&gt;;
BlockMinusBraces &lt;Yield&gt;: statements+=Statement&lt;Yield&gt;*;
ExpressionDisguisedAsBlock &lt;In&gt;:
statements+=AssignmentExpressionStatement&lt;In&gt;
;
AssignmentExpressionStatement &lt;In&gt;: expression=AssignmentExpression&lt;In,Yield=false&gt;;
AnnotatedExpression &lt;Yield&gt;:
ExpressionAnnotationList (
{N4ClassExpression.annotationList=current}
'class' name=BindingIdentifier&lt;Yield&gt;?
ClassExtendsClause&lt;Yield&gt;?
Members&lt;Yield&gt;
| {FunctionExpression.annotationList=current} AsyncNoTrailingLineBreak
FunctionImpl&lt;Yield=false,YieldIfGenerator=true,Expression=true&gt;
)
;
TypeVariable:
name=IdentifierOrThis
( 'extends' declaredUpperBounds+=ParameterizedTypeRefNominal
('&amp;' declaredUpperBounds+=ParameterizedTypeRefNominal)*
)?
;
FormalParameter &lt;Yield&gt;:
BindingElementFragment&lt;Yield&gt;
;
fragment BindingElementFragment &lt;Yield&gt;*:
(=&gt; bindingPattern=BindingPattern&lt;Yield&gt;
| annotations+=Annotation*
(
variadic?='...'? name=BindingIdentifier&lt;Yield&gt; ColonSepTypeRef?
)
)
('=' initializer=AssignmentExpression&lt;In=true, Yield&gt;)?
;
fragment ColonSepTypeRef*:
':' declaredTypeRef=TypeRef
;
Block &lt;Yield&gt;: =&gt; ('{') statements+=Statement&lt;Yield&gt;* '}';
RootStatement &lt;Yield&gt;:
Block&lt;Yield&gt;
| FunctionDeclaration&lt;Yield&gt;
| VariableStatement&lt;In=true,Yield&gt;
| EmptyStatement
| LabelledStatement&lt;Yield&gt;
| ExpressionStatement&lt;Yield&gt;
| IfStatement&lt;Yield&gt;
| IterationStatement&lt;Yield&gt;
| ContinueStatement&lt;Yield&gt;
| BreakStatement&lt;Yield&gt;
| ReturnStatement&lt;Yield&gt;
| WithStatement&lt;Yield&gt;
| SwitchStatement&lt;Yield&gt;
| ThrowStatement&lt;Yield&gt;
| TryStatement&lt;Yield&gt;
| DebuggerStatement
;
Statement &lt;Yield&gt;:
AnnotatedFunctionDeclaration&lt;Yield,Default=false&gt;
| RootStatement&lt;Yield&gt;
;
enum VariableStatementKeyword:
var='var' | const='const' | let='let'
;
VariableStatement &lt;In, Yield&gt;:
=&gt;(varStmtKeyword=VariableStatementKeyword
)
varDeclsOrBindings+=VariableDeclarationOrBinding&lt;In,Yield,false&gt;
(',' varDeclsOrBindings+=VariableDeclarationOrBinding&lt;In,Yield,false&gt;)* Semi
;
ExportedVariableStatement:
(declaredModifiers+=N4Modifier)*
varStmtKeyword=VariableStatementKeyword
varDeclsOrBindings+=ExportedVariableDeclarationOrBinding&lt;Yield=false&gt;
(',' varDeclsOrBindings+=ExportedVariableDeclarationOrBinding&lt;Yield=false&gt;)* Semi
;
VariableDeclarationOrBinding &lt;In, Yield, OptionalInit&gt;:
VariableBinding&lt;In,Yield,OptionalInit&gt;
| VariableDeclaration&lt;In,Yield,true&gt;
;
VariableBinding &lt;In, Yield, OptionalInit&gt;:
=&gt; pattern=BindingPattern&lt;Yield&gt; (
&lt;OptionalInit&gt; ('=' expression=AssignmentExpression&lt;In,Yield&gt;)?
| &lt;!OptionalInit&gt; '=' expression=AssignmentExpression&lt;In,Yield&gt;
)
;
VariableDeclaration &lt;In, Yield, AllowType&gt;:
VariableDeclarationImpl&lt;In,Yield,AllowType&gt;;
fragment VariableDeclarationImpl &lt;In, Yield, AllowType&gt;*:
annotations+=Annotation*
(
&lt;AllowType&gt; =&gt;(
name=BindingIdentifier&lt;Yield&gt; ColonSepTypeRef?
) ('=' expression=AssignmentExpression&lt;In,Yield&gt;)?
| &lt;!AllowType&gt; =&gt;(
name=BindingIdentifier&lt;Yield&gt;
) ('=' expression=AssignmentExpression&lt;In,Yield&gt;)?
)
;
ExportedVariableDeclarationOrBinding &lt;Yield&gt;:
ExportedVariableBinding&lt;Yield&gt;
| ExportedVariableDeclaration&lt;Yield&gt;
;
ExportedVariableBinding &lt;Yield&gt;:
=&gt; pattern=BindingPattern&lt;Yield&gt; '=' expression=AssignmentExpression&lt;In=true,Yield&gt;
;
ExportedVariableDeclaration &lt;Yield&gt;:
VariableDeclarationImpl&lt;In=true,Yield,AllowType=true&gt;
;
EmptyStatement: ';';
ExpressionStatement &lt;Yield&gt;: expression=Expression&lt;In=true,Yield&gt; Semi;
IfStatement &lt;Yield&gt;: 'if' '(' expression=Expression&lt;In=true,Yield&gt; ')'
ifStmt=Statement&lt;Yield&gt; (=&gt; 'else' elseStmt=Statement&lt;Yield&gt;)?;
IterationStatement &lt;Yield&gt;:
DoStatement&lt;Yield&gt;
| WhileStatement&lt;Yield&gt;
| ForStatement&lt;Yield&gt;
;
DoStatement &lt;Yield&gt;: 'do' statement=Statement&lt;Yield&gt; 'while'
'(' expression=Expression&lt;In=true,Yield&gt; ')' =&gt; Semi?;
WhileStatement &lt;Yield&gt;: 'while' '(' expression=Expression&lt;In=true,Yield&gt; ')'
statement=Statement&lt;Yield&gt;;
ForStatement &lt;Yield&gt;:
'for' '('
(
=&gt;(initExpr=LetIdentifierRef forIn?='in' expression=Expression&lt;In=true,Yield&gt; ')')
| ( -&gt;varStmtKeyword=VariableStatementKeyword
(
=&gt;(varDeclsOrBindings+=BindingIdentifierAsVariableDeclaration&lt;In=false,Yield&gt;
(forIn?='in' | forOf?='of') -&gt;expression=AssignmentExpression&lt;In=true,Yield&gt;?)
| varDeclsOrBindings+=VariableDeclarationOrBinding&lt;In=false,Yield,OptionalInit=true&gt;
(
(',' varDeclsOrBindings+=VariableDeclarationOrBinding&lt;In=false,Yield,false&gt;)* ';'
expression=Expression&lt;In=true,Yield&gt;? ';' updateExpr=Expression&lt;In=true,Yield&gt;?
| forIn?='in' expression=Expression&lt;In=true,Yield&gt;?
| forOf?='of' expression=AssignmentExpression&lt;In=true,Yield&gt;?
)
)
| initExpr=Expression&lt;In=false,Yield&gt;
(
';' expression=Expression&lt;In=true,Yield&gt;? ';' updateExpr=Expression&lt;In=true,Yield&gt;?
| forIn?='in' expression=Expression&lt;In=true,Yield&gt;?
| forOf?='of' expression=AssignmentExpression&lt;In=true,Yield&gt;?
)
| ';' expression=Expression&lt;In=true,Yield&gt;? ';' updateExpr=Expression&lt;In=true,Yield&gt;?
)
')'
) statement=Statement&lt;Yield&gt;
;
LetIdentifierRef:
id=[types::IdentifiableElement|LetAsIdentifier]
;
LetAsIdentifier: 'let';
BindingIdentifierAsVariableDeclaration &lt;In, Yield&gt;:
name=BindingIdentifier&lt;Yield&gt;
;
ContinueStatement &lt;Yield&gt;: 'continue' (label=[LabelledStatement|BindingIdentifier&lt;Yield&gt;])? Semi;
BreakStatement &lt;Yield&gt;: 'break' (label=[LabelledStatement|BindingIdentifier&lt;Yield&gt;])? Semi;
ReturnStatement &lt;Yield&gt;: 'return' (expression=Expression&lt;In=true,Yield&gt;)? Semi;
WithStatement &lt;Yield&gt;: 'with' '(' expression=Expression&lt;In=true,Yield&gt; ')' statement=Statement&lt;Yield&gt;;
SwitchStatement &lt;Yield&gt;:
'switch' '(' expression=Expression&lt;In=true,Yield&gt; ')' '{'
(cases+=CaseClause&lt;Yield&gt;)*
((cases+=DefaultClause&lt;Yield&gt;)
(cases+=CaseClause&lt;Yield&gt;)*)? '}'
;
CaseClause &lt;Yield&gt;: 'case' expression=Expression&lt;In=true,Yield&gt; ':' (statements+=Statement&lt;Yield&gt;)*;
DefaultClause &lt;Yield&gt;: 'default' ':' (statements+=Statement&lt;Yield&gt;)*;
LabelledStatement &lt;Yield&gt;: =&gt; (name=BindingIdentifier&lt;Yield&gt; ':') statement=Statement&lt;Yield&gt;;
ThrowStatement &lt;Yield&gt;:
'throw' expression=Expression&lt;In=true,Yield&gt; Semi;
TryStatement &lt;Yield&gt;:
'try' block=Block&lt;Yield&gt;
((catch=CatchBlock&lt;Yield&gt; finally=FinallyBlock&lt;Yield&gt;?) | finally=FinallyBlock&lt;Yield&gt;)
;
CatchBlock &lt;Yield&gt;: 'catch' '(' catchVariable=CatchVariable&lt;Yield&gt; ')' block=Block&lt;Yield&gt;;
CatchVariable &lt;Yield&gt;:
=&gt;bindingPattern=BindingPattern&lt;Yield&gt;
| =&gt;(name=BindingIdentifier&lt;Yield&gt; -&gt; ColonSepTypeRef)
| name=BindingIdentifier&lt;Yield&gt;
;
FinallyBlock &lt;Yield&gt;: 'finally' block=Block&lt;Yield&gt;;
DebuggerStatement:
'debugger' Semi;
PrimaryExpression &lt;Yield&gt;:
ThisLiteral
| SuperLiteral
| IdentifierRef&lt;Yield&gt;
| ParameterizedCallExpression&lt;Yield&gt;
| Literal
| ArrayLiteral&lt;Yield&gt;
| ObjectLiteral&lt;Yield&gt;
| ParenExpression&lt;Yield&gt;
| AnnotatedExpression&lt;Yield&gt;
| FunctionExpression
| AsyncFunctionExpression
| N4ClassExpression&lt;Yield&gt;
| TemplateLiteral&lt;Yield&gt;
;
ParenExpression &lt;Yield&gt;: '(' expression=Expression&lt;In=true,Yield&gt; ')';
IdentifierRef &lt;Yield&gt;:
id=[types::IdentifiableElement|BindingIdentifier&lt;Yield&gt;]
;
SuperLiteral: 'super';
ThisLiteral: 'this';
ArrayLiteral &lt;Yield&gt;:
'['
elements+=ArrayPadding* (
elements+=ArrayElement&lt;Yield&gt;
(',' elements+=ArrayPadding* elements+=ArrayElement&lt;Yield&gt;)*
(trailingComma?=',' elements+=ArrayPadding*)?
)?
']'
;
ArrayPadding: ',';
ArrayElement &lt;Yield&gt;: spread?='...'? expression=AssignmentExpression&lt;In=true,Yield&gt;;
ObjectLiteral &lt;Yield&gt;: '{'
( propertyAssignments+=PropertyAssignment&lt;Yield&gt;
(',' propertyAssignments+=PropertyAssignment&lt;Yield&gt;)* ','?
)?
'}'
;
PropertyAssignment &lt;Yield&gt;:
AnnotatedPropertyAssignment&lt;Yield&gt;
| PropertyNameValuePair&lt;Yield&gt;
| PropertyGetterDeclaration&lt;Yield&gt;
| PropertySetterDeclaration&lt;Yield&gt;
| PropertyMethodDeclaration&lt;Yield&gt;
| PropertyNameValuePairSingleName&lt;Yield&gt;
;
AnnotatedPropertyAssignment &lt;Yield&gt;:
PropertyAssignmentAnnotationList (
=&gt;( {PropertyNameValuePair.annotationList=current} declaredTypeRef=TypeRef?
LiteralOrComputedPropertyName&lt;Yield&gt; ':'
) expression=AssignmentExpression&lt;In=true,Yield&gt;
| =&gt;({PropertyGetterDeclaration.annotationList=current}
GetterHeader&lt;Yield&gt;
) body=Block&lt;Yield=false&gt;
| =&gt;({PropertySetterDeclaration.annotationList=current}
'set' -&gt;LiteralOrComputedPropertyName &lt;Yield&gt;
) '(' fpar=FormalParameter&lt;Yield&gt; ')' body=Block&lt;Yield=false&gt;
| =&gt;({PropertyMethodDeclaration.annotationList=current}
TypeVariables? returnTypeRef=TypeRef?
(generator?='*' LiteralOrComputedPropertyName&lt;Yield&gt; -&gt;MethodParamsAndBody &lt;Generator=true&gt;
| LiteralOrComputedPropertyName&lt;Yield&gt; -&gt; MethodParamsAndBody &lt;Generator=false&gt;
)
) ';'?
| {PropertyNameValuePairSingleName.annotationList=current}
declaredTypeRef=TypeRef? identifierRef=IdentifierRef&lt;Yield&gt;
( '=' expression=AssignmentExpression&lt;In=true,Yield&gt;)?)
;
PropertyMethodDeclaration &lt;Yield&gt;:
=&gt; (TypeVariables? returnTypeRef=TypeRef?
(
generator?='*' LiteralOrComputedPropertyName&lt;Yield&gt;
-&gt;MethodParamsAndBody&lt;Generator=true&gt;
| LiteralOrComputedPropertyName&lt;Yield&gt; -&gt;MethodParamsAndBody &lt;Generator=false&gt;
)
)
';'?
;
PropertyNameValuePair &lt;Yield&gt;:
=&gt; (
declaredTypeRef=TypeRef? LiteralOrComputedPropertyName&lt;Yield&gt; ':'
)
expression=AssignmentExpression&lt;In=true,Yield&gt;
;
PropertyNameValuePairSingleName &lt;Yield&gt;:
declaredTypeRef=TypeRef?
identifierRef=IdentifierRef&lt;Yield&gt;
('=' expression=AssignmentExpression&lt;In=true,Yield&gt;)?
;
PropertyGetterDeclaration &lt;Yield&gt;:
=&gt;(
GetterHeader&lt;Yield&gt;
)
body=Block&lt;Yield=false&gt;
;
PropertySetterDeclaration &lt;Yield&gt;:
=&gt;(
'set'
-&gt;LiteralOrComputedPropertyName &lt;Yield&gt;
)
'(' fpar=FormalParameter&lt;Yield&gt; ')' body=Block&lt;Yield=false&gt;
;
ParameterizedCallExpression &lt;Yield&gt;:
TypeArguments
target=IdentifierRef&lt;Yield&gt;
ArgumentsWithParentheses&lt;Yield&gt;
;
LeftHandSideExpression &lt;Yield&gt;:
MemberExpression&lt;Yield&gt; (
{ParameterizedCallExpression.target=current} ArgumentsWithParentheses&lt;Yield&gt;
(
{ParameterizedCallExpression.target=current} ArgumentsWithParentheses&lt;Yield&gt;
| {IndexedAccessExpression.target=current} IndexedAccessExpressionTail&lt;Yield&gt;
| {ParameterizedPropertyAccessExpression.target=current}
ParameterizedPropertyAccessExpressionTail&lt;Yield&gt;
| -&gt;({TaggedTemplateString.target=current} template=TemplateLiteral&lt;Yield&gt;)
)*
)?
;
fragment Arguments &lt;Yield&gt;*:
arguments+=AssignmentExpression&lt;In=true,Yield&gt;
(',' arguments+=AssignmentExpression&lt;In=true,Yield&gt;)*
(',' spread?='...' arguments+=AssignmentExpression&lt;In=true,Yield&gt;)?
| spread?='...' arguments+=AssignmentExpression&lt;In=true,Yield&gt;
;
fragment TypeArguments*:
'&lt;' typeArgs+=TypeRef (',' typeArgs+=TypeRef)* '&gt;'
;
fragment ArgumentsWithParentheses &lt;Yield&gt;*:
'(' Arguments&lt;Yield&gt;? ')'
;
MemberExpression &lt;Yield&gt;:
=&gt;('new' '.') 'target'
| =&gt; ('new') callee=MemberExpression&lt;Yield&gt; (-&gt; TypeArguments)?
(=&gt; withArgs?='(' Arguments&lt;Yield&gt;? ')'
(
{IndexedAccessExpression.target=current} IndexedAccessExpressionTail&lt;Yield&gt;
| {ParameterizedPropertyAccessExpression.target=current}
ParameterizedPropertyAccessExpressionTail&lt;Yield&gt;
| {TaggedTemplateString.target=current} template=TemplateLiteral&lt;Yield&gt;
)*
)?
| PrimaryExpression&lt;Yield&gt; (
{IndexedAccessExpression.target=current} IndexedAccessExpressionTail&lt;Yield&gt;
| {ParameterizedPropertyAccessExpression.target=current}
ParameterizedPropertyAccessExpressionTail&lt;Yield&gt;
| {TaggedTemplateString.target=current} template=TemplateLiteral&lt;Yield&gt;
)*
;
fragment IndexedAccessExpressionTail &lt;Yield&gt;*:
'[' index=Expression&lt;In=true,Yield&gt; ']'
;
fragment ParameterizedPropertyAccessExpressionTail &lt;Yield&gt;*:
'.' TypeArguments? property=[types::IdentifiableElement|IdentifierName]
;
PostfixExpression &lt;Yield&gt;:
LeftHandSideExpression&lt;Yield&gt; (
=&gt;({PostfixExpression.expression=current} op=PostfixOperator
)
)?
;
enum PostfixOperator: inc='++' | dec='--';
CastExpression &lt;Yield&gt;: PostfixExpression&lt;Yield&gt;
(=&gt;({CastExpression.expression=current} 'as') targetTypeRef=TypeRefForCast)?;
UnaryExpression &lt;Yield&gt;:
CastExpression&lt;Yield&gt;
| (op=UnaryOperator expression=UnaryExpression&lt;Yield&gt;);
enum UnaryOperator: delete | void | typeof | inc='++' | dec='--' | pos='+' | neg='-' | inv='~' | not='!';
MultiplicativeExpression &lt;Yield&gt;: UnaryExpression&lt;Yield&gt;
(=&gt;({MultiplicativeExpression.lhs=current} op=MultiplicativeOperator)
rhs=UnaryExpression&lt;Yield&gt;)*;
enum MultiplicativeOperator: times='*' | div='/' | mod='%';
AdditiveExpression &lt;Yield&gt;: MultiplicativeExpression&lt;Yield&gt;
(=&gt;({AdditiveExpression.lhs=current} op=AdditiveOperator)
rhs=MultiplicativeExpression&lt;Yield&gt;)*;
enum AdditiveOperator: add='+' | sub='-';
ShiftExpression &lt;Yield&gt;: AdditiveExpression&lt;Yield&gt;
(=&gt;({ShiftExpression.lhs=current} op=ShiftOperator rhs=AdditiveExpression&lt;Yield&gt;))*
;
ShiftOperator:
'&gt;' '&gt;' '&gt;'?
| '&lt;&lt;'
;
RelationalExpression &lt;In, Yield&gt;: ShiftExpression&lt;Yield&gt;
=&gt;({RelationalExpression.lhs=current} op=RelationalOperator&lt;In&gt;
-&gt;rhs=ShiftExpression&lt;Yield&gt;)*;
RelationalOperator &lt;In&gt;:
'&lt;' | '&gt;' | '&lt;=' | '&gt;=' | 'instanceof' | &lt;In&gt; 'in';
EqualityExpression &lt;In, Yield&gt;: RelationalExpression&lt;In,Yield&gt;
(=&gt;({EqualityExpression.lhs=current} op=EqualityOperator) rhs=RelationalExpression&lt;In,Yield&gt;)*;
enum EqualityOperator: same='===' | nsame='!==' | eq='==' | neq='!=';
BitwiseANDExpression &lt;In, Yield&gt;: EqualityExpression&lt;In,Yield&gt;
(=&gt;({BinaryBitwiseExpression.lhs=current} op=BitwiseANDOperator) rhs=EqualityExpression&lt;In,Yield&gt;)*;
BitwiseANDOperator: '&amp;';
BitwiseXORExpression &lt;In, Yield&gt;: BitwiseANDExpression&lt;In,Yield&gt;
(=&gt;({BinaryBitwiseExpression.lhs=current} op=BitwiseXOROperator) rhs=BitwiseANDExpression&lt;In,Yield&gt;)*;
BitwiseXOROperator: '^';
BitwiseORExpression &lt;In, Yield&gt;: BitwiseXORExpression&lt;In,Yield&gt;
(=&gt;({BinaryBitwiseExpression.lhs=current} op=BitwiseOROperator) rhs=BitwiseXORExpression&lt;In,Yield&gt;)*;
BitwiseOROperator: '|';
LogicalANDExpression &lt;In, Yield&gt;: BitwiseORExpression&lt;In,Yield&gt;
(=&gt; ({BinaryLogicalExpression.lhs=current} op=LogicalANDOperator) rhs=BitwiseORExpression&lt;In,Yield&gt;)*;
LogicalANDOperator: '&amp;&amp;';
LogicalORExpression &lt;In, Yield&gt;: LogicalANDExpression&lt;In,Yield&gt;
(=&gt;({BinaryLogicalExpression.lhs=current} op=LogicalOROperator) rhs=LogicalANDExpression&lt;In,Yield&gt;)*;
LogicalOROperator: '||';
ConditionalExpression &lt;In, Yield&gt;: LogicalORExpression&lt;In,Yield&gt;
(=&gt; ({ConditionalExpression.expression=current} '?') trueExpression=AssignmentExpression&lt;In=true,Yield&gt;
':' falseExpression=AssignmentExpression&lt;In,Yield&gt;)?;
AssignmentExpression &lt;In, Yield&gt;:
AwaitExpression&lt;In,Yield&gt;
| PromisifyExpression&lt;In,Yield&gt;
| ArrowExpression&lt;In,Yield&gt;
| &lt;Yield&gt; YieldExpression&lt;In&gt;
| ConditionalExpression&lt;In,Yield&gt;
(=&gt; ({AssignmentExpression.lhs=current} op=AssignmentOperator)
rhs=AssignmentExpression&lt;In,Yield&gt;)?
;
YieldExpression &lt;In&gt;:
'yield' =&gt; many?='*'? -&gt; expression=AssignmentExpression&lt;In,Yield=true&gt;?
;
AssignmentOperator:
'=' | '*=' | '/=' | '%=' | '+=' | '-='
| '&lt;&lt;='
| '&gt;' '&gt;'? '&gt;='
| '&amp;=' | '^=' | '|='
;
AwaitExpression &lt;In, Yield&gt;:
=&gt;('await') expression=AssignmentExpression&lt;In,Yield&gt;;
PromisifyExpression &lt;In, Yield&gt;:
=&gt; ('@' 'Promisify') expression=AssignmentExpression&lt;In,Yield&gt;;
Expression &lt;In, Yield&gt;:
AssignmentExpression&lt;In,Yield&gt; ({CommaExpression.exprs+=current}
',' exprs+=AssignmentExpression&lt;In,Yield&gt;
(',' exprs+=AssignmentExpression&lt;In,Yield&gt;)*)?
;
TemplateLiteral &lt;Yield&gt;:
(
segments+=NoSubstitutionTemplate
| segments+=TemplateHead segments+=Expression&lt;In=true,Yield&gt;? TemplateExpressionEnd
(
segments+=TemplateMiddle segments+=Expression&lt;In=true,Yield&gt;?
TemplateExpressionEnd
)*
segments+=TemplateTail
)
;
TemplateExpressionEnd:
'}'
;
NoSubstitutionTemplate:
rawValue=NO_SUBSTITUTION_TEMPLATE_LITERAL
;
TemplateHead:
rawValue=TEMPLATE_HEAD
;
TemplateTail:
rawValue=TemplateTailLiteral;
TemplateMiddle:
rawValue=TemplateMiddleLiteral;
Literal:
NumericLiteral | BooleanLiteral | StringLiteral
| NullLiteral | RegularExpressionLiteral;
NullLiteral: 'null';
BooleanLiteral: (true?='true' | 'false');
StringLiteral: value=STRING;
NumericLiteral:
DoubleLiteral | IntLiteral | BinaryIntLiteral | OctalIntLiteral
| LegacyOctalIntLiteral | HexIntLiteral | ScientificIntLiteral;
DoubleLiteral: value=DOUBLE;
IntLiteral: value=INT;
OctalIntLiteral: value=OCTAL_INT;
LegacyOctalIntLiteral: value=LEGACY_OCTAL_INT;
HexIntLiteral: value=HEX_INT;
BinaryIntLiteral: value=BINARY_INT;
ScientificIntLiteral: value=SCIENTIFIC_INT;
RegularExpressionLiteral: value=REGEX_LITERAL;
NumericLiteralAsString:
DOUBLE | INT | OCTAL_INT | HEX_INT | SCIENTIFIC_INT
;
IdentifierOrThis:
IDENTIFIER
| 'This'
| 'Promisify'
| 'target';
AnnotationName:
IDENTIFIER
| 'This'
| 'target';
BindingIdentifier &lt;Yield&gt;:
IDENTIFIER
| &lt;!Yield&gt; 'yield'
| N4Keyword
;
IdentifierName:
IDENTIFIER | ReservedWord | N4Keyword
;
ReservedWord:
'break' | 'case' | 'catch' | 'class' | 'const' | 'continue' | 'debugger' | 'default' | 'delete'
| 'do' | 'else' | 'export' | 'extends' | 'finally' | 'for' | 'function' | 'if' | 'import'
| 'in' | 'instanceof' | 'new' | 'return' | 'super' | 'switch' | 'this' | 'throw' | 'try'
| 'typeof' | 'var' | 'void' | 'while' | 'with' | 'yield'
| 'null'
| 'true' | 'false'
| 'enum';
N4Keyword:
'get' | 'set'
| 'let'
| 'project'
| 'external' | 'abstract' | 'static'
| 'as' | 'from' | 'constructor' | 'of' | 'target'
| 'type' | 'union' | 'intersection'
| 'This' | 'Await' | 'Promisify'
| 'await'
| 'async'
| 'implements' | 'interface'
| 'private' | 'protected' | 'public'
;
SymbolLiteralComputedName &lt;Yield&gt;:
BindingIdentifier&lt;Yield&gt; ('.' IdentifierName)?
;
terminal DOUBLE:
'.' DECIMAL_DIGIT_FRAGMENT+ EXPONENT_PART?
| DECIMAL_INTEGER_LITERAL_FRAGMENT '.' DECIMAL_DIGIT_FRAGMENT* EXPONENT_PART?
;
terminal HEX_INT: '0' ('x' | 'X') INT_SUFFIX;
terminal BINARY_INT: '0' ('b' | 'B') INT_SUFFIX;
terminal OCTAL_INT: '0' ('o' | 'O') INT_SUFFIX;
terminal LEGACY_OCTAL_INT: '0' DECIMAL_DIGIT_FRAGMENT INT_SUFFIX;
terminal fragment INT_SUFFIX: IDENTIFIER_PART*;
terminal SCIENTIFIC_INT:
DECIMAL_INTEGER_LITERAL_FRAGMENT EXPONENT_PART
;
terminal fragment EXPONENT_PART:
('e' | 'E') SIGNED_INT
| IDENTIFIER
;
terminal fragment SIGNED_INT:
('+' | '-') DECIMAL_DIGIT_FRAGMENT+ IDENTIFIER?
;
terminal STRING:
'"' DOUBLE_STRING_CHAR* '"'?
| "'" SINGLE_STRING_CHAR* "'"?
;
terminal fragment DOUBLE_STRING_CHAR:
!(LINE_TERMINATOR_FRAGMENT | '"' | '\\')
| '\\' (LINE_TERMINATOR_SEQUENCE_FRAGMENT | !LINE_TERMINATOR_FRAGMENT)?
;
terminal fragment SINGLE_STRING_CHAR:
!(LINE_TERMINATOR_FRAGMENT | "'" | '\\')
| '\\' (LINE_TERMINATOR_SEQUENCE_FRAGMENT | !LINE_TERMINATOR_FRAGMENT)?
;
terminal fragment BACKSLASH_SEQUENCE:
'\\' !(LINE_TERMINATOR_FRAGMENT)?
;
terminal fragment REGEX_CHAR:
!(LINE_TERMINATOR_FRAGMENT | '\\' | '/' | '[')
| BACKSLASH_SEQUENCE
| '[' REGEX_CHAR_OR_BRACKET* ']'?
;
terminal fragment REGEX_CHAR_OR_BRACKET:
!(LINE_TERMINATOR_FRAGMENT | '\\' | ']')
| BACKSLASH_SEQUENCE
;
REGEX_LITERAL:
('/' | '/=') REGEX_TAIL?
;
terminal fragment ACTUAL_REGEX_TAIL:
REGEX_CHAR+ ('/' IDENTIFIER_PART*)?
| '/' IDENTIFIER_PART*
;
terminal fragment REGEX_START:
('/' | '/=')
;
terminal REGEX_TAIL: // post processed
'//1'
;
terminal TEMPLATE_HEAD:
"`" TEMPLATE_LITERAL_CHAR* '$'+ '{'
;
terminal NO_SUBSTITUTION_TEMPLATE_LITERAL:
'`' TEMPLATE_LITERAL_CHAR* '$'* "`"?
;
terminal fragment ACTUAL_TEMPLATE_END:
TEMPLATE_LITERAL_CHAR* ('$'+ ('{' | '`'?) | '`'?)
;
terminal fragment TEMPLATE_LITERAL_CHAR:
!(LINE_TERMINATOR_FRAGMENT | '`' | '\\' | '$')
| '$'+ !('{' | '`' | '$')
| LINE_TERMINATOR_SEQUENCE_FRAGMENT
| '\\' (LINE_TERMINATOR_SEQUENCE_FRAGMENT | !LINE_TERMINATOR_FRAGMENT)?
;
TemplateTailLiteral:
TEMPLATE_END?
;
TemplateMiddleLiteral:
TEMPLATE_MIDDLE
;
terminal TEMPLATE_MIDDLE:
'//2' // will never be lexed
;
terminal TEMPLATE_END:
'//3' // will never be lexed
;
terminal fragment TEMPLATE_CONTINUATION:
'//4' // actually '}'
;
Semi: ';'; // automatic semicolon instertion, post-processed
fragment NoLineTerminator*: NO_LINE_TERMINATOR?;
terminal NO_LINE_TERMINATOR:
'//5' // post-processed, will never be lexed
;
Annotation:'@' AnnotationNoAtSign;
ScriptAnnotation: '@@' AnnotationNoAtSign;
AnnotationNoAtSign:
name=AnnotationName (=&gt; '(' (args+=AnnotationArgument (',' args+=AnnotationArgument)*)? ')')?;
AnnotationArgument:
LiteralAnnotationArgument | TypeRefAnnotationArgument
;
LiteralAnnotationArgument:
literal=Literal
;
TypeRefAnnotationArgument:
typeRef=TypeRef
;
AnnotationList:
=&gt;('@' -&gt; annotations+=AnnotationNoAtSign) annotations+=Annotation*
;
ExpressionAnnotationList:
annotations+=Annotation+
;
PropertyAssignmentAnnotationList:
annotations+=Annotation+
;
N4MemberAnnotationList:
{N4MemberAnnotationList} annotations+=Annotation+
;
TypeReferenceName:
'void' | 'This' | 'await' | 'Promisify' | 'target' | QualifiedTypeReferenceName
;
QualifiedTypeReferenceName:
IDENTIFIER ('.' IDENTIFIER)?
;
N4ClassDeclaration &lt;Yield&gt;:
=&gt;(
{N4ClassDeclaration}
(declaredModifiers+=N4Modifier)*
'class' typingStrategy=TypingStrategyDefSiteOperator? name=BindingIdentifier&lt;Yield&gt;?
)
TypeVariables?
ClassExtendsClause&lt;Yield&gt;?
Members&lt;Yield&gt;
;
fragment Members &lt;Yield&gt;*:
'{'
ownedMembersRaw+=N4MemberDeclaration&lt;Yield&gt;*
'}'
;
fragment ClassExtendsClause &lt;Yield&gt;*:
'extends' (
=&gt;superClassRef=ParameterizedTypeRefNominal ('implements' ClassImplementsList)?
| superClassExpression=LeftHandSideExpression&lt;Yield&gt;
)
| 'implements' ClassImplementsList
;
fragment ClassImplementsList*:
implementedInterfaceRefs+=ParameterizedTypeRefNominal
(',' implementedInterfaceRefs+=ParameterizedTypeRefNominal)*
;
N4ClassExpression &lt;Yield&gt;:
{N4ClassExpression}
'class' name=BindingIdentifier&lt;Yield&gt;?
ClassExtendsClause&lt;Yield&gt;?
Members&lt;Yield&gt;;
N4InterfaceDeclaration &lt;Yield&gt;:
=&gt; (
{N4InterfaceDeclaration}
(declaredModifiers+=N4Modifier)*
'interface' typingStrategy=TypingStrategyDefSiteOperator? name=BindingIdentifier&lt;Yield&gt;?
)
TypeVariables?
InterfaceImplementsList?
Members&lt;Yield&gt;
;
fragment InterfaceImplementsList*:
'extends' superInterfaceRefs+=ParameterizedTypeRefNominal
(',' superInterfaceRefs+=ParameterizedTypeRefNominal)*
;
N4EnumDeclaration &lt;Yield&gt;:
=&gt;(
{N4EnumDeclaration}
(declaredModifiers+=N4Modifier)*
'enum' name=BindingIdentifier&lt;Yield&gt;?
)
'{'
(literals+=N4EnumLiteral (',' literals+=N4EnumLiteral)*)?
'}'
;
N4EnumLiteral: name=IdentifierOrThis (':' value=STRING)?;
enum N4Modifier: // validator applies further checks
private | project | protected | public
| external | abstract | static | const;
N4MemberDeclaration &lt;Yield&gt;:
AnnotatedN4MemberDeclaration&lt;Yield&gt;
| N4GetterDeclaration&lt;Yield&gt;
| N4SetterDeclaration&lt;Yield&gt;
| N4MethodDeclaration&lt;Yield&gt;
| N4FieldDeclaration&lt;Yield&gt;
| N4CallableConstructorDeclaration&lt;Yield&gt;
;
AnnotatedN4MemberDeclaration &lt;Yield&gt; returns N4MemberDeclaration:
N4MemberAnnotationList (
=&gt; ({N4GetterDeclaration.annotationList=current}
(declaredModifiers+=N4Modifier)* GetterHeader&lt;Yield&gt;) (body=Block&lt;Yield&gt;)? ';'?
| =&gt; ({N4SetterDeclaration.annotationList=current}
(declaredModifiers+=N4Modifier)* 'set' -&gt; LiteralOrComputedPropertyName &lt;Yield&gt;)
'(' fpar=FormalParameter&lt;Yield&gt; ')' (body=Block&lt;Yield&gt;)? ';'?
| =&gt; (
{N4MethodDeclaration.annotationList=current} (declaredModifiers+=N4Modifier)* TypeVariables?
(
generator?='*' LiteralOrComputedPropertyName&lt;Yield&gt;
-&gt;MethodParamsReturnAndBody &lt;Generator=true&gt;
| AsyncNoTrailingLineBreak LiteralOrComputedPropertyName&lt;Yield&gt;
-&gt;MethodParamsReturnAndBody &lt;Generator=false&gt;
)
)';'?
| {N4FieldDeclaration.annotationList=current} FieldDeclarationImpl&lt;Yield&gt;
)
;
fragment LiteralOrComputedPropertyName &lt;Yield&gt;*:
name=IdentifierName | name=STRING | name=NumericLiteralAsString
| '[' (=&gt;((name=SymbolLiteralComputedName&lt;Yield&gt; | name=StringLiteralAsName) ']')
| computeNameFrom=AssignmentExpression&lt;In=true,Yield&gt; ']')
;
fragment LiteralPropertyName &lt;Yield&gt;*:
name=IdentifierName | name=STRING | name=NumericLiteralAsString
| '[' (name=SymbolLiteralComputedName&lt;Yield&gt; | name=StringLiteralAsName) ']'
;
StringLiteralAsName:
STRING
;
fragment FieldDeclarationImpl &lt;Yield&gt;*:
(declaredModifiers+=N4Modifier)*
LiteralPropertyName&lt;Yield&gt; ColonSepTypeRef? ('=' expression=Expression&lt;In=true,Yield&gt;)? ';'
;
N4FieldDeclaration &lt;Yield&gt;:
{N4FieldDeclaration}
FieldDeclarationImpl&lt;Yield&gt;
;
N4MethodDeclaration &lt;Yield&gt;:
=&gt; ({N4MethodDeclaration} (declaredModifiers+=N4Modifier)* TypeVariables?
(
generator?='*' LiteralOrComputedPropertyName&lt;Yield&gt;
-&gt;MethodParamsReturnAndBody &lt;Generator=true&gt;
| AsyncNoTrailingLineBreak LiteralOrComputedPropertyName&lt;Yield&gt;
-&gt;MethodParamsReturnAndBody &lt;Generator=false&gt;
)
) ';'?
;
N4CallableConstructorDeclaration &lt;Yield&gt; returns N4MethodDeclaration:
MethodParamsReturnAndBody &lt;Generator=false&gt; ';'?
;
fragment MethodParamsAndBody &lt;Generator&gt;*:
StrictFormalParameters&lt;Yield=Generator&gt;
(body=Block&lt;Yield=Generator&gt;)?
;
fragment MethodParamsReturnAndBody &lt;Generator&gt;*:
StrictFormalParameters&lt;Yield=Generator&gt;
(':' returnTypeRef=TypeRef)?
(body=Block&lt;Yield=Generator&gt;)?
;
N4GetterDeclaration &lt;Yield&gt;:
=&gt; ({N4GetterDeclaration}
(declaredModifiers+=N4Modifier)*
GetterHeader&lt;Yield&gt;)
(body=Block&lt;Yield&gt;)? ';'?
;
fragment GetterHeader &lt;Yield&gt;*:
('get' -&gt; LiteralOrComputedPropertyName &lt;Yield&gt; '(' ')' ColonSepTypeRef?)
;
N4SetterDeclaration &lt;Yield&gt;:
=&gt;({N4SetterDeclaration}
(declaredModifiers+=N4Modifier)*
'set'
-&gt;LiteralOrComputedPropertyName &lt;Yield&gt;
)
'(' fpar=FormalParameter&lt;Yield&gt; ')' (body=Block&lt;Yield&gt;)? ';'?
;
BindingPattern &lt;Yield&gt;:
ObjectBindingPattern&lt;Yield&gt;
| ArrayBindingPattern&lt;Yield&gt;
;
ObjectBindingPattern &lt;Yield&gt;:
'{' (properties+=BindingProperty&lt;Yield,AllowType=false&gt;
(',' properties+=BindingProperty&lt;Yield,AllowType=false&gt;)*)? '}'
;
ArrayBindingPattern &lt;Yield&gt;:
'['
elements+=Elision* (
elements+=BindingRestElement&lt;Yield&gt;
(',' elements+=Elision* elements+=BindingRestElement&lt;Yield&gt;)*
(',' elements+=Elision*)?
)?
']'
;
BindingProperty &lt;Yield, AllowType&gt;:
=&gt;(LiteralBindingPropertyName&lt;Yield&gt; ':') value=BindingElement&lt;Yield&gt;
| value=SingleNameBinding&lt;Yield,AllowType&gt;
;
fragment LiteralBindingPropertyName &lt;Yield&gt;*:
declaredName=IdentifierName | declaredName=STRING | declaredName=NumericLiteralAsString
| '[' (declaredName=SymbolLiteralComputedName&lt;Yield&gt; | declaredName=STRING) ']'
;
SingleNameBinding &lt;Yield, AllowType&gt;:
varDecl=VariableDeclaration&lt;In=true,Yield,AllowType&gt;
;
BindingElement &lt;Yield&gt;:
=&gt;(nestedPattern=BindingPattern&lt;Yield&gt;) ('=' expression=AssignmentExpression&lt;In=true,Yield&gt;)?
| varDecl=VariableDeclaration&lt;In=true,Yield,AllowType=true&gt;
;
BindingRestElement &lt;Yield&gt;:
rest?='...'?
(
=&gt;(nestedPattern=BindingPattern&lt;Yield&gt;)
('=' expression=AssignmentExpression&lt;In=true,Yield&gt;)?
| varDecl=VariableDeclaration&lt;In=true,Yield,AllowType=true&gt;
)
;
Elision:
','
;</programlisting>
</section>
</chapter>
<chapter xml:id="_jsobjects">
<title>JSObjects</title>
<simpara>The built-in ECMAScript Objects
[<link linkend="ECMA11a">ECMA11a(p.S15, p.p.102)</link>] are supported and their properties are annotated with types as described in this chapter. The semantics of these properties do not change. The short description is copied from [<link linkend="ECMA11a">ECMA11a</link>] repeated here for convenience.</simpara>
<section xml:id="_object" role="language-n4js">
<title>Object</title>
<simpara><literal>Object</literal> is the super type of all declared types and <literal>N4Object</literal>. It is almost similar to the JavaScript type <literal>Object</literal> except that no properties may be dynamically added to it. In order to declare a variable to which properties can be dynamically added, the <literal>Object+</literal> type has to be declared (cf.
<xref linkend="_dynamic"/>).</simpara>
<bridgehead xml:id="_attributes" renderas="sect3">Attributes</bridgehead>
<variablelist>
<varlistentry>
<term><literal>constructor:Object</literal></term>
<listitem>
<simpara>Returns a reference to the Object function that created the instance’s
prototype.</simpara>
</listitem>
</varlistentry>
</variablelist>
<bridgehead xml:id="_methods-2" renderas="sect3">Methods</bridgehead>
<variablelist>
<varlistentry>
<term><literal>toString():Object</literal></term>
<listitem>
<simpara>Returns a string representing the specified object.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>toLocaleString():Object</literal></term>
<listitem>
<simpara>Returns a string representing the object. This method is meant to be
overridden by derived objects for locale-specific purposes.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>valueOf():Object</literal></term>
<listitem>
<simpara>Returns the primitive value of the specified object.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>hasOwnProperty(prop:String):Boolean</literal></term>
<listitem>
<simpara>Returns a boolean indicating whether an object contains the specified
property as a direct property of that object and not inherited through
the prototype chain.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>isPrototypeOf(object:Object):Boolean</literal></term>
<listitem>
<simpara>Returns a boolean indication whether the specified object is in the
prototype chain of the object this method is called upon.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>propertyIsEnumerable(prop:String):Boolean</literal></term>
<listitem>
<simpara>Returns a boolean indicating if the internal ECMAScript DontEnum
attribute is set.</simpara>
</listitem>
</varlistentry>
</variablelist>
<bridgehead xml:id="_static-methods" renderas="sect3">Static Methods</bridgehead>
<variablelist>
<varlistentry>
<term><literal>getPrototypeOf(object:Object):Object</literal></term>
<listitem>
<simpara>Returns the prototype of the specified object.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>create(object:Object,properties:Object=):Object</literal></term>
<listitem>
<simpara>Creates a new object with the specified prototype object and properties.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>defineProperty(object:Object,prop:Object,descriptor:Object):Object</literal></term>
<listitem>
<simpara>Defines a new property directly on an object or modifies an existing
property on an object and returns the object.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>defineProperties(object:Object,properties:Object):Object</literal></term>
<listitem>
<simpara>Defines new or modifies existing properties directly on an object,
returning the object.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>seal(object:Object,properties:Object)</literal></term>
<listitem>
<simpara>Seals an object, preventing new properties from being added to it and
marking all existing properties as non-configurable. Values of present
properties can still be changed as long as they are writable.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>freeze(object:Object):Object</literal></term>
<listitem>
<simpara>Freezes an object: that is, prevents new properties from being added to it, prevents existing properties from being removed, prevents existing properties or their enumerability, configurability, or writability from being changed.
In essence, the object is made effectively immutable.
The method returns the object being frozen.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>preventExtensions(object:Object):Object</literal></term>
<listitem>
<simpara>Prevents new properties from ever being added to an object (i.e.
prevents future extensions to the object).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>isSealed(object:Object):Boolean static</literal></term>
<listitem>
<simpara>Determine if an object is sealed.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>isFrozen(object:Object):Boolean</literal></term>
<listitem>
<simpara>Determine if an object is frozen.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>isExtensible(object:Object):Boolean</literal></term>
<listitem>
<simpara>Determines if an object is extensible (whether it can have new
properties added to it).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>keys(object:Object):Array&lt;String&gt;</literal></term>
<listitem>
<simpara>Returns an array of all own enumerable properties found upon a given
object in the same order as that provided by a for-in loop (the
difference being that a for-in loop enumerates properties in the
prototype chain as well).</simpara>
</listitem>
</varlistentry>
</variablelist>
</section>
<section xml:id="_string" role="language-n4js">
<title>String</title>
<simpara>String is a global object that may be used to construct String
instances and is a sub class of Object.</simpara>
<bridgehead xml:id="string-attributes" renderas="sect3">Attributes</bridgehead>
<variablelist>
<varlistentry>
<term><literal>number: length</literal> </term>
<listitem>
<simpara>The length of a string.</simpara>
</listitem>
</varlistentry>
</variablelist>
<bridgehead xml:id="methods-1" renderas="sect3">Methods</bridgehead>
<variablelist>
<varlistentry>
<term><literal>String(thing:Object=)</literal></term>
<listitem>
<simpara>-</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>anchor(anchorname:String):String</literal></term>
<listitem>
<simpara>Creates an HTML anchor.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>big():String</literal></term>
<listitem>
<simpara>Returns a string in a big font.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>blink():String</literal></term>
<listitem>
<simpara>Returns a string in a blinking string.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>bold():String</literal></term>
<listitem>
<simpara>Returns a string in a bold font.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>charAt(index:Number):String</literal></term>
<listitem>
<simpara>Returns the character at a specified position.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>charCodeAt(index:Number):Number</literal></term>
<listitem>
<simpara>Returns the Unicode of the character at a specified position.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>concat(strings:String&#8230;&#8203;):String</literal></term>
<listitem>
<simpara>Joins two or more strings.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>equals(object:Object):Boolean</literal> </term>
<listitem>
<simpara>-</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>equalsIgnoreCase(object:Object):Boolean</literal> </term>
<listitem>
<simpara>-</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>fromCharCode(num:Any&#8230;&#8203;):String</literal></term>
<listitem>
<simpara>Returns a string created by using the specified sequence of Unicode values.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>fixed():String</literal></term>
<listitem>
<simpara>Returns a string as teletype text.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>fontcolor(color):String</literal></term>
<listitem>
<simpara>Returns a string in a specified color.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>fontsize(size):String</literal></term>
<listitem>
<simpara>Returns a string in a specified size.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>indexOf(searchValue, fromIndex:Number=):Number</literal></term>
<listitem>
<simpara>Returns the position of the first occurrence of a specified string value in a string.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>italics():String</literal></term>
<listitem>
<simpara>Returns a string in italic.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>lastIndexOf(searchValue, fromIndex:Number=):Number</literal></term>
<listitem>
<simpara>Returns the position of the last occurrence of a specified string value, searching backwards from the specified position in a string.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>link(url):String</literal></term>
<listitem>
<simpara>Returns a string as a hyperlink.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>localeCompare(otherString):Number</literal></term>
<listitem>
<simpara>This method returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>match(search value):String</literal></term>
<listitem>
<simpara>Searches for a specified value in a string.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>replace(findString,newString):String</literal></term>
<listitem>
<simpara>Replaces some characters with some other characters in a string.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>search(search string):Number</literal></term>
<listitem>
<simpara>Searches a string for a specified value.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>slice(beginSlice:Number, endSclice:Number=):String</literal></term>
<listitem>
<simpara>Extracts a part of a string and returns the extracted part in a new string.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>small():String</literal></term>
<listitem>
<simpara>Returns a string in a small font.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>split(separator, howmany:Number=):Array&lt;String&gt;</literal></term>
<listitem>
<simpara>Splits a string into an array of strings.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>strike():String</literal></term>
<listitem>
<simpara>Returns a string with a strikethrough.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>sub():String</literal></term>
<listitem>
<simpara>Returns a string as subscript.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>substr(start:Number,length:Number=):String</literal></term>
<listitem>
<simpara>Extracts a specified number of characters in a string, from a start index.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>substring(beginIndex:number,endIndex:Number=):String</literal></term>
<listitem>
<simpara>Extracts the characters in a string between two specified indices.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>sup():String</literal></term>
<listitem>
<simpara>Returns a string as superscript.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>toLocaleUpperCase():String</literal></term>
<listitem>
<simpara>Returns a string in lowercase letters.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>toString():String</literal></term>
<listitem>
<simpara>Returns a String value for this object.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>toUpperCase():String</literal></term>
<listitem>
<simpara>Returns a string in uppercase letters.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>valueOf():String</literal></term>
<listitem>
<simpara>Returns the primitive value of a String object.</simpara>
</listitem>
</varlistentry>
</variablelist>
<bridgehead xml:id="static-methods-1" renderas="sect3">Static Methods</bridgehead>
<variablelist>
<varlistentry>
<term><literal>String(value:Object=)</literal></term>
<listitem>
<simpara>Static constructor.</simpara>
</listitem>
</varlistentry>
</variablelist>
</section>
<section xml:id="_boolean" role="language-n4js">
<title>Boolean</title>
<simpara><literal>Boolean</literal> does not have a super class.</simpara>
<bridgehead xml:id="static-methods-2" renderas="sect3">Static Methods</bridgehead>
<simpara><literal>Boolean(value:Object=):Boolean</literal></simpara>
</section>
<section xml:id="_number" role="language-n4js">
<title>Number</title>
<simpara><literal>Number</literal> does not have a super class.</simpara>
<section xml:id="_static-attributes">
<title>Static Attributes</title>
<variablelist>
<varlistentry>
<term><literal>MAX_VALUE:Number</literal></term>
<listitem>
<simpara>The largest representable number.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>MIN_VALUE:Number</literal></term>
<listitem>
<simpara>The smallest representable number.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>NaN:Number</literal></term>
<listitem>
<simpara>Special 'not a number' value.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>NEGATIVE_INFINITY:Number</literal></term>
<listitem>
<simpara>Special value representing negative infinity, returned on overflow.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>POSITIVE_INFINITY:Number</literal></term>
<listitem>
<simpara>Special value representing infinity, returned on overflow.</simpara>
</listitem>
</varlistentry>
</variablelist>
<bridgehead xml:id="methods-2" renderas="sect3">Methods</bridgehead>
<variablelist>
<varlistentry>
<term><literal>toExponential(numberOfDecimals:Number=):String</literal></term>
<listitem>
<simpara>Converts the value of the object into an exponential notation.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>toFixed(numberOfDecimals:Number=):String</literal></term>
<listitem>
<simpara>Formats a number to the specified number of decimals.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>toPrecision(numberOfDecimals:Number=):String</literal></term>
<listitem>
<simpara>Converts a number into an exponential notation if it has more digits than specified.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>valueOf():Number</literal></term>
<listitem>
<simpara>Returns the primitive value of a Number object.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>toString(radix:Number=):String</literal></term>
<listitem>
<simpara>Returns a String value for this object. The toString method parses its first argument and attempts to return a string representation in the specified radix (base).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>toLocaleString(locales: String|[String]=undefined, options: <subscript>r</subscript>NumberFormatOptions=undefined): String</literal></term>
<listitem>
<simpara>Returns a locale-specific String value for this object. The toLocalString accepts two optional arguments. The semantics of these arguments
is defined in <link xl:href="https://www.ecma-international.org/ecma-402/4.0/index.html#sup-number.prototype.tolocalestring">ECMA-402 (Internationalization API Specification)</link>.
In N4JS, the base definition does not define that method, instead Number inherits <literal>toLocaleString</literal> from Object. The specialized
definition is found in the runtime library <literal>n4js-runtime-ecma402</literal>.</simpara>
</listitem>
</varlistentry>
</variablelist>
<bridgehead xml:id="static-methods-3" renderas="sect3">Static Methods</bridgehead>
<variablelist>
<varlistentry>
<term><literal>Number(value:Object=):Number</literal></term>
<listitem>
<simpara>Static constructor.</simpara>
</listitem>
</varlistentry>
</variablelist>
</section>
</section>
<section xml:id="function" role="language-n4js">
<title>Function</title>
<simpara><literal>Function</literal> does not have a super class.</simpara>
<bridgehead xml:id="attributes-2" renderas="sect3">Attributes</bridgehead>
<variablelist>
<varlistentry>
<term><literal>prototype:Object</literal></term>
<listitem>
<simpara>Allows the addition of properties to the instance of the object created by the constructor function.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>length:Number</literal></term>
<listitem>
<simpara>Specifies the number of arguments expected by the functio</simpara>
</listitem>
</varlistentry>
</variablelist>
<bridgehead xml:id="methods-3" renderas="sect3">Methods</bridgehead>
<variablelist>
<varlistentry>
<term><literal>apply(thisArg,argsArray:Array=):Object</literal></term>
<listitem>
<simpara>Applies the method of another object in the context of a different object (the calling object); arguments can be passed as an Array object.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>call(thisArg,arg&#8230;&#8203;):Object</literal></term>
<listitem>
<simpara>Calls (executes) a method of another object in the context of a different object (the calling object); arguments can be passed as they are.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>bind(thisArg:Object,arg&#8230;&#8203;):Function</literal></term>
<listitem>
<simpara>Creates a new function that, when called, itself calls this function in the context of the provided this value with a given sequence of arguments preceding any provided when the new function was called.</simpara>
</listitem>
</varlistentry>
</variablelist>
</section>
<section xml:id="_error" role="language-n4js">
<title>Error</title>
<simpara><literal>Error</literal> does not have a super class.</simpara>
<bridgehead xml:id="attributes-3" renderas="sect3">Attributes</bridgehead>
<variablelist>
<varlistentry>
<term><literal>name:String</literal></term>
<listitem>
<simpara>Error name.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>message:String</literal></term>
<listitem>
<simpara>Error message.</simpara>
</listitem>
</varlistentry>
</variablelist>
<bridgehead xml:id="static-methods-4" renderas="sect3">Static Methods</bridgehead>
<variablelist>
<varlistentry>
<term><literal>Error(message:Object=):Error</literal></term>
<listitem>
<simpara>Static Constructor.</simpara>
</listitem>
</varlistentry>
</variablelist>
</section>
<section xml:id="_array" role="language-n4js">
<title>Array</title>
<simpara><literal>Array</literal> is a generic type with the type parameter <literal>E</literal> and does not have a super class.</simpara>
<bridgehead xml:id="methods-4" renderas="sect3">Methods</bridgehead>
<variablelist>
<varlistentry>
<term><literal>concat(array&#8230;&#8203;):Array&lt;E&gt;)</literal></term>
<listitem>
<simpara>Joins two or more arrays and returns the result.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>every(callback:Function):Boolean</literal></term>
<listitem>
<simpara>Tests whether all elements in the array pass the test implemented by the provided function. The callback will be called with 3 arguments (elementValue,elementIndex,traversedArray).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>filter(callback:Function):Array&lt;E&gt;</literal></term>
<listitem>
<simpara>Creates a new array with all elements that pass the test implemented by the provided function. The callback will be called with 3 arguments (elementValue,elementIndex,traversedArray).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>forEach(callback:Function,thisArg=)</literal></term>
<listitem>
<simpara>Calls a function for each element in the array. The callback will be called with 3 arguments (elementValue,elementIndex,traversedArray). Optionally with a thisObject argument to use as this when executing callback.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>indexOf(searchElement,fromIndex=):Number</literal></term>
<listitem>
<simpara>Returns the first index at which a given element can be found in the array, or -1 if it is not present.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>join(separator=):String</literal></term>
<listitem>
<simpara>Puts all the elements of an array into a string. The elements are separated by a specified delimiter.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>lastIndexOf(searchElement,fromIndex=):Number</literal></term>
<listitem>
<simpara>Returns the last (greatest) index of an element within the array equal to the specified value. Will return -1 if none are found.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>length():Number</literal></term>
<listitem>
<simpara>The length returns an integer representing the length of an array.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>map(callback:Function,thisArg=):Array</literal></term>
<listitem>
<simpara>Creates a new array with the results of calling a provided function on every element in this array. The callback will be called with 3 arguments (elementValue,elementIndex,traversedArray). Optionally, with a thisObject argument to use as this when executing callback.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>pop():E</literal></term>
<listitem>
<simpara>Removes and returns the last element of an array.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>push(element&#8230;&#8203;):E</literal></term>
<listitem>
<simpara>Adds one or more elements to the end of an array and returns the new length.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>reverse():Array&lt;E&gt;</literal></term>
<listitem>
<simpara>Reverses the order of the elements in an array.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>shift()</literal></term>
<listitem>
<simpara>Removes and returns the first element of an array.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>slice(start:Number,end:Number=):Array&lt;E&gt;</literal></term>
<listitem>
<simpara>Returns selected elements from an existing array.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>some(callback:Function,thisArg=):Boolean</literal></term>
<listitem>
<simpara>Tests whether some element in the array passes the test implemented by the provided function. The callback will be called with 3 arguments (elementValue,elementIndex,traversedArray). Optionally, with a thisObject argument to use as this when executing callback.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>sort(sortByFunction:Function=):Array&lt;E&gt;</literal></term>
<listitem>
<simpara>Sorts the elements of an array. The function will be called with 2 arguments (a,b).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>splice(index:Number,how many:Number,element&#8230;&#8203;):Array&lt;E&gt;</literal></term>
<listitem>
<simpara>Removes and adds new elements to an array. Returns the removed elements as an Array.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>toLocaleString():String</literal></term>
<term><literal>toString():String</literal></term>
<listitem>
<simpara>Returns a String value for Array.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>unshift(element&#8230;&#8203;):E</literal></term>
<listitem>
<simpara>Adds one or more elements to the beginning of an array and returns the new length.</simpara>
</listitem>
</varlistentry>
</variablelist>
<bridgehead xml:id="static-methods-5" renderas="sect3">Static Methods</bridgehead>
<variablelist>
<varlistentry>
<term><literal>Array(item:Object&#8230;&#8203;)</literal></term>
<listitem>
<simpara>Static constructor.</simpara>
</listitem>
</varlistentry>
</variablelist>
</section>
<section xml:id="_date" role="language-n4js">
<title>Date</title>
<simpara><literal>Date</literal> does not have a super class.</simpara>
<bridgehead xml:id="static-methods-6" renderas="sect3">Static Methods</bridgehead>
<variablelist>
<varlistentry>
<term><literal>Date():Date</literal></term>
<listitem>
<simpara>Static constructor.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>Date(milliseconds:Number):Date</literal></term>
<listitem>
<simpara>Constructor.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>Date(date:Date):Date</literal></term>
<listitem>
<simpara>Constructor.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>Date(dateString:String):Date</literal></term>
<listitem>
<simpara>Constructor.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>Date(year:Number,month:Number,day=Number=,hour:Number=,minute:Number=,second:Number=,millisecond:Number=):Date</literal></term>
<listitem>
<simpara>Constructor.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>parse(dateString:String):Date</literal></term>
<listitem>
<simpara>Parses a string representation of a date, and returns the number of milliseconds since midnight Jan 1, 1970.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>now():Number</literal></term>
<listitem>
<simpara>Returns the numeric value corresponding to the current time.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>UTC(year:Number,month:Number,date:Number=,hrs:Number=,min:Number=,sec:Number=,ms:Number=):Number</literal></term>
<listitem>
<simpara>UTC takes comma-delimited date parameters and returns the number of milliseconds between January 1, 1970, 00:00:00, Universal Time and the time you specified.</simpara>
</listitem>
</varlistentry>
</variablelist>
<bridgehead xml:id="methods-5" renderas="sect3">Methods</bridgehead>
<variablelist>
<varlistentry>
<term><literal>getDate():Number</literal></term>
<listitem>
<simpara>Returns the day of the month from a Date object (from 1-31).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>getDay():Number</literal></term>
<listitem>
<simpara>Returns the day of the week from a Date object (from 0-6).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>getFullYear():Number</literal></term>
<listitem>
<simpara>Returns the year, as a four-digit number.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>getHours():Number</literal></term>
<listitem>
<simpara>Returns the hour of a day (from 0-23).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>getMilliseconds():Number</literal></term>
<listitem>
<simpara>Returns the milliseconds of a Date object (from 0-999).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>getMinutes():Number</literal></term>
<listitem>
<simpara>Returns the minutes of a date (from 0-59).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>getMonth():Number</literal></term>
<listitem>
<simpara>Returns the month from a date (from 0-11).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>getSeconds():Number</literal></term>
<listitem>
<simpara>Returns the seconds of a date (from 0-59).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>getTime():Number</literal></term>
<listitem>
<simpara>Returns the number of milliseconds since midnight Jan 1, 1970.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>valueOf():Number</literal></term>
<listitem>
<simpara>Returns the primitive value of a Date object as a number data type, the number of milliseconds since midnight 01 January, 1970 UTC. This method is functionally equivalent to the getTime method.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>getTimezoneOffset():Number</literal></term>
<listitem>
<simpara>Returns the difference in minutes between local time and Greenwich Mean Time (GMT).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>getUTCDate():Number</literal></term>
<listitem>
<simpara>Returns the day of the month from a date according to Universal Time (from 1-31).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>getUTCDay():Number</literal></term>
<listitem>
<simpara>Returns the day of the week from a date according to Universal Time (from 0-6).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>getUTCFullYear():Number</literal></term>
<listitem>
<simpara>Returns the four-digit year from a date according to Universal Time.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>getUTCHours():Number</literal></term>
<listitem>
<simpara>Returns the hour of a date according to Universal Time (from 0-23).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>getUTCMilliseconds():Number</literal></term>
<listitem>
<simpara>Returns the milliseconds of a date according to Universal Time (from 0-999).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>getUTCMinutes():Number</literal></term>
<listitem>
<simpara>Returns the minutes of a date according to Universal Time (from 0-59).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>getUTCMonth():Number</literal></term>
<listitem>
<simpara>Returns the month from a Date object according to Universal Time (from 0-11).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>getUTCSeconds():Number</literal></term>
<listitem>
<simpara>Returns the seconds of a date according to Universal Time (from 0-59).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>getYear():Number deprecated</literal></term>
<listitem>
<simpara>Returns the year as a two-digit or a three/four-digit number, depending on the browser. Use getFullYear() instead!</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>setDate(day):Number</literal></term>
<listitem>
<simpara>Sets the day of the month from a Date object (from 1-31).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>setFullYear(full year, month=, day=):Number</literal></term>
<listitem>
<simpara>Sets the year as a four-digit number.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>setHours(hours,minutes=,seconds=,milis=):Number</literal></term>
<listitem>
<simpara>Sets the hour of a day (from 0-23).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>setMilliseconds(mills):Number</literal></term>
<listitem>
<simpara>Sets the milliseconds of a Date object (from 0-999).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>setMinutes(minutes,=seconds,=millis):Number</literal></term>
<listitem>
<simpara>Sets the minutes of a date (from 0-59).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>setMonth" directType="Number(month,day=):Number</literal></term>
<listitem>
<simpara>Sets the month from a date (from 0-11).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>setSeconds(seconds,millis=):number</literal></term>
<listitem>
<simpara>Sets the seconds of a date (from 0-59).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>setTime(mills):Number</literal></term>
<listitem>
<simpara>Sets the number of milliseconds since midnight Jan 1, 1970.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>setUTCDate(day):Number</literal></term>
<listitem>
<simpara>Sets the day of the month from a date according to Universal Time (from 0-6).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>setUTCFullYear(fullyear,month=,day=):Number</literal></term>
<listitem>
<simpara>Sets the four-digit year from a date according to Universal Time.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>setUTCHours(hours,minutes=,seconds=,millis=):Number</literal></term>
<listitem>
<simpara>Sets the hour of a date according to Universal Time (from 0-23).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>setUTCMilliseconds(mills):Number</literal></term>
<listitem>
<simpara>Sets the milliseconds of a date according to Universal Time (from 0-999).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>setUTCMinutes(minutes,seconds=,millis=):Number</literal></term>
<listitem>
<simpara>Sets the minutes of a date according to Universal Time time (from 0-59).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>setUTCMonth(month,day=):Number</literal></term>
<listitem>
<simpara>Sets the month from a Date object according to Universal Time (from 0-11).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>setUTCSeconds(seconds,millis=):Number</literal></term>
<listitem>
<simpara>Sets the seconds of a date according to Universal Time (from 0-59).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>setYear(year):Number deprecated</literal></term>
<listitem>
<simpara>Sets the year, as a two-digit or a three/four-digit number, depending on the browser. Use setFullYear() instead!!</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>toDateString():String</literal></term>
<listitem>
<simpara>Returns the date portion of a Date object in readable form.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>toLocaleDateString(locales: String|[String]=undefined, options: <subscript>r</subscript>DateTimeFormatOptions=undefined): String</literal></term>
<listitem>
<simpara>Converts a Date object, using locales and options as defined in DateTimeFormat of ECMA-402 (Internationalization API), to a string and returns the date and time portion.
The toLocalString accepts two optional arguments. The semantics of these arguments is defined in
<link xl:href="https://www.ecma-international.org/ecma-402/4.0/index.html#sup-date.prototype.tolocaledatestring">ECMA-402 (Internationalization API Specification)</link>.
The specialized definition is found in the runtime library <literal>n4js-runtime-ecma402</literal>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>toLocaleString(locales: String|[String]=undefined, options: <subscript>r</subscript>DateTimeFormatOptions=undefined): String</literal></term>
<listitem>
<simpara>Converts a Date object, using locales and options as defined in DateTimeFormat of ECMA-402 (Internationalization API), to a string.
The toLocalString accepts two optional arguments. The semantics of these arguments is defined in
<link xl:href="https://www.ecma-international.org/ecma-402/4.0/index.html#sup-date.prototype.tolocalestring">ECMA-402 (Internationalization API Specification)</link>.
In N4JS, the base definition does not define that method, instead Date inherits <literal>toLocaleString</literal> from Object. The specialized
definition is found in the runtime library <literal>n4js-runtime-ecma402</literal>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>toLocaleTimeString(locales: String|[String]=undefined, options: <subscript>r</subscript>DateTimeFormatOptions=undefined): String</literal></term>
<listitem>
<simpara>Converts a Date object, using locales and options as defined in DateTimeFormat of ECMA-402 (Internationalization API), to a string and returns the time portion.
The semantics of these arguments is defined in
<link xl:href="https://www.ecma-international.org/ecma-402/4.0/index.html#sup-date.prototype.tolocaletimestring">ECMA-402 (Internationalization API Specification)</link>.
The specialized definition is found in the runtime library <literal>n4js-runtime-ecma402</literal>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>toString():String</literal></term>
<listitem>
<simpara>Returns a String value for this object.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>toTimeString():String</literal></term>
<listitem>
<simpara>Returns the time portion of a Date object in readable form.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>toUTCString():String</literal></term>
<listitem>
<simpara>Converts a Date object, according to Universal Time, to a string.</simpara>
</listitem>
</varlistentry>
</variablelist>
</section>
<section xml:id="_math" role="language-n4js">
<title>Math</title>
<simpara><literal>Math</literal> is not instantiable and only provides static properties and methods.</simpara>
<section xml:id="static-attributes-1">
<title>Static Attributes</title>
<variablelist>
<varlistentry>
<term><literal>E:Number</literal></term>
<listitem>
<simpara>Euler&#8217;s constant and the base of natural logarithms, approximately 2.718.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>LN2:Number</literal></term>
<listitem>
<simpara>Natural logarithm of 2, approximately 0.693.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>LN10:Number</literal></term>
<listitem>
<simpara>Natural logarithm of 10, approximately 2.302.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>LOG2E:Number</literal></term>
<listitem>
<simpara>Base 2 logarithm of E, approximately 1.442.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>LOG10E:Number</literal></term>
<listitem>
<simpara>Base 10 logarithm of E, approximately 0.434.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>PI:Number</literal></term>
<listitem>
<simpara>Ratio of the circumference of a circle to its diameter, approximately 3.14159.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>SQRT1_2:Number</literal></term>
<listitem>
<simpara>Square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>SQRT2:Number</literal></term>
<listitem>
<simpara>Square root of 2, approximately 1.414.</simpara>
</listitem>
</varlistentry>
</variablelist>
<bridgehead xml:id="static-methods-7" renderas="sect3">Static Methods</bridgehead>
<variablelist>
<varlistentry>
<term><literal>abs(x):Number</literal></term>
<listitem>
<simpara>Returns the absolute value of a number.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>acos(x:Number):Number</literal></term>
<listitem>
<simpara>Returns the arccosine of a number.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>asinx:Number):Number</literal></term>
<listitem>
<simpara>Returns the arcsine of a number.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>atan(x:Number):Number</literal></term>
<listitem>
<simpara>Returns the arctangent of a number.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>atan2(y:Number,x:Number):Number</literal></term>
<listitem>
<simpara>Returns the arctangent of the quotient of its arguments.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>ceil(x):Number</literal></term>
<listitem>
<simpara>Returns the smallest integer greater than or equal to a number.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cos(x):Number</literal></term>
<listitem>
<simpara>Returns the arctangent of the quotient of its arguments.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>exp(x):Number</literal></term>
<listitem>
<simpara>Returns Enumber, where number is the argument, and E is Euler&#8217;s constant (2.718&#8230;&#8203;), the base of the natural logarithm.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>floor(x):Number</literal></term>
<listitem>
<simpara>Returns the largest integer less than or equal to a number.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>log(x):Number</literal></term>
<listitem>
<simpara>Returns the natural logarithm (loge, also ln) of a number.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>max(value&#8230;&#8203;):Number</literal></term>
<listitem>
<simpara>Returns the largest of zero or more numbers.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>min(value&#8230;&#8203;):Number</literal></term>
<listitem>
<simpara>Returns the smallest of zero or more numbers.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>pow(base:Number,exponent:Number):Number</literal></term>
<listitem>
<simpara>Returns base to the exponent power, that is, baseexponent.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>random():Number</literal></term>
<listitem>
<simpara>Returns a pseudorandom number between 0 and 1.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>round(x:Number):Number</literal></term>
<listitem>
<simpara>Returns the value of a number rounded to the nearest integer.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>sin(x:Number):Number</literal></term>
<listitem>
<simpara>Returns the sine of a number.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>sqrt(x:Number):Number</literal></term>
<listitem>
<simpara>Returns the positive square root of a number.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>tan(x:Number):Number</literal></term>
<listitem>
<simpara>Returns the tangent of a number.</simpara>
</listitem>
</varlistentry>
</variablelist>
</section>
</section>
<section xml:id="_regexp" role="language-n4js">
<title>RegExp</title>
<simpara><literal>RegExp</literal> does not have a super class.</simpara>
<bridgehead xml:id="attributes-4" renderas="sect3">Attributes</bridgehead>
<variablelist>
<varlistentry>
<term><literal>global:Boolean</literal></term>
<listitem>
<simpara>Whether to test the regular expression against all possible matches in a string, or only against the first.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>ignoreCase:Boolean</literal></term>
<listitem>
<simpara>Whether to ignore case while attempting a match in a string.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>lastIndex:Number</literal></term>
<listitem>
<simpara>The index at which to start the next match.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>multiline:Boolean</literal></term>
<listitem>
<simpara>Whether or not to search in strings across multiple lines.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>source:String</literal></term>
<listitem>
<simpara>The text of the pattern.</simpara>
</listitem>
</varlistentry>
</variablelist>
<bridgehead xml:id="methods-6" renderas="sect3">Methods</bridgehead>
<variablelist>
<varlistentry>
<term><literal>exec(str:String):Array</literal></term>
<listitem>
<simpara>Executes a search for a match in its string parameter.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>test(str:String):Boolean</literal></term>
<listitem>
<simpara>Tests for a match in its string parameter.</simpara>
</listitem>
</varlistentry>
</variablelist>
</section>
<section xml:id="_json" role="language-n4js">
<title>JSON</title>
<simpara><literal>JSON</literal> is a global object and a subclass of <literal>Object</literal>. Its functionality is provided by two static methods.
It is not possible to create new instances of type JSON.</simpara>
<bridgehead xml:id="attributes-5" renderas="sect3">Attributes</bridgehead>
<simpara>The JSON object does not define own properties.</simpara>
<bridgehead xml:id="methods-7" renderas="sect3">Methods</bridgehead>
<simpara>The JSON object does not define own methods.</simpara>
<bridgehead xml:id="static-methods-8" renderas="sect3">Static Methods</bridgehead>
<simpara>The parse function parses a JSON text (a JSON-formatted String) and
produces an ECMAScript value. The JSON format is a restricted form of
ECMAScript literal. JSON objects are realized as ECMAScript objects.
JSON arrays are realized as ECMAScript arrays. JSON strings, numbers,
booleans, and null are realized as ECMAScript Strings, Numbers,
Booleans, and null. For detailed information see [<link linkend="ECMA11a">ECMA11a(p.S15.12.2)</link>]</simpara>
<simpara>The optional reviver parameter is a function that takes two parameters
(key and value). It can filter and transform the results. It is called
with each of the key/value pairs produced by the parse and its return
value is used instead of the original value. If it returns what it
received, the structure is not modified. If it returns then the property
is deleted from the result.</simpara>
<simpara>The stringify function returns a String in JSON format representing an
ECMAScript value. It can take three parameters. The first parameter is
required. The value parameter is an ECMAScript value which is usually an
object or array, although it can also be a String, Boolean, Number or
null.</simpara>
<simpara>The optional replacer parameter is either a function that alters the way
objects and arrays are stringified or an array of Strings and Numbers
that act as a white list for selecting the object properties that will
be stringified.</simpara>
<simpara>The optional space parameter is a String or Number that allows the
result to have whitespace injected into it to improve human readability.</simpara>
<simpara>For detailed information see [<link linkend="ECMA11a">ECMA11a(p.S15.12.3)</link>].</simpara>
</section>
</chapter>
<chapter xml:id="_n4js-objects">
<title>N4JS Objects</title>
<section xml:id="_reflection-model">
<title>Reflection Model</title>
<simpara>N4JS provided metadata for reflection (and introspection). This
information is available via instances of some reflection model classes
(described below) attached to types and (in some cases) functions.</simpara>
<simpara>The following class diagrams shows the defined classes. Note that for
performance reasons, the actual structure may vary from the model.</simpara>
<figure xml:id="fig-n4js-reflection-classes">
<title>N4JS Reflection Classes</title>
<mediaobject>
<imageobject>
<imagedata fileref="chapters/a03_n4jsobjects/fig/cd_reflectionModel.svg"/>
</imageobject>
<textobject><phrase>cd reflectionModel</phrase></textobject>
</mediaobject>
</figure>
<simpara>Remark: This section is work in progress. The final goal is to provide a
metamodel similar to Ecore, but we will only add new features if needed.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">/**
* Base class for all N4 reflective elements.
*/
export public abstract class N4Element extends Object {
/**
* Annotations attached to this element.
*/
public Array&lt;N4Annotation&gt; annotations = [];
/**
* The origin string formed as &lt;ID-VERSION&gt;, where ID is containing project artefact id, and VERSION is its version
*/
public String origin;
/**
* Returns true if an annotation with the given name is attached to the element.
*/
public boolean hasAnnotation(string name) {
return !!this.anyAnnotation(name)
}
/**
* Returns any annotation with the given name attached to the element, or null if no such annotation is found.
*/
public N4Annotation anyAnnotation(string name) {
for (var i=this.annotations.length-1; i&gt;=0; i--) {
var a = this.annotations[i];
if (a) {
return a;
}
}
return null;
}
/**
* Returns all annotations with the given name attached to the element, or an empty array if no such annotations are found.
*/
public Array&lt;N4Annotation&gt; allAnnotations(string name) {
return this.annotations.filter(function(a){return a.name==name});
}
}
/**
* Base class for all reflective classes with a name.
*/
export public abstract class N4NamedElement extends N4Element {
/**
* The simple name of a named element.
*/
public string name;
}
/**
* Base class for all reflective classes describing a type (declaration).
*/
export public abstract class N4Type extends N4NamedElement {
/**
* The FQN of the type.
*/
public string fqn;
/**
* Returns true if this N4Class describes an N4-class declaration.
*/
public boolean get isClass() { return false; }
/**
* Returns true if this N4Class describes an N4-interface declaration.
*/
public boolean get isInterface() { return false; }
/**
* Returns true if this N4Class describes an N4-enumeration declaration.
*/
public boolean get isEnum() { return false; }
}
/**
* Base class for meta types of classes or interfaces.
*/
export public abstract class N4Classifier extends N4Type {
/**
* The N4Class of the super type, may be null if super type is a not an N4Class.
*/
public N4Class n4superType;
/**
* Array of the FQN of all (transitively) implemented interfaces, i.e. interfaces directly implemented by this class, its super
* class or interfaces extended by directly implemented interfaces.
*/
public Array&lt;string&gt; allImplementedInterfaces;
/**
* Array of all owned members, that is members defined in the class directly.
* This field is private as it is an internal detail, members are accessed via methods defined in this class.
*/
private Array&lt;N4Member&gt; ownedMembers;
/**
* Array of all consumed members, that is members mixed into the classifier via interface implementation and default methods.
* This field is private as it is an internal detail, members are accessed via methods defined in this class.
*/
private Array&lt;N4Member&gt; consumedMembers;
/**
* Only used internally, must not be called by client.
*/
protected constructor(@Spec ~~this spec) {}
/**
* Returns all members defined by this class directly, consumed, and inherited. The boolean flags control which members are returned.
*
* @param consumed if set, consumed members are returned as well (false by default)
* @param inherited if set, inherited members are returned as well (false by default)
* @param _static if set, static members are returned, otherwise instance members (false by default).
* @return array of members, may be empty but never null
*/
public Array&lt;? extends N4Member&gt; members(boolean? consumed, boolean? inherited, boolean? _static) {
return null; // TODO
}
/**
* Returns all members defined in this classifier (or inherited) with an annotation
* of given name attached to it. The boolean flags control which methods are returned.
*
* @param name name of annotation to be used as filter
* @param consumed if set, consumed members are returned as well (false by default)
* @param inherited if set, inherited members are returned as well (false by default)
* @param _static if set, static members are returned, otherwise instance members (false by default).
* @return array of members, may be empty but never null
*/
public Array&lt;? extends N4Member&gt; membersWithAnnotation(string name, boolean? consumed, boolean? inherited, boolean? _static) {
return null; // TODO
}
/**
* Returns all data fields defined by this class directly, consumed, and inherited. The boolean flags control which data fields are returned.
*
* @param consumed if set, consumed data fields are returned as well (false by default)
* @param inherited if set, inherited data fields are returned as well (false by default)
* @param _static if set, static data fields are returned, otherwise instance members (false by default).
* @return array of data fields, may be empty but never null
*/
public Array&lt;? extends N4DataField&gt; dataFields(boolean? consumed, boolean? inherited, boolean? _static) {
return null; // TODO
}
/**
* Returns all data fields defined in this classifier (or inherited) with an annotation
* of given name attached to it. The boolean flags control which data fields are returned.
*
* @param name name of annotation to be used as filter
* @param consumed if set, consumed data fields are returned as well (false by default)
* @param inherited if set, inherited data fields are returned as well (false by default)
* @param _static if set, static data fields are returned, otherwise instance members (false by default).
* @return array of data fields, may be empty but never null
*/
public Array&lt;? extends N4DataField&gt; dataFieldsWithAnnotation(string name, boolean? consumed, boolean? inherited, boolean? _static) {
return null; // TODO
}
/**
* Returns all methods defined by this class directly, consumed, and inherited. The boolean flags control which methods are returned.
*
* @param consumed if set, consumed methods are returned as well (false by default)
* @param inherited if set, inherited methods are returned as well (false by default)
* @param _static if set, static methods are returned, otherwise instance members (false by default).
* @return array of methods, may be empty but never null
*/
public Array&lt;? extends N4Method&gt; methods(boolean? consumed, boolean? inherited, boolean? _static) {
return null; // TODO
}
/**
* Returns all methods defined in this classifier (or inherited) with an annotation
* of given name attached to it. The boolean flags control which methods are returned.
*
* @param name name of annotation to be used as filter
* @param consumed if set, consumed methods are returned as well (false by default)
* @param inherited if set, inherited methods are returned as well (false by default)
* @param _static if set, static methods are returned, otherwise instance members (false by default).
* @return array of methods, may be empty but never null
*/
public Array&lt;? extends N4Method&gt; methodsWithAnnotation(string name, boolean? consumed, boolean? inherited, boolean? _static) {
return null; // TODO
}
}
/**
* Meta information of an n4 class.
*/
export @Final public class N4Class extends N4Classifier {
/**
* Returns the N4Class instance for a given n4object. This is similar to
* {@code n4object.constructor.n4type}, however it can also be used in interfaces
* to get reflective information of the implementor.
*/
public static N4Class of(N4Object n4object) {
return n4object.constructor.n4type
}
/**
* Returns true if this N4Class describes an N4-class declaration.
*/
@Override
public boolean get isClass() { return true; }
}
/**
* Meta information of an n4 interface.
*/
export @Final public class N4Interface extends N4Classifier {
/**
* Returns true if this N4Class describes an N4-interface declaration.
*/
@Override
public boolean get isInterface() { return true; }
}
/**
* Description of a member, that is a method or field.
*/
export public abstract class N4Member extends N4Element {
public string name;
}
/**
* Description of a method.
*/
export @Final public class N4Method extends N4Member {
public Function jsFunction;
}
/**
* Description of a simple data field.
*/
export @Final public class N4DataField extends N4Member {
}
/**
* Description of an accessor, that is a getter or setter.
*/
export @Final public class N4Accessor extends N4Member {
/**
* Flag indicating whether accessor is a getter or setter, internal detail.
*/
private boolean getter;
/**
* Returns true if accessor is a getter.
*/
public boolean isGetter() { return this.getter; }
/**
* Returns true if accessor is a setter.
*/
public boolean isSetter() { return ! this.getter; }
}
/**
* Description of an N4Enum
*/
export @Final public class N4EnumType extends N4Type {
/**
* Returns true if this N4Clasifier describes an N4-enumeration declaration.
*/
@Override public boolean get isEnum() { return true; }
/**
* Returns the N4EnumType instance for a given enum literal. This is similar to
* {@code n4enum.constructor.n4type}.
*/
public static N4EnumType of(N4Enum n4enum) {
return n4enum.constructor.n4type
}
}
/**
* Base class for all enumeration, literals are assumed to be static constant fields of concrete subclasses.
*/
export public abstract class N4Enum extends Object {
/**
* Returns the name of a concrete literal
*/
public abstract string get name();
/**
* Returns the value of a concrete literal. If no value is
* explicitly set, it is similar to the name.
*/
public abstract string get value()
/**
* Returns a string representation of a concrete literal, it returns
* the same result as value()
*/
@Override public string toString() { return this.value }
/**
* Returns the enum class object of this enum literal for reflection.
* The very same meta class object can be retrieved from the enumeration type directly.
*/
public abstract N4Enum get n4Enum()
/**
* Natively overridden by concrete enums.
*/
public static Array&lt;? extends N4Enum&gt; get values() { return null; }
/**
* Natively overridden by concrete enums.
*/
public static N4Enum valueByName(string name) { return null; }
/**
* Returns the meta class object of this class for reflection.
* The very same meta class object can be retrieved from an instance by calling
* &lt;code&gt;instance.constructor.n4type&lt;/code&gt;
*/
public static N4EnumType get n4type() { return null; }
}
/**
* Annotation with value.
*/
export @Final public class N4Annotation extends Object {
public string name;
public union{string,number} value;
}
/**
* The base class for all instances of n4 classes.
*/
export public class N4Object {
/**
* Returns the meta class object of this class for reflection.
* The very same meta class object can be retrieved from an instance by calling
* &lt;code&gt;instance.constructor.n4type&lt;/code&gt;
*/
// defined in types model, added by $makeClass:
// public static N4Class get n4type() { return null; }
}</programlisting>
</section>
<section xml:id="_error-types">
<title>Error Types</title>
<simpara>N4JS provides additional Error types as subtypes of <literal>Error</literal>.</simpara>
<section xml:id="_n4apinotimplemented" role="language-n4js">
<title>N4ApiNotImplemented</title>
<simpara>Considering API definitions and concrete implementations of those APIs
the error <literal>N4-Api-Not-Implemented-Error</literal> is introduced to specifically report missing implementations.
Instances of this error type are inserted internally during the
transpilation of API-implementing projects. Whenever a difference to the
API in form of a missing implementation is encountered, the transpiler
will insert stub-code throwing an instance of <literal>N4-Api-Not-Implemented-Error</literal>.</simpara>
<simpara>API-testing projects can catch those errors and act accordingly. This
enables tracking of completeness of implementations by counting the
occasions an <literal>N4-Api-NotImplemented-Error</literal> was encountered.</simpara>
<programlisting language="n4js" linenumbering="unnumbered">/**
* Error type reporting a not implemented situation.
*/
public class N4ApiNotImplementedError extends Error { }</programlisting>
</section>
</section>
</chapter>
<appendix xml:id="_acronyms">
<title>Acronyms</title>
<informaltable xml:id="AC" role="language-bash" frame="all" rowsep="1" colsep="1">
<tgroup cols="4">
<colspec colname="col_1" colwidth="8.3333*"/>
<colspec colname="col_2" colwidth="41.6666*"/>
<colspec colname="col_3" colwidth="8.3333*"/>
<colspec colname="col_4" colwidth="41.6668*"/>
<tbody>
<row>
<entry align="center" valign="top"><simpara><literal>CDep</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Compile-Time Dependency</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><literal>RDep</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Run-Time Dependency</emphasis></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><literal>LDep</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Load-Time Dependency</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><literal>IDep</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Initialization-Time Dependency</emphasis></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><literal>EDep</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Execution-Time Dependency</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><literal>ANTLR</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">ANother Tool for Language Recognition</emphasis></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><literal>API</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Application Programming Interface</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><literal>AST</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Abstract Syntax Tree</emphasis></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><literal>ASI</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Automatic Semicolon Insertion</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><literal>BNF</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Backus-Naur Form</emphasis></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><literal>CLI</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Command Line Interface (including a headless compiler and runner.)</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><literal>DI</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Dependency Injection</emphasis></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><literal>DIC</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">DI Component</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><literal>DOM</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Document Object Model</emphasis></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><literal>DSL</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Domain Specific Language</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><literal>EBNF</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Extended Backus-Naur Form</emphasis></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><literal>EMF</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Eclipse Modeling Framework <anchor xml:id="EMF" xreflabel="[EMF]"/></emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><literal>FQN</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Fully Qualified Name</emphasis></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><literal>GLB</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Greatest Lower Bound, also known as <emphasis>infimum</emphasis></emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><literal>GCST</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Greatest Common Sub Type, also known as <emphasis>meet</emphasis></emphasis></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><literal>IDE</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Integrated Development Environment</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><literal>IDL</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Interface Definition Language</emphasis></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><literal>LSP</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Liskov Substitution Principle [<link linkend="Martin96b">Martin96b</link>]</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><literal>LUB</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Least Upper Bound, also known as <emphasis>supremum</emphasis></emphasis></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><literal>LCST</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Least Common Super Type, also known as <emphasis>join</emphasis></emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><literal>N4JS</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">N4JS for JavaScript</emphasis></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><literal>N4JSED</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">N4JS Environment Definition</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><literal>N4JSIDE</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">N4JS Integrated Development Environment (Eclipse-based IDE for all N4JS related languages and projects)</emphasis></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><literal>VM</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Virtual Machine</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><literal>XML</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Extensible Markup Language</emphasis></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><literal>XSLT / XSL</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">XSL Transformations</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><literal>XSL</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Extensible Stylesheet Language</emphasis></simpara></entry>
</row>
<row>
<entry align="center" valign="top"><simpara><literal>WYSIWYG</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">What You See Is What You Get</emphasis></simpara></entry>
<entry align="center" valign="top"><simpara><literal>WLOG</literal></simpara></entry>
<entry align="left" valign="top"><simpara><emphasis role="strong">Without loss of generality</emphasis></simpara></entry>
</row>
</tbody>
</tgroup>
</informaltable>
</appendix>
<appendix xml:id="sec:License">
<title>License</title>
<simpara>This specification and the accompanying materials is made available
under the terms of the Eclipse Public License v1.0 which accompanies
this distribution, and is available at <link xl:href="http://www.eclipse.org/legal/epl-v10.html">http://www.eclipse.org/legal/epl-v10.html</link></simpara>
<bridgehead xml:id="_eclipse-public-license-v-1-0" renderas="sect2">Eclipse Public License - v 1.0</bridgehead>
<simpara>THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE
PUBLIC LICENSE (<literal>AGREEMENT</literal>). ANY USE, REPRODUCTION OR DISTRIBUTION OF
THE PROGRAM CONSTITUTES RECIPIENT’S ACCEPTANCE OF THIS AGREEMENT.</simpara>
<bridgehead xml:id="_1-definitions" renderas="sect3">1. DEFINITIONS</bridgehead>
<variablelist>
<varlistentry>
<term><literal>Contribution</literal> means: </term>
<listitem>
<orderedlist numeration="arabic">
<listitem>
<simpara>in the case of the initial Contributor, the initial code and
documentation distributed under this Agreement, and</simpara>
</listitem>
<listitem>
<simpara>in the case of each subsequent Contributor:</simpara>
<orderedlist numeration="loweralpha">
<listitem>
<simpara>changes to the Program, and</simpara>
</listitem>
<listitem>
<simpara>additions to the Program;</simpara>
<simpara>where such changes and/or additions to the Program originate from and
are distributed by that particular Contributor. A Contribution
’originates’ from a Contributor if it was added to the Program by such
Contributor itself or anyone acting on such Contributor’s behalf.
Contributions do not include additions to the Program which:</simpara>
<orderedlist numeration="lowerroman">
<listitem>
<simpara>are separate modules of software distributed in conjunction with the Program
under their own license agreement, and</simpara>
</listitem>
<listitem>
<simpara>are not derivative works of the Program.</simpara>
</listitem>
</orderedlist>
</listitem>
</orderedlist>
</listitem>
</orderedlist>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>Contributor</literal></term>
<listitem>
<simpara>means any person or entity that distributes the Program.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>Licensed Patents</literal> </term>
<listitem>
<simpara>mean patent claims licensable by a Contributor
which are necessarily infringed by the use or sale of its Contribution
alone or when combined with the Program.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>Program</literal> </term>
<listitem>
<simpara>means the Contributions distributed in accordance with this
Agreement.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>Recipient</literal> </term>
<listitem>
<simpara>means anyone who receives the Program under this
Agreement, including all Contributors.</simpara>
</listitem>
</varlistentry>
</variablelist>
<bridgehead xml:id="_2-grant-of-rights" renderas="sect3">2. GRANT OF RIGHTS</bridgehead>
<orderedlist numeration="arabic">
<listitem>
<simpara>Subject to the terms of this Agreement, each Contributor hereby
grants Recipient a non-exclusive, worldwide, royalty-free copyright
license to reproduce, prepare derivative works of, publicly display,
publicly perform, distribute and sublicense the Contribution of such
Contributor, if any, and such derivative works, in source code and
object code form.</simpara>
</listitem>
<listitem>
<simpara>Subject to the terms of this Agreement, each Contributor hereby
grants Recipient a non-exclusive, worldwide, royalty-free patent license
under Licensed Patents to make, use, sell, offer to sell, import and
otherwise transfer the Contribution of such Contributor, if any, in
source code and object code form. This patent license shall apply to the
combination of the Contribution and the Program if, at the time the
Contribution is added by the Contributor, such addition of the
Contribution causes such combination to be covered by the Licensed
Patents. The patent license shall not apply to any other combinations
which include the Contribution. No hardware per se is licensed
hereunder.</simpara>
</listitem>
<listitem>
<simpara>Recipient understands that although each Contributor grants the
licenses to its Contributions set forth herein, no assurances are
provided by any Contributor that the Program does not infringe the
patent or other intellectual property rights of any other entity. Each
Contributor disclaims any liability to Recipient for claims brought by
any other entity based on infringement of intellectual property rights
or otherwise. As a condition to exercising the rights and licenses
granted hereunder, each Recipient hereby assumes sole responsibility to
secure any other intellectual property rights needed, if any. For
example, if a third party patent license is required to allow Recipient
to distribute the Program, it is Recipient’s responsibility to acquire
that license before distributing the Program.</simpara>
</listitem>
<listitem>
<simpara>Each Contributor represents that to its knowledge it has sufficient
copyright rights in its Contribution, if any, to grant the copyright
license set forth in this Agreement.</simpara>
</listitem>
</orderedlist>
<bridgehead xml:id="_3-requirements" renderas="sect3">3. REQUIREMENTS</bridgehead>
<simpara>A Contributor may choose to distribute the Program in object code form
under its own license agreement, provided that:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>it complies with the terms and conditions of this Agreement; and</simpara>
</listitem>
<listitem>
<simpara>its license agreement:</simpara>
<orderedlist numeration="loweralpha">
<listitem>
<simpara>effectively disclaims on behalf of all Contributors all warranties
and conditions, express and implied, including warranties or conditions
of title and non-infringement, and implied warranties or conditions of
merchantability and fitness for a particular purpose;</simpara>
</listitem>
<listitem>
<simpara>effectively excludes on behalf of all Contributors all liability for
damages, including direct, indirect, special, incidental and
consequential damages, such as lost profits;</simpara>
</listitem>
<listitem>
<simpara>states that any provisions which differ from this Agreement are
offered by that Contributor alone and not by any other party; and</simpara>
</listitem>
<listitem>
<simpara>states that source code for the Program is available from such
Contributor, and informs licensees how to obtain it in a reasonable
manner on or through a medium customarily used for software exchange.</simpara>
</listitem>
</orderedlist>
</listitem>
</orderedlist>
<simpara>When the Program is made available in source code form:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>it must be made available under this Agreement; and</simpara>
</listitem>
<listitem>
<simpara>a copy of this Agreement must be included with each copy of the
Program.</simpara>
</listitem>
</orderedlist>
<simpara>Contributors may not remove or alter any copyright notices contained
within the Program.</simpara>
<simpara>Each Contributor must identify itself as the originator of its
Contribution, if any, in a manner that reasonably allows subsequent
Recipients to identify the originator of the Contribution.</simpara>
<bridgehead xml:id="_4-commercial-distribution" renderas="sect3">4. COMMERCIAL DISTRIBUTION</bridgehead>
<simpara>Commercial distributors of software may accept certain responsibilities
with respect to end users, business partners and the like. While this
license is intended to facilitate the commercial use of the Program, the
Contributor who includes the Program in a commercial product offering
should do so in a manner which does not create potential liability for
other Contributors. Therefore, if a Contributor includes the Program in
a commercial product offering, such Contributor (<literal>Commercial
Contributor</literal>) hereby agrees to defend and indemnify every other
Contributor (<literal>Indemnified Contributor</literal>) against any losses, damages
and costs (collectively <literal>Losses</literal>) arising from claims, lawsuits and
other legal actions brought by a third party against the Indemnified
Contributor to the extent caused by the acts or omissions of such
Commercial Contributor in connection with its distribution of the
Program in a commercial product offering. The obligations in this
section do not apply to any claims or Losses relating to any actual or
alleged intellectual property infringement. In order to qualify, an
Indemnified Contributor must: a) promptly notify the Commercial
Contributor in writing of such claim, and b) allow the Commercial
Contributor to control, and cooperate with the Commercial Contributor
in, the defense and any related settlement negotiations. The Indemnified
Contributor may participate in any such claim at its own expense.</simpara>
<simpara>For example, a Contributor might include the Program in a commercial
product offering, Product X. That Contributor is then a Commercial
Contributor. If that Commercial Contributor then makes performance
claims, or offers warranties related to Product X, those performance
claims and warranties are such Commercial Contributor’s responsibility
alone. Under this section, the Commercial Contributor would have to
defend claims against the other Contributors related to those
performance claims and warranties, and if a court requires any other
Contributor to pay any damages as a result, the Commercial Contributor
must pay those damages.</simpara>
<bridgehead xml:id="_5-no-warranty" renderas="sect3">5. NO WARRANTY</bridgehead>
<simpara>EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED
ON AN <literal>AS IS</literal> BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES
OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR
A PARTICULAR PURPOSE. Each Recipient is solely responsible for
determining the appropriateness of using and distributing the Program
and assumes all risks associated with its exercise of rights under this
Agreement , including but not limited to the risks and costs of program
errors, compliance with applicable laws, damage to or loss of data,
programs or equipment, and unavailability or interruption of operations.</simpara>
<bridgehead xml:id="_6-disclaimer-of-liability" renderas="sect3">6. DISCLAIMER OF LIABILITY</bridgehead>
<simpara>EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR
ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING
WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR
DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.</simpara>
<bridgehead xml:id="_7-general" renderas="sect3">7. GENERAL</bridgehead>
<simpara>If any provision of this Agreement is invalid or unenforceable under
applicable law, it shall not affect the validity or enforceability of
the remainder of the terms of this Agreement, and without further action
by the parties hereto, such provision shall be reformed to the minimum
extent necessary to make such provision valid and enforceable.</simpara>
<simpara>If Recipient institutes patent litigation against any entity (including
a cross-claim or counterclaim in a lawsuit) alleging that the Program
itself (excluding combinations of the Program with other software or
hardware) infringes such Recipient’s patent(s), then such Recipient’s
rights granted under Section 2(b) shall terminate as of the date such
litigation is filed.</simpara>
<simpara>All Recipient’s rights under this Agreement shall terminate if it fails
to comply with any of the material terms or conditions of this Agreement
and does not cure such failure in a reasonable period of time after
becoming aware of such noncompliance. If all Recipient’s rights under
this Agreement terminate, Recipient agrees to cease use and distribution
of the Program as soon as reasonably practicable. However, Recipient’s
obligations under this Agreement and any licenses granted by Recipient
relating to the Program shall continue and survive.</simpara>
<simpara>Everyone is permitted to copy and distribute copies of this Agreement,
but in order to avoid inconsistency the Agreement is copyrighted and may
only be modified in the following manner. The Agreement Steward reserves
the right to publish new versions (including revisions) of this
Agreement from time to time. No one other than the Agreement Steward has
the right to modify this Agreement. The Eclipse Foundation is the
initial Agreement Steward. The Eclipse Foundation may assign the
responsibility to serve as the Agreement Steward to a suitable separate
entity. Each new version of the Agreement will be given a distinguishing
version number. The Program (including Contributions) may always be
distributed subject to the version of the Agreement under which it was
received. In addition, after a new version of the Agreement is
published, Contributor may elect to distribute the Program (including
its Contributions) under the new version. Except as expressly stated in
Sections 2(a) and 2(b) above, Recipient receives no rights or licenses
to the intellectual property of any Contributor under this Agreement,
whether expressly, by implication, estoppel or otherwise. All rights in
the Program not expressly granted under this Agreement are reserved.</simpara>
<simpara>This Agreement is governed by the laws of the State of New York and the
intellectual property laws of the United States of America. No party to
this Agreement will bring a legal action under this Agreement more than
one year after the cause of action arose. Each party waives its rights
to a jury trial in any resulting litigation.</simpara>
</appendix>
<appendix xml:id="_bibliography">
<title>Bibliography</title>
<simpara><anchor xml:id="ECMA15a" xreflabel="[ECMA15a]"/>ECMA. (2015). <emphasis>ECMAScript 2015 Language Specification</emphasis>. Retrieved from <link xl:href="http://www.ecma-international.org/ecma-262/6.0/index.html">http://www.ecma-international.org/ecma-262/6.0/index.html</link></simpara>
<simpara><anchor xml:id="ECMA11a" xreflabel="[ECMA11a]"/>(2011). <emphasis>ECMAScript Language Specification</emphasis>. Retrieved from <link xl:href="http://www.ecma-international.org/publications/standards/Ecma-262.htm">http://www.ecma-international.org/publications/standards/Ecma-262.htm</link></simpara>
<simpara><anchor xml:id="Pierce02a" xreflabel="[Pierce02a]"/>Pierce, Benjamin C.. (2002). <emphasis>Types and Programming Languages</emphasis>. </simpara>
<simpara><anchor xml:id="Gosling15a" xreflabel="[Gosling15a]"/>Gosling, James and Joy, Bill and Steele, Guy and Bracha, Gilad and Buckley, Alex. (2015). <emphasis>The Java Language Specification. Java SE 8 Edition</emphasis>. Retrieved from <link xl:href="http://docs.oracle.com/javase/specs/jls/se8/jls8.pdf">http://docs.oracle.com/javase/specs/jls/se8/jls8.pdf</link></simpara>
<simpara><anchor xml:id="Gosling12a" xreflabel="[Gosling12a]"/>Gosling, James and Joy, Bill and Steele, Guy and Bracha, Gilad and Buckley, Alex. (2012). <emphasis>The Java Language Specification. Java SE 7 Edition</emphasis>. Retrieved from <link xl:href="http://docs.oracle.com/javase/specs/jls/se7/jls7.pdf">http://docs.oracle.com/javase/specs/jls/se7/jls7.pdf</link></simpara>
<simpara><anchor xml:id="Nielson99a" xreflabel="[Nielson99a]"/>Nielson, Flemming and Nielson, HanneRiis. (1999). <emphasis>Type and Effect Systems</emphasis>. Retrieved from <link xl:href="http://dx.doi.org/10.1007/3-540-48092-7_6">http://dx.doi.org/10.1007/3-540-48092-7_6</link></simpara>
<simpara><anchor xml:id="Crary02a" xreflabel="[Crary02a]"/>Crary, Karl and Weirich, Stephanie and Morrisett, Greg. (2002). <emphasis>Intensional polymorphism in type-erasure semantics</emphasis>. Retrieved from <link xl:href="http://journals.cambridge.org/article_S0956796801004282">http://journals.cambridge.org/article_S0956796801004282</link></simpara>
<simpara><anchor xml:id="Igarashi01a" xreflabel="[Igarashi01a]"/>Igarashi, Atsushi and Pierce, Benjamin C. and Wadler, Philip. (2001). <emphasis>Featherweight Java: a minimal core calculus for Java and GJ</emphasis>. </simpara>
<simpara><anchor xml:id="Torgersen05a" xreflabel="[Torgersen05a]"/>Torgersen, Mads and Ernst, Erik and Hansen, Christian Plesner. (2005). <emphasis>Wild FJ</emphasis>. Retrieved from <link xl:href="http://homepages.inf.ed.ac.uk/wadler/fool/program/14.html">http://homepages.inf.ed.ac.uk/wadler/fool/program/14.html</link></simpara>
<simpara><anchor xml:id="Cameron08b" xreflabel="[Cameron08b]"/>Cameron, Nicholas and Drossopoulou, Sophia and Ernst, Erik. (2008). <emphasis>A Model for Java with Wildcards</emphasis>. Retrieved from <link xl:href="http://dx.doi.org/10.1007/978-3-540-70592-5_2">http://dx.doi.org/10.1007/978-3-540-70592-5_2</link></simpara>
<simpara><anchor xml:id="Cameron09a" xreflabel="[Cameron09a]"/>Cameron, Nicholas. (2009). <emphasis>Existential Types for Variance&#8201;&#8212;&#8201;Java Wildcards and Ownership Types</emphasis>. Retrieved from <link xl:href="http://www.doc.ic.ac.uk/~ncameron/papers/cameron_thesis.pdf">http://www.doc.ic.ac.uk/~ncameron/papers/cameron_thesis.pdf</link></simpara>
<simpara><anchor xml:id="Summers10a" xreflabel="[Summers10a]"/>Summers, Alexander J. and Cameron, Nicholas and Dezani-Ciancaglini, Mariangiola and Drossopoulou, Sophia. (2010). <emphasis>Towards a semantic model for Java wildcards</emphasis>. </simpara>
<simpara><anchor xml:id="Wehr08a" xreflabel="[Wehr08a]"/>Wehr, Stefan and Thiemann, Peter. (2008). <emphasis>Subtyping Existential Types</emphasis>. Retrieved from <link xl:href="http://www.informatik.uni-freiburg.de/~wehr/publications/Wehr_Subtyping_existential_types.pdf">http://www.informatik.uni-freiburg.de/~wehr/publications/Wehr_Subtyping_existential_types.pdf</link></simpara>
<simpara><anchor xml:id="Igarashi07a" xreflabel="[Igarashi07a]"/>Igarashi, Atsushi and Nagira, Hideshi. (2007). <emphasis>Union Types for Object-Oriented Programming</emphasis>. Retrieved from <link xl:href="http://www.jot.fm/issues/issue_2007_02/article3/">http://www.jot.fm/issues/issue_2007_02/article3/</link></simpara>
<simpara><anchor xml:id="King13a" xreflabel="[King13a]"/>King, Gavin. (2013). <emphasis>The Ceylon Language</emphasis>. Red Hat, Inc.. Retrieved from <link xl:href="http://ceylon-lang.org/documentation/1.0/spec/pdf/ceylon-language-specification.pdf">http://ceylon-lang.org/documentation/1.0/spec/pdf/ceylon-language-specification.pdf</link></simpara>
<simpara><anchor xml:id="Laurent12a" xreflabel="[Laurent12a]"/>Laurent, Olivier. (2012). <emphasis>Intersection types with subtyping by means of cut elimination</emphasis>. </simpara>
<simpara><anchor xml:id="W3C:Steen:14:XL" xreflabel="[W3C:Steen:14:XL]"/>Steen, Hallvord and Aubourg, Julian and van Kesteren, Anne and Song, Jungkee. (2014). <emphasis>XMLHttpRequest Level 1</emphasis>. </simpara>
<simpara><anchor xml:id="Dart13a" xreflabel="[Dart13a]"/>Dart Team. (2013). <emphasis>Dart Programming Language Specification</emphasis>. Retrieved from <link xl:href="http://www.dartlang.org/docs/spec/latest/dart-language-specification.pdf">http://www.dartlang.org/docs/spec/latest/dart-language-specification.pdf</link></simpara>
<simpara><anchor xml:id="OMG14a" xreflabel="[OMG14a]"/>OMG. (2014). <emphasis>Interface Definition Language</emphasis>. Object Management Group. Retrieved from <link xl:href="http://www.omg.org/cgi-bin/doc?formal/2014-03-01.pdf">http://www.omg.org/cgi-bin/doc?formal/2014-03-01.pdf</link></simpara>
<simpara><anchor xml:id="W3C12a" xreflabel="[W3C12a]"/>W3C. (2012). <emphasis>Web IDL Specification</emphasis>. Retrieved from <link xl:href="http://www.w3.org/TR/2012/CR-WebIDL-20120419/">http://www.w3.org/TR/2012/CR-WebIDL-20120419/</link></simpara>
<simpara><anchor xml:id="Kuizinas14a" xreflabel="[Kuizinas14a]"/>Kuizinas, Gajus. (2014). <emphasis>The Definitive Guide to the JavaScript Generators</emphasis>. Retrieved from <link xl:href="http://gajus.com/blog/2/the-definetive-guide-to-the-javascript-generators">http://gajus.com/blog/2/the-definetive-guide-to-the-javascript-generators</link></simpara>
<simpara><anchor xml:id="West06a" xreflabel="[West06a]"/>West, Mike. (2006). <emphasis>Scope in JavaScript</emphasis>. Retrieved from <link xl:href="http://www.digital-web.com/articles/scope_in_javascript/">http://www.digital-web.com/articles/scope_in_javascript/</link></simpara>
<simpara><anchor xml:id="ECMA18a" xreflabel="[ECMA18a]"/>ECMA. (2018). <emphasis>ECMAScript 2018 Language Specification</emphasis>. Retrieved from <link xl:href="http://www.ecma-international.org/ecma-262/9.0/index.html">http://www.ecma-international.org/ecma-262/9.0/index.html</link></simpara>
<simpara><anchor xml:id="MozillaJSRef" xreflabel="[MozillaJSRef]"/><emphasis>JavaScript Reference</emphasis>. Retrieved from <link xl:href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference</link></simpara>
<simpara><anchor xml:id="Fowler04b" xreflabel="[Fowler04b]"/>Fowler, Martin. (2004). <emphasis>Inversion of Control Containers and the Dependency Injection pattern</emphasis>. Retrieved from <link xl:href="http://martinfowler.com/articles/injection.html">http://martinfowler.com/articles/injection.html</link></simpara>
<simpara><anchor xml:id="Prasanna09a" xreflabel="[Prasanna09a]"/>Prasanna, Dhanji R. (2009). <emphasis>Dependency Injection: Design Patterns Using Spring and Guice</emphasis>. </simpara>
<simpara><anchor xml:id="Zhu13a" xreflabel="[Zhu13a]"/>Zhu, He and Jagannathan, Suresh. (2013). <emphasis>Compositional and Lightweight Dependent Type Inference for ML</emphasis>. Retrieved from <link xl:href="http://dx.doi.org/10.1007/978-3-642-35873-9_19">http://dx.doi.org/10.1007/978-3-642-35873-9_19</link></simpara>
<simpara><anchor xml:id="Hudli13a" xreflabel="[Hudli13a]"/>Hudli, Shrinidhi R. and Hudli, Raghu V.. (2013). <emphasis>A Verification Strategy for Dependency Injection</emphasis>. </simpara>
<simpara><anchor xml:id="Lesiecki08a" xreflabel="[Lesiecki08a]"/>Lesiecki, Nicholas. (2008). <emphasis>Dependency injection with Guice</emphasis>. Retrieved from <link xl:href="http://www.ibm.com/developerworks/library/j-guice/">http://www.ibm.com/developerworks/library/j-guice/</link></simpara>
<simpara><anchor xml:id="Betts13a" xreflabel="[Betts13a]"/>Betts, Dominic and Melnik, Grigori and Simonazzi, Fernando and Subramanian, Mani. (2013). <emphasis>Dependency Injection with Unity</emphasis>. </simpara>
<simpara><anchor xml:id="Knol13a" xreflabel="[Knol13a]"/>Knol, Alex. (2013). <emphasis>Dependency injection with AngularJS</emphasis>. </simpara>
<simpara><anchor xml:id="Dagger" xreflabel="[Dagger]"/><emphasis>Dagger, Project Website</emphasis>. Retrieved from <link xl:href="http://square.github.io/dagger/">http://square.github.io/dagger/</link></simpara>
<simpara><anchor xml:id="ECMA12a" xreflabel="[ECMA12a]"/>(2012). <emphasis>ECMAScript Internationalization API Specification</emphasis>. Retrieved from <link xl:href="http://www.ecma-international.org/publications/standards/Ecma-402.htm">http://www.ecma-international.org/publications/standards/Ecma-402.htm</link></simpara>
<simpara><anchor xml:id="Martin96b" xreflabel="[Martin96b]"/>Martin, Robert C. (1996). <emphasis>The Liskov Substitution Principle</emphasis>. Retrieved from <link xl:href="http://www.objectmentor.com/publications/lsp.pdf">http://www.objectmentor.com/publications/lsp.pdf</link></simpara>
</appendix>
</book>