blob: e4454afc1146cb2826a5177223da488edfdeb6f9 [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]-->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 1.5.5">
<meta name="author" content="2019-08-08 13:15:33 CEST">
<title>N4JS Language Specification</title>
<link rel="stylesheet" href="styles/spec.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css">
<!-- ************* docinfo ******************************************************************* -->
<!-- ************* Favicon ************-->
<link rel="icon" href="images/favicon.ico" />
<!-- ************* Back-to-top JQuery ************* -->
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
<link href="styles/prism.min.css" rel="stylesheet" />
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-MML-AM_CHTML">
</script>
<!-- ************* Styles ************* -->
<link rel="stylesheet" type="text/css" href="styles/n4jsspec-adoc.css">
<!-- ****************** NavBar ****************** -->
<div id="menubar">
<div class="banner">
<a href="https://www.eclipse.org/n4js/#"><img id="logo" src="images/n4js-logo.png" alt="Eclipse N4JS"></a>
</div>
<ul>
<li><a href="index.html">Index</a></li>
</ul>
</div>
<!-- ************* docinfo ******************************************************************* -->
<style>
.admonitionblock td.icon .icon-todo:before{content:"\f249";color:#f4ee42}
</style>
</head>
<body class="book toc2 toc-left">
<div id="header">
<h1>N4JS Language Specification</h1>
<div class="details">
<span id="author" class="author">2019-08-08 13:15:33 CEST</span><br>
<span id="revnumber">version 0.9</span>
</div>
<div id="toc" class="toc2">
<div id="toctitle">Table of Contents</div>
<ul class="sectlevel1">
<li><a href="#_introduction">1. Introduction</a>
<ul class="sectlevel2">
<li><a href="#_notation">1.1. Notation</a>
<ul class="sectlevel3">
<li><a href="#_grammar-notation">1.1.1. Grammar Notation</a></li>
<li><a href="#_type-judgments-and-rules-and-constraints-notation">1.1.2. Type Judgments and Rules and Constraints Notation</a>
<ul class="sectlevel4">
<li><a href="#_typing-rules-and-judgments">1.1.2.1. Typing Rules and Judgments</a></li>
<li><a href="#_types-of-an-element">1.1.2.2. Types of an Element</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#_auxiliary-functions">1.2. Auxiliary Functions</a>
<ul class="sectlevel3">
<li><a href="#_binding">1.2.1. Binding</a></li>
<li><a href="#_merging-types">1.2.2. Merging Types</a>
<ul class="sectlevel4">
<li><a href="#_logic-formulas">1.2.2.1. Logic Formulas</a></li>
</ul>
</li>
<li><a href="#_symbols-and-font-convention">1.2.3. Symbols and Font Convention</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#_grammar">2. Grammar</a>
<ul class="sectlevel2">
<li><a href="#_lexical-conventions">2.1. Lexical Conventions</a>
<ul class="sectlevel3">
<li><a href="#_identifier-names-and-identifiers">2.1.1. Identifier Names and Identifiers</a></li>
<li><a href="#_this-keyword">2.1.2. This Keyword</a></li>
<li><a href="#_regular-expression-literals">2.1.3. Regular Expression Literals</a></li>
<li><a href="#_automatic-semicolon-insertion">2.1.4. Automatic Semicolon Insertion</a></li>
<li><a href="#_jsdoc">2.1.5. JSDoc</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#_names">3. Names</a>
<ul class="sectlevel2">
<li><a href="#_access-control">3.1. Access Control</a></li>
<li><a href="#_accessibility-of-types-top-level-variables-and-function-declarations">3.2. Accessibility of Types, Top-Level Variables and Function Declarations</a>
<ul class="sectlevel3">
<li><a href="#_accessibility-of-members">3.2.1. Accessibility of Members</a></li>
<li><a href="#_valid-names">3.2.2. Valid Names</a></li>
<li><a href="#_qualified-names">3.2.3. Qualified Names</a></li>
<li><a href="#_name-duplicates">3.2.4. Name Duplicates</a>
<ul class="sectlevel4">
<li><a href="#_lexical-environment">3.2.4.1. Lexical Environment</a></li>
<li><a href="#_duplicates-and-shadowing">3.2.4.2. Duplicates and Shadowing</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="#_types">4. Types</a>
<ul class="sectlevel2">
<li><a href="#_overview">4.1. Overview</a></li>
<li><a href="#_type-expressions">4.2. Type Expressions</a>
<ul class="sectlevel3">
<li><a href="#_syntax">4.2.1. Syntax</a></li>
<li><a href="#_properties">4.2.2. Properties</a></li>
<li><a href="#_semantics">4.2.3. Semantics</a></li>
</ul>
</li>
<li><a href="#_type-inference">4.3. Type Inference</a></li>
<li><a href="#_generic-and-parameterized-types">4.4. Generic and Parameterized Types</a>
<ul class="sectlevel3">
<li><a href="#_generic-types">4.4.1. Generic Types</a></li>
<li><a href="#_type-variables">4.4.2. Type Variables</a></li>
<li><a href="#_parameterized-types">4.4.3. Parameterized Types</a></li>
</ul>
</li>
<li><a href="#_primitive-ecmascript-types">4.5. Primitive ECMAScript Types</a>
<ul class="sectlevel3">
<li><a href="#_undefined-type">4.5.1. Undefined Type</a></li>
<li><a href="#_null-type">4.5.2. Null Type</a></li>
<li><a href="#_primitive-boolean-type">4.5.3. Primitive Boolean Type</a></li>
<li><a href="#_primitive-string-type">4.5.4. Primitive String Type</a></li>
<li><a href="#_primitive-number-type">4.5.5. Primitive Number Type</a></li>
<li><a href="#_primitive-type-int">4.5.6. Primitive Type int</a></li>
<li><a href="#_primitive-symbol-type">4.5.7. Primitive Symbol Type</a></li>
</ul>
</li>
<li><a href="#_primitive-n4js-types">4.6. Primitive N4JS Types</a>
<ul class="sectlevel3">
<li><a href="#_any-type">4.6.1. Any Type</a>
<ul class="sectlevel4">
<li><a href="#any-type-semantics">4.6.1.1. Semantics</a></li>
<li><a href="#any-type-type-inference">4.6.1.2. Type Inference</a>
<ul class="sectlevel5">
<li><a href="#_default-type-of-variables">4.6.1.2.1. Default Type of Variables</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#_void-type">4.6.2. Void Type</a>
<ul class="sectlevel4">
<li><a href="#void-type-semantics">4.6.2.1. Semantics</a></li>
</ul>
</li>
<li><a href="#_unknown-type">4.6.3. Unknown Type</a></li>
<li><a href="#_primitive-pathselector-and-i18nkey">4.6.4. Primitive Pathselector and I18nKey</a>
<ul class="sectlevel4">
<li><a href="#pathselector-semantics">4.6.4.1. Semantics</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#_built-in-ecmascript-object-types">4.7. Built-in ECMAScript Object Types</a>
<ul class="sectlevel3">
<li><a href="#ECMAScript-objects-semantics">4.7.1. Semantics</a></li>
<li><a href="#_object-type">4.7.2. Object Type</a></li>
<li><a href="#_function-object-type">4.7.3. Function-Object-Type</a></li>
<li><a href="#_array-object-type">4.7.4. Array Object Type</a></li>
<li><a href="#_string-object-type">4.7.5. String Object Type</a></li>
<li><a href="#_boolean-object-type">4.7.6. Boolean Object Type</a></li>
<li><a href="#_number-object-type">4.7.7. Number Object Type</a></li>
<li><a href="#_global-object-type">4.7.8. Global Object Type</a></li>
<li><a href="#_symbol">4.7.9. Symbol</a></li>
<li><a href="#_promise">4.7.10. Promise</a></li>
<li><a href="#_iterator-interface">4.7.11. Iterator Interface</a></li>
<li><a href="#_iterable-interface">4.7.12. Iterable Interface</a></li>
</ul>
</li>
<li><a href="#_built-in-n4js-types">4.8. Built-In N4JS Types</a>
<ul class="sectlevel3">
<li><a href="#_n4object">4.8.1. N4Object</a>
<ul class="sectlevel4">
<li><a href="#N4Object-semantics">4.8.1.1. Semantics</a></li>
</ul>
</li>
<li><a href="#_n4class">4.8.2. N4Class</a></li>
<li><a href="#IterableN">4.8.3. IterableN</a></li>
</ul>
</li>
<li><a href="#_type-modifiers">4.9. Type Modifiers</a>
<ul class="sectlevel3">
<li><a href="#Type_Modifiers_Dynamic">4.9.1. Dynamic</a></li>
<li><a href="#_optional-return-types">4.9.2. Optional Return Types</a></li>
</ul>
</li>
<li><a href="#_union-and-intersection-type-composed-types">4.10. Union and Intersection Type (Composed Types)</a>
<ul class="sectlevel3">
<li><a href="#_union-type">4.10.1. Union Type</a>
<ul class="sectlevel4">
<li><a href="#union-type-syntax">4.10.1.1. Syntax</a></li>
<li><a href="#union-type-semantics">4.10.1.2. Semantics</a></li>
<li><a href="#_warnings">4.10.1.3. Warnings</a></li>
</ul>
</li>
<li><a href="#_intersection-type">4.10.2. Intersection Type</a>
<ul class="sectlevel4">
<li><a href="#intersection-type-syntax">4.10.2.1. Syntax</a></li>
<li><a href="#intersection-type-semantics">4.10.2.2. Semantics</a></li>
<li><a href="#_warnings-2">4.10.2.3. Warnings</a></li>
</ul>
</li>
<li><a href="#_composed-types-in-wildcards">4.10.3. Composed Types in Wildcards</a></li>
<li><a href="#_property-access-for-composed-types">4.10.4. Property Access for Composed Types</a>
<ul class="sectlevel4">
<li><a href="#_properties-of-union-type">4.10.4.1. Properties of Union Type</a>
<ul class="sectlevel5">
<li><a href="#_remarks-on-union-type-s-members">4.10.4.1.1. Remarks on union type’s members:</a></li>
</ul>
</li>
<li><a href="#_properties-of-intersection-type">4.10.4.2. Properties of Intersection Type</a>
<ul class="sectlevel5">
<li><a href="#_remarks-on-intersection-type-s-methods">4.10.4.2.1. Remarks on intersection type’s methods:</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="#_constructor-and-classifier-type">4.11. Constructor and Classifier Type</a>
<ul class="sectlevel3">
<li><a href="#_syntax-3">4.11.1. Syntax</a></li>
<li><a href="#_semantics-2">4.11.2. Semantics</a></li>
<li><a href="#_constructors-and-prototypes-in-ecmascript-2015">4.11.3. Constructors and Prototypes in ECMAScript 2015</a></li>
</ul>
</li>
<li><a href="#_this-type">4.12. This Type</a>
<ul class="sectlevel3">
<li><a href="#this-type-syntax">4.12.1. Syntax</a></li>
<li><a href="#this-keyword-semantics">4.12.2. Semantics</a></li>
</ul>
</li>
<li><a href="#_enums">4.13. Enums</a>
<ul class="sectlevel3">
<li><a href="#_enums-n4js">4.13.1. Enums (N4JS)</a>
<ul class="sectlevel4">
<li><a href="#enums-syntax">4.13.1.1. Syntax</a></li>
<li><a href="#enums-semantics">4.13.1.2. Semantics</a></li>
</ul>
</li>
<li><a href="#_string-based-enums">4.13.2. String-Based Enums</a></li>
</ul>
</li>
<li><a href="#_short-hand-syntax">4.14. Short-Hand Syntax</a>
<ul class="sectlevel3">
<li><a href="#_array-short-hand-syntax">4.14.1. Array Short-Hand Syntax</a></li>
<li><a href="#_iterablen-short-hand-syntax">4.14.2. IterableN Short-Hand Syntax</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#_classifiers">5. Classifiers</a>
<ul class="sectlevel2">
<li><a href="#_n4js-specific-classifiers">5.1. N4JS Specific Classifiers</a>
<ul class="sectlevel3">
<li><a href="#_properties-2">5.1.1. Properties</a></li>
<li><a href="#_common-semantics-of-classifiers">5.1.2. Common Semantics of Classifiers</a></li>
<li><a href="#_classes">5.1.3. Classes</a>
<ul class="sectlevel4">
<li><a href="#_definition-of-classes">5.1.3.1. Definition of Classes</a>
<ul class="sectlevel5">
<li><a href="#class-syntax">5.1.3.1.1. Syntax</a></li>
<li><a href="#class-properties">5.1.3.1.2. Properties</a></li>
<li><a href="#class-type-inference">5.1.3.1.3. Type Inference</a></li>
</ul>
</li>
<li><a href="#class-semantics">5.1.3.2. Semantics</a></li>
<li><a href="#_final-modifier">5.1.3.3. Final Modifier</a></li>
<li><a href="#_abstract-classes">5.1.3.4. Abstract Classes</a></li>
<li><a href="#_non-instantiable-classes">5.1.3.5. Non-Instantiable Classes</a></li>
<li><a href="#_superclass">5.1.3.6. Superclass</a></li>
</ul>
</li>
<li><a href="#_interfaces">5.1.4. Interfaces</a>
<ul class="sectlevel4">
<li><a href="#_definition-of-interfaces">5.1.4.1. Definition of Interfaces</a>
<ul class="sectlevel5">
<li><a href="#interfaces-syntax">5.1.4.1.1. Syntax</a></li>
<li><a href="#interfaces-properties">5.1.4.1.2. Properties</a></li>
<li><a href="#interfaces-type-inference">5.1.4.1.3. Type Inference</a></li>
<li><a href="#interfaces-semantics">5.1.4.1.4. Semantics</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#_generic-classifiers">5.1.5. Generic Classifiers</a></li>
<li><a href="#sec:definition-site-variance">5.1.6. Definition-Site Variance</a></li>
</ul>
</li>
<li><a href="#_members">5.2. Members</a>
<ul class="sectlevel3">
<li><a href="#_syntax-4">5.2.1. Syntax</a>
<ul class="sectlevel4">
<li><a href="#_properties-3">5.2.1.1. Properties</a></li>
</ul>
</li>
<li><a href="#_semantics-3">5.2.2. Semantics</a></li>
<li><a href="#_methods">5.2.3. Methods</a>
<ul class="sectlevel4">
<li><a href="#_syntax-5">5.2.3.1. Syntax</a></li>
<li><a href="#_properties-4">5.2.3.2. Properties</a></li>
<li><a href="#_semantics-4">5.2.3.3. Semantics</a></li>
<li><a href="#_final-methods">5.2.3.4. Final Methods</a></li>
<li><a href="#_abstract-methods">5.2.3.5. Abstract Methods</a></li>
<li><a href="#_generic-methods">5.2.3.6. Generic Methods</a></li>
</ul>
</li>
<li><a href="#_default-methods-in-interfaces">5.2.4. Default Methods in Interfaces</a>
<ul class="sectlevel4">
<li><a href="#_asynchronous-methods">5.2.4.1. Asynchronous Methods</a></li>
</ul>
</li>
<li><a href="#_constructors">5.2.5. Constructors</a>
<ul class="sectlevel4">
<li><a href="#_structural-this-type-in-constructor">5.2.5.1. Structural This Type in Constructor</a></li>
<li><a href="#spec-constructor">5.2.5.2. @Spec Constructor</a></li>
<li><a href="#_callable-constructors">5.2.5.3. Callable Constructors</a></li>
<li><a href="#_covariant-constructors">5.2.5.4. Covariant Constructors</a></li>
</ul>
</li>
<li><a href="#_data-fields">5.2.6. Data Fields</a>
<ul class="sectlevel4">
<li><a href="#data-fields-syntax">5.2.6.1. Syntax</a></li>
<li><a href="#data-fields-properties">5.2.6.2. Properties</a>
<ul class="sectlevel5">
<li><a href="#data-fields-semantics">5.2.6.2.1. Semantics</a></li>
<li><a href="#data-fields-type-inference">5.2.6.2.2. Type Inference</a></li>
</ul>
</li>
<li><a href="#_assignment-modifiers">5.2.6.3. Assignment Modifiers</a></li>
<li><a href="#_field-accessors-getter-setter">5.2.6.4. Field Accessors (Getter/Setter)</a>
<ul class="sectlevel5">
<li><a href="#field-acessors-syntax">5.2.6.4.1. Syntax</a></li>
<li><a href="#field-acessors-properties">5.2.6.4.2. Properties</a></li>
<li><a href="#field-accessors-semantics">5.2.6.4.3. Semantics</a></li>
</ul>
</li>
<li><a href="#optional-fields">5.2.6.5. Optional Fields</a>
<ul class="sectlevel5">
<li><a href="#_syntax-6">5.2.6.5.1. Syntax</a></li>
<li><a href="#_semantics-5">5.2.6.5.2. Semantics</a></li>
<li><a href="#_background">5.2.6.5.3. Background</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#_static-members">5.2.7. Static Members</a>
<ul class="sectlevel4">
<li><a href="#_access-from-and-to-static-members">5.2.7.1. Access From and To Static Members</a></li>
<li><a href="#_generic-static-methods">5.2.7.2. Generic static methods</a></li>
<li><a href="#_static-members-of-interfaces">5.2.7.3. Static Members of Interfaces</a></li>
</ul>
</li>
<li><a href="#_redefinition-of-members">5.2.8. Redefinition of Members</a>
<ul class="sectlevel4">
<li><a href="#_overriding-of-members">5.2.8.1. Overriding of Members</a></li>
<li><a href="#_implementation-of-members">5.2.8.2. Implementation of Members</a>
<ul class="sectlevel5">
<li><a href="#_member-consumption">5.2.8.2.1. Member Consumption</a></li>
<li><a href="#_member-implementation">5.2.8.2.2. Member Implementation</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="#_structural-typing">5.3. Structural Typing</a>
<ul class="sectlevel3">
<li><a href="#_syntax-7">5.3.1. Syntax</a></li>
<li><a href="#_definition-site-structural-typing">5.3.2. Definition Site Structural Typing</a></li>
<li><a href="#_use-site-structural-typing">5.3.3. Use-Site Structural Typing</a></li>
<li><a href="#structural-readWriteInit-field-typing">5.3.4. Structural Read-only, Write-only and Initializer Field Typing</a></li>
<li><a href="#_public-setter-annotated-with-code-providesinitializer-code">5.3.5. Public Setter Annotated With <code>ProvidesInitializer</code></a></li>
<li><a href="#_structural-types-with-optional-fields">5.3.6. Structural Types With Optional Fields</a></li>
<li><a href="#_structural-types-with-access-modifier">5.3.7. Structural Types With Access Modifier</a></li>
<li><a href="#_structural-types-with-additional-members">5.3.8. Structural Types With Additional Members</a>
<ul class="sectlevel4">
<li><a href="#_syntax-8">5.3.8.1. Syntax</a>
<ul class="sectlevel5">
<li><a href="#_semantics-6">5.3.8.1.1. Semantics</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="#_functions">6. Functions</a>
<ul class="sectlevel2">
<li><a href="#_function-type">6.1. Function Type</a>
<ul class="sectlevel3">
<li><a href="#_properties-5">6.1.1. Properties</a></li>
<li><a href="#function-type-inference">6.1.2. Type Inference</a></li>
<li><a href="#_autoboxing-of-function-type">6.1.3. Autoboxing of Function Type</a></li>
<li><a href="#_arguments-object">6.1.4. Arguments Object</a></li>
</ul>
</li>
<li><a href="#_ecmascript-5-function-definition">6.2. ECMAScript 5 Function Definition</a>
<ul class="sectlevel3">
<li><a href="#_function-declaration">6.2.1. Function Declaration</a>
<ul class="sectlevel4">
<li><a href="#_syntax-9">6.2.1.1. Syntax</a></li>
<li><a href="#_semantics-8">6.2.1.2. Semantics</a></li>
</ul>
</li>
<li><a href="#_function-expression">6.2.2. Function Expression</a>
<ul class="sectlevel4">
<li><a href="#function-expression-syntax">6.2.2.1. Syntax</a></li>
<li><a href="#_semantics-and-type-inference">6.2.2.2. Semantics and Type Inference</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#_ecmascript-2015-function-definition">6.3. ECMAScript 2015 Function Definition</a>
<ul class="sectlevel3">
<li><a href="#_formal-parameters">6.3.1. Formal Parameters</a>
<ul class="sectlevel4">
<li><a href="#Type_Modifiers_Optional">6.3.1.1. Optional Parameters</a></li>
<li><a href="#Type_Modifiers_Default">6.3.1.2. Default Parameters</a></li>
<li><a href="#Type_Modifiers_Variadic">6.3.1.3. Variadic</a></li>
</ul>
</li>
<li><a href="#_generator-functions">6.3.2. Generator Functions</a>
<ul class="sectlevel4">
<li><a href="#generator-functions-syntax">6.3.2.1. Syntax</a></li>
<li><a href="#generator-functions-semantics">6.3.2.2. Semantics</a></li>
<li><a href="#_generator-arrow-functions">6.3.2.3. Generator Arrow Functions</a></li>
</ul>
</li>
<li><a href="#_arrow-function-expression">6.3.3. Arrow Function Expression</a>
<ul class="sectlevel4">
<li><a href="#arrow-function-expression-syntax">6.3.3.1. Syntax</a></li>
<li><a href="#arrow-function-expression-semantics-and-type-inference">6.3.3.2. Semantics and Type Inference</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#_ecmascript-proposals-function-definition">6.4. ECMAScript Proposals Function Definition</a>
<ul class="sectlevel3">
<li><a href="#_asynchronous-functions">6.4.1. Asynchronous Functions</a>
<ul class="sectlevel4">
<li><a href="#asynchronous-functions-syntax">6.4.1.1. Syntax</a></li>
<li><a href="#asynchronous-functions-semantics">6.4.1.2. Semantics</a></li>
<li><a href="#_asynchronous-arrow-functions">6.4.1.3. Asynchronous Arrow Functions</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#_n4js-extended-function-definition">6.5. N4JS Extended Function Definition</a>
<ul class="sectlevel3">
<li><a href="#_generic-functions">6.5.1. Generic Functions</a></li>
<li><a href="#_promisifiable-functions">6.5.2. Promisifiable Functions</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#_conversions-and-reflection">7. Conversions and Reflection</a>
<ul class="sectlevel2">
<li><a href="#_autoboxing-and-coercing">7.1. Autoboxing and Coercing</a>
<ul class="sectlevel3">
<li><a href="#_coercing">7.1.1. Coercing</a></li>
<li><a href="#_autoboxing-of-primitives">7.1.2. Autoboxing of Primitives</a></li>
<li><a href="#_autoboxing-of-function-expressions-and-declarations">7.1.3. Autoboxing of Function Expressions and Declarations</a></li>
</ul>
</li>
<li><a href="#_auto-conversion-of-objects">7.2. Auto-Conversion of Objects</a>
<ul class="sectlevel3">
<li><a href="#_auto-conversion-of-class-instances">7.2.1. Auto-Conversion of Class Instances</a>
<ul class="sectlevel4">
<li><a href="#_auto-conversion-of-interface-instances">7.2.1.1. Auto-Conversion of Interface Instances</a></li>
</ul>
</li>
<li><a href="#_auto-conversion-of-enum-literals">7.2.2. Auto-Conversion of Enum Literals</a></li>
</ul>
</li>
<li><a href="#_type-cast-and-type-check">7.3. Type Cast and Type Check</a>
<ul class="sectlevel3">
<li><a href="#_type-cast">7.3.1. Type Cast</a></li>
<li><a href="#_type-check">7.3.2. Type Check</a></li>
</ul>
</li>
<li><a href="#_reflection-meta-information">7.4. Reflection meta-information</a>
<ul class="sectlevel3">
<li><a href="#_reflection-for-classes">7.4.1. Reflection for Classes</a></li>
<li><a href="#_reflection-for-interfaces">7.4.2. Reflection for Interfaces</a></li>
<li><a href="#_reflection-for-enumerations">7.4.3. Reflection for Enumerations</a></li>
</ul>
</li>
<li><a href="#_conversion-of-primitive-types">7.5. Conversion of primitive types</a></li>
</ul>
</li>
<li><a href="#_expressions">8. Expressions</a>
<ul class="sectlevel2">
<li><a href="#_ecmascript-5-expressions">8.1. ECMAScript 5 Expressions</a>
<ul class="sectlevel3">
<li><a href="#_the-this-literal">8.1.1. The this Literal</a></li>
<li><a href="#_identifier">8.1.2. Identifier</a></li>
<li><a href="#_literals">8.1.3. Literals</a>
<ul class="sectlevel4">
<li><a href="#_integer-literals">8.1.3.1. Integer Literals</a></li>
</ul>
</li>
<li><a href="#_array-literal">8.1.4. Array Literal</a></li>
<li><a href="#_object-literal">8.1.5. Object Literal</a>
<ul class="sectlevel4">
<li><a href="#_properties-6">8.1.5.1. Properties</a></li>
<li><a href="#_scoping-and-linking">8.1.5.2. Scoping and linking</a></li>
</ul>
</li>
<li><a href="#_parenthesized-expression-and-grouping-operator">8.1.6. Parenthesized Expression and Grouping Operator</a></li>
<li><a href="#_property-accessors">8.1.7. Property Accessors</a>
<ul class="sectlevel4">
<li><a href="#properties-1">8.1.7.1. Properties</a></li>
</ul>
</li>
<li><a href="#_new-expression">8.1.8. New Expression</a></li>
<li><a href="#_function-expression-2">8.1.9. Function Expression</a></li>
<li><a href="#_function-calls">8.1.10. Function Calls</a></li>
<li><a href="#_postfix-expression">8.1.11. Postfix Expression</a></li>
<li><a href="#_unary-expression">8.1.12. Unary Expression</a></li>
<li><a href="#_multiplicative-expression">8.1.13. Multiplicative Expression</a></li>
<li><a href="#_additive-expression">8.1.14. Additive Expression</a>
<ul class="sectlevel4">
<li><a href="#type-inference-10">8.1.14.1. Type Inference</a></li>
</ul>
</li>
<li><a href="#_bitwise-shift-expression">8.1.15. Bitwise Shift Expression</a></li>
<li><a href="#_relational-expression">8.1.16. Relational Expression</a></li>
<li><a href="#_equality-expression">8.1.17. Equality Expression</a></li>
<li><a href="#_binary-bitwise-expression">8.1.18. Binary Bitwise Expression</a></li>
<li><a href="#_binary-logical-expression">8.1.19. Binary Logical Expression</a></li>
<li><a href="#_conditional-expression">8.1.20. Conditional Expression</a></li>
<li><a href="#_assignment-expression">8.1.21. Assignment Expression</a></li>
<li><a href="#_comma-expression">8.1.22. Comma Expression</a></li>
</ul>
</li>
<li><a href="#_ecmascript-6-expressions">8.2. ECMAScript 6 Expressions</a>
<ul class="sectlevel3">
<li><a href="#_the-super-keyword">8.2.1. The super Keyword</a></li>
</ul>
</li>
<li><a href="#_ecmascript-7-expressions">8.3. ECMAScript 7 Expressions</a>
<ul class="sectlevel3">
<li><a href="#_await-expression">8.3.1. Await Expression</a></li>
</ul>
</li>
<li><a href="#_n4js-specific-expressions">8.4. N4JS Specific Expressions</a>
<ul class="sectlevel3">
<li><a href="#_class-expression">8.4.1. Class Expression</a></li>
<li><a href="#_cast-as-expression">8.4.2. Cast (As) Expression</a>
<ul class="sectlevel4">
<li><a href="#cast-as-expression-semantics-type-inference">8.4.2.1. Semantics and Type Inference</a></li>
</ul>
</li>
<li><a href="#Import_Calls">8.4.3. Import Calls</a></li>
</ul>
</li>
<li><a href="#compile-time-expressions">8.5. Compile-Time Expressions</a></li>
</ul>
</li>
<li><a href="#_statements">9. Statements</a>
<ul class="sectlevel2">
<li><a href="#_ecmascript-5-statements">9.1. ECMAScript 5 Statements</a>
<ul class="sectlevel3">
<li><a href="#_function-or-field-accessor-bodies">9.1.1. Function or Field Accessor Bodies</a></li>
<li><a href="#_variable-statement">9.1.2. Variable Statement</a></li>
<li><a href="#_if-statement">9.1.3. If Statement</a></li>
<li><a href="#_iteration-statements">9.1.4. Iteration Statements</a></li>
<li><a href="#_return-statement">9.1.5. Return Statement</a></li>
<li><a href="#_with-statement">9.1.6. With Statement</a></li>
<li><a href="#_switch-statement">9.1.7. Switch Statement</a></li>
<li><a href="#_throw-try-and-catch-statements">9.1.8. Throw, Try, and Catch Statements</a></li>
<li><a href="#_debugger-statement">9.1.9. Debugger Statement</a></li>
</ul>
</li>
<li><a href="#_ecmascript-6-statements">9.2. ECMAScript 6 Statements</a>
<ul class="sectlevel3">
<li><a href="#_let">9.2.1. Let</a></li>
<li><a href="#_const">9.2.2. Const</a></li>
<li><a href="#_for-of-statement">9.2.3. <code>for &#8230;&#8203; of</code> statement</a></li>
<li><a href="#_import-statement">9.2.4. Import Statement</a>
<ul class="sectlevel4">
<li><a href="#Dynamic_Imports">9.2.4.1. Dynamic Imports</a></li>
<li><a href="#_immutabilaty-of-imports">9.2.4.2. Immutabilaty of Imports</a></li>
</ul>
</li>
<li><a href="#_export-statement">9.2.5. Export Statement</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#_annotations">10. Annotations</a>
<ul class="sectlevel2">
<li><a href="#_introduction-2">10.1. Introduction</a>
<ul class="sectlevel3">
<li><a href="#_syntax-13">10.1.1. Syntax</a></li>
<li><a href="#_properties-7">10.1.2. Properties</a></li>
<li><a href="#_element-specific-annotations">10.1.3. Element-Specific Annotations</a></li>
<li><a href="#_general-annotations">10.1.4. General Annotations</a>
<ul class="sectlevel4">
<li><a href="#_idebug">10.1.4.1. IDEBUG</a></li>
</ul>
</li>
<li><a href="#idebug-syntax">10.1.5. Syntax</a>
<ul class="sectlevel4">
<li><a href="#_semantics-11">10.1.5.1. Semantics</a></li>
<li><a href="#_suppress-warnings">10.1.5.2. Suppress Warnings</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#_declaration-of-annotations">10.2. Declaration of Annotations</a></li>
</ul>
</li>
<li><a href="#_extended-fetaures">11. Extended Fetaures</a>
<ul class="sectlevel2">
<li><a href="#_array-and-object-destructuring">11.1. Array and Object Destructuring</a>
<ul class="sectlevel3">
<li><a href="#_syntax-14">11.1.1. Syntax</a></li>
<li><a href="#_semantics-12">11.1.2. Semantics</a></li>
</ul>
</li>
<li><a href="#_dependency-injection">11.2. Dependency Injection</a>
<ul class="sectlevel3">
<li><a href="#_di-components-and-injectors">11.2.1. DI Components and Injectors</a>
<ul class="sectlevel4">
<li><a href="#_dicomponent-relations">11.2.1.1. DIComponent Relations</a></li>
</ul>
</li>
<li><a href="#_binders-and-bindings">11.2.2. Binders and Bindings</a></li>
<li><a href="#_injection-points">11.2.3. Injection Points</a>
<ul class="sectlevel4">
<li><a href="#_field-injection">11.2.3.1. Field Injection</a></li>
<li><a href="#_constructor-injection">11.2.3.2. Constructor Injection</a></li>
<li><a href="#_method-injection">11.2.3.3. Method Injection</a>
<ul class="sectlevel5">
<li><a href="#_provider">11.2.3.3.1. Provider</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#_n4js-di-life-cycle-and-scopes">11.2.4. N4JS DI Life Cycle and Scopes</a>
<ul class="sectlevel4">
<li><a href="#_injection-cylces">11.2.4.1. Injection Cylces</a></li>
<li><a href="#_default-scope">11.2.4.2. Default Scope</a></li>
<li><a href="#_singleton-scope">11.2.4.3. Singleton Scope</a></li>
<li><a href="#_per-injection-chain-singleton">11.2.4.4. Per Injection Chain Singleton</a></li>
</ul>
</li>
<li><a href="#_validation-of-callsites-targeting-n4injector-methods">11.2.5. Validation of callsites targeting N4Injector methods</a></li>
<li><a href="#_n4js-di-annotations">11.2.6. N4JS DI Annotations</a>
<ul class="sectlevel4">
<li><a href="#_n4js-di-generateinjector">11.2.6.1. N4JS DI @GenerateInjector</a></li>
<li><a href="#_n4js-di-withparentinjector">11.2.6.2. N4JS DI @WithParentInjector</a></li>
<li><a href="#_n4js-di-usebinder">11.2.6.3. N4JS DI @UseBinder</a></li>
<li><a href="#_n4js-di-binder">11.2.6.4. N4JS DI @Binder</a></li>
<li><a href="#_n4js-di-bind">11.2.6.5. N4JS DI @Bind</a></li>
<li><a href="#_n4js-di-provides">11.2.6.6. N4JS DI @Provides</a></li>
<li><a href="#_n4js-di-inject">11.2.6.7. N4JS DI @Inject</a></li>
<li><a href="#_n4js-di-singleton">11.2.6.8. N4JS DI @Singleton</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#_test-support">11.3. Test Support</a></li>
<li><a href="#_polyfill-definitions">11.4. Polyfill Definitions</a>
<ul class="sectlevel3">
<li><a href="#_runtime-polyfill-definitions">11.4.1. Runtime Polyfill Definitions</a></li>
<li><a href="#_static-polyfill-definitions">11.4.2. Static Polyfill Definitions</a></li>
<li><a href="#_transpiling-static-polyfilled-classes">11.4.3. Transpiling static polyfilled classes</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#_components">12. Components</a>
<ul class="sectlevel2">
<li><a href="#_overview-2">12.1. Overview</a></li>
<li><a href="#Component_Types">12.2. Component Types</a>
<ul class="sectlevel3">
<li><a href="#_libraries">12.2.1. Libraries</a></li>
<li><a href="#_runtime-environment-and-runtime-libraries">12.2.2. Runtime Environment and Runtime Libraries</a></li>
<li><a href="#_tests">12.2.3. Tests</a></li>
<li><a href="#_type-definitions">12.2.4. Type Definitions</a></li>
</ul>
</li>
<li><a href="#package-json">12.3. Package.json File</a>
<ul class="sectlevel3">
<li><a href="#_basic-properties">12.3.1. Basic Properties</a></li>
<li><a href="#_n4js-properties">12.3.2. N4JS Properties</a></li>
<li><a href="#_constraints">12.3.3. Constraints</a></li>
</ul>
</li>
<li><a href="#_support-for-npm-scopes">12.4. Support for NPM Scopes</a></li>
<li><a href="#sec:N4JS-Type-Definitions">12.5. N4JS Type Definitions</a>
<ul class="sectlevel3">
<li><a href="#_specify-type-definition">12.5.1. Specify Type Definition</a></li>
<li><a href="#_name-conventions">12.5.2. Name Conventions</a></li>
<li><a href="#_version-conventions">12.5.3. Version Conventions</a>
<ul class="sectlevel4">
<li><a href="#_define-a-new-type-definition-package">12.5.3.1. Define a New Type Definition Package</a></li>
<li><a href="#_using-a-type-definition-package">12.5.3.2. Using a Type Definition Package</a></li>
<li><a href="#_rational">12.5.3.3. Rational</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#_modules">12.6. Modules</a></li>
<li><a href="#_api-and-implementation-component">12.7. API and Implementation Component</a>
<ul class="sectlevel3">
<li><a href="#_execution-of-api-and-implementation-components">12.7.1. Execution of API and Implementation Components</a></li>
<li><a href="#_api-and-implementation-with-di">12.7.2. API and Implementation With DI</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#_plainjs">13. PlainJS</a>
<ul class="sectlevel2">
<li><a href="#_type-inference-and-validation-for-plain-js">13.1. Type Inference and Validation for Plain JS</a></li>
<li><a href="#_external-declarations">13.2. External Declarations</a>
<ul class="sectlevel3">
<li><a href="#_declaring-externals">13.2.1. Declaring externals</a></li>
<li><a href="#_instantiating-external-classes">13.2.2. Instantiating external classes</a></li>
<li><a href="#_implementation-of-external-declarations">13.2.3. Implementation of External Declarations</a></li>
<li><a href="#_example">13.2.4. Example</a></li>
</ul>
</li>
<li><a href="#_global-definitions">13.3. Global Definitions</a></li>
<li><a href="#_runtime-definitions">13.4. Runtime Definitions</a></li>
<li><a href="#_applying-polyfills">13.5. Applying Polyfills</a></li>
</ul>
</li>
<li><a href="#_jsdoc-2">14. JSDoc</a>
<ul class="sectlevel2">
<li><a href="#_general-n4jsdoc-features">14.1. General N4JSDoc Features</a>
<ul class="sectlevel3">
<li><a href="#_provided-inline-tags">14.1.1. Provided Inline Tags</a>
<ul class="sectlevel4">
<li><a href="#jsdoc_tag__code">14.1.1.1. @code</a></li>
<li><a href="#jsdoc_tag__link">14.1.1.2. @link</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#_n4jsdoc-for-user-projects">14.2. N4JSdoc for User Projects</a>
<ul class="sectlevel3">
<li><a href="#_standard-tags">14.2.1. Standard Tags</a>
<ul class="sectlevel4">
<li><a href="#_-author">14.2.1.1. @author</a></li>
<li><a href="#jsdoc_tag_param">14.2.1.2. @param</a></li>
<li><a href="#jsdoc_tag_return">14.2.1.3. @return</a></li>
</ul>
</li>
<li><a href="#_test-related-tags">14.2.2. Test Related Tags</a>
<ul class="sectlevel4">
<li><a href="#jsdoc_tag__testee">14.2.2.1. @testee</a></li>
<li><a href="#jsdoc_tag__testeeFromType">14.2.2.2. @testeeFromType</a></li>
<li><a href="#_testeeType_and__testeeMember">14.2.2.3. @testeeType and @testeeMember</a></li>
<li><a href="#jsdoc_tag_reqid_in_Tests">14.2.2.4. @reqid in Tests</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#_n4jsdoc-for-api-and-implementation-projects">14.3. N4JSDoc for API and Implementation Projects</a>
<ul class="sectlevel3">
<li><a href="#jsdoc_tag__apiNote">14.3.1. @apiNote</a></li>
<li><a href="#API_Project_Tags">14.3.2. API Project Tags</a>
<ul class="sectlevel4">
<li><a href="#jsdoc_tag_apiState">14.3.2.1. @apiState</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="#_jsx">15. JSX</a>
<ul class="sectlevel2">
<li><a href="#_jsx-support">15.1. JSX Support</a></li>
<li><a href="#_jsx-backend">15.2. JSX Backend</a></li>
</ul>
</li>
<li><a href="#_grammars">16. Grammars</a>
<ul class="sectlevel2">
<li><a href="#_type-expressions-grammar">16.1. Type Expressions Grammar</a></li>
<li><a href="#_n4js-language-grammar">16.2. N4JS Language Grammar</a></li>
</ul>
</li>
<li><a href="#_jsobjects">17. JSObjects</a>
<ul class="sectlevel2">
<li><a href="#_object">17.1. Object</a></li>
<li><a href="#_string">17.2. String</a></li>
<li><a href="#_boolean">17.3. Boolean</a></li>
<li><a href="#_number">17.4. Number</a>
<ul class="sectlevel3">
<li><a href="#_static-attributes">17.4.1. Static Attributes</a></li>
</ul>
</li>
<li><a href="#function">17.5. Function</a></li>
<li><a href="#_error">17.6. Error</a></li>
<li><a href="#_array">17.7. Array</a></li>
<li><a href="#_date">17.8. Date</a></li>
<li><a href="#_math">17.9. Math</a>
<ul class="sectlevel3">
<li><a href="#static-attributes-1">17.9.1. Static Attributes</a></li>
</ul>
</li>
<li><a href="#_regexp">17.10. RegExp</a></li>
<li><a href="#_json">17.11. JSON</a></li>
</ul>
</li>
<li><a href="#_n4js-objects">18. N4JS Objects</a>
<ul class="sectlevel2">
<li><a href="#_reflection-model">18.1. Reflection Model</a></li>
<li><a href="#_error-types">18.2. Error Types</a>
<ul class="sectlevel3">
<li><a href="#_n4apinotimplemented">18.2.1. N4ApiNotImplemented</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#_acronyms">Appendix A: Acronyms</a></li>
<li><a href="#sec:License">Appendix B: License</a></li>
<li><a href="#_bibliography">Appendix C: Bibliography</a></li>
</ul>
</div>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph center">
<p><strong>Last Updated: 2019-08-08</strong></p>
</div>
<div class="paragraph center">
<p><strong>Authors:</strong><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</p>
</div>
<div style="page-break-after: always;"></div>
<h2 id="_abstract" class="discrete">Abstract</h2>
<div class="paragraph">
<p>This document contains the N4JS Specification.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_introduction"><a class="anchor" href="#_introduction"></a><a class="link" href="#_introduction">1. Introduction</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>This specification defines the N4JS language.</p>
</div>
<div class="paragraph">
<p>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 [<a href="#ECMA15a">ECMA15a</a>].</p>
</div>
<div class="sect2 language-n4js">
<h3 id="_notation"><a class="anchor" href="#_notation"></a><a class="link" href="#_notation">1.1. Notation</a></h3>
<div class="sect3">
<h4 id="_grammar-notation"><a class="anchor" href="#_grammar-notation"></a><a class="link" href="#_grammar-notation">1.1.1. Grammar Notation</a></h4>
<div class="paragraph">
<p>For the specification of the syntax and structure of elements, we use a
slightly augmented similar to the grammar language of Xtext <a href="http://www.eclipse.org/Xtext/documentation/301_grammarlanguage.html">Grammar Language</a>.</p>
</div>
<div class="paragraph">
<p>Similar to [<a href="#ECMA11a">ECMA11a</a>], 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 (<code>+=</code>) and boolean (<code>?=</code>) 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.</p>
</div>
<div class="paragraph">
<p>The following lists informally defines the grammar:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1">Terminal</dt>
<dd>
<p>Terminals (or terminal strings) are enclosed in single quotes, e.g., <code>terminal</code>.</p>
</dd>
<dt class="hdlist1">Enumerations</dt>
<dd>
<p>Rules which contain only terminals used as values for properties are
marked with <code>enum</code> for enumeration.</p>
</dd>
<dt class="hdlist1">Properties</dt>
<dd>
<p>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., <code><em>name=</em>Identifier</code>.</p>
</dd>
<dt class="hdlist1">Collection Properties</dt>
<dd>
<p>If a property is a collection, values are added to that list via <code>+=</code>.
E.g.,<code><em>property+=</em>Value</code> .</p>
</dd>
<dt class="hdlist1">Boolean Properties</dt>
<dd>
<p>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., <code><em>final?</em>='final'?</code>.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>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.</p>
</div>
</div>
<div class="sect3">
<h4 id="_type-judgments-and-rules-and-constraints-notation"><a class="anchor" href="#_type-judgments-and-rules-and-constraints-notation"></a><a class="link" href="#_type-judgments-and-rules-and-constraints-notation">1.1.2. Type Judgments and Rules and Constraints Notation</a></h4>
<div class="sect4">
<h5 id="_typing-rules-and-judgments"><a class="anchor" href="#_typing-rules-and-judgments"></a><a class="link" href="#_typing-rules-and-judgments">1.1.2.1. Typing Rules and Judgments</a></h5>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="rule"></a><strong>Definition:</strong> <a href="#rule">Rule</a></p>
</div>
<div class="paragraph">
<p>We use the common notation for rules such as type inference rules <sup class="footnote">[<a id="_footnoteref_1" class="footnote" href="#_footnote_1" title="View footnote.">1</a>]</sup>, that is</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p><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.</p>
</div>
<div class="paragraph">
<p>Both parts of the rule may contain multiple expressions, which are concatenated via 'and'.</p>
</div>
<div class="paragraph">
<p>For example, the following</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>can be read as</p>
</div>
<div class="quoteblock">
<blockquote>
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>, <em>and</em> <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.
</blockquote>
</div>
</div>
</div>
<div class="paragraph">
<p>The following judgments (with relation symbols) are used:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1">subtype <math xmlns="http://www.w3.org/1998/Math/MathML"><mo>&lt;</mo></math> </dt>
<dd>
<p>-</p>
</dd>
<dt class="hdlist1">type <math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle></math> </dt>
<dd>
<p>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.</p>
</dd>
<dt class="hdlist1">expectedTypeIn <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>:</mi></math> </dt>
<dd>
<p>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></p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>The following statement, for example, defines transitivity of subtypes
(in a simplified manner):</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p><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 <code>entails</code>.
Thus, the rule can be read as follows:</p>
</div>
<div class="quoteblock">
<blockquote>
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>.
</blockquote>
</div>
<div class="paragraph">
<p>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.</p>
</div>
</div>
<div class="sect4">
<h5 id="_types-of-an-element"><a class="anchor" href="#_types-of-an-element"></a><a class="link" href="#_types-of-an-element">1.1.2.2. Types of an Element</a></h5>
<div class="paragraph">
<p>A variable or other typed element may be associated with three types:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Declared type: the type explicitly specified in the code, e.g., <code>var s: string</code>.</p>
</li>
<li>
<p>Inferred type: the type inferred by the type inferencer, e.g., <code>var s = "Hello"</code> infers the type of s to <code>string</code>. 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.</p>
</li>
<li>
<p>Actual type: the actual type of a variable during runtime.
This type information is not available at compile time and ignored in this specification.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1">declaredType<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>R</mi><mi>e</mi><mi>f</mi></math> </dt>
<dd>
<p>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.</p>
</dd>
<dt class="hdlist1">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></dt>
<dd>
<p>This pseudo property is the inferred type computed by the type inferencer.</p>
</dd>
<dt class="hdlist1">type<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>R</mi><mi>e</mi><mi>f</mi></math> </dt>
<dd>
<p>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></p>
</dd>
</dl>
</div>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_auxiliary-functions"><a class="anchor" href="#_auxiliary-functions"></a><a class="link" href="#_auxiliary-functions">1.2. Auxiliary Functions</a></h3>
<div class="paragraph">
<p>This section describes some auxiliary functions required for definition of type inference rules later on.</p>
</div>
<div class="sect3">
<h4 id="_binding"><a class="anchor" href="#_binding"></a><a class="link" href="#_binding">1.2.1. Binding</a></h4>
<div class="paragraph">
<p>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.</p>
</div>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="binding_relation"></a><strong>Definition:</strong> <a href="#binding_relation">Binding Relation</a></p>
</div>
<div class="paragraph">
<p>We define a pseudo relation</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>which binds a reference, i.e. an identifier, to a declaration (e.g.,variable declaration).</p>
</div>
<div class="paragraph">
<p>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 <a href="#_type-expressions">Type Expressions</a>.</p>
</div>
<div class="paragraph">
<p>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).<sup class="footnote">[<a id="_footnoteref_2" class="footnote" href="#_footnote_2" title="View footnote.">2</a>]</sup></p>
</div>
</div>
</div>
<div class="paragraph">
<p>A <code>DeclaredType</code> 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:</p>
</div>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="binding_relation_of_types"></a><strong>Definition:</strong> <a href="#binding_relation_of_types">Binding Relation of Types</a></p>
</div>
<div class="paragraph">
<p>We define a pseudo relation</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>which binds a type reference, i.e. a simple name, to the type declaration.</p>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_merging-types"><a class="anchor" href="#_merging-types"></a><a class="link" href="#_merging-types">1.2.2. Merging Types</a></h4>
<div class="paragraph">
<p>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.</p>
</div>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="merge_function"></a><strong>Definition:</strong> <a href="#merge_function">Merge Function</a></p>
</div>
<div class="paragraph">
<p>We define a pseudo function</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_logic-formulas"><a class="anchor" href="#_logic-formulas"></a><a class="link" href="#_logic-formulas">1.2.2.1. Logic Formulas</a></h5>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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>.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_symbols-and-font-convention"><a class="anchor" href="#_symbols-and-font-convention"></a><a class="link" href="#_symbols-and-font-convention">1.2.3. Symbols and Font Convention</a></h4>
<div class="paragraph">
<p>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>.</p>
</div>
<div class="paragraph">
<p>We use the following symbols and font conventions:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><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></dt>
<dd>
<p>Logical and, or, exclusive or (xor), and not.</p>
</dd>
<dt class="hdlist1"><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> </dt>
<dd>
<p>Logical implication, if and only if, and if-then-else.</p>
</dd>
<dt class="hdlist1"><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></dt>
<dd>
<p>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.</p>
</dd>
<dt class="hdlist1"><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></dt>
<dd>
<p>Element of, not an element of, union set, intersection set,
cardinality of set x.</p>
</dd>
<dt class="hdlist1"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi mathvariant="script">P</mi><mfenced close=")" open="("><mi>X</mi></mfenced></math></dt>
<dd>
<p>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>.</p>
</dd>
<dt class="hdlist1"><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></dt>
<dd>
<p>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</p>
<div class="quoteblock">
<blockquote>
"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".
</blockquote>
</div>
<div class="paragraph">
<p>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>.</p>
</div>
</dd>
<dt class="hdlist1"><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></dt>
<dd>
<p>(mu) read "<strong>metatype of</strong>"; metatype of a variable or property, e.g.,</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</dd>
<dt class="hdlist1"><math xmlns="http://www.w3.org/1998/Math/MathML"><mover accent="true"><mi>x</mi><mo>¯</mo></mover></math></dt>
<dd>
<p>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</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>instead of</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>or even more complicated with index variables.</p>
</div>
</dd>
</dl>
</div>
<div class="paragraph">
<p>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>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_grammar"><a class="anchor" href="#_grammar"></a><a class="link" href="#_grammar">2. Grammar</a></h2>
<div class="sectionbody">
<div class="sect2 language-n4js">
<h3 id="_lexical-conventions"><a class="anchor" href="#_lexical-conventions"></a><a class="link" href="#_lexical-conventions">2.1. Lexical Conventions</a></h3>
<div class="paragraph">
<p>As a super language on top of ECMAScript, the same lexical conventions are supported as described in [<a href="#ECMA11a">ECMA11a(p.S7)</a>] within strict mode.
Some further constraints are defined, however, restricting certain constructs. These constraints are described in the following.</p>
</div>
<div class="sect3">
<h4 id="_identifier-names-and-identifiers"><a class="anchor" href="#_identifier-names-and-identifiers"></a><a class="link" href="#_identifier-names-and-identifiers">2.1.1. Identifier Names and Identifiers</a></h4>
<div class="paragraph">
<p>Cf. [<a href="#ECMA11a">ECMA11a(p.S7.6)</a>], [<a href="#ECMA11a">ECMA11a(p.S11.1.2, p.p.63)</a>] and [<a href="#ECMA11a">ECMA11a(p.S01.2, p.p.51ff)</a>].</p>
</div>
<div class="paragraph">
<p>As a reminder, identifiers are defined as follows in the ECMAScript specification:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">IdentifierName: IdentifierStart* IdentifierPart;
IdentifierStart : UnicodeLetter | '_';
\ UnicodeEscapeSequence</code></pre>
</div>
</div>
<div class="paragraph">
<p>N4JS supports a limited form of computed-names for member declarations:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">N4JSPropertyComputedName:
'[' (SymbolLiteralComputedName | StringLiteralComputedName) ']'
;
SymbolLiteralComputedName: N4JSIdentifier '.' N4JSIdentifier ;
StringLiteralComputedName: STRING ;</code></pre>
</div>
</div>
<div class="paragraph">
<p>As can be seen, a computed-name must be either</p>
</div>
<div class="ulist">
<ul>
<li>
<p>a symbol reference, e.g., <code>Symbol.iterator</code></p>
</li>
<li>
<p>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)</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-1"></a><strong>Req. IDE-1:</strong> <a href="#Req-IDE-1">N4JS Identifier Restrictions</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>If the following constraints do not hold, errors are created.</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>Leading <code>$</code> (dollar sign) character is prohibited for any variable name such as fields, variables, types functions and methods.</p>
</li>
<li>
<p>Leading <code>_</code> (underscore) character is not allowed for identifying any functions or methods.</p>
</li>
</ol>
</div>
</li>
</ol>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-2"></a><strong>Req. IDE-2:</strong> <a href="#Req-IDE-2">N4JS identifier recommendations</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>If the following constraints do not hold, warnings are created.</p>
</li>
<li>
<p>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 <code>_</code> (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 <sup class="footnote">[<a id="_footnoteref_3" class="footnote" href="#_footnote_3" title="View footnote.">3</a>]</sup>.</p>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Type (and Type Variable) Identifiers</p>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">TypeIdentifier: [_A-Z][_a-zA-Z0-9]*
TypeVariableIdentifier: [_A-Z][_a-zA-Z0-9]*</code></pre>
</div>
</div>
</li>
<li>
<p>Package Identifiers</p>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">PackageIdentifier: [_a-z][._a-zA-Z0-9]*; // i.e. the folder names, must not end with .</code></pre>
</div>
</div>
</li>
<li>
<p>Member Identifiers and Enum Literals</p>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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]+)*</code></pre>
</div>
</div>
</li>
<li>
<p>Variable and Parameter Names</p>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">VariableIdentifier: [_a-zA-Z0-9]*
􏰀ParameterIdentifier: [_a-z][_a-zA-Z0-9]*</code></pre>
</div>
</div>
</li>
<li>
<p>Methods</p>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">MethodIdentifier: [_a-z][_a-zA-Z0-9]*;</code></pre>
</div>
</div>
</li>
<li>
<p>Annotations</p>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">AnnotationIdentifier: [_A-Z][_a-zA-Z0-9]*</code></pre>
</div>
</div>
</li>
</ol>
</div>
</li>
</ol>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">TypeIdentifier: [A-Z][a-zA-Z0-9]*;
PackageIdentifier: [a-z][a-zA-Z0-9]*;
FQNType: (PackageIdentifier '.')+ TypeIdentifier;</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_this-keyword"><a class="anchor" href="#_this-keyword"></a><a class="link" href="#_this-keyword">2.1.2. This Keyword</a></h4>
<div class="paragraph">
<p>Cf. [<a href="#ECMA11a">ECMA11a(p.S11.1.1, p.p.63)</a>]</p>
</div>
</div>
<div class="sect3">
<h4 id="_regular-expression-literals"><a class="anchor" href="#_regular-expression-literals"></a><a class="link" href="#_regular-expression-literals">2.1.3. Regular Expression Literals</a></h4>
<div class="paragraph">
<p>Cf. [<a href="#ECMA11a">ECMA11a(p.S7.8.5)</a>]</p>
</div>
</div>
<div class="sect3">
<h4 id="_automatic-semicolon-insertion"><a class="anchor" href="#_automatic-semicolon-insertion"></a><a class="link" href="#_automatic-semicolon-insertion">2.1.4. Automatic Semicolon Insertion</a></h4>
<div class="paragraph">
<p>Cf. [<a href="#ECMA11a">ECMA11a(p.S7.9)</a>]</p>
</div>
<div class="paragraph">
<p><a href="#_acronyms">ASI</a> is supported by the parser, however warnings are issued.</p>
</div>
</div>
<div class="sect3">
<h4 id="_jsdoc"><a class="anchor" href="#_jsdoc"></a><a class="link" href="#_jsdoc">2.1.5. JSDoc</a></h4>
<div class="paragraph">
<p>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 <a href="https://developers.google.com/closure/compiler/docs/js-for-compiler">Google Closure Compiler</a>, Java&#8217;s <a href="http://www.oracle.com/technetwork/java/javase/documentation/index-jsp-135444.html">JavaDoc</a> tool and N4-specific tags.</p>
</div>
<div class="paragraph">
<p>JSDoc comments are multiline comments, starting with <code>/**</code> (instead of simple multiline comments, starting with <code>/*</code>).</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">MultiLineComment: '/*' MultiLineCommentChars? '*/' // from ECMAScript specification
JSDoc: '/**' MultiLineCommentChars? '*/'</code></pre>
</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">MLVALUE: ([^@]+[^\n]+)+;
SLVALUE: ([^\n]+);</code></pre>
</div>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>MLVALUE</code> </dt>
<dd>
<p>short for <code>multi-line value</code>. This is usually only used for the general description of types or members.</p>
</dd>
<dt class="hdlist1"><code>SLVALUE</code> </dt>
<dd>
<p>short for <code>single-line value</code>. 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.</p>
</dd>
</dl>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_names"><a class="anchor" href="#_names"></a><a class="link" href="#_names">3. Names</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p><em>Visibility</em> defines the scope in which a declaration is visible, that is in which context references can be bound to the declaration.
<em>Access control</em> 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.</p>
</div>
<div class="paragraph">
<p><em>Extensibility</em> 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 <a href="#_n4js-specific-classifiers">N4JS Specific Classifiers</a></p>
</div>
<div class="sect2 language-n4js">
<h3 id="_access-control"><a class="anchor" href="#_access-control"></a><a class="link" href="#_access-control">3.1. Access Control</a></h3>
<div class="paragraph">
<p>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?</p>
</div>
<div class="paragraph">
<p>Accessing a type or member actually means that a reference is bound to a
declaration with the same identifier.</p>
</div>
<div class="paragraph">
<p>We distinguish the following contexts from which an element is accessed
as follows:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><strong>Module or type</strong>: access from elements in the same module or type.</p>
</li>
<li>
<p><strong>Subtype</strong>: access from a subtype.</p>
</li>
<li>
<p><strong>Project</strong>: access from the same project.</p>
</li>
<li>
<p><strong>Vendor</strong>: access from different project of the same vendor.</p>
</li>
<li>
<p><strong>World</strong>: access from anything else.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>Accessibility is defined by modifiers on types and members, e.g <code>public</code>, <code>protected</code>, <code>project</code>, <code>private</code>, via the <code>export</code> statement, and by the <code>@Internal</code> annotation.
Extensibility is defined by the <code>@Final</code> annotation respectively.</p>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_accessibility-of-types-top-level-variables-and-function-declarations"><a class="anchor" href="#_accessibility-of-types-top-level-variables-and-function-declarations"></a><a class="link" href="#_accessibility-of-types-top-level-variables-and-function-declarations">3.2. Accessibility of Types, Top-Level Variables and Function Declarations</a></h3>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>The same is true for variable declarations and function declarations defined as top-level elements of a module.</p>
</div>
<div class="paragraph">
<p>The following type access modifiers are supported by N4JS:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">enum TypeAccessModifier: project
| public;</code></pre>
</div>
</div>
<div class="paragraph">
<p>If a type is not exported, its visibility is private.
If a type has declared visibility <code>public</code>, it may additionally be marked as internal via the annotation <code>@Internal</code>.
Thus, we have the following set of type access modifiers:</p>
</div>
<div class="paragraph">
<p>TAM = <code>private</code> <code>project</code> <code>public@Internal</code> <code>public</code></p>
</div>
<div class="paragraph">
<p>That is, in N4JS, only the type access modifiers and are available.
The redundant <code>project</code> modifier serves only documentation purpose and can be synthesized if the <code>export</code> modifier is preset.</p>
</div>
<div class="paragraph">
<p>All other modifiers used here are synthesized as shown in the next example:</p>
</div>
<div class="listingblock">
<div class="title">Synthesized Type Access Modifiers in N4JS</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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</code></pre>
</div>
</div>
<div class="paragraph">
<p>The access control levels are defined as listed in <a href="#tab:type-access-control">Type Access Control</a>.</p>
</div>
<table id="tab:type-access-control" class="tableblock frame-all grid-all spread">
<caption class="title">Table 1. Type Access Control</caption>
<colgroup>
<col style="width: 20%;">
<col style="width: 20%;">
<col style="width: 20%;">
<col style="width: 20%;">
<col style="width: 20%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-center valign-top" colspan="5"><strong>Type Access</strong></th>
</tr>
</thead>
<tbody>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">Modifier</p></th>
<th class="tableblock halign-center valign-top"><p class="tableblock">Module</p></th>
<th class="tableblock halign-center valign-top"><p class="tableblock">Project</p></th>
<th class="tableblock halign-center valign-top"><p class="tableblock">Vendor</p></th>
<th class="tableblock halign-center valign-top"><p class="tableblock">World</p></th>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>private</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>project</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>public@Internal</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>public</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p><code>TAM</code> is a totally ordered set:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-3"></a><strong>Req. IDE-3:</strong> <a href="#Req-IDE-3">Type Access Modifiers</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>It is an error if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math> is not exported but defined as <code>project</code>, <code>public</code> or <code>public@Internal</code>.</p>
</li>
<li>
<p>It is an error if an annotation <code>@Internal</code> is present on a module private or <code>project</code> visible type.</p>
</li>
<li>
<p>The type modifier for all built-in ECMAScript types is <code>public</code>.</p>
</li>
<li>
<p>The <em>default modifier</em> for user declared exported declarations is <code>project</code>.
That is, this modifier is assumed if no modifier is explicitly specified.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="type_accessibility_t"></a><strong>Definition:</strong> <a href="#type_accessibility_t">Type Accessibility T</a></p>
</div>
<div class="paragraph">
<p>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 <a href="#tab:type-access-control">Type Access Control</a>.</p>
</div>
<div class="paragraph">
<p>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 <sup class="footnote">[<a id="_footnoteref_4" class="footnote" href="#_footnote_4" title="View footnote.">4</a>]</sup>:</p>
</div>
<div class="paragraph">
<p><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><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><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><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></p>
</div>
<div class="paragraph">
<p>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>.</p>
</div>
<div class="paragraph">
<p>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 <a href="#ex:implicit-type-references">Implicit, allowed type references in N4JS</a>:</p>
</div>
<div id="ex:implicit-type-references" class="exampleblock">
<div class="title">Example 1. Implicit, allowed type references in N4JS</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">export public class D {
public takeC(): C { .. }
public acceptC(c: C): void { .. }
}
/* private */ class C {}</code></pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var d: D = new D()
d.acceptC( d.takeC() )</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_accessibility-of-members"><a class="anchor" href="#_accessibility-of-members"></a><a class="link" href="#_accessibility-of-members">3.2.1. Accessibility of Members</a></h4>
<div class="paragraph">
<p>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 <code>B extends A</code>, and if <code>A</code> is not accessible to some client <code>C</code> but <code>B</code> is, then the members of <code>A</code> are indirectly accessible to <code>C</code> in so far as they are accessed via <code>B</code>.
This is true in particular for interfaces, as their properties are possibly merged into the consuming class (cf. <a href="#_implementation-of-members">Implementation of Members</a>).</p>
</div>
<div class="paragraph">
<p>The following member access modifiers are supported by N4JS:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">enum MemberAccessModifier: private
| project
| protected
| public;</code></pre>
</div>
</div>
<div class="paragraph">
<p>The modifiers <code>protected</code> and <code>public</code> may be annotated with <code>@Internal</code>.
Thus, we can define the following set of member access modifiers:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p><code>protected@Internal</code> and <code>public@Internal</code> are synthesized tags and were introduced as shorthand notation for the <code>@Internal</code> annotation together with <code>protected</code> or <code>public</code> access modifiers.
The <code>project</code> 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.</p>
</div>
<div class="exampleblock">
<div class="title">Example 2. Synthesized Member Access Modifiers in N4JS</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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
}</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p><code>MAM</code> does not define a totally ordered set. However, its subset</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>is a totally ordered set <sup class="footnote">[<a id="_footnoteref_5" class="footnote" href="#_footnote_5" title="View footnote.">5</a>]</sup> :</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p><a href="#tab:Member-Access-Controls">Member Access Control</a> shows which members are accessible from where.</p>
</div>
<table id="tab:Member-Access-Controls" class="tableblock frame-all grid-all spread">
<caption class="title">Table 2. Member Access Control</caption>
<colgroup>
<col style="width: 25%;">
<col style="width: 12.5%;">
<col style="width: 12.5%;">
<col style="width: 12.5%;">
<col style="width: 12.5%;">
<col style="width: 12.5%;">
<col style="width: 12.5%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Access Modifier</th>
<th class="tableblock halign-center valign-top">Inside Module</th>
<th class="tableblock halign-center valign-top">Inside Project</th>
<th class="tableblock halign-center valign-top">Vendor</th>
<th class="tableblock halign-center valign-top">Vendor Subtypes</th>
<th class="tableblock halign-center valign-top">Other Projects</th>
<th class="tableblock halign-center valign-top">Everywhere</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>private</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>project</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>protected@Internal</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>protected</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>public@Internal</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>public</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
</tr>
</tbody>
</table>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="type_and_member_accessibility_relation"></a><strong>Definition:</strong> <a href="#type_and_member_accessibility_relation">Type and Member Accessibility Relation</a></p>
</div>
<div class="paragraph">
<p>We define the relation</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>as follows:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>Less, greater then etc. are defined accordingly.</p>
</div>
</div>
</div>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="member_accessibility"></a><strong>Definition:</strong> <a href="#member_accessibility">Member Accessibility</a></p>
</div>
<div class="paragraph">
<p>The function</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>computes if a given reference can access the member declaration that it
references.</p>
</div>
</div>
</div>
<div class="paragraph">
<p>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).</p>
</div>
<div class="paragraph">
<p>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: <sup class="footnote">[<a id="_footnoteref_6" class="footnote" href="#_footnote_6" title="View footnote.">6</a>]</sup> <sup class="footnote">[<a id="_footnoteref_7" class="footnote" href="#_footnote_7" title="View footnote.">7</a>]</sup></p>
</div>
<div class="paragraph">
<p><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><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><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><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><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><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></p>
</div>
<div class="paragraph">
<p>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>.</p>
</div>
<div class="paragraph">
<p>Although private members are accessible inside a module, it is not possible to redefine (override etc.) these members (see <a href="#_redefinition-of-members">Redefinition of Members</a>).</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-4"></a><strong>Req. IDE-4:</strong> <a href="#Req-IDE-4">Default Member Access Modifiers</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>The following constraints for member access modifiers must hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The <em>default modifier</em> for members of user-declared classes is <code>project</code>.</p>
</li>
<li>
<p>The <em>default modifier</em> 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 <code>project</code>.</p>
</li>
<li>
<p>The modifier for enum literals is always <code>public</code>.</p>
</li>
<li>
<p>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.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 3. Type and Member Access Modifiers</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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
...
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>As demonstrated in the following snippet, class <code>C</code> can be used but not subclassed in other modules:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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();</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="exampleblock">
<div class="title">Example 4. Declaring type vs receiver type</div>
<div class="content">
<div class="paragraph">
<p>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.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class Base {
public m(b: Base): void {}
}
export public class ApiType extends Base {
}</code></pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">import * as N from "Base";
var t = new N.ApiType();
// member can be accessed although type Base is not exported:
t.m(t);</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>The property access to the member <code>m</code> is valid because it fulfills the constraints for accessibility.
The receiver of the property access is <code>t</code> of type <code>ApiType</code>.
That type is exported and accessible.
Therefore, the inherited member <code>m</code> is also considered valid since it is also defined <code>public</code>.</p>
</div>
<div class="paragraph">
<p>This rule allows for defining a common functionality in module or project visible types that becomes accessible via exported, visible subtypes.</p>
</div>
<div class="exampleblock">
<div class="title">Example 5. Member Access and Type Access Interplay</div>
<div class="content">
<div class="paragraph">
<p>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.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class A {
foo(): void{}
}
export public class C {
public getHidden(): A { return new A() };
}</code></pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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();
}
}</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_valid-names"><a class="anchor" href="#_valid-names"></a><a class="link" href="#_valid-names">3.2.2. Valid Names</a></h4>
<div class="paragraph">
<p>For identifier and property names, the same constraints as in ECMAScript
[<a href="#ECMA11a">ECMA11a(p.S7.6)</a>]
[<a href="#ECMA11a">ECMA11a(p.S7.6.1.2)</a>]
[<a href="#ECMA11a">ECMA11a(p.S11.6)</a>] are applied.</p>
</div>
<div class="paragraph">
<p>Identifier names in N4JS are defined similar to [<a href="#ECMA11a">ECMA11a(p.S11.6)</a>], 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.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-5"></a><strong>Req. IDE-5:</strong> <a href="#Req-IDE-5">Forbidden Identifier Names in N4JS</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>In N4JS mode, errors are generated in the following cases:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>A name of a type equals</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>an access modifier</p>
</li>
<li>
<p><code>set</code> or <code>get</code></p>
</li>
<li>
<p>an ECMAScript keyword</p>
</li>
<li>
<p>a boolean literal</p>
</li>
<li>
<p>the name of a base type</p>
</li>
</ol>
</div>
</li>
<li>
<p>The name of a function or function expression equals (but not the method)</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>an ECMAScript keyword</p>
</li>
<li>
<p>a reserved future ECMAScript word</p>
</li>
</ol>
</div>
</li>
</ol>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-6"></a><strong>Req. IDE-6:</strong> <a href="#Req-IDE-6">Undesired Identifier Names in N4JS</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>In N4JS mode, warnings are generated in the following cases:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The name of a member (of a non external type)</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>equals the name of a base type <sup class="footnote">[<a id="_footnoteref_8" class="footnote" href="#_footnote_8" title="View footnote.">8</a>]</sup> but the type of the variable is different from that type</p>
</li>
<li>
<p>is not static nor const but starts with an upper case letter</p>
</li>
</ol>
</div>
</li>
<li>
<p>The name of a non-external N4 types (class, interface, enum) starts with a lower case letter</p>
</li>
<li>
<p>The name of a variable (incl. formal parameter or catch variable and fields)</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>equals an N4JS keyword</p>
</li>
<li>
<p>equals the name of a base type but the type of the variable is different from that type</p>
</li>
<li>
<p>is not const but starts with an upper case letter</p>
</li>
</ol>
</div>
</li>
</ol>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_qualified-names"><a class="anchor" href="#_qualified-names"></a><a class="link" href="#_qualified-names">3.2.3. Qualified Names</a></h4>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p><a href="#tab:typenames">Different forms of module and type specifiers.</a> shows the different names of a given type <code>C</code>, defined in a module
<code>M.n4js</code>, defined in a package <code>p</code> of a project <code>MyProject</code>.</p>
</div>
<div class="paragraph">
<p>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 <code>from</code> keyword.</p>
</div>
<table id="tab:typenames" class="tableblock frame-all grid-all spread">
<caption class="title">Table 3. Different forms of module and type specifiers.</caption>
<colgroup>
<col style="width: 50%;">
<col style="width: 50%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Name</th>
<th class="tableblock halign-center valign-top">Example</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">Simple Type Name</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>C</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">(Plain) Module Specifier</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>p/M</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">Complete Module Specifier</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>MyProject/p/M</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">Complete Type Specifier</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>MyProject/p/M.C</code></p></td>
</tr>
</tbody>
</table>
</div>
<div class="sect3">
<h4 id="_name-duplicates"><a class="anchor" href="#_name-duplicates"></a><a class="link" href="#_name-duplicates">3.2.4. Name Duplicates</a></h4>
<div class="paragraph">
<p>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.</p>
</div>
<div class="sect4">
<h5 id="_lexical-environment"><a class="anchor" href="#_lexical-environment"></a><a class="link" href="#_lexical-environment">3.2.4.1. Lexical Environment</a></h5>
<div class="paragraph">
<p>N4JS handles scopes similar to ECMAScript, so that function scope is applied to variables declared with <code>var</code> (and parameters), and block scope for variables is declared with <code>let</code> or <code>const</code>.
In general, ECMAScript defines <em>Lexical Environments</em> 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 [<a href="#ECMA11a">ECMA11a(p.10.2)</a>].</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1">Elements that introduce lexical environments: </dt>
<dd>
<p><code>FunctionDefinition</code>, <code>VariableDeclaration</code>, <code>CatchBlock</code>, <code>WithStatement</code>, <code>ImportDeclaration</code></p>
</dd>
<dt class="hdlist1">N4JS specific declarations: </dt>
<dd>
<p><code>N4ClassDeclaration</code>, <code>N4InterfaceDeclaration</code>,
<code>N4EnumDeclaration</code>, <code>N4MethodDeclaration</code>.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>Additionally, a built-in lexical environment that defines global scope exists for every <code>Script</code>.</p>
</div>
<div class="paragraph">
<p>Since N4JS is extended (and a bit more strict) JS strict mode, <em>Object environment record</em>s created by <code>WithStatement</code> are not taken into account when resolving duplicates.
This applies to both N4JS mode and JS strict mode.
In unrestricted JS the <code>WithStatement</code> is allowed but duplicates are not validated.</p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
In case of names introduced by <code>ImportDeclaration</code>s only <code>NamedImportSpecifiers</code>s are taken into account (their import name or its alias if available).
<code>WildcardImportSpecifiers</code>s are not taken into account.
Potential optimizations by compiler or user annotation are also not currently taken into account during analysis.
</td>
</tr>
</table>
</div>
</div>
<div class="sect4">
<h5 id="_duplicates-and-shadowing"><a class="anchor" href="#_duplicates-and-shadowing"></a><a class="link" href="#_duplicates-and-shadowing">3.2.4.2. Duplicates and Shadowing</a></h5>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="shadowing_overriding_duplicates"></a><strong>Definition:</strong> <a href="#shadowing_overriding_duplicates">Shadowing Overriding Duplicates</a></p>
</div>
<div class="paragraph">
<p>Two elements with the same name declared in the same lexical environment (cf. [<a href="#ECMA11a">ECMA11a(p.S10.2.2.1)</a>] are called <em>duplicates</em>.
An element defined in an environment <em>shadows</em> all elements with the same name in outer environments.</p>
</div>
<div class="paragraph">
<p>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 <a href="#_redefinition-of-members">Redefinition of Members</a>.</p>
</div>
<div class="paragraph">
<p>For the following constraints, we make the following assumptions:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>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.</p>
</li>
<li>
<p>The implicit formal parameter <code>arguments</code> is treated similar to declared formal parameters.</p>
</li>
<li>
<p>Formal parameters are defined in the lexical environment of a function, that is, they are defined in the same lexical environment as local <code>var</code>-variables or other declarations in that function.</p>
</li>
<li>
<p>The "global" environment contains objects globally defined by the execution environment.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-7"></a><strong>Req. IDE-7:</strong> <a href="#Req-IDE-7">Forbidden Duplicates</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>There must be no two elements defined in the same lexical environment with the same name,
that is, there must be no duplicates.</p>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-8"></a><strong>Req. IDE-8:</strong> <a href="#Req-IDE-8">Forbidden Shadowing</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>In general, shadowing is allowed in N4JS.
But it is not allowed in the following cases:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>No element defined in the standard global scope must be shadowed.</p>
</li>
<li>
<p>There must be no function shadowing another function.</p>
</li>
<li>
<p>Elements defined in catch blocks must not shadow elements defined all parent non-catch-block environments.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-9"></a><strong>Req. IDE-9:</strong> <a href="#Req-IDE-9">Forbidden Names</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>In the script environment, it is not allowed to use the name
’arguments’.<sup class="footnote">[<a id="_footnoteref_9" class="footnote" href="#_footnote_9" title="View footnote.">9</a>]</sup></p>
<div class="paragraph">
<p><a href="#fig-forbidden-shadowing">Forbidden Shadowing</a> shows nested lexical environments with named elements declared inside (all named <code>x</code> here), the forbidden cases are marked with arrows
(the numbers at the left side refer to the numbers in <a href="#Req-IDE-8">[Req-IDE-8]</a>.</p>
</div>
</li>
</ol>
</div>
<div id="fig-forbidden-shadowing" class="imageblock" style="text-align: center">
<div class="content">
<img src="chapters/03_names/fig/shadowing.png" alt="shadowing" width="50%">
</div>
<div class="title">Figure 1. Forbidden Shadowing</div>
</div>
<div class="paragraph">
<p>Rationale:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>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.</p>
</li>
<li>
<p>It is typical that nested environments define local variables.
In particular helper variables (such as <code>i: number i</code> or <code>s: string</code> ) are expected to be used quite often.
Since this is a typical case, we allow shadowing for local variables.</p>
</li>
<li>
<p>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.</p>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_types"><a class="anchor" href="#_types"></a><a class="link" href="#_types">4. Types</a></h2>
<div class="sectionbody">
<div class="sect2 language-n4js">
<h3 id="_overview"><a class="anchor" href="#_overview"></a><a class="link" href="#_overview">4.1. Overview</a></h3>
<div class="paragraph">
<p>N4JS is essentially ECMAScript with the inclusion of types.
In the following sections we will describe how types are defined and used in N4JS.</p>
</div>
<div class="paragraph">
<p>Besides standard JavaScript types, the following metatypes are introduced:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Classifiers, that is class or interface (see <a href="#_classifiers">Classifiers</a>)</p>
</li>
<li>
<p>Enum</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Classifiers, methods and functions may be declared generic.</p>
</div>
<div class="paragraph">
<p>Types are related to each other by the subtype relation.</p>
</div>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="subtype_relation"></a><strong>Definition:</strong> <a href="#subtype_relation">Subtype Relation</a></p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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 <em>T</em>.
Generally in structural typing, this means that <em>T</em> <em>conforms</em> 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.</p>
</div>
<div class="paragraph">
<p>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></p>
</div>
</div>
</div>
<div class="paragraph">
<p>Whether nominal or structural typing is used depends on the declaration of the type or the reference.
This is explained further in <a href="#_structural-typing">Structural Typing</a>.</p>
</div>
<div class="paragraph">
<p>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 <em>T</em> is not type conforming to <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi></math>. (cf. [<a href="#Gosling12a">Gosling12a(p.S4.10)</a>])</p>
</div>
<div class="paragraph">
<p>Join and meet are defined as follows:</p>
</div>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="join_and_meet"></a><strong>Definition:</strong> <a href="#join_and_meet">Join and Meet</a></p>
</div>
<div class="paragraph">
<p>A type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>J</mi></math> is called a <em>join</em> (or least common supertype, ) of a pair of types <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi></math> and <em>T</em>, 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</p>
</div>
<div class="paragraph">
<p><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><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><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></p>
</div>
<div class="paragraph">
<p>Similarly, we say that a type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi></math> is a <em>meet</em> (or greatest common subtype, ) of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi></math> and <em>T</em>, 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<br></p>
</div>
<div class="paragraph">
<p><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><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><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></p>
</div>
</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p><a href="#fig-cd-predefined-type-hierarchy">Predefined Types Hierarchy</a> 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 <code>any</code> and <code>undefined</code> as the top and bottom type (cf. [<a href="#Pierce02a">Pierce02a(p.15.4)</a>]) We define these types here explicitly:</p>
</div>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="top_and_bottom_type"></a><strong>Definition:</strong> <a href="#top_and_bottom_type">Top and Bottom Type</a></p>
</div>
<div class="paragraph">
<p>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 <em>T</em> 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 <em>T</em> 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>.</p>
</div>
</div>
</div>
<div class="paragraph">
<p><code>null</code> 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 <code>undefined</code>.</p>
</div>
<div id="fig-cd-predefined-type-hierarchy" class="imageblock" style="text-align: center">
<div class="content">
<img src="chapters/04_types/fig/cdPredefinedTypesHierarchy.svg" alt="cdPredefinedTypesHierarchy">
</div>
<div class="title">Figure 2. Predefined Types Hierarchy</div>
</div>
<div class="paragraph">
<p>For every primitive type there is a corresponding built-in type as defined in [<a href="#ECMA11a">ECMA11a</a>], e.g. <code>string</code> and <code>String</code>.
There is no inheritance supported for primitive types and built-in types – these types are final.</p>
</div>
<div class="paragraph">
<p>Although the diagram shows inheritance between <code>void</code> and <code>undefined</code>, this relationship is only semantic: <code>void</code> is a refinement of <code>undefined</code> from a type system viewpoint.
The same applies to the relation of <code>Object</code> as well as the subtypes shown for <code>string</code> and <code>String</code>.</p>
</div>
<div id="ex:class-hierarchy" class="exampleblock">
<div class="title">Example 6. Type Examples, Class Hierarchy</div>
<div class="content">
<div class="paragraph">
<p>In the following examples, we assume the following classes to be given:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">// 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;
}</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_type-expressions"><a class="anchor" href="#_type-expressions"></a><a class="link" href="#_type-expressions">4.2. Type Expressions</a></h3>
<div class="paragraph">
<p>In contrast to ECMAScript, N4JS defines static types.
Aside from simple type references, type expressions may be used to specify the type of variables.</p>
</div>
<div class="sect3">
<h4 id="_syntax"><a class="anchor" href="#_syntax"></a><a class="link" href="#_syntax">4.2.1. Syntax</a></h4>
<div class="paragraph">
<p>The listing <a href="#lst:EBNFTypeExpression">EBNF Type Expression Grammar</a> 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.</p>
</div>
<div class="paragraph">
<p>References to user-declared types are expressed via <code>ParameterizedTypeRef</code>.
This is also true for non-generic types, as the type arguments are optional.
See <a href="#_parameterized-types">Parameterized Types</a> for details on that reference.</p>
</div>
<div class="paragraph">
<p>For qualified names and type reference names, see <a href="#_qualified-names">Qualified Names</a></p>
</div>
<div class="paragraph">
<p>The type expressions are usually added to parameter, field, or variable declarations as a suffix, separated with colon (<code>:</code>).
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.</p>
</div>
<div class="exampleblock">
<div class="title">Example 7. Type Annotation Syntax</div>
<div class="content">
<div class="paragraph">
<p>The following two listings show the very same code and type annotations are provided on
the left hand side. For simplicity, <code>string</code> is always used as type expression.<sup class="footnote">[<a id="_footnoteref_10" class="footnote" href="#_footnote_10" title="View footnote.">10</a>]</sup></p>
</div>
<table class="tableblock frame-none grid-none spread">
<colgroup>
<col style="width: 50%;">
<col style="width: 50%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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;
}
}</code></pre>
</div>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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;
}
}</code></pre>
</div>
</div></div></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>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.</p>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_properties"><a class="anchor" href="#_properties"></a><a class="link" href="#_properties">4.2.2. Properties</a></h4>
<div class="paragraph">
<p>Besides the properties indirectly defined by the grammar, the following pseudo properties are used for type expressions:</p>
</div>
<div class="paragraph">
<p>Properties of <code>TypeExpression</code>:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>v</mi><mi>a</mi><mi>r</mi></math> </dt>
<dd>
<p>If true, variable of that type is variadic. This is only allowed for parameters. Default value: <code>false</code>.</p>
</dd>
<dt class="hdlist1"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>o</mi><mi>p</mi><mi>t</mi></math> </dt>
<dd>
<p>If true, variable of that type is optional. This is only allowed for parameters and return types.
This actually means that the type <em>T</em> actually is a union type of <code>Undef|<em>T</em></code>.
Default value: <code>false</code>.</p>
</dd>
<dt class="hdlist1"><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> </dt>
<dd>
<p><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></p>
</dd>
<dt class="hdlist1"><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> </dt>
<dd>
<p>Pseudo property referencing the variable declaration (or expression) which <code>owns</code> the type expression.</p>
</dd>
</dl>
</div>
</div>
<div class="sect3">
<h4 id="_semantics"><a class="anchor" href="#_semantics"></a><a class="link" href="#_semantics">4.2.3. Semantics</a></h4>
<div class="paragraph">
<p>The ECMAScript types <em>undefined</em> and <em>null</em> are also supported.
These types cannot be referenced directly, however.
Note that <code>void</code> and <em>undefined</em> are almost similar.
Actually, the inferred type of a types element with declared type of <code>void</code> will be <em>undefined</em>.
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.
<code>void</code> is only used for function and method return types.</p>
</div>
<div class="paragraph">
<p>Note that not any type reference is allowed in any context.
Variables or formal parameters must not be declared <code>void</code> or union types must not be declared dynamic, for example.
These constraints are explained in the following section.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<h5 id="_ecmascript-types" class="discrete">ECMAScript Types</h5>
<div class="dlist">
<dl>
<dt class="hdlist1">Predefined Type</dt>
<dd>
<p>Predefined types, such as String, Number, or Object; and .</p>
</dd>
<dt class="hdlist1">Array Type</dt>
<dd>
<p><a href="#_array-object-type">Array Object Type</a>.</p>
</dd>
<dt class="hdlist1">Function Type</dt>
<dd>
<p>Described in <a href="#_functions">Functions</a>, <a href="#_function-type">Function Type</a>.</p>
</dd>
<dt class="hdlist1">Any Type</dt>
<dd>
<p><a href="#_any-type">Any Type</a>.</p>
</dd>
</dl>
</div>
<h5 id="_n4types" class="discrete">N4Types</h5>
<div class="dlist">
<dl>
<dt class="hdlist1">Declared Type</dt>
<dd>
<p>(Unparameterized) Reference to defined class <a href="#_classes">Classes</a> or enum <a href="#_enums">Enums</a>.</p>
</dd>
<dt class="hdlist1">Parameterized Type</dt>
<dd>
<p>Parameterized reference to defined generic class or interface; <a href="#_parameterized-types">Parameterized Types</a>.</p>
</dd>
<dt class="hdlist1">This Type</dt>
<dd>
<p><a href="#_this-type">This Type</a>.</p>
</dd>
<dt class="hdlist1">Constructor and Type Type</dt>
<dd>
<p>Class type, that is the meta class of a defined class or interface, <a href="#_constructor-and-classifier-type">Constructor and Classifier Type</a>.</p>
</dd>
<dt class="hdlist1">Union Types</dt>
<dd>
<p>Union of types, <a href="#_union-type">Union Type</a>.</p>
</dd>
<dt class="hdlist1">Type Variable</dt>
<dd>
<p>Type variable, <a href="#_type-variables">Type Variables</a>.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>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).</p>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_type-inference"><a class="anchor" href="#_type-inference"></a><a class="link" href="#_type-inference">4.3. Type Inference</a></h3>
<div class="paragraph">
<p>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.</p>
</div>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="default_type"></a><strong>Definition:</strong> <a href="#default_type">Default Type</a></p>
</div>
<div class="paragraph">
<p>In N4JS mode , if no type is explicitly specified and if no type information can be inferred, <code>any</code> is assumed as the default type.</p>
</div>
<div class="paragraph">
<p>In JS mode, the default type is <code>any+</code>.</p>
</div>
<div class="paragraph">
<p>Once the type of a variable is either declared or inferred, it is not supposed to be changed.</p>
</div>
</div>
</div>
<div class="paragraph">
<p>Given the following example.</p>
</div>
<div class="listingblock">
<div class="title">Variable type is not changeable</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var x: any;
x = 42;
x-5; // error: any is not a subtype of number.</code></pre>
</div>
</div>
<div class="paragraph">
<p>Type of <code>x</code> is declared as <code>any</code> in line 1. Although a number is assigned to <code>x</code> in line 2, the type of <code>x</code> is not changed. Thus an error is issued in line 3 because the type of <code>x</code> is still <code>any</code>.</p>
</div>
<div class="paragraph todo">
<p>At the moment, N4JS does not support type guards or, more general, effect system (cf. [<a href="#Nielson99a">Nielson99a</a>]).</p>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_generic-and-parameterized-types"><a class="anchor" href="#_generic-and-parameterized-types"></a><a class="link" href="#_generic-and-parameterized-types">4.4. Generic and Parameterized Types</a></h3>
<div class="paragraph">
<p>Some notes on terminology:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1">Type Parameter vs. Type Argument</dt>
<dd>
<p>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.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>This is similar to function declarations (with formal parameters) and function calls (with arguments).</p>
</div>
<div class="sect3">
<h4 id="_generic-types"><a class="anchor" href="#_generic-types"></a><a class="link" href="#_generic-types">4.4.1. Generic Types</a></h4>
<div class="paragraph">
<p>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.</p>
</div>
</div>
<div class="sect3">
<h4 id="_type-variables"><a class="anchor" href="#_type-variables"></a><a class="link" href="#_type-variables">4.4.2. Type Variables</a></h4>
<div class="paragraph">
<p>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.</p>
</div>
<h5 id="_syntax-2" class="discrete">Syntax</h5>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">TypeVariable:
(declaredCovariant?='out' | declaredContravariant?='in')?
name=IdentifierOrThis ('extends' declaredUpperBound=TypeRef)?
;</code></pre>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 8. Type Variable as Upper Bound</div>
<div class="content">
<div class="paragraph">
<p>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:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class G&lt;T&gt; {
&lt;X extends T&gt; foo(x: X): void { }
}</code></pre>
</div>
</div>
</div>
</div>
<h5 id="type-variables-properties" class="discrete">Properties</h5>
<div class="paragraph">
<p>A type parameter defines a type variable, which type may be constrained with an upper bound.</p>
</div>
<div class="paragraph">
<p>Properties of <code>TypeVariable</code>:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math> </dt>
<dd>
<p>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.</p>
</dd>
<dt class="hdlist1"><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> </dt>
<dd>
<p>Upper bound of the concrete type being bound to this type variable, i.e. a super class.</p>
</dd>
</dl>
</div>
<h5 id="type-variables-semantics" class="discrete">Semantics</h5>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-10"></a><strong>Req. IDE-10:</strong> <a href="#Req-IDE-10">Type Variable Semantics</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>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>.</p>
</li>
<li>
<p>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>.</p>
</li>
<li>
<p>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>.</p>
</li>
<li>
<p>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>.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>A type variable can be used in any type expression contained in the generic class, generic interface, or generic function / method definition.</p>
</div>
<div class="exampleblock">
<div class="title">Example 9. F bounded quantification</div>
<div class="content">
<div class="paragraph">
<p>Using a type variable in the upper bound reference may lead to recursive definition.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class Chain&lt;C extends Chain&lt;C, T&gt;, T&gt; {
next() : C { return null; }
m() : T { return null; }
}</code></pre>
</div>
</div>
</div>
</div>
<h5 id="type-variables-type-inference" class="discrete">Type Inference</h5>
<div class="paragraph">
<p>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:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Inside a generic declaration.</p>
</li>
<li>
<p>If the generic type is used as raw type.</p>
</li>
<li>
<p>If a generic function / method is called without type arguments and without the possibility to infer the type from the context.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="exampleblock">
<div class="title">Example 10. Type variables vs. existential types</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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" <i class="conum" data-value="1"></i><b>(1)</b>
// existential types:
var ga: G&lt;? extends A&gt;;
var gb: G&lt;? extends B&gt;;
ga = gb; <i class="conum" data-value="2"></i><b>(2)</b>
}
}</code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>Even though the upper bound of <code>S</code> is a subtype of <code>T</code>’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 <code>S</code> is a subtype of <code>T</code>,
because there are valid concrete bindings for which this would not be true: for example, if <code>T</code> were bound to <code>C</code> and <code>S</code> to <code>B</code>.</td>
</tr>
<tr>
<td><i class="conum" data-value="2"></i><b>2</b></td>
<td>This differs from existential types (see <code>ga</code> and <code>gb</code> and line 21):<br>
<code>G&lt;? extends B&gt;</code> <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> <code>G&lt;? extends A&gt;</code> ).</td>
</tr>
</table>
</div>
</div>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>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.</p>
</li>
<li>
<p>If we have a type variable on the left side and no type variable on the right side:<br>
the result is true if and only if the type variable on the left has one or more declared upper bounds.<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><br>
This is the case for</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>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>.</p>
</div>
</li>
<li>
<p>In all other cases the result is false.<br>
This includes cases such as</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>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</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>which is always false, even if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>A</mi><mo>=</mo><mi>B</mi></math>.</p>
</div>
</li>
</ol>
</div>
<div class="paragraph">
<p>We thus obtain the following defintion:</p>
</div>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="subtype_relation_for_type_variables"></a><strong>Definition:</strong> <a href="#subtype_relation_for_type_variables">Subtype Relation for Type Variables</a></p>
</div>
<div class="paragraph">
<p>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</p>
</div>
<div class="ulist">
<ul>
<li>
<p>if both <em>T</em> and <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi></math> are type variables:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>if <em>T</em> is a type variable and <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi></math> is not:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_parameterized-types"><a class="anchor" href="#_parameterized-types"></a><a class="link" href="#_parameterized-types">4.4.3. Parameterized Types</a></h4>
<div class="paragraph">
<p>References to generic types (cf. <a href="#_classes">Classes</a>) can be parameterized with type arguments.
A type reference with type arguments is called parameterized type.</p>
</div>
<h5 id="parameterized-types-syntax" class="discrete">Syntax</h5>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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
)?
;</code></pre>
</div>
</div>
<h5 id="parameterized-types-properties" class="discrete">Properties</h5>
<div class="paragraph">
<p>Properties of parameterized type references (nominal or structural):</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>declaredType</code> </dt>
<dd>
<p>Referenced type by type reference name (either the simple name or a qualified name, e.g. in case of namespace imports).</p>
</dd>
<dt class="hdlist1"><code>typeArgs</code> </dt>
<dd>
<p>The type arguments, may be empty.</p>
</dd>
<dt class="hdlist1"><code>definedTypingStrategy</code> </dt>
<dd>
<p>Typing strategy, by default nominal, see <a href="#_structural-typing">Structural Typing</a> for details</p>
</dd>
<dt class="hdlist1"><code>structuralMembers</code> </dt>
<dd>
<p>in case of structural typing, reference can add additional members to the structural type, see <a href="#_structural-typing">Structural Typing</a> for details.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p><strong>Pseudo Properties:</strong></p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>importSpec</code> </dt>
<dd>
<p>The <code>ImportSpecifier</code>, may be null if this is a local type reference.
Note that this may be a <code>NamedImportSpecifier</code>. See <a href="#_import-statement">Import Statement</a> for details for details.</p>
</dd>
<dt class="hdlist1"><code>moduleWideName</code> </dt>
<dd>
<p>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.</p>
</dd>
</dl>
</div>
<h5 id="parameterized-types-semantics" class="discrete">Semantics</h5>
<div class="paragraph">
<p>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 <em>substitution</em> 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.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-11"></a><strong>Req. IDE-11:</strong> <a href="#Req-IDE-11">Parameterized Types</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The actual type arguments must conform to the type parameters, that is:</p>
<div class="paragraph">
<p><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><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></p>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="paragraph">
<p>We define type erasure similar to Java [<a href="#Gosling12a">Gosling12a(p.S4.6)</a>] 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><sup>o</sup>
for the erasure of type <em>T</em>.<sup class="footnote">[<a id="_footnoteref_11" class="footnote" href="#_footnote_11" title="View footnote.">11</a>]</sup></p>
</div>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="parameterized_type"></a><strong>Definition:</strong> <a href="#parameterized_type">Parameterized Type</a></p>
</div>
<div class="paragraph">
<p>A parameterized type reference <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>R</mi></math> defines a parameterized type <em>T</em>, 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 <em>raw type</em> or <em>erasure</em> of <em>T</em>.</p>
</div>
<div class="paragraph">
<p>We define for types in general:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The erasure <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>G</mi></math><sup>o</sup> 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>.</p>
</li>
<li>
<p>The erasure of a type variable is the erasure of its upper bound.</p>
</li>
<li>
<p>The erasure of any other type is the type itself.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="paragraph">
<p>This concept of type erasure is purely defined for specification purposes.
It is not to be confused with the <code>real</code> type erasure which takes place at runtime, in which almost no types (except primitive types) are available.</p>
</div>
<div class="paragraph">
<p>That is, the type reference in <code>var G&lt;string&gt; gs;</code> actually defines a type <code>G&lt;string&gt;</code>, 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 <code>class G&lt;T&gt;</code>.
It is important that the type <code>G&lt;string&gt;</code> is different from <code>G&lt;T&gt;</code>.</p>
</div>
<div class="paragraph">
<p>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>.</p>
</div>
<div class="paragraph">
<p>In the following, we do not distinguish between parameter type reference and parameter type – they are both two sides of the same coin.</p>
</div>
<div class="exampleblock">
<div class="title">Example 11. Raw Types</div>
<div class="content">
<div class="paragraph">
<p>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:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class A{}
class B extends A{}
class G&lt;T extends A&gt; { t: T; }
var g: G;</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>In this case, variable <code>g</code> refers to the <em>raw type</em> <code>G</code>.
This is forbidden in N4JS, because two interpretations are possible:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><code>g</code> is of type <code>G&lt;? extends&gt;</code></p>
</li>
<li>
<p><code>g</code> is of type <code>G&lt;A&gt;</code></p>
</li>
</ol>
</div>
<div class="paragraph">
<p>In the first case, an existential type would be created, and <code>g.t = new A();</code> must fail.</p>
</div>
<div class="paragraph">
<p>In the second case, <code>g = new G&lt;B&gt;();</code> must fail.</p>
</div>
<div class="paragraph">
<p>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. <sup class="footnote">[<a id="_footnoteref_12" class="footnote" href="#_footnote_12" title="View footnote.">12</a>]</sup></p>
</div>
<div class="paragraph">
<p>Calls to generic functions and methods can also be parameterized, this is described in <a href="#_function-calls">Function Calls</a>.
Note that invocation of generic functions or methods does not need to be parameterized.</p>
</div>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="type_conformance"></a><strong>Definition:</strong> <a href="#type_conformance">Type Conformance</a></p>
</div>
<div class="paragraph">
<p>We define type conformance for non-primitive type references as follows:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>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>,</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>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></p>
<div class="paragraph">
<p><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><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><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><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></p>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 12. Subtyping with parameterized types</div>
<div class="content">
<div class="paragraph">
<p>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:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>The figure <a href="#cdVarianceChart">Cheat Sheet: Subtype Relation of Parameterized Types</a> shows the subtype relations of parameterized types (of a single generic type), which can be used as a cheat sheet.</p>
</div>
<div id="cdVarianceChart" class="imageblock" style="text-align: center">
<div class="content">
<img src="chapters/04_types/fig/cdVarianceChart.svg" alt="cdVarianceChart">
</div>
<div class="title">Figure 3. Cheat Sheet: Subtype Relation of Parameterized Types</div>
</div>
<div class="exampleblock">
<div class="title">Example 13. Subtyping between different generic types</div>
<div class="content">
<div class="paragraph">
<p>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:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class G&lt;T&gt; {}
class H&lt;T&gt; extends G&lt;T&gt; {}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Given a simple, non-parameterized class <em>A</em>, the following
subtype relations are evaluated as indicated:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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</code></pre>
</div>
</div>
</div>
</div>
<h5 id="parameterized-types-type-inference" class="discrete">Type Inference</h5>
<div class="paragraph">
<p>Type inference for parameterized types uses the concept of existential types (in Java, a slightly modified version called capture conversion is implemented).</p>
</div>
<div class="paragraph">
<p>The general concept for checking type conformance and inferring types for generic and parameterized types is described in [<a href="#Igarashi01a">Igarashi01a</a>] for <em>Featherweight Java with Generics</em>.</p>
</div>
<div class="paragraph">
<p>The concept of existential types with wildcard capture (a special kind of existential type) is published in [<a href="#Torgersen05a">Torgersen05a</a>], further developed in [<a href="#Cameron08b">Cameron08b</a>] (further developed in [<a href="#Cameron09a">Cameron09a</a>] [<a href="#Summers10a">Summers10a</a>], also see [<a href="#Wehr08a">Wehr08a</a>] for a similar approach).
The key feature of the Java generic wildcard handling is called capture conversion, described in [<a href="#Gosling12a">Gosling12a(p.S5.1.10)</a>].
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</p>
</div>
<div class="paragraph">
<p>The idea is quite simple: All unbound wildcards are replaced with freshly created new types <sup class="footnote">[<a id="_footnoteref_13" class="footnote" href="#_footnote_13" title="View footnote.">13</a>]</sup>,
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.</p>
</div>
<div class="exampleblock">
<div class="title">Example 14. Existential Type</div>
<div class="content">
<div class="paragraph">
<p>The inferred type of a variable
declared as</p>
</div>
<div class="paragraph">
<p><code>var x: G&lt;? extends A&gt;;</code>,</p>
</div>
<div class="paragraph">
<p>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</p>
</div>
<div class="paragraph">
<p><code>var y: G&lt;? extends A&gt;;</code></p>
</div>
<div class="paragraph">
<p>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. <code>set(T t)</code> and <code>T get()</code>, the following code snippet will produce an error:</p>
</div>
<div class="paragraph">
<p><code>y.set(x.get())</code></p>
</div>
<div class="paragraph">
<p>This is no surprise, as <code>x.get()</code> 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>.</p>
</div>
</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-12"></a><strong>Req. IDE-12:</strong> <a href="#Req-IDE-12">Join of Parameterized Types</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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 <em>T</em>, the following constraints hold:<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></p>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 15. Upper and lower bound of parameterized types</div>
<div class="content">
<div class="paragraph">
<p>Assuming the given classes listed above, the following upper and lower bounds are expected:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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</code></pre>
</div>
</div>
<div class="paragraph">
<p>This leads to the following expected subtype relations:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">(? extends A) &lt;: A -&gt; true
(? super A) &lt;: A -&gt; false
A &lt;: (? extends A) -&gt; false
A &lt;: (? super A) -&gt; true</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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 <a href="#ex:use-declared-interfaces-for-lower-bounds">Use declared interfaces for lower bounds</a> below.
However, type variables must not be constrained using structural types (see constraint <a href="#Req-IDE-76">[Req-IDE-76]</a>).
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 <a href="#ex:use-declared-interfaces-for-lower-bounds">Use declared interfaces for lower bounds</a>).</p>
</div>
<div id="ex:use-declared-interfaces-for-lower-bounds" class="exampleblock">
<div class="title">Example 16. Use declared interfaces for lower bounds</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">interface I1&lt;T extends any with {prop : int}&gt; { // error
}
interface ~J {
prop : int;
}
interface I2&lt;T extends J&gt; {
}</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_primitive-ecmascript-types"><a class="anchor" href="#_primitive-ecmascript-types"></a><a class="link" href="#_primitive-ecmascript-types">4.5. Primitive ECMAScript Types</a></h3>
<div class="paragraph">
<p>N4JS provides the same basic types as ECMAScript [<a href="#ECMA11a">ECMA11a(p.p.28)</a>].</p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
In ECMAScript, basic types come in two flavors: as primitive types [<a href="#ECMA11a">ECMA11a(p.S8Types, p.p.28)</a>] and as Objects [<a href="#ECMA11a">ECMA11a(p.S15, p.p.102)</a>].
In N4JS, primitive types are written with lower cases, object types with first case capitalized.
For example, <code>String</code> is the primitive ECMAScript string type, while <code>String</code> is an object.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>The following ECMAScript primitive types are supported, they are written
with lower case letters::</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>undefined</code>: [<a href="#ECMA11a">ECMA11a(p.S8.3)</a>]; cannot be used in type expression, see void below.</p>
</li>
<li>
<p><code>null</code> [<a href="#ECMA11a">ECMA11a(p.S8.3)</a>]; cannot be used in type expression</p>
</li>
<li>
<p><code>boolean</code> [<a href="#ECMA11a">ECMA11a(p.S8.3)</a>]</p>
</li>
<li>
<p><code>string</code> [<a href="#ECMA11a">ECMA11a(p.S8.4)</a>]</p>
</li>
<li>
<p><code>number</code> [<a href="#ECMA11a">ECMA11a(p.S8.5)</a>]</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Although Object is a primitive type in [<a href="#ECMA11a">ECMA11a(p.S8.5)</a>], it is interpreted here as an object type and described in <a href="#_object-type">Object Type</a>.</p>
</div>
<div class="paragraph">
<p>Please note that primitive types are values (= no objects) so they have no properties and you cannot inherit from them.</p>
</div>
<div class="sect3">
<h4 id="_undefined-type"><a class="anchor" href="#_undefined-type"></a><a class="link" href="#_undefined-type">4.5.1. Undefined Type</a></h4>
<div class="paragraph">
<p>As a built-in type, the type <code>undefined</code> cannot be declared explicitly by the user by means of a type expression.
Note in ECMAScript there are three distinct use cases of <code>undefined</code>:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>undefined</code> as type (as used here)</p>
</li>
<li>
<p><code>undefined</code> as value (the only value of the undefined type)</p>
</li>
<li>
<p><code>undefined</code> 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.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The type <code>undefined</code> will be inferred to false in a boolean expression.
It is important to note that something that is not assigned to a value is <code>undefined</code> but not <code>null</code>.</p>
</div>
<div class="paragraph">
<p>The type <code>undefined</code> is a subtype of all types. That is,</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>is an axiom and true for all types <em>T</em>.</p>
</div>
<div class="paragraph">
<p>Whenever an expression <em>E</em> has an inferred type of <code>undefined</code>, which means it will always evaluate to
value <code>undefined</code> at runtime, a warning is shown, unless <em>E</em> is &#8230;&#8203;</p>
</div>
<div class="ulist">
<ul>
<li>
<p>a <code>void</code> expression</p>
</li>
<li>
<p>the direct child expression of a <code>void</code> expression,</p>
</li>
<li>
<p>the direct child expression of an expression statement,</p>
</li>
<li>
<p>the <code>undefined</code> literal (i.e. the literal representing the <code>undefined</code> value),</p>
</li>
<li>
<p>the <code>this</code> literal.</p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="_null-type"><a class="anchor" href="#_null-type"></a><a class="link" href="#_null-type">4.5.2. Null Type</a></h4>
<div class="paragraph">
<p>The <code>null</code> type cannot be declared explicitly by the user. Only the keyword <code>null</code> is inferred to type <code>null</code>.</p>
</div>
<h5 id="null-type-semantics" class="discrete">Semantics</h5>
<div class="paragraph">
<p>In contrast to <code>undefined</code>, it expresses the intentional absence of a value.</p>
</div>
<div class="paragraph">
<p>The <code>null</code> type can be assigned to any other type.
That is, the type <code>null</code> is a subtype of all other types except <code>undefined</code>:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>Please note that</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>null==undefined</code> evaluates to <code>true</code></p>
</li>
<li>
<p><code>null===undefined</code> evaluates to <code>false</code></p>
</li>
<li>
<p><code>typeof null</code> evaluates to <code>object</code></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Only the <code>null</code> keyword is inferred to type null. If <code>null</code> is assigned to a variable, the type of the variable is not changed.
This is true, in particular, for variable declarations.
For example in</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var x = null;</code></pre>
</div>
</div>
<div class="paragraph">
<p>the type of variable <code>x</code> is inferred to <code>any</code> (cf. <a href="#_variable-statement">Variable Statement</a>).</p>
</div>
<div class="paragraph">
<p>The type <code>null</code> will be inferred to false in a boolean expression.</p>
</div>
<div class="paragraph">
<p>The call <code>typeof null</code> will return ’object’.</p>
</div>
</div>
<div class="sect3">
<h4 id="_primitive-boolean-type"><a class="anchor" href="#_primitive-boolean-type"></a><a class="link" href="#_primitive-boolean-type">4.5.3. Primitive Boolean Type</a></h4>
<div class="paragraph">
<p>Represents a logical entity having two values, true and false.</p>
</div>
<div class="paragraph">
<p>Please note that a boolean primitive is coerced to a number in a comparison operation so that</p>
</div>
<table class="tableblock frame-all grid-all spread">
<colgroup>
<col style="width: 66.6666%;">
<col style="width: 33.3334%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-center valign-top">Source</th>
<th class="tableblock halign-center valign-middle">Result</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var a = true; console.log(a == 1)</code></pre>
</div>
</div></div></td>
<th class="tableblock halign-center valign-middle"><p class="tableblock">prints true</p></th>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var b = false; console.log(b == 0)</code></pre>
</div>
</div></div></td>
<th class="tableblock halign-center valign-middle"><p class="tableblock">prints true</p></th>
</tr>
</tbody>
</table>
<h5 id="primitive-boolean-type-semantics" class="discrete">Semantics</h5>
<div class="paragraph">
<p>The type <code>boolean</code> is subtype of <code>any</code>:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>Variables of type <code>boolean</code> can be auto-converted (coerced) to <code>Boolean</code>, as described in <a href="#_autoboxing-and-coercing">Autoboxing and Coercing</a>.</p>
</div>
</div>
<div class="sect3">
<h4 id="_primitive-string-type"><a class="anchor" href="#_primitive-string-type"></a><a class="link" href="#_primitive-string-type">4.5.4. Primitive String Type</a></h4>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var a = "MyString"
console.log(typeof a) // string
console.log(a.length) // 8
console.log(typeof a) // string</code></pre>
</div>
</div>
<div class="paragraph">
<p>You can handle a primitive <code>string</code> like an object type <code>String</code> but with these exceptions:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>typeof "MyString"</code> is <code>'string'</code> but <code>typeof new String("MyString")</code> is <code>'object'</code></p>
</li>
<li>
<p><code>"MyString" instanceof String</code> or <code>instanceof Object</code> will return <code>false</code>, for <code>new String("MyString")</code> both checks evaluate to <code>true</code></p>
</li>
<li>
<p><code>console.log(eval("2+2"))</code> returns <code>4</code>, <code>console.log(eval(new String("2+2")))</code> returns string <code>"2+2"</code></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>This marks a difference to Java.
In JavaScript, Unicode escape sequences are never interpreted as a special character.</p>
</div>
<h5 id="primitive-string-type-semantics" class="discrete">Semantics</h5>
<div class="paragraph">
<p>The <code>string</code> type is a subtype of <code>any</code>:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>It is supertype of the N4JS primitive type <code>pathselector</code>, <code>typeName</code> and <code>i18nKey</code>.
<a href="#_primitive-pathselector-and-i18nkey">Primitive Pathselector and I18nKey</a></p>
</div>
<div class="paragraph">
<p>However, variables of type <code>string</code> can be auto-converted (coerced) to <code>string</code>, as described in <a href="#_autoboxing-and-coercing">Autoboxing and Coercing</a>.</p>
</div>
</div>
<div class="sect3">
<h4 id="_primitive-number-type"><a class="anchor" href="#_primitive-number-type"></a><a class="link" href="#_primitive-number-type">4.5.5. Primitive Number Type</a></h4>
<div class="paragraph">
<p>In ECMAScript numbers are usually 64-bit floating point numbers.
For details see [<a href="#ECMA11a">ECMA11a(p.8.5)</a>].
The prefix <code>0</code> indicates that the number is octal-based and the prefix <code>0x</code> marks it as hexadecimal-based.</p>
</div>
<div class="paragraph">
<p><code>NaN</code> can be produced by e.g. &#8216;0 / 0&#8217;' or &#8216;1 - x&#8217;. <code>typeof NaN</code> will return <code>number</code>.</p>
</div>
<h5 id="primitive-number-type-semantics" class="discrete">Semantics</h5>
<div class="paragraph">
<p>The type <code>number</code> is subtype of <code>any</code>:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>However, variables of type <code>number</code> can be auto-converted (coerced) to <code>Number</code>, as described in <a href="#_integer-literals">Integer Literals</a> .</p>
</div>
</div>
<div class="sect3">
<h4 id="_primitive-type-int"><a class="anchor" href="#_primitive-type-int"></a><a class="link" href="#_primitive-type-int">4.5.6. Primitive Type int</a></h4>
<div class="paragraph">
<p>Actually ECMAScript defines an internal type <code>int32</code>.
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 <code>int</code>.
For details on how numeric literals map to types <code>number</code> and <code>int</code>, refer to <a href="#_integer-literals">Integer Literals</a>.</p>
</div>
<div class="admonitionblock important">
<table>
<tr>
<td class="icon">
<i class="fa icon-important" title=""></i>
</td>
<td class="content">
for the time being, built-in type <code>int</code> is synonymous to type <code>number</code>.
This means one can be assigned to the other and a value declared to be of type <code>int</code> may actually be a 64-bit floating
point number.<sup class="footnote">[<a id="_footnoteref_14" class="footnote" href="#_footnote_14" title="View footnote.">14</a>]</sup>
</td>
</tr>
</table>
</div>
</div>
<div class="sect3">
<h4 id="_primitive-symbol-type"><a class="anchor" href="#_primitive-symbol-type"></a><a class="link" href="#_primitive-symbol-type">4.5.7. Primitive Symbol Type</a></h4>
<div class="paragraph">
<p>The primitive type <code>symbol</code> 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 <code>Symbol.iterator</code>.
Other built-in symbols from ECMAScript 6 and the creation of new symbols are not supported.
For more details, see <a href="#_symbol">Symbol</a>.</p>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_primitive-n4js-types"><a class="anchor" href="#_primitive-n4js-types"></a><a class="link" href="#_primitive-n4js-types">4.6. Primitive N4JS Types</a></h3>
<div class="paragraph">
<p>Additionally to the primitive ECMAScript types, the following N4JS-specific primitive types are supported:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>any</code></dt>
<dd>
<p>enables ECMAScript-like untyped variable declarations</p>
</dd>
<dt class="hdlist1"><code>void</code></dt>
<dd>
<p>almost similar to undefined, except it can be used as a return type of functions and methods</p>
</dd>
<dt class="hdlist1"><code>unknown</code></dt>
<dd>
<p>inferred in case of a type inference error</p>
</dd>
<dt class="hdlist1"><code>pathSelector&lt;T&gt;</code>, <code>i18nKey</code></dt>
<dd>
<p>subtypes of string</p>
</dd>
</dl>
</div>
<div class="sect3">
<h4 id="_any-type"><a class="anchor" href="#_any-type"></a><a class="link" href="#_any-type">4.6.1. Any Type</a></h4>
<div class="paragraph">
<p>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 <code>any</code>, but a variable declared <code>any</code> can only be assigned to another variable declared with the type <code>any</code>.</p>
</div>
<div class="sect4">
<h5 id="any-type-semantics"><a class="anchor" href="#any-type-semantics"></a><a class="link" href="#any-type-semantics">4.6.1.1. Semantics</a></h5>
<div class="paragraph">
<p><code>any</code> is supertype of all other types. That is,</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>is an axiom and true for all types.</p>
</div>
</div>
<div class="sect4">
<h5 id="any-type-type-inference"><a class="anchor" href="#any-type-type-inference"></a><a class="link" href="#any-type-type-inference">4.6.1.2. Type Inference</a></h5>
<div class="paragraph">
<p>If a variable is explicitly declared as type <code>any</code>, the inferred type of that variable will always be <code>any</code>.</p>
</div>
<div class="sect5">
<h6 id="_default-type-of-variables"><a class="anchor" href="#_default-type-of-variables"></a><a class="link" href="#_default-type-of-variables">4.6.1.2.1. Default Type of Variables</a></h6>
<div class="paragraph">
<p>If a type annotation is missing and no initializer is provided, then the type of a variable is implicitly set to <code>any</code>.</p>
</div>
<div class="paragraph">
<p>In that case, the inferred type of that variable will always be <code>any</code> 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. <a href="#_variable-statement">Variable Statement</a>).</p>
</div>
<div class="paragraph">
<p>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 <a href="#_identifier">Identifier</a>.</p>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_void-type"><a class="anchor" href="#_void-type"></a><a class="link" href="#_void-type">4.6.2. Void Type</a></h4>
<div class="paragraph">
<p>The type <code>void</code> is used to denote that there is no value at all, as opposed to type
<code>undefined</code> which denotes that there is a value, but it is always undefined.</p>
</div>
<div class="paragraph">
<p>The only valid use of type <code>void</code> is to declare that a function or method does not
return anything. In particular, this means:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>void</code> is disallowed as type argument,</p>
</li>
<li>
<p><code>void</code> is disallowed as upper/lower bound of type parameters and wild cards,</p>
</li>
<li>
<p>when used as return type of functions or methods, <code>void</code> may not be nested, i.e.</p>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">function foo(): void {} // ok
function bar(): any|void {} // error</code></pre>
</div>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>In all the above disallowed cases, type <code>undefined</code> should be used instead of <code>void</code>.</p>
</div>
<div class="sect4">
<h5 id="void-type-semantics"><a class="anchor" href="#void-type-semantics"></a><a class="link" href="#void-type-semantics">4.6.2.1. Semantics</a></h5>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-13"></a><strong>Req. IDE-13:</strong> <a href="#Req-IDE-13">Void Type</a> (ver. 1)</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The type <code>void</code> may only be used as the immediate return type of a function or method.</p>
</li>
<li>
<p>If a function <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi></math> is declared to return <code>void</code>, an error is created if a return statement contains an expression:</p>
<div class="paragraph">
<p><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><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></p>
</div>
</li>
<li>
<p>If a function <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi></math> is declared to return <code>void</code>, an error is issued if the function is called in any statement or expression but an expression statement directly:</p>
<div class="paragraph">
<p><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><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></p>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="paragraph">
<p>The following type hierarchy is defined: <code>void</code> is only a subtype of itself but not of any other type and no other type is a subtype of void.</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>Since <code>void</code> 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).</p>
</div>
<div class="paragraph">
<p>The ECMAScript <code>void</code> operator (see <a href="#_unary-expression">Unary Expressions</a>) has a type
of <code>undefined</code>, not <code>void</code>, because it evaluates to value <code>undefined</code> and can be used
on the right-hand side of assignments, etc.</p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_unknown-type"><a class="anchor" href="#_unknown-type"></a><a class="link" href="#_unknown-type">4.6.3. Unknown Type</a></h4>
<div class="paragraph">
<p>Internally N4JS defines the type <code>unknown</code>.
This type cannot be used by the user.
Instead, it is inferred in case of errors.
<code>unknown</code> behaves almost similar to <code>any+</code>.
However no error messages once a variable or expression has been inferred to <code>unknown</code> in order to avoid consequential errors.</p>
</div>
</div>
<div class="sect3">
<h4 id="_primitive-pathselector-and-i18nkey"><a class="anchor" href="#_primitive-pathselector-and-i18nkey"></a><a class="link" href="#_primitive-pathselector-and-i18nkey">4.6.4. Primitive Pathselector and I18nKey</a></h4>
<div class="paragraph">
<p>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.</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>pathSelector&lt;T&gt;</code> 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.</p>
</li>
<li>
<p>The type variable <code>T</code> 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</p>
</li>
<li>
<p><code>i18nKey</code> is a string which refers to an internationalization key.
The <code>i18nKey</code> 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 <code>i18nKey</code> 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 <code>i18nKey</code> type defines a subset of all string literals that can be assigned to a variable of type <code>i18nKey</code> in the current project.
That means that an assignment of a string literal to a variable of type <code>i18nKey</code> is only valid if that string literal is contained in the set defined by <code>i18nKey</code>.
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.</p>
</li>
</ul>
</div>
<div class="paragraph todo">
<p>I18nkeys are not yet validated</p>
</div>
<div class="sect4">
<h5 id="pathselector-semantics"><a class="anchor" href="#pathselector-semantics"></a><a class="link" href="#pathselector-semantics">4.6.4.1. Semantics</a></h5>
<div class="paragraph">
<p>The N4JS primitive types <code>pathSelector&lt;T&gt;</code>, <code>i18nKey</code> and <code>pathSelector&lt;T&gt;</code> are basically only marker types of strings for enabling additional validation.
Thus, they are completely interchangeable with string types:</p>
</div>
<div class="paragraph">
<p><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><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><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></p>
</div>
<div class="paragraph">
<p>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, <code>"some.path."segment".prop"</code> cannot be evaluated at compile time.</p>
</div>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_built-in-ecmascript-object-types"><a class="anchor" href="#_built-in-ecmascript-object-types"></a><a class="link" href="#_built-in-ecmascript-object-types">4.7. Built-in ECMAScript Object Types</a></h3>
<div class="paragraph">
<p>N4JS supports all built-in ECMAScript objects [<a href="#ECMA11a">ECMA11a(p.S15)</a>], 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.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>Object</code> [<a href="#ECMA11a">ECMA11a(p.p.111)</a>];</p>
</li>
<li>
<p><code>Function</code> [<a href="#ECMA11a">ECMA11a(p.p.117)</a>]; representing functions and function objects <a href="#_function-type">Function Type</a> but also methods (<a href="#_methods">Methods</a>)</p>
</li>
<li>
<p><code>Array</code> [<a href="#ECMA11a">ECMA11a(p.p.122)</a>], representing array objects, see <a href="#_array-object-type">Array Object Type</a></p>
</li>
<li>
<p><code>String</code> [<a href="#ECMA11a">ECMA11a(p.p.141)</a>]</p>
</li>
<li>
<p><code>Boolean</code> [<a href="#ECMA11a">ECMA11a(p.p.141)</a>]</p>
</li>
<li>
<p><code>Number</code> [<a href="#ECMA11a">ECMA11a(p.p.141)</a>]</p>
</li>
<li>
<p><code>RegExp</code> [<a href="#ECMA11a">ECMA11a(p.p.180)</a>]; they can be constructed by means of special literals (cf. <a href="#_literals">Literals</a>)</p>
</li>
<li>
<p>global object type</p>
</li>
<li>
<p><code>Symbol</code></p>
</li>
<li>
<p><code>Promise</code></p>
</li>
<li>
<p><code>Iterator</code> and <code>Iterable</code></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>All other ECMAScript types ([<a href="#ECMA11a">ECMA11a(p.S15)</a>], such as <code>Math</code>, <code>Date</code>, or <code>Error</code> 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 <code>Object</code>.</p>
</div>
<div class="sect3">
<h4 id="ECMAScript-objects-semantics"><a class="anchor" href="#ECMAScript-objects-semantics"></a><a class="link" href="#ECMAScript-objects-semantics">4.7.1. Semantics</a></h4>
<div class="paragraph">
<p>It is not possible to inherit from any of the built-in ECMAScript object types except for <code>Object</code> and <code>Error</code>, 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.</p>
</div>
</div>
<div class="sect3">
<h4 id="_object-type"><a class="anchor" href="#_object-type"></a><a class="link" href="#_object-type">4.7.2. Object Type</a></h4>
<div class="paragraph">
<p><code>Object</code> [<a href="#ECMA11a">ECMA11a(p.S8.6)</a>] is the (implicit) supertype of all declared (i.e., non-primtive) types, including native types.
It models the ECMAScript type <code>Object</code>, 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 <code>Object+</code> has to be declared
(cf. <a href="#_type-modifiers">Type Modifiers</a>).</p>
</div>
</div>
<div class="sect3">
<h4 id="_function-object-type"><a class="anchor" href="#_function-object-type"></a><a class="link" href="#_function-object-type">4.7.3. Function-Object-Type</a></h4>
<div class="paragraph">
<p>The built-in object type <code>Function</code>, a subtype of <code>Object</code>, represents all functions, regardless of how they are defined (either via function expression,
function declaration, or method declaration).
They are described in detail in <a href="#_function-object-type">Function-Object-Type</a>.</p>
</div>
<div class="paragraph">
<p>Since <code>Function</code> 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 <code>Function</code>.
For the time being, however, an instance of <code>Function</code> 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.  <a href="#Req-IDE-101">[Req-IDE-101]</a> and <a href="#Req-IDE-102">[Req-IDE-102]</a> do not apply.</p>
</div>
</div>
<div class="sect3">
<h4 id="_array-object-type"><a class="anchor" href="#_array-object-type"></a><a class="link" href="#_array-object-type">4.7.4. Array Object Type</a></h4>
<div class="paragraph">
<p>The <code>Array</code> 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 <code>Number</code>.
The type of the stored values is <em>typeArgs[0]</em> (cf. <a href="#_array-literal">Array Literal</a>). 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.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-14"></a><strong>Req. IDE-14:</strong> <a href="#Req-IDE-14">Array Type</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For an array type <em>A</em>, the following conditions must be true:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><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></p>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_string-object-type"><a class="anchor" href="#_string-object-type"></a><a class="link" href="#_string-object-type">4.7.5. String Object Type</a></h4>
<div class="paragraph">
<p>Object type version of <code>string</code>. 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.</p>
</div>
</div>
<div class="sect3">
<h4 id="_boolean-object-type"><a class="anchor" href="#_boolean-object-type"></a><a class="link" href="#_boolean-object-type">4.7.6. Boolean Object Type</a></h4>
<div class="paragraph">
<p>Object type version of <code>boolean</code>. 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.</p>
</div>
</div>
<div class="sect3">
<h4 id="_number-object-type"><a class="anchor" href="#_number-object-type"></a><a class="link" href="#_number-object-type">4.7.7. Number Object Type</a></h4>
<div class="paragraph">
<p>Object type version of <code>number</code>. 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.</p>
</div>
</div>
<div class="sect3">
<h4 id="_global-object-type"><a class="anchor" href="#_global-object-type"></a><a class="link" href="#_global-object-type">4.7.8. Global Object Type</a></h4>
<div class="paragraph">
<p>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.</p>
</div>
</div>
<div class="sect3">
<h4 id="_symbol"><a class="anchor" href="#_symbol"></a><a class="link" href="#_symbol">4.7.9. Symbol</a></h4>
<div class="paragraph">
<p>The symbol constructor function of ECMAScript 2015. Support for symbols
is kept to a minimum in N4JS:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>creating symbols with <code>var sym = Symbol("description")</code> is not supported.</p>
</li>
<li>
<p>creating shared symbols with <code>var sym = Symbol.for("key")</code> is not supported.
Also the inverse <code>Symbol.keyFor(sym)</code> is not supported.</p>
</li>
<li>
<p>retrieving built-in symbols via properties in <code>Symbol</code> is supported, however, the only built-in symbol available in N4JS is the iterator symbol that can be retrieved with <code>Symbol.iterator</code>.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>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 <code>for&#8230;&#8203;of</code>
loop with as little support for symbols as possible.</p>
</div>
</div>
<div class="sect3">
<h4 id="_promise"><a class="anchor" href="#_promise"></a><a class="link" href="#_promise">4.7.10. Promise</a></h4>
<div class="paragraph">
<p><code>Promise</code> is provided as a built-in type as in ECMAScript 2015.
Also see <a href="#_asynchronous-functions">Asynchronous Functions</a> for asynchronous functions.</p>
</div>
</div>
<div class="sect3">
<h4 id="_iterator-interface"><a class="anchor" href="#_iterator-interface"></a><a class="link" href="#_iterator-interface">4.7.11. Iterator Interface</a></h4>
<div class="paragraph">
<p>A structurally typed interface for <em>iterators</em> as defined by theECMAScript 6 iterator protocol.</p>
</div>
<div class="listingblock">
<div class="title">Iterable in N4JS</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">// 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?;
}</code></pre>
</div>
</div>
<div class="paragraph todo">
<p>Interface <code>IteratorEntry</code> was introduced mainly as a workaround; this interface could be removed and replaced with a corresponding
structural type reference as return type of method <code>next()</code></p>
</div>
</div>
<div class="sect3">
<h4 id="_iterable-interface"><a class="anchor" href="#_iterable-interface"></a><a class="link" href="#_iterable-interface">4.7.12. Iterable Interface</a></h4>
<div class="paragraph">
<p>A structurally typed interface for objects that can be iterated over, i.e. <em>iterables</em> as defined by the ECMAScript 6 iterator protocol.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">// providedByRuntime
export public interface ~Iterable&lt;T&gt; {
public [Symbol.iterator](): Iterator&lt;T&gt;
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_built-in-n4js-types"><a class="anchor" href="#_built-in-n4js-types"></a><a class="link" href="#_built-in-n4js-types">4.8. Built-In N4JS Types</a></h3>
<div class="paragraph">
<p>N4JS additionally provides some built-in classes which are always available with the need to explicitly import them.</p>
</div>
<div class="sect3">
<h4 id="_n4object"><a class="anchor" href="#_n4object"></a><a class="link" href="#_n4object">4.8.1. N4Object</a></h4>
<div class="paragraph">
<p>Although <code>N4Object</code> is a built-in type, it is not the default supertype.
It is a subtype of <code>Object</code>.</p>
</div>
<div class="sect4">
<h5 id="N4Object-semantics"><a class="anchor" href="#N4Object-semantics"></a><a class="link" href="#N4Object-semantics">4.8.1.1. Semantics</a></h5>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_n4class"><a class="anchor" href="#_n4class"></a><a class="link" href="#_n4class">4.8.2. N4Class</a></h4>
<div class="paragraph">
<p>The type <code>N4Class</code> is used for extended reflection in N4JS.</p>
</div>
<div class="paragraph todo">
<p>Add further docs for this type</p>
</div>
</div>
<div class="sect3">
<h4 id="IterableN"><a class="anchor" href="#IterableN"></a><a class="link" href="#IterableN">4.8.3. IterableN</a></h4>
<div class="paragraph todo">
<p>Work in progress.</p>
</div>
<div class="paragraph">
<p>Currently there are built-in types <code>Iterable2&lt;T1,T2&gt;</code>&#8230;&#8203;<code>Iterable9&lt;T1,&#8230;&#8203;,T9&gt;</code>.
They are mainly intended for type system support of array destructuring literals.</p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
This is not documented in detail yet, because we want to gain experience with the current solution first, major refinement may be incoming.
</td>
</tr>
</table>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_type-modifiers"><a class="anchor" href="#_type-modifiers"></a><a class="link" href="#_type-modifiers">4.9. Type Modifiers</a></h3>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="sect3">
<h4 id="Type_Modifiers_Dynamic"><a class="anchor" href="#Type_Modifiers_Dynamic"></a><a class="link" href="#Type_Modifiers_Dynamic">4.9.1. Dynamic</a></h4>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p><code>T</code> <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> <code>T+</code> and <code>T+</code> <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> <code>T</code> is always true.
Using dynamically added members of a dynamic type is never type safe.
Using the <code>delete</code> operator on a subtype of <code>N4Object</code> is not allowed.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-15"></a><strong>Req. IDE-15:</strong> <a href="#Req-IDE-15">Non-Dynamic Primitive Types</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>All primitive types except <code>any</code> must not be declared dynamic.</p>
</li>
<li>
<p>Only parameterized type references and this type reference can be declared dynamic.<sup class="footnote">[<a id="_footnoteref_15" class="footnote" href="#_footnote_15" title="View footnote.">15</a>]</sup></p>
</li>
</ol>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_optional-return-types"><a class="anchor" href="#_optional-return-types"></a><a class="link" href="#_optional-return-types">4.9.2. Optional Return Types</a></h4>
<div class="paragraph">
<p>Only formal parameters and return types can be marked as optional.</p>
</div>
<div class="paragraph">
<p>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 <code>undefined</code>.
For constraints on using the optional modifier, see <a href="#_function-object-type">Function-Object-Type</a>.</p>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_union-and-intersection-type-composed-types"><a class="anchor" href="#_union-and-intersection-type-composed-types"></a><a class="link" href="#_union-and-intersection-type-composed-types">4.10. Union and Intersection Type (Composed Types)</a></h3>
<div class="paragraph">
<p>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.</p>
</div>
<div class="exampleblock">
<div class="title">Example 17. Composed types</div>
<div class="content">
<div class="paragraph">
<p>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.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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;</code></pre>
</div>
</div>
<div class="paragraph">
<p>Note that no instance <code>aib</code> of intersection type <code>A&amp;B</code> can be instantiated, since the instance&#8217;s class would have to define a field <code>f</code> which would have to comply to both of the interfaces <code>A</code> and <code>B</code>.
Still the function <code>i()</code> shows in general how variables of intersection types can be casted and accessed.</p>
</div>
</div>
</div>
<div class="paragraph">
<p>The following sections define these <em>union</em> and <em>intersection types</em> in detail.</p>
</div>
<div class="sect3">
<h4 id="_union-type"><a class="anchor" href="#_union-type"></a><a class="link" href="#_union-type">4.10.1. Union Type</a></h4>
<div class="paragraph">
<p>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.
<em>The most common use case for union types is for emulating method overloading</em>, as
we describe later on.<sup class="footnote">[<a id="_footnoteref_16" class="footnote" href="#_footnote_16" title="View footnote.">16</a>]</sup></p>
</div>
<div class="sect4">
<h5 id="union-type-syntax"><a class="anchor" href="#union-type-syntax"></a><a class="link" href="#union-type-syntax">4.10.1.1. Syntax</a></h5>
<div class="paragraph">
<p>For convenience, we repeat the definition of union type expression:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">UnionTypeExpression: 'union' '{' typeRefs+=TypeRefWithoutModifiers (',' typeRefs+=TypeRefWithoutModifiers)* '}';</code></pre>
</div>
</div>
</div>
<div class="sect4">
<h5 id="union-type-semantics"><a class="anchor" href="#union-type-semantics"></a><a class="link" href="#union-type-semantics">4.10.1.2. Semantics</a></h5>
<div class="paragraph">
<p>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.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-18"></a><strong>Req. IDE-18:</strong> <a href="#Req-IDE-18">Union Type</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Non-empty: At least one element has to be specified:<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></p>
</li>
<li>
<p>Non-dynamic: The union type itself must not be declared dynamic:<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></p>
</li>
<li>
<p>Non-optional elements:<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></p>
</li>
</ol>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-19"></a><strong>Req. IDE-19:</strong> <a href="#Req-IDE-19">Union Type Subtyping Rules</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>Let <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>U</mi></math> be an union type.</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The union type is a common supertype of all its element types:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>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:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>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 [<a href="#Igarashi07a">Igarashi07a(p.p.40)</a>]</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>Commutativity: The order of element does not matter:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>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></p>
</li>
<li>
<p>Uniqueness of elements: A union type may not contain duplicates
(similar to sets):</p>
<div class="paragraph">
<p><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></p>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-20"></a><strong>Req. IDE-20:</strong> <a href="#Req-IDE-20">Implicit simplification of union types</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Simplification of union type with one element:
If a union type contains only one element, it is reduced to the element:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>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>:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>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:</p>
<div class="paragraph">
<p><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><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></p>
</div>
</li>
</ul>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
Simplification rules for union types with one element are applied first.
</td>
</tr>
</table>
</div>
<div class="ulist">
<ul>
<li>
<p>The structural typing strategy is propagated to the types of the union:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="paragraph">
<p>Remarks:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The simplification rules may be applied recursively.</p>
</li>
<li>
<p>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>.<sup class="footnote">[<a id="_footnoteref_17" class="footnote" href="#_footnote_17" title="View footnote.">17</a>]</sup></p>
</li>
</ul>
</div>
<div class="exampleblock">
<div class="title">Example 18. Subtyping with union type</div>
<div class="content">
<div class="paragraph">
<p>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>)</p>
</div>
<div class="paragraph">
<p>The following subtyping relations with union types are to be evaluated as follows: <sup class="footnote">[<a id="_footnoteref_18" class="footnote" href="#_footnote_18" title="View footnote.">18</a>]</sup></p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>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</p>
</div>
<div class="exampleblock">
<div class="title">Example 19. Superfluous elements in union type</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class A{}
class B extends A{}
class C extends A{}
function foo(p: union{A,B}) {..}</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>Although <code>B</code> is superfluous, it may indicate that the function handles parameters of type differently than one of type <code>A</code> or <code>C</code>.</p>
</div>
<div class="paragraph">
<p>Although a union type is a <code><a href="#_acronyms">LCST</a></code> 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.</p>
</div>
<div class="paragraph">
<p>For the definition of join and meet for union types, we define how a type is added to a union type:</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-21"></a><strong>Req. IDE-21:</strong> <a href="#Req-IDE-21">Union of union type</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>The union of union types is defined similar to the union of sets.
The union is not simplified, but it contains no duplicates.</p>
</div>
<div class="paragraph">
<p>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 <code><a href="#_acronyms">LCST</a></code> of both types.
This finding is the foundation of the definition of join of a (non-union) type with a union type:</p>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-22"></a><strong>Req. IDE-22:</strong> <a href="#Req-IDE-22">Join with Union Type</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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 <em>T</em> is the union of both types:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>Remarks:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Joining a union type with another type is not similar to joining the elements of the union type directly with another type.
That is</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>The computed join is simplified according to the constraints defined above.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-23"></a><strong>Req. IDE-23:</strong> <a href="#Req-IDE-23">Meet with Union Type</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>The meet of union types is defined as the meet of the elements.
That is</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>Remarks:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>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 <em>A</em>).
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.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-24"></a><strong>Req. IDE-24:</strong> <a href="#Req-IDE-24">Upper and Lower Bound of a Union Type</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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>:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_warnings"><a class="anchor" href="#_warnings"></a><a class="link" href="#_warnings">4.10.1.3. Warnings</a></h5>
<div class="paragraph">
<p>In case the <code>any</code> type is used in a union type, all other types in the union type definition become obsolete.
However, defining other typers along with the <code>any</code> 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 <code>any</code> 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.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-25"></a><strong>Req. IDE-25:</strong> <a href="#Req-IDE-25">Any type in union types</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>No union type shall conatin an type:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>Similar to the documentary purpose of using specific classes along with the <code>any</code> 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.</p>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-26"></a><strong>Req. IDE-26:</strong> <a href="#Req-IDE-26">Redundant subtypes in union types</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_intersection-type"><a class="anchor" href="#_intersection-type"></a><a class="link" href="#_intersection-type">4.10.2. Intersection Type</a></h4>
<div class="paragraph">
<p>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.<sup class="footnote">[<a id="_footnoteref_19" class="footnote" href="#_footnote_19" title="View footnote.">19</a>]</sup></p>
</div>
<div class="sect4">
<h5 id="intersection-type-syntax"><a class="anchor" href="#intersection-type-syntax"></a><a class="link" href="#intersection-type-syntax">4.10.2.1. Syntax</a></h5>
<div class="paragraph">
<p>For convenience, we repeat the definition of intersection type expression and of type variables in which intersection types can be defined as in Java:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">InterSectionTypeExpression: 'intersection' '{' typeRefs+=TypeRefWithoutModifiers (',' typeRefs+=TypeRefWithoutModifiers)* '}';
TypeVariable: name=IDENTIFIER ('extends' declaredUpperBounds+=ParameterizedTypeRefNominal ('&amp;' declaredUpperBounds+=ParameterizedTypeRefNominal)*)?</code></pre>
</div>
</div>
</div>
<div class="sect4">
<h5 id="intersection-type-semantics"><a class="anchor" href="#intersection-type-semantics"></a><a class="link" href="#intersection-type-semantics">4.10.2.2. Semantics</a></h5>
<div class="paragraph">
<p>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.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-27"></a><strong>Req. IDE-27:</strong> <a href="#Req-IDE-27">Intersection Type</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a given intersection type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>I</mi></math>, the following conditions must hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The intersection must contain at least one type:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>Only one nominally typed class must be contained in the intersection type:</p>
<div class="openblock">
<div class="content">
<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><mo></mo><mi>C</mi><mo>.</mo><mi>i</mi><mi>s</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></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><mo></mo><mi>T</mi><mo>.</mo><mi>i</mi><mi>s</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>
</div>
</div>
<div class="paragraph">
<p>A warning is produced when more than one nominal class is contained in the intersection type, since
only undefined (or null) can be assigned to a type reference of this type.</p>
</div>
</li>
<li>
<p>Non-optional elements:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>If the intersection contains multiple references to the same generic type, a warning is produced if only undefined (or null) can be assigned to a type reference of this type. There are some rare cases in which this does not happen. This is true if for all type arguments one of the following conditions hold:</p>
<div class="ulist">
<ul>
<li>
<p>a type argument corresponding to a type parameter without def-site variance is a wildcard with an upper bound (use "extends" or no bound) or a type argument not defining an upper bound corresponds to a covariant (out) type parameter, and this constraint (IDE-27) holds for an intersection created from the upper bounds of the type argument (or the lower bound of the type parameter).</p>
</li>
<li>
<p>a type argument is a wildcard with lower bounds (since Object would be a solution)</p>
</li>
</ul>
</div>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>Definition of structural typing attributes see <a href="#Req-ID-78701">[Req-ID-78701]</a>.</p>
</div>
<div class="paragraph">
<p>The combination of intersection types and generics is a bit tricky. The following example demonstrates that:</p>
</div>
<div class="exampleblock">
<div class="title">Example 20. Intersection and generics</div>
<div class="content">
<div class="paragraph">
<p>Given the following types:</p>
</div>
<div class="listingblock">
<div class="content">
<pre>class G&lt;T&gt; {
private T: t
set(t: T) { this.t = t;}
get(): T { return this.t; }
}
class C { public y; }
class D { public x; }
interface I {}</pre>
</div>
</div>
<div class="paragraph">
<p>We use the generic with the getter and setter here only to demonstrate co- and contra variance effects.</p>
</div>
<div class="paragraph">
<p>Let</p>
</div>
<div class="listingblock">
<div class="content">
<pre>let g1: G&lt;C&gt; &amp; G&lt;D&gt;;</pre>
</div>
</div>
<div class="paragraph">
<p>be a variable. We can only assign undefined to g1, since any other value would not be confirm to the intersection.
If we for example would assign</p>
</div>
<div class="listingblock">
<div class="content">
<pre>let gc = new G&lt;C&gt;()
g1 = gc;</pre>
</div>
</div>
<div class="paragraph">
<p>we would run into contra-variance problems:</p>
</div>
<div class="listingblock">
<div class="content">
<pre>gc.set(new C());</pre>
</div>
</div>
<div class="paragraph">
<p>This would implicitly also set a <code>C</code> in <code>g1</code>, which would not be compatible with <code>D</code>. This would lead to a problem in the following lines:</p>
</div>
<div class="listingblock">
<div class="content">
<pre>let gd: G&lt;D&gt; = g1;
let d: D = gd.get();</pre>
</div>
</div>
<div class="paragraph">
<p>This is the typical contra variance problem.</p>
</div>
</div>
</div>
<div class="paragraph">
<p>Similar problems arise even with structural types.</p>
</div>
<div class="paragraph">
<p>Note that in theory more warnings could be produced, in particular in combination with
structural types (and the fact that N4JS classes must explicitly implement even
structural interfaces). We omit these kind of warnings for two reasons:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>performance</p>
</li>
<li>
<p>anticipated slight changes in semantics (e.g. we may remove the requirement of explicitly implementing structural interfaces)</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Since problems caused by not instanceable type references will be detected by programmers before runtime anyway, we do not need to be strict here. They are merely convenience features and they do not affect the correctness of the type system.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-175"></a><strong>Req. IDE-175:</strong> <a href="#Req-IDE-175">Intersection Type Subtyping Rules</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>Let <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>I</mi></math> be an intersection type.</p>
</div>
<div class="ulist">
<ul>
<li>
<p>An intersection type is a subtype of another type, if at least one of
its contained types is a subtype of that type: <sup class="footnote">[<a id="_footnoteref_20" class="footnote" href="#_footnote_20" title="View footnote.">20</a>]</sup></p>
</li>
</ul>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="ulist">
<ul>
<li>
<p>A type is a subtype of an intersection type, if it is a subtype of all
types contained in the intersection type: <sup class="footnote">[<a id="_footnoteref_21" class="footnote" href="#_footnote_21" title="View footnote.">21</a>]</sup></p>
</li>
</ul>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="ulist">
<ul>
<li>
<p>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></p>
</li>
</ul>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-28"></a><strong>Req. IDE-28:</strong> <a href="#Req-IDE-28">Implicit simplification of intersection types</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The structural typing strategy is propagated to the types of the intersection:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>These subtyping rules are similar to Ceylon. <sup class="footnote">[<a id="_footnoteref_22" class="footnote" href="#_footnote_22" title="View footnote.">22</a>]</sup></p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="exampleblock">
<div class="title">Example 21. Subtyping with intersection type</div>
<div class="content">
<div class="paragraph">
<p>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>)</p>
</div>
<div class="paragraph">
<p>The following subtyping relations with intersection types are to be
evaluated as follows: <sup class="footnote">[<a id="_footnoteref_23" class="footnote" href="#_footnote_23" title="View footnote.">23</a>]</sup></p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>The join of intersection types is defined as the join of the elements.
That is:</p>
</div>
<div class="paragraph">
<p><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><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></p>
</div>
<div class="openblock">
<div class="content">
</div>
</div>
<div class="paragraph">
<p>The meet of intersection types is defined over their elements.
That is:</p>
</div>
<div class="paragraph">
<p><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><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></p>
</div>
<div class="openblock">
<div class="content">
</div>
</div>
<div class="paragraph">
<p>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>:</p>
</div>
<div class="paragraph">
<p><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><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></p>
</div>
<div class="openblock">
<div class="content">
</div>
</div>
</div>
<div class="sect4">
<h5 id="_warnings-2"><a class="anchor" href="#_warnings-2"></a><a class="link" href="#_warnings-2">4.10.2.3. Warnings</a></h5>
<div class="paragraph">
<p>Using <code>any</code> types in intersection types is obsolete since they do not change the resulting intersection type.
E.g. the intersection type of A, B and <code>any</code> is equivialent to the intersection type of A and B.
However, using the <code>any</code> type is no error because it can be seen as a neutral argument to the intersection.
Nevertheless the use of the <code>any</code> type produces a warning, since its use can indicate a misunderstanding of the intersection type concept and since it always can be omitted.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-32"></a><strong>Req. IDE-32:</strong> <a href="#Req-IDE-32">Any type in intersection types</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>No intersection type shall contain an type:</p>
</div>
<div class="paragraph">
<p><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></p>
</div>
</div>
</div>
<div class="paragraph">
<p>The use of the <code>any</code> 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.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-33"></a><strong>Req. IDE-33:</strong> <a href="#Req-IDE-33">Redundant supertypes in intersection types</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_composed-types-in-wildcards"><a class="anchor" href="#_composed-types-in-wildcards"></a><a class="link" href="#_composed-types-in-wildcards">4.10.3. Composed Types in Wildcards</a></h4>
<div class="paragraph">
<p>Composed types may appear as the bound of a wildcard.
The following constraints apply: <sup class="footnote">[<a id="_footnoteref_24" class="footnote" href="#_footnote_24" title="View footnote.">24</a>]</sup></p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-34"></a><strong>Req. IDE-34:</strong> <a href="#Req-IDE-34">Composed Types as Bound of a Wildcard</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>A composed type may appear as the upper or lower bound of a wildcard.
In the covariant case, the following subtype relations apply:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-ebnf" data-lang="ebnf">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; }</code></pre>
</div>
</div>
<div class="paragraph">
<p>In the contra variant case, the following subtype relations apply:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-ebnf" data-lang="ebnf">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; }</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_property-access-for-composed-types"><a class="anchor" href="#_property-access-for-composed-types"></a><a class="link" href="#_property-access-for-composed-types">4.10.4. Property Access for Composed Types</a></h4>
<div class="paragraph">
<p>It is possible to directly access properties of union and intersection types.
The following sections define which properties are accessible.</p>
</div>
<div class="sect4">
<h5 id="_properties-of-union-type"><a class="anchor" href="#_properties-of-union-type"></a><a class="link" href="#_properties-of-union-type">4.10.4.1. Properties of Union Type</a></h5>
<div class="paragraph">
<p>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.</p>
</div>
<hr>
<div class="paragraph">
<p>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:</p>
</div>
<div class="paragraph">
<p><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></p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p><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></p>
</div>
<div class="paragraph">
<p><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><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><br></p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p><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><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><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><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><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></p>
</div>
<hr>
<div class="paragraph">
<p>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.</p>
</div>
<table class="tableblock frame-all grid-all spread">
<caption class="title">Table 4. Merged Members of Unions</caption>
<colgroup>
<col style="width: 20%;">
<col style="width: 20%;">
<col style="width: 20%;">
<col style="width: 20%;">
<col style="width: 20%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-center valign-top">Members</th>
<th class="tableblock halign-center valign-top" colspan="2">S=T</th>
<th class="tableblock halign-center valign-top" colspan="2">S≠T</th>
</tr>
</thead>
<tbody>
<tr>
<th class="tableblock halign-center valign-top"></th>
<th class="tableblock halign-center valign-top"><p class="tableblock">R</p></th>
<th class="tableblock halign-center valign-top"><p class="tableblock">W</p></th>
<th class="tableblock halign-center valign-top"><p class="tableblock">R</p></th>
<th class="tableblock halign-center valign-top"><p class="tableblock">W</p></th>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">field:S | field:T</p></th>
<td class="tableblock halign-center valign-top" colspan="2"><p class="tableblock">field:S</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">getter:S|T</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">setter:S&amp;T</p></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">getter:S | getter:T</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock">getter:S</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">-</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">getter:S|T</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">-</p></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">setter:S | setter:T</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock">-</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">setter:S</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">-</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">setter:S&amp;T</p></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">field:S | getter:T</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock">getter:S</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">-</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">getter:S|T</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">-</p></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">field:S | setter:T</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock">-</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">setter:S</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">-</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">setter:S&amp;T</p></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">getter:S | setter:T</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock">-</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">-</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">-</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">-</p></td>
</tr>
</tbody>
</table>
<div class="sect5">
<h6 id="_remarks-on-union-type-s-members"><a class="anchor" href="#_remarks-on-union-type-s-members"></a><a class="link" href="#_remarks-on-union-type-s-members">4.10.4.1.1. Remarks on union type’s members:</a></h6>
<div class="ulist">
<ul>
<li>
<p>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.</p>
</li>
<li>
<p>The return type of a composed getter is the <em>union</em> type of the return types of the merged getters.</p>
</li>
<li>
<p>The type of a composed setter is the <em>intersection</em> type of the types of the merged setters.</p>
</li>
<li>
<p>Fields can be combined with getters and/or setters:</p>
<div class="ulist">
<ul>
<li>
<p>fields combined with getters allow read-access.</p>
</li>
<li>
<p>non-const fields combined with setters allow write-access.</p>
</li>
<li>
<p>non-const fields combined with getters <em>and</em> 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.</p>
<div class="paragraph">
<p>Again, types need not be identical; for read-access the <em>union</em> of the fields’ types and the getters’ return types is formed, for write-access the <em>intersection</em> 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.</p>
</div>
</li>
</ul>
</div>
</li>
<li>
<p>The name of a method’s parameter is only used for error or warning messages and cannot be referenced otherwise.</p>
</li>
<li>
<p>The return type of a composed method is the <em>union</em> type of the return types of the merged methods.</p>
</li>
<li>
<p>A composed method parameter’s type is the <em>intersection</em> type of the merged parameters types.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_properties-of-intersection-type"><a class="anchor" href="#_properties-of-intersection-type"></a><a class="link" href="#_properties-of-intersection-type">4.10.4.2. Properties of Intersection Type</a></h5>
<div class="paragraph">
<p>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.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-36"></a><strong>Req. IDE-36:</strong> <a href="#Req-IDE-36">Members of an Intersection Type</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="paragraph">
<p><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></p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div style="page-break-after: always;"></div>
<div class="paragraph">
<p><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>:</p>
</div>
<div class="paragraph">
<p><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></p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p><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><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><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><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><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></p>
</div>
</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<table class="tableblock frame-all grid-all spread">
<caption class="title">Table 5. Merged Members of Intersections</caption>
<colgroup>
<col style="width: 20%;">
<col style="width: 20%;">
<col style="width: 20%;">
<col style="width: 20%;">
<col style="width: 20%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-center valign-top">Members</th>
<th class="tableblock halign-center valign-top" colspan="2">S=T</th>
<th class="tableblock halign-center valign-top" colspan="2">S≠T</th>
</tr>
</thead>
<tbody>
<tr>
<th class="tableblock halign-center valign-top"></th>
<th class="tableblock halign-center valign-top"><p class="tableblock">R</p></th>
<th class="tableblock halign-center valign-top"><p class="tableblock">W</p></th>
<th class="tableblock halign-center valign-top"><p class="tableblock">R</p></th>
<th class="tableblock halign-center valign-top"><p class="tableblock">W</p></th>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">field:S &amp; field:T</p></th>
<td class="tableblock halign-center valign-top" colspan="2"><p class="tableblock">field:S</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">getter:S&amp;T</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">setter:S|T</p></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">getter:S &amp; getter:T</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock">getter:S</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">-</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">getter:S&amp;T</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">-</p></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">setter:S &amp; setter:T</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock">-</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">setter:S</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">-</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">setter:S|T</p></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">field:S &amp; getter:T</p></th>
<td class="tableblock halign-center valign-top" colspan="2"><p class="tableblock">field:S</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">getter:S&amp;T</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">setter:S</p></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">field:S &amp; setter:T</p></th>
<td class="tableblock halign-center valign-top" colspan="2"><p class="tableblock">field:S</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">getter:S</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">setter:S|T</p></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">getter:S &amp; setter:T</p></th>
<td class="tableblock halign-center valign-top" colspan="2"><p class="tableblock">field:S</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">getter:S</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">setter:T</p></td>
</tr>
</tbody>
</table>
<div class="sect5">
<h6 id="_remarks-on-intersection-type-s-methods"><a class="anchor" href="#_remarks-on-intersection-type-s-methods"></a><a class="link" href="#_remarks-on-intersection-type-s-methods">4.10.4.2.1. Remarks on intersection type’s methods:</a></h6>
<div class="ulist">
<ul>
<li>
<p>The name of a method’s parameter is only used for error or warning messages and cannot be referenced otherwise.</p>
</li>
<li>
<p>The return type of a method is the <em>intersection</em> type of the return types of the merged methods.</p>
</li>
<li>
<p>A method parameter’s type is the <em>union</em> type of the merged parameters types.</p>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_constructor-and-classifier-type"><a class="anchor" href="#_constructor-and-classifier-type"></a><a class="link" href="#_constructor-and-classifier-type">4.11. Constructor and Classifier Type</a></h3>
<div class="paragraph">
<p>A class definition as described in <a href="#_classes">Classes</a> 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.<sup class="footnote">[<a id="_footnoteref_25" class="footnote" href="#_footnote_25" title="View footnote.">25</a>]</sup>
The constructor is basically the classifier type with the additional possibility to call it via <code>new</code> in order to create new instances of the declared type.</p>
</div>
<div class="paragraph">
<p>Both <code>meta</code> types are different from Java’s type <code>Class&lt;T&gt;</code>, 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 [<a href="#ECMA15a">ECMA15a(p.14.5)</a>].</p>
</div>
<div class="sect3">
<h4 id="_syntax-3"><a class="anchor" href="#_syntax-3"></a><a class="link" href="#_syntax-3">4.11.1. Syntax</a></h4>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">ConstructorTypeRef returns ConstructorTypeRef: 'constructor' '{' typeArg = [TypeArgument] '}';
ClassifierTypeRef returns ClassifierTypeRef: 'type' '{' typeArg = [TypeRef] '}';</code></pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_semantics-2"><a class="anchor" href="#_semantics-2"></a><a class="link" href="#_semantics-2">4.11.2. Semantics</a></h4>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Static members of a type <em>T</em> are actually members of the classifier type <code>type{T}</code>.</p>
</li>
<li>
<p>The keyword <code>this</code> in a static method of a type <em>T</em> actually binds to the classifier type <code>type{T}</code>.</p>
</li>
<li>
<p>The constructor type <code>constructor</code><em>{T}</em> is a subtype of the classifier type <code>type{T}</code>:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>If a class <em>B</em> is a subtype (subclass) of a class <em>A</em>, then the classifier type <code>type{B}</code> also is a subtype of <code>type{A}</code>:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>If a class <em>B</em> is a subtype (subclass) of a class <em>A</em>, and if the constructor function of <em>B</em> is a subtype of the constructor function of <em>A</em>, then the
classifier type <code>constructor{B}</code> also is a subtype of <code>constructor{A}</code> :</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>The subtype relation of the constructor function is defined in <a href="#_function-type">Function Type</a>.
In the case of the default <code>N4Object</code> constructor, the type of the object literal argument depends on required attributes.</p>
</div>
<div class="paragraph">
<p>This subtype relation for the constructor type is enforced if the constructor of the super class is marked as <code>final</code>, see <a href="#_constructor-and-classifier-type">Constructor and Classifier Type</a> for details.</p>
</div>
</li>
<li>
<p>The type of a classifier declaration or classifier expression is the constructor of that class:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>A class cannot be called as a function in ECMAScript.
Thus, the constructor and type type are only subtype of <code>Object</code>:</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>T</mi><mi>:</mi></math><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><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></p>
</div>
</li>
<li>
<p>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:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
</li>
</ol>
</div>
<div class="paragraph">
<p>The figure <a href="#cdConstructorClassifierType">Classifier and Constructor Type Subtype Relations</a> shows the subtype relations defined by the preceding rules.</p>
</div>
<div id="cdConstructorClassifierType" class="imageblock" style="text-align: center">
<div class="content">
<img src="chapters/04_types/fig/cdConstructorClassifierType.svg" alt="cdConstructorClassifierType">
</div>
<div class="title">Figure 4. Classifier and Constructor Type Subtype Relations</div>
</div>
<div class="paragraph">
<p>Consequences:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Overriding of static methods is possible and by using the constructor or classifier type, polymorphism for static methods is possible as well.</p>
<div id="_polymorphism-and-static-methods" class="exampleblock">
<div class="title">Example 22. Static Polymorphism</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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</code></pre>
</div>
</div>
</div>
</div>
</li>
<li>
<p>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.</p>
<div class="exampleblock">
<div class="title">Example 23. Constructor of Abstract Class</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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</code></pre>
</div>
</div>
</div>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>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 <code>any</code>, 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.</p>
</div>
<div class="paragraph">
<p>Using wildcards on classifier types would have the same meaning as using the upper bound directly.
That is, a type reference <code>type{? extends C}</code> can simply be replaced with <code>type{c}</code>, and <code>type{?}</code> with <code>type{any}</code>.</p>
</div>
<div class="paragraph">
<p>To conclude this chapter, let us compare the different types introduced above depending on whether they are used with wildcards or not:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>having a value of type <code>constructor{C}</code>, we know we have</p>
<div class="ulist">
<ul>
<li>
<p>a constructor function of <code>{C}</code> or a subclass of <code>{C}</code>,</p>
</li>
<li>
<p>that can be used for instantiation (i.e. the represented class is not abstract),</p>
</li>
<li>
<p>that has a signature compatible to the owned or inherited constructor of <code>{C}</code>.</p>
<div class="paragraph">
<p>This means we have the constructor function of class <code>{C}</code> (but only if is non-abstract) or the constructor function of any non-abstract
subclass of <code>{C}</code> with an override compatible signature to that of <code>{C}</code>'s constructor function.</p>
</div>
</li>
</ul>
</div>
</li>
<li>
<p>having a value of type <code>constructor{? extends C}</code>, we know we have</p>
<div class="ulist">
<ul>
<li>
<p>a constructor function of <code>{C}</code> or a subclass of <code>{C}</code>,</p>
</li>
<li>
<p>that can be used for instantiation (i.e. the represented class is not abstract).</p>
<div class="paragraph">
<p>So, same situation as before except that we know nothing about the constructor function’s signature.
However, if <code>{C}</code> has a covariant constructor, cf. <a href="#_covariant-constructors">Covariant Constructors</a>, we can still conclude that we have an override compatible
constructor function to that of <code>{C}</code>, because classes with covariant constructors enforce all their subclasses to have override compatible constructors.</p>
</div>
</li>
</ul>
</div>
</li>
<li>
<p>have a value of type <code>type{? extends C}</code> or <code>type{C}</code> (the two types are equivalent), we know we have:</p>
<div class="ulist">
<ul>
<li>
<p>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),</p>
</li>
<li>
<p>that represents type <code>{C}</code> or a subtype thereof,</p>
</li>
<li>
<p>that cannot be used for instantiation (e.g. could be the constructor function of an abstract class, the object representing an interface, etc.).</p>
</li>
</ul>
</div>
</li>
</ol>
</div>
<div class="paragraph">
<p>Slightly simplified, we can say that in the first above case we can always use the value for creating an instance with <code>new</code>, in the second case only if the
referenced type has a covariant constructor, cf. <a href="#_covariant-constructors">Covariant Constructors</a>, and never in the third case.</p>
</div>
</div>
<div class="sect3">
<h4 id="_constructors-and-prototypes-in-ecmascript-2015"><a class="anchor" href="#_constructors-and-prototypes-in-ecmascript-2015"></a><a class="link" href="#_constructors-and-prototypes-in-ecmascript-2015">4.11.3. Constructors and Prototypes in ECMAScript 2015</a></h4>
<div class="paragraph">
<p><a href="#fig-constructors-and-prototypes">Constructors and prototypes for two classes A and B in ECMAScript 2015 (not N4JS!)</a> 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 <a href="#ex-constructors-and-prototypes">Constructors and Prototypes</a>.</p>
</div>
<div id="ex-constructors-and-prototypes" class="exampleblock">
<div class="title">Example 24. Constructors and Prototypes</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-javascript" data-lang="javascript">class A {}
class B extends A {}
var b = new B();</code></pre>
</div>
</div>
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
<a href="#fig-constructors-and-prototypes">Constructors and prototypes for two classes A and B in ECMAScript 2015 (not N4JS!)</a> shows plain ECMAScript 2015 only.
Also note that <code>A</code> is defined without an <code>extends</code> clause, which is what ECMAScript 2015 calls a <em>base class</em> (as opposed to a <em>derived class</em>).
The constructor of a base class always has Function.prototype as its prototype.
If we had defined <code>A</code> as <code>class A extends Object {}</code> in the listing above, then the constructor of <code>A</code> would have Object’s constructor as its prototype
(depicted in as a dashed red arrow), which would make a more consistent overall picture.
</td>
</tr>
</table>
</div>
<div id="fig-constructors-and-prototypes" class="imageblock" style="text-align: center">
<div class="content">
<img src="chapters/04_types/fig/ctorsProtosInES6.svg" alt="ctorsProtosInES6">
</div>
<div class="title">Figure 5. Constructors and prototypes for two classes A and B in ECMAScript 2015 (not N4JS!)</div>
</div>
<div class="paragraph">
<p>Base classes in the above sense are not available in N4JS.
If an N4JS class does not provide an <code>extends</code> clause, it will implicitly inherit from built-in class <code>N4Object</code>,
if it provides an <code>extends</code> clause stating <code>Object</code> as its super type, then it corresponds to what is shown in <a href="#fig-constructors-and-prototypes">Constructors and prototypes for two classes A and B in ECMAScript 2015 (not N4JS!)</a> with the red dashed arrow.</p>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_this-type"><a class="anchor" href="#_this-type"></a><a class="link" href="#_this-type">4.12. This Type</a></h3>
<div class="paragraph">
<p>The <code>this</code> keyword may represent either a <code>this</code> literal (cf. <a href="#ex:this-keyword-and-type-in-instance-and-static-context">This keyword and type in instance and static context</a>) or may refer to the <code>this</code> type.
In this section, we describe the latter case.</p>
</div>
<div class="paragraph">
<p>Typical use cases of the <code>this</code> type include:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>declaring the return type of instance methods</p>
</li>
<li>
<p>declaring the return type of static methods</p>
</li>
<li>
<p>as formal parameter type of constructors in conjunction with use-site structural typing</p>
</li>
<li>
<p>the parameter type of a function type expression, which appears as type of a method parameter</p>
</li>
<li>
<p>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><code>{this}</code>,<code>constructor{this}</code>)</p>
</li>
<li>
<p>an existential type argument inside a return type expression for methods (e.g.<code>ArrayList&lt;? extends this&gt; method(){&#8230;&#8203;}</code>)</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The precise rule where it may appear is given below in <a href="#Req-IDE-37">[Req-IDE-37]</a>.</p>
</div>
<div class="paragraph">
<p>The <code>this</code> 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 <code>this</code> keyword or a variable value implicitly inferred to a <code>this</code> type (e.g. <code>var x = this; return x;</code>).</p>
</div>
<div class="listingblock">
<div class="title">Simple This Type</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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</code></pre>
</div>
</div>
<div class="paragraph">
<p><code>this</code> can be thought of as a type variable which is implicitly substituted with the declaring class (i.e. this type used in a class <code>{A}</code> actually means <code>&lt;? extends A&gt;</code>).</p>
</div>
<div class="sect3">
<h4 id="this-type-syntax"><a class="anchor" href="#this-type-syntax"></a><a class="link" href="#this-type-syntax">4.12.1. Syntax</a></h4>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">ThisTypeRef returns ThisTypeRef:
ThisTypeRefNominal | ThisTypeRefStructural;
ThisTypeRefNominal returns ThisTypeRefNominal:
{ThisTypeRefNominal} 'this'
;
ThisTypeRefStructural returns ThisTypeRefStructural:
typingStrategy=TypingStrategyUseSiteOperator
'this'
('with' '{' ownedStructuralMembers+=TStructMember* '}')?
;</code></pre>
</div>
</div>
<div class="paragraph">
<p>The keyword <code>this</code> and the type expression <code>this</code> look similar, however they can refer to different types.
The type always refers to the type of instances of a class.
The <code>this</code> 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 <a href="#_this-keyword">This Keyword</a> for details.</p>
</div>
<div id="ex:this-keyword-and-type-in-instance-and-static-context" class="exampleblock">
<div class="title">Example 25. This keyword and type in instance and static context</div>
<div class="content">
<div class="paragraph">
<p>Note that the following code is not working, because some usages below are
not valid in N4JS. This is only to demonstrate the types.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {
instanceMethod() {
var c: this = this;
}
static staticMethod() {
var C: type{this} = this;
}
}</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>Structural typing and additional members in structural referenced types is described in <a href="#_structural-typing">Structural Typing</a>.</p>
</div>
</div>
<div class="sect3">
<h4 id="this-keyword-semantics"><a class="anchor" href="#this-keyword-semantics"></a><a class="link" href="#this-keyword-semantics">4.12.2. Semantics</a></h4>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-37"></a><strong>Req. IDE-37:</strong> <a href="#Req-IDE-37">This Type</a> (ver. 1)</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>this</code> used in the context of a class is actually inferred to an existential type <code>? extends A</code> inside the class itself.</p>
</li>
<li>
<p>the <code>this</code> type may only be used</p>
<div class="ulist">
<ul>
<li>
<p>as the type of a formal parameter of a constructor, if and only if combined with use-site structural typing.</p>
</li>
<li>
<p>at covariant positions within member declarations, except for static members of interfaces.</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="paragraph">
<p>Remarks</p>
</div>
<div class="ulist">
<ul>
<li>
<p>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 <code>this</code> type in formal parameters but only as return type. The following listing demonstrates that problem:</p>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class A {
bar(x: this): void { ... } // error
// virtually defines: bar(x: A): void
}
class B extends A {
// virtually defines: bar(x: B): void
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>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 <code>bar</code> in is not override compatible with <code>bar</code> in <code>A</code>.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
</li>
<li>
<p>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 <code>this</code> 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 <code>this</code> is only available at runtime, while the type used at compilation time is only a heuristically-computed type, in other words,a good guess.</p>
</li>
<li>
<p>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 <code>@This</code> 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 <code>@This</code> annotations can be used.</p>
</li>
<li>
<p>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 <code>type{this}</code> is used.</p>
</li>
</ul>
</div>
<div style="page-break-after: always;"></div>
<div class="exampleblock">
<div class="title">Example 26. This type in function-type-expression</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class A {
alive: boolean = true;
methodA(func: {function(this)}): string {
func(this); // applying the passed-in function
return "done";
}
}</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>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. <sup class="footnote">[<a id="_footnoteref_26" class="footnote" href="#_footnote_26" title="View footnote.">26</a>]</sup></p>
</div>
<div class="exampleblock">
<div class="title">Example 27. Problems with this type and type arguments</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">// 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!</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_enums"><a class="anchor" href="#_enums"></a><a class="link" href="#_enums">4.13. Enums</a></h3>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="sect3">
<h4 id="_enums-n4js"><a class="anchor" href="#_enums-n4js"></a><a class="link" href="#_enums-n4js">4.13.1. Enums (N4JS)</a></h4>
<div class="paragraph">
<p>Definition and usage of an enumeration:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">// 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</code></pre>
</div>
</div>
<div class="sect4">
<h5 id="enums-syntax"><a class="anchor" href="#enums-syntax"></a><a class="link" href="#enums-syntax">4.13.1.1. Syntax</a></h5>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">N4EnumDeclaration &lt;Yield&gt;:
=&gt;( {N4EnumDeclaration}
(declaredModifiers+=N4Modifier)*
'enum' name=BindingIdentifier&lt;Yield&gt;? )
'{'
(literals+=N4EnumLiteral (',' literals+=N4EnumLiteral)*)?
'}';
N4EnumLiteral: name=IdentifierName (':' value=STRING)?;</code></pre>
</div>
</div>
</div>
<div class="sect4 language-n4js">
<h5 id="enums-semantics"><a class="anchor" href="#enums-semantics"></a><a class="link" href="#enums-semantics">4.13.1.2. Semantics</a></h5>
<div class="paragraph">
<p>The enum declaration <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>E</mi></math> is of type <code>type{E}</code> and every enumeration is implicitly derived from <code>N4Enum</code>.
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:</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-38"></a><strong>Req. IDE-38:</strong> <a href="#Req-IDE-38">Enum Type Rules</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Every enumeration <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>E</mi></math> is a subtype of the base type <code>N4Enum</code>:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>which itself is a subtype of <code>Object</code>:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>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:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
</ol>
</div>
<div class="paragraph">
<p>This means that every literal is a subtype of <code>N4Enum</code> and <code>Object</code>:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>The base enumeration type <code>N4Enum</code> is defined as follows:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">/**
* 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
}</code></pre>
</div>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-39"></a><strong>Req. IDE-39:</strong> <a href="#Req-IDE-39">Unique literal names</a> (ver. 1)</p>
</div>
<div class="ulist">
<ul>
<li>
<p><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></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Literal names have to be unique.</p>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-40"></a><strong>Req. IDE-40:</strong> <a href="#Req-IDE-40">Enum Literals are Singletons</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>Enum literals are singletons:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 28. Enumeration List</div>
<div class="content">
<div class="paragraph">
<p>Due to the common base type <code>N4Enum</code> it is possible to define generics accepting only enumeration, as shown in this example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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();</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_string-based-enums"><a class="anchor" href="#_string-based-enums"></a><a class="link" href="#_string-based-enums">4.13.2. String-Based Enums</a></h4>
<div class="paragraph">
<p>In current web standards [<a href="#W3C:Steen:14:XL">W3C:Steen:14:XL</a>], 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 <code>string</code>.
To provide the N4JS user with some validations regarding the validity of a statement at compile time, a special kind of subtypes of <code>string</code> are introduced: the string-based enum using the <code>@StringBased</code> annotation.
(See also other string-based types like <code>typename&lt;T&gt;</code> <code>pathSelector&lt;T&gt;</code> and <code>i18nKey</code> in <a href="#_primitive-pathselector-and-i18nkey">Primitive Pathselector and I18nKey</a>.)</p>
</div>
<div class="paragraph">
<p>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 <code>n4type</code> property is not available.
The only exception is the static getter <code>literals</code>: 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.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-41"></a><strong>Req. IDE-41:</strong> <a href="#Req-IDE-41">String-Based Enum Type Rules</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>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 <code>N4StringBasedEnum</code>:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>which itself is not related to the standard enumeration type <code>N4Enum</code></p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p><code>N4StringBasedEnum</code> is a subtype of <code>string</code></p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>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.</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p><a href="#Req-IDE-39">[Req-IDE-39]</a> also applies for <code>N4StringBasedEnum</code>.</p>
</li>
<li>
<p><a href="#Req-IDE-40">[Req-IDE-40]</a> also applies for <code>N4StringBasedEnum</code>.</p>
</li>
<li>
<p>References to string-based enums may only be used in the following places:</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>in type annotations</p>
</li>
<li>
<p>in property access expressions to refer to one of the enum’s literals</p>
</li>
<li>
<p>in property access expressions to read from the static getter <code>literals</code></p>
<div class="paragraph">
<p>In particular, it is invalid to use the type of a string-based enum as a
value, as in</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js"> @StringBased enum Color { RED, GREEN, BLUE }
var c = Color;</code></pre>
</div>
</div>
</li>
</ol>
</div>
</li>
</ol>
</div>
<div class="exampleblock">
<div class="title">Example 29. WebIDL example</div>
<div class="content">
<div class="listingblock">
<div class="title">Gecko-Engine webIDL XMLHttpRequestResponseType as taken from [<a href="#W3C:Steen:14:XL">W3C:Steen:14:XL</a>]</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">enum XMLHttpRequestResponseType {
"",
"arraybuffer",
"blob",
"document",
"json",
"text" //, ... and some mozilla-specific additions
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Compatible Definition of this Enumeration in N4JS, provided through a
runtime-library definition:</p>
</div>
<div class="listingblock">
<div class="title">File in source-folder: w3c/dom/XMLHttpRequestResponseType.n4js</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">@StringBased enum XMLHttpRequestResponseType {
vacant : "",
arrayBuffer : "arraybuffer",
blob : "blob",
document : "document",
json : "json",
text : "text"
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Usage of the enumeration in the definition files of the runtime-library.
Note the explicit import of the enumeration.</p>
</div>
<div class="listingblock">
<div class="title">XMLHttpRequestResponse.n4jsd</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">@@ProvidedByRuntime
import XMLHttpRequestResponseType from "w3c/dom/XMLHttpRequestResponseType";
@Global
export external public class XMLHttpRequestResponse extends XMLHttpRequestEventTarget {
// ...
// Setter Throws TypeError Exception
public responseType: XMLHttpRequestResponseType;
// ...
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Client code importing the runtime-library as defined above can now use the Enumeration in a type-safe way:</p>
</div>
<div class="listingblock">
<div class="title">String-Based Enumeration Usage</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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 );
}
}</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_short-hand-syntax"><a class="anchor" href="#_short-hand-syntax"></a><a class="link" href="#_short-hand-syntax">4.14. Short-Hand Syntax</a></h3>
<div class="paragraph">
<p>Short-hand syntax is available for a number of built-in types.</p>
</div>
<div class="sect3">
<h4 id="_array-short-hand-syntax"><a class="anchor" href="#_array-short-hand-syntax"></a><a class="link" href="#_array-short-hand-syntax">4.14.1. Array Short-Hand Syntax</a></h4>
<div class="paragraph">
<p>For the built-in type <code>Array</code> a convenience short form is available. Thus, writing</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">let arr: string[];</code></pre>
</div>
</div>
<div class="paragraph">
<p>is equivalent to</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">let arr: Array&lt;string&gt;;</code></pre>
</div>
</div>
<div class="paragraph">
<p>Multi-dimensional arrays can be declared as such:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">let arr: string[][][];</code></pre>
</div>
</div>
<div class="paragraph">
<p>which is equivalent to</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">let arr: Array&lt;Array&lt;Array&lt;string&gt;&gt;&gt;;</code></pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_iterablen-short-hand-syntax"><a class="anchor" href="#_iterablen-short-hand-syntax"></a><a class="link" href="#_iterablen-short-hand-syntax">4.14.2. IterableN Short-Hand Syntax</a></h4>
<div class="paragraph">
<p>The built-in IterableN types (i.e. <code>Iterable2</code>, <code>Iterable3</code>, &#8230;&#8203; <code>Iterable9</code>, see <a href="#IterableN">IterableN</a>) are
also provided with a short-hand syntax. For example, writing</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">let i3: [string,number,string[]];</code></pre>
</div>
</div>
<div class="paragraph">
<p>would be equivalent to</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">let i3: Iterable3&lt;string,number,Array&lt;string&gt;&gt;;</code></pre>
</div>
</div>
<div class="paragraph">
<p>Note the following special cases:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">let i0: [];
let i1: [string];
let union: string|number[];</code></pre>
</div>
</div>
<div class="paragraph">
<p>which is equivalent to</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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;</code></pre>
</div>
</div>
<div class="paragraph">
<p>Further note: while this syntax is very similar to TypeScript&#8217;s tuple syntax, the semantics
of tuples and IterableN are very different.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_classifiers"><a class="anchor" href="#_classifiers"></a><a class="link" href="#_classifiers">5. Classifiers</a></h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_n4js-specific-classifiers"><a class="anchor" href="#_n4js-specific-classifiers"></a><a class="link" href="#_n4js-specific-classifiers">5.1. N4JS Specific Classifiers</a></h3>
<div class="paragraph">
<p>N4JS provides three new metatypes: class, interface, and enums.
In this section we describe classes and interfaces.
These metatypes, called <em>classifiers</em>, share some common properties which are described before type specific properties are outlined in the following sections.</p>
</div>
<div class="paragraph">
<p>All of these metatypes can be marked with type access modifiers:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">enum N4JSTypeAccessModifier: project | public;</code></pre>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_properties-2"><a class="anchor" href="#_properties-2"></a><a class="link" href="#_properties-2">5.1.1. Properties</a></h4>
<div class="paragraph">
<p><strong>Properties defined by syntactic elements:</strong></p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>annotations</code> </dt>
<dd>
<p>Arbitrary annotations, see <a href="#_annotations">Annotations</a> for details.</p>
</dd>
<dt class="hdlist1"><code>accessModifier</code> </dt>
<dd>
<p>N4JS type access modifier: <code>public</code>, or <code>project</code>; <code>public</code> can be combined with <code>@Internal</code>; if
<strong>export</strong> is <code>true</code> the default is else the default is <code>private</code>.</p>
</dd>
<dt class="hdlist1"><code>name</code> </dt>
<dd>
<p>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 <a href="#_valid-names">Valid Names</a>.</p>
</dd>
<dt class="hdlist1"><code>typePars</code> </dt>
<dd>
<p>Collection of type parameters of a generic classifier; empty by default.</p>
</dd>
<dt class="hdlist1"><code>ownedMembers</code> </dt>
<dd>
<p>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.</p>
</dd>
<dt class="hdlist1"><code>typingStrategy</code> </dt>
<dd>
<p>The definition-site typing strategy. By default nominal typing is used.
See <a href="#_structural-typing">Structural Typing</a> for details.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p><strong>The following pseudo properties are defined via annotations:</strong></p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>export</code> </dt>
<dd>
<p>Boolean property set to true if the <code>export</code> modifier is set.
If value is true, the classifier may be accessible outside the project.</p>
</dd>
<dt class="hdlist1"><code>final</code> </dt>
<dd>
<p>Boolean property which is set to final if annotation <code>@Final</code> is set.
Also see <a href="#_final-modifier">Final Modifier</a></p>
</dd>
<dt class="hdlist1"><code>deprecated</code> </dt>
<dd>
<p>Boolean property set to true if annotation <code>@Deprecated</code> is set.</p>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
Version 0.4, not implemented in Version 0.3
</td>
</tr>
</table>
</div>
</dd>
</dl>
</div>
<div class="paragraph">
<p><strong>We additionally define the following pseudo properties:</strong></p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>acc</code> </dt>
<dd>
<p>Type access modifier as described in <a href="#_accessibility-of-types-top-level-variables-and-function-declarations">Accessibility of Types, Top-Level Variables and Function Declarations</a>, it is the aggregated value of the
<code>accessModifier</code> and the <strong>export</strong> property.</p>
</dd>
<dt class="hdlist1"><code>owned{Fields|Methods|Getters|Setters|Accessors}</code> </dt>
<dd>
<p>Filters ownedMembers by metatype, short for<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.</p>
</dd>
<dt class="hdlist1"><code>members</code> </dt>
<dd>
<p>Reflexive transitive closure of all members of a classifier and its super classifiers, see <a href="#_common-semantics-of-classifiers">Common Semantics of Classifiers</a> on how this is calculated.</p>
</dd>
<dt class="hdlist1"><code>fields|methods|getters|setters|accessors</code> </dt>
<dd>
<p>Filters members by metatype, short for<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.</p>
</dd>
<dt class="hdlist1"><code>superClassifiers</code> </dt>
<dd>
<p>Classes and interface may extend or implement classes or interfaces.
Any class or interface extended or interface implemented is called <em>super classifier</em>.
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></p>
</dd>
</dl>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_common-semantics-of-classifiers"><a class="anchor" href="#_common-semantics-of-classifiers"></a><a class="link" href="#_common-semantics-of-classifiers">5.1.2. Common Semantics of Classifiers</a></h4>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-42"></a><strong>Req. IDE-42:</strong> <a href="#Req-IDE-42">Subtyping of Classifiers</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a given type <code>C</code>, 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
<code>C</code>, the following constraints must be true:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The supertypes must be accessible to the subtype:<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 <code>C</code>.</p>
</li>
<li>
<p>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.</p>
<div class="paragraph">
<p><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><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></p>
</div>
</li>
<li>
<p>Wildcards may not be used as type argument when binding a supertype’s type parameters.</p>
</li>
<li>
<p>A classifier cannot be directly subtyped directly multiple times:<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></p>
</li>
</ol>
</div>
<div class="paragraph">
<p>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. <a href="#_redefinition-of-members">Redefinition of Members</a>).
A classifier only inherits its members from its direct supertypes, although the supertypes may contains members inherited from their supertypes.</p>
</div>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_classes"><a class="anchor" href="#_classes"></a><a class="link" href="#_classes">5.1.3. Classes</a></h4>
<div class="sect4">
<h5 id="_definition-of-classes"><a class="anchor" href="#_definition-of-classes"></a><a class="link" href="#_definition-of-classes">5.1.3.1. Definition of Classes</a></h5>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>At the current stage, class expressions are effectively disabled at least until the semantics of them are finalized in ECMAScript 6.</p>
</div>
<div class="paragraph">
<p>In N4JS (as in many other languages) multi-inheritance of classes is not supported.
Although the <code>diamond problem</code> (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.<sup class="footnote">[<a id="_footnoteref_27" class="footnote" href="#_footnote_27" title="View footnote.">27</a>]</sup>
Interfaces, however, allow for multi-inheritance. Since the former can also define functions with bodies, this is not a hard restriction.</p>
</div>
<div class="sect5">
<h6 id="class-syntax"><a class="anchor" href="#class-syntax"></a><a class="link" href="#class-syntax">5.1.3.1.1. Syntax</a></h6>
<div class="listingblock">
<div class="title">Syntax N4 Class Declaration and Expression</div>
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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;*
'}'
;</code></pre>
</div>
</div>
</div>
<div class="sect5">
<h6 id="class-properties"><a class="anchor" href="#class-properties"></a><a class="link" href="#class-properties">5.1.3.1.2. Properties</a></h6>
<div class="paragraph">
<p>These are the properties of class, which can be specified by the user:
Syntax N4 Class Declaration and Expression</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>abstract</code> </dt>
<dd>
<p>Boolean flag indicating whether class may be instantiable; default is <code>false</code>, see <a href="#_abstract-classes">Abstract Classes</a>.</p>
</dd>
<dt class="hdlist1"><code>external</code> </dt>
<dd>
<p>Boolean flag indicating whether class is a declaration without implementation or with an external (non-N4JS) implementation; default is <code>false</code>, see <a href="#_definition-site-structural-typing">Definition Site Structural Typing</a>.</p>
</dd>
<dt class="hdlist1"><code>defStructural</code> </dt>
<dd>
<p>Boolean flag indicating whether subtype relation uses nominal or structural typing, see <a href="#_definition-site-structural-typing">Definition Site Structural Typing</a> for details.</p>
</dd>
<dt class="hdlist1"><code>superType/sup</code> </dt>
<dd>
<p>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 <code>N4Object</code>.</p>
</dd>
<dt class="hdlist1"><code>implementedInterfaces</code>/<code>interfaces</code> </dt>
<dd>
<p>Collection of interfaces directly <em>implemented</em> by the class; empty by default.
Instead of <code>implementedInterfaces</code>, we simply write <code>interfaces</code>.</p>
</dd>
<dt class="hdlist1"><code>ownedCtor</code> </dt>
<dd>
<p>Explicit constructor of a class (if any), see <a href="#_constructor-and-classifier-type">Constructor and Classifier Type</a>.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>And we additionally define the following pseudo properties:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>ctor</code> </dt>
<dd>
<p>Explicit or implicit constructor of a class, see <a href="#_constructor-and-classifier-type">Constructor and Classifier Type</a>.</p>
</dd>
<dt class="hdlist1"><code>fields</code> </dt>
<dd>
<p>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>.</p>
</dd>
</dl>
</div>
</div>
<div class="sect5">
<h6 id="class-type-inference"><a class="anchor" href="#class-type-inference"></a><a class="link" href="#class-type-inference">5.1.3.1.3. Type Inference</a></h6>
<div class="paragraph">
<p>The type of a class declaration or class expression <code>C</code> (i.e., a class definition in general) is of type <code>constructor{C}</code> if it is not abstract,
that is if it can be instantiated.
If it is abstract, the type of the definition simply is <code>type{C}</code>:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-43"></a><strong>Req. IDE-43:</strong> <a href="#Req-IDE-43">Structural and Nominal Supertypes</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>The type of supertypes and implemented interfaces is always the nominal type, even if the supertype is declared structurally.</p>
</div>
<div class="openblock">
<div class="content">
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="class-semantics"><a class="anchor" href="#class-semantics"></a><a class="link" href="#class-semantics">5.1.3.2. Semantics</a></h5>
<div class="paragraph">
<p>This section deals with the (more or less) type-independent constraints on classes.</p>
</div>
<div class="paragraph">
<p>Class expressions are not fully supported at the moment.</p>
</div>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="transitive_closure_of_members"></a><strong>Definition:</strong> <a href="#transitive_closure_of_members">Transitive closure of members</a></p>
</div>
<div class="paragraph">
<p>The reflexive transitive closure of members of a class is indirectly defined by the override and implementation constraints defined in <a href="#_redefinition-of-members">Redefinition of Members</a>.</p>
</div>
<div class="paragraph">
<p>Note that since overloading is forbidden, the following constraint is true <sup class="footnote">[<a id="_footnoteref_28" class="footnote" href="#_footnote_28" title="View footnote.">28</a>]</sup>:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>Remarks: Class and method definition is quite similar to the proposed ECMAScript version 6 draft [<a href="#ECMA15a">ECMA15a(p.S13.5)</a>], except that an N4 class and members may contain</p>
</div>
<div class="ulist">
<ul>
<li>
<p>annotations, abstract and access modifiers</p>
</li>
<li>
<p>fields</p>
</li>
<li>
<p>types</p>
</li>
<li>
<p>implemented interfaces</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Note that even <code>static</code> is used in ECMAScript 6.</p>
</div>
</div>
</div>
<div class="paragraph">
<p>Mixing in members (i.e. interface’s methods with default implementation or fields) is similar to mixing in members from roles as defined in [<a href="#Dart13a">Dart13a(p.S9.1)</a>].
It is also similar to default implementations in Java 8 [<a href="#Gosling15a">Gosling15a</a>].
In Java, however, more constraints exist, (for example, methods of interfaces must be public).</p>
</div>
<div class="exampleblock">
<div class="title">Example 30. Simple Class</div>
<div class="content">
<div class="paragraph">
<p>This first example shows a very simple class with a field, a constructor and a method.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {
data: any;
constructor(data: any) {
this.data = data;
}
foo(): void { }
}</code></pre>
</div>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 31. Extend and implement</div>
<div class="content">
<div class="paragraph">
<p>The following example demonstrate how a class can extend a superclass and implement an interface.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">interface I {
foo(): void
}
class C{}
class X extends C implements I {
@Override
foo(): void {}
}</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>A class <code>C</code> is a subtype of another classifier <code>S</code> (which can be a class or interface) if the other classifier <code>S</code> is (transitively) contained in the supertypes (superclasses or implemented interfaces) of the class:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-44"></a><strong>Req. IDE-44:</strong> <a href="#Req-IDE-44">Implicit Supertype of Classes</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The implicit supertype of all classes is <code>N4Object</code>.
All classes with no explicit supertype are inherited from <code>N4Object</code>.</p>
</li>
<li>
<p>If the supertype is explicitly set to <code>Object</code>, then the class is not derived from <code>N4Object</code>.
Meta-information is created similar to an <code>N4Object</code>-derived class.
Usually, there is no reason to explicitly derive a class from <code>Object</code>.</p>
</li>
<li>
<p>External classes are implicitly derived from , unless they are annotated with <code>@N4JS</code>(cf.<a href="#_external-declarations">External Declarations</a>).</p>
</li>
</ol>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_final-modifier"><a class="anchor" href="#_final-modifier"></a><a class="link" href="#_final-modifier">5.1.3.3. Final Modifier</a></h5>
<div class="paragraph">
<p>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 <code>@Final</code>, 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:</p>
</div>
<table class="tableblock frame-all grid-all spread">
<caption class="title">Table 6. Extensibility of Types</caption>
<colgroup>
<col style="width: 62.5%;">
<col style="width: 12.5%;">
<col style="width: 12.5%;">
<col style="width: 12.5%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-center valign-top">Type <code>C</code> Settings</th>
<th class="tableblock halign-center valign-top" colspan="3">Subclassed in</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><strong>Project</strong></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><strong>Vendor</strong></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><strong>World</strong></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>C.final</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>C.ctor.accessModifier=\lenum{project}</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>C.ctor.accessModifier=\lenum{public@Internal}</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>Since interfaces are always to be implemented, they must not be declared final.</p>
</div>
</div>
<div class="sect4">
<h5 id="_abstract-classes"><a class="anchor" href="#_abstract-classes"></a><a class="link" href="#_abstract-classes">5.1.3.4. Abstract Classes</a></h5>
<div class="paragraph">
<p>A class with modifier <code>abstract</code> is called an <em>abstract class</em> 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 <em>concrete</em> classes.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-45"></a><strong>Req. IDE-45:</strong> <a href="#Req-IDE-45">Abstract Class</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>A class <code>C</code> 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.</p>
</li>
<li>
<p>An abstract class may not be instantiated.</p>
</li>
<li>
<p>An abstract class cannot be set to final (with annotation <code>@Final</code>).</p>
</li>
</ol>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-46"></a><strong>Req. IDE-46:</strong> <a href="#Req-IDE-46">Abstract Member</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p></p>
</div>
<div class="sidebarblock">
<div class="content">
<a href="https://github.com/eclipse/n4js/issues/1047" class="gray" title="GitHub Issues"><span class="image"><img src="images/issue.svg" alt=""></span> #1047</a>
</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>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).</p>
</li>
<li>
<p>Only methods, getters and setters can be declared as abstract (fields cannot be abstract).</p>
</li>
<li>
<p>It is not possible to inherit from an abstract class which contains abstract members which are not visible in the subclass.</p>
</li>
<li>
<p>An abstract member must not be set to final (with annotation <code>@Final</code>).</p>
</li>
<li>
<p>Static members must not be declared abstract.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>Remarks:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>We decided to disallow abstract static members, since we cannot guarantee that a static members is not accessed in all cases</p>
</li>
<li>
<p>Only static members can override static members and only instance members can override other instance members of course.</p>
</li>
<li>
<p>An abstract member must not be declared in a final class (i.e. a class annotated with <code>@Final</code>). This is not explicitly defined as constraint in <a href="#Req-IDE-46">[Req-IDE-46]</a> 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.</p>
</li>
</ul>
</div>
<div class="admonitionblock warning">
<table>
<tr>
<td class="icon">
<i class="fa icon-warning" title=""></i>
</td>
<td class="content">
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. <a href="https://github.com/eclipse/n4js/issues/1221" class="gray" title="GitHub Issues"><span class="image"><img src="images/issue.svg" alt=""></span> #1221</a>. 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.
</td>
</tr>
</table>
</div>
</div>
<div class="sect4">
<h5 id="_non-instantiable-classes"><a class="anchor" href="#_non-instantiable-classes"></a><a class="link" href="#_non-instantiable-classes">5.1.3.5. Non-Instantiable Classes</a></h5>
<div class="paragraph">
<p>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.</p>
</div>
</div>
<div class="sect4">
<h5 id="_superclass"><a class="anchor" href="#_superclass"></a><a class="link" href="#_superclass">5.1.3.6. Superclass</a></h5>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-47"></a><strong>Req. IDE-47:</strong> <a href="#Req-IDE-47">Superclass</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a class <code>C</code> 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;</p>
</div>
<div class="ulist">
<ul>
<li>
<p><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 <code>S</code></p>
</li>
<li>
<p><code>S</code> must be be extendable in the project of <code>C</code></p>
</li>
<li>
<p><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></p>
</li>
<li>
<p>All abstract members in <code>S</code> must be accessible from <code>C</code>:</p>
<div class="paragraph">
<p><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><br>
<code>M</code> is accessible from <code>C</code>.<br>
Note that <code>M</code> need not be an owned member of <code>S</code> and that this constraint applies even if <code>C</code> is abstract).</p>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>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.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_interfaces"><a class="anchor" href="#_interfaces"></a><a class="link" href="#_interfaces">5.1.4. Interfaces</a></h4>
<div class="sect4">
<h5 id="_definition-of-interfaces"><a class="anchor" href="#_definition-of-interfaces"></a><a class="link" href="#_definition-of-interfaces">5.1.4.1. Definition of Interfaces</a></h5>
<div class="sect5">
<h6 id="interfaces-syntax"><a class="anchor" href="#interfaces-syntax"></a><a class="link" href="#interfaces-syntax">5.1.4.1.1. Syntax</a></h6>
<div class="listingblock">
<div class="title">Syntax N4 Interface Declaration</div>
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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)*
;</code></pre>
</div>
</div>
</div>
<div class="sect5">
<h6 id="interfaces-properties"><a class="anchor" href="#interfaces-properties"></a><a class="link" href="#interfaces-properties">5.1.4.1.2. Properties</a></h6>
<div class="paragraph">
<p>These are the additional properties of interfaces, which can be specified by the user:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>superInterfaces</code></dt>
<dd>
<p>Collection of interfaces extended by this interface; empty by default.
Instead of <code>superInterfaces</code>, we simply write <code>interfaces</code>.</p>
</dd>
</dl>
</div>
</div>
<div class="sect5">
<h6 id="interfaces-type-inference"><a class="anchor" href="#interfaces-type-inference"></a><a class="link" href="#interfaces-type-inference">5.1.4.1.3. Type Inference</a></h6>
<div class="paragraph">
<p>The type of an interface declaration <code>I</code> is of type <code>type{I}</code>:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</div>
<div class="sect5">
<h6 id="interfaces-semantics"><a class="anchor" href="#interfaces-semantics"></a><a class="link" href="#interfaces-semantics">5.1.4.1.4. Semantics</a></h6>
<div class="paragraph">
<p>Interfaces are used to describe the public <a href="#_acronyms">API</a> 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.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>All instance methods, getters and setters declared in an interface are implicitly abstract if they do not provide a default implementation.
The modifier <code>abstract</code> is not required, therefore, in the source code.
The following constraints apply:</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-48"></a><strong>Req. IDE-48:</strong> <a href="#Req-IDE-48">Interfaces</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For any interface <code>I</code>, the following must hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Interfaces may not be instantiated.</p>
</li>
<li>
<p>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>.</p>
</li>
<li>
<p>Members of an interface must not be declared private.
The default access modifier in interfaces is the the type’s visibility or <code>project</code>, if the type’s visibility is <code>private</code>.</p>
</li>
<li>
<p>Members of an interface, except methods, must not be declared <code>@Final</code>:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
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
</td>
</tr>
</table>
</div>
</li>
<li>
<p>The literal may not be used in the initializer expression of a field of an interface.<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, <code>this</code> is also disallowed due to <a href="#_static-members-of-interfaces">Static Members of Interfaces</a>.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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 <a href="#_acronyms">IDL</a> [<a href="#OMG14a">OMG14a</a>] is used to describe these elements in specifications (and mapped to JavaScript via rules described in [<a href="#W3C12a">W3C12a</a>])
constant data fields are an often-used technique there and they can be modeled in N4JS 1:1.</p>
</div>
<div class="paragraph">
<p>As specified in <a href="#Req-IDE-56">[Req-IDE-56]</a>, interfaces cannot contain a constructor i.e.<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>.</p>
</div>
<div class="exampleblock">
<div class="title">Example 32. Simple Interfaces</div>
<div class="content">
<div class="paragraph">
<p>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.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">interface I {
foo(): void
}
interface I2 extends I {
someText: string;
bar(): void
}</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>If a classifier <code>C</code> <em>implements</em> an interface <code>I</code>, we say <code>I</code> is <em>implemented</em> by <code>C</code>.
If <code>C</code> redefines members declared in <code>I</code>, we say that these members are <em>implemented</em> by <code>C</code>.
Members not redefined by <code>C</code> but with a default implementations are <em>mixed in</em> or <em>consumed by</em> <code>C</code>.
We all cases we call <code>C</code> the <em>implementor</em>.</p>
</div>
<div class="paragraph">
<p>Besides the general constraints described in <a href="#_common-semantics-of-classifiers">Common Semantics of Classifiers</a>, the following constraints must hold for extending or implementing interfaces:</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-49"></a><strong>Req. IDE-49:</strong> <a href="#Req-IDE-49">Extending Interfaces</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a given type
<code>I</code>, 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 <code>I</code>, the following constraints must be true:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>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.</p>
</li>
<li>
<p>An interface may not directly extend the same interface more than once:<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>.</p>
</li>
<li>
<p>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</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>J</mi></math> is not parameterized, or</p>
</li>
<li>
<p>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.<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.</p>
</li>
</ol>
</div>
</li>
<li>
<p>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 <code>I</code>:<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> <code>M</code> is accessible from <code>I</code>.<br>
Note that <code>M</code> 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>.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-50"></a><strong>Req. IDE-50:</strong> <a href="#Req-IDE-50">Implementing Interfaces</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a given type
<code>C</code>, 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 <code>C</code>, the following constraints must be true:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Only classes can implement interfaces: <code>C</code> must be a Class.</p>
</li>
<li>
<p>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.</p>
</li>
<li>
<p>A class may not directly implement the same interface more than once:<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>.</p>
</li>
<li>
<p>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</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>J</mi></math> is not parameterized, or</p>
</li>
<li>
<p>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.<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.</p>
</li>
</ol>
</div>
</li>
<li>
<p>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 <code>C</code>:<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> <code>M</code> is accessible from <code>C</code>.<br>
Note that <code>M</code> 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>.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>For default methods in interfaces, see <a href="#_default-methods-in-interfaces">Default Methods in Interfaces</a>.</p>
</div>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_generic-classifiers"><a class="anchor" href="#_generic-classifiers"></a><a class="link" href="#_generic-classifiers">5.1.5. Generic Classifiers</a></h4>
<div class="paragraph">
<p>Classifiers can be declared generic by defining a type parameter via <code>type-param</code>.</p>
</div>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="generic_classifiers"></a><strong>Definition:</strong> <a href="#generic_classifiers">Generic Classifiers</a></p>
</div>
<div class="paragraph">
<p>A generic classifier is a classifier with at least one type parameter.
That is, a given classifier <code>C</code> 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>.</p>
</div>
<div class="paragraph">
<p>If a classifier does not define any type parameters, it is not generic, even if its superclass or any implemented interface is generic.</p>
</div>
<div class="paragraph">
<p>The format of the type parameter expression is described in <a href="#_parameterized-types">Parameterized Types</a>.
The type variable defined by the type parameter’s type expression can be used just like a normal type inside the class definition.</p>
</div>
<div class="paragraph">
<p>If using a generic classifier as type of a variable, it may be parameterized.
This is usually done via a type expression (cf. <a href="#_parameterized-types">Parameterized Types</a>) or via <code>typearg</code> 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 <a href="#_parameterized-types">Parameterized Types</a> for details).</p>
</div>
<div class="paragraph">
<p>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 <code>extend</code>, <code>with</code>, or <code>implements</code> section using <code>typearg</code>.
If a type variable is used in <code>typearg</code> to bound a type variable of a type parameter, it has to fulfil possible type constraints (upper/lower bound) specified in the type parameter.</p>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 33. Generic Type Definition and Usage as Type of Variable</div>
<div class="content">
<div class="paragraph">
<p>This example demonstrates how to define a generic type and how to refer to it in a variable definition.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">export class Container&lt;T&gt; {
private item: T;
getItem(): T {
return this.item;
}
setItem(item: T): void {
this.item = item;
}
}</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>This type can now be used as a type of a variable as follows</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">import Container from "p/Container"
var stringContainer: Container&lt;string&gt; = new Container&lt;string&gt;();
stringContainer.setItem("Hello");
var s: string = stringContainer.getItem();</code></pre>
</div>
</div>
<div class="paragraph">
<p>In line 3, the type variable <code>T</code> of the generic class <code>Container</code> is bound to <code>string</code>.</p>
</div>
<div class="exampleblock">
<div class="title">Example 34. Binding of type variables with multiple types</div>
<div class="content">
<div class="paragraph">
<p>For a given generic class <code>G</code></p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class A{}
class B{}
class C extends A{}
class G&lt;S, T extends A, U extends B&gt; {
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>the variable definition</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var x: G&lt;Number,C,B&gt;;</code></pre>
</div>
</div>
<div class="paragraph">
<p>would bind the type variables as follows:</p>
</div>
<table class="tableblock frame-all grid-all spread">
<colgroup>
<col style="width: 16.6666%;">
<col style="width: 16.6666%;">
<col style="width: 66.6668%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>S</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>Number</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Bound by first type argument, no bound constraints defined for <code>S</code>.</p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>T</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>C</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Bound by second type argument, <code>C</code> must be a subtype of in order to fulfill the type constraint.</p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>U</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>B</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Bound by third type argument, <code>extends</code> is reflexive, that is <code>B</code> fulfills the
type constraint.</p></td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-51"></a><strong>Req. IDE-51:</strong> <a href="#Req-IDE-51">Generic Superclass, Type Argument with Type Variable</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a given generic superclass <code>SuperClass</code></p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class SuperClass&lt;S, T extends A, U extends B&gt; {};</code></pre>
</div>
</div>
<div class="paragraph">
<p>and a generic subclass <code>SubClass</code></p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class SubClass&lt;X extends A&gt; extends SuperClass&lt;Number, X, B&gt; {..};</code></pre>
</div>
</div>
<div class="paragraph">
<p>the variable definition</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var s: SubClass&lt;C&gt;;</code></pre>
</div>
</div>
<div class="paragraph">
<p>would bind the type variables as follows:<br></p>
</div>
<table class="tableblock frame-all grid-all spread">
<colgroup>
<col style="width: 16.6666%;">
<col style="width: 16.6666%;">
<col style="width: 66.6668%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-center valign-top">TypeVariable</th>
<th class="tableblock halign-center valign-top">Bound to</th>
<th class="tableblock halign-center valign-top">Explanation</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>SuperClass.S</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>Number</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Type variable <code>s</code> of supertype <code>SuperClass</code> is bound to <code>Number</code>.</p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>SuperClass.T</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>SubClass.X=C</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Type variable <code>T</code> of supertype <code>SuperClass</code> is bound to type variable <code>X</code> of <code>SubClass</code>. It gets
then indirectly bound to <code>C</code> as specified by the type argument of the
variable definition.</p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>SuperClass.U</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>B</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Type variable <code>U</code> of supertype <code>SuperClass</code> is auto-bound to <code>C</code> as no explicit binding for the third type variable is specified.</p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>SubClass.X</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>C</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Bound by first type argument specified in variable definition.</p></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="sec:definition-site-variance"><a class="anchor" href="#sec:definition-site-variance"></a><a class="link" href="#sec:definition-site-variance">5.1.6. Definition-Site Variance</a></h4>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>The <em>variance</em> of a parameterized type states how its subtyping relates to its type arguments’ subtyping.
For example, given a parameterized type <code>G&lt;T&gt;</code> and plain types <code>A</code> and <code>B</code>, we know</p>
</div>
<div class="ulist">
<ul>
<li>
<p>if <code>G</code> is <strong>covariant</strong> w.r.t. its parameter <code>T</code>, then</p>
<div class="paragraph">
<p><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></p>
</div>
</li>
<li>
<p>if <code>G</code> is <strong>contravariant</strong> w.r.t. its parameter <code>T</code>, then</p>
<div class="paragraph">
<p><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></p>
</div>
</li>
<li>
<p>if <code>G</code> is <strong>invariant</strong> w.r.t. its parameter <code>T</code>, then</p>
<div class="paragraph">
<p><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><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></p>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>Strictly speaking, a type parameter/variable itself is not co- or contravariant;<br>
however, for the sake of simplicity we say <em><code>T</code> is covariant</em> as a short form for <em><code>G</code> is covariant with respect to its type parameter <code>T</code></em> (for contravariant and invariant accordingly).</p>
</div>
<div class="paragraph">
<p>To declare the variance of a parameterized classifier on definition site, simply add keyword <code>in</code> or <code>out</code> before the corresponding type parameter:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class ReadOnlyList&lt;out T&gt; { // covariance
// ...
}
interface Consumer&lt;in T&gt; { // contravariance
// ...
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>In such cases, the following constraints apply.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-174"></a><strong>Req. IDE-174:</strong> <a href="#Req-IDE-174">Definition-Site Variance</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>Given a parameterized type with a type parameter , the following must hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><code>T</code> may only appear in variance-compatible positions:</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>if <code>T</code> is declared on definition site to be <strong>covariant</strong>, then it may only appear in covariant positions within the type’s non-private member declarations.</p>
</li>
<li>
<p>if <code>T</code> is declared on definition site to be <strong>contravariant</strong>, then it may only appear in contravariant positions within the type’s non-private member declarations.</p>
</li>
<li>
<p>if <code>T</code> is <strong>invariant</strong>, i.e. neither declared covariant nor declared contravariant on definition site, then it may appear in any position (where type variables are allowed).</p>
<div class="paragraph">
<p>Thus, no restrictions apply within the declaration of private members and within the body of field accessors and methods.</p>
</div>
</li>
</ol>
</div>
</li>
<li>
<p>definition-site variance may not be combined with incompatible use-site variance:</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>if <code>T</code> is declared on definition site to be <strong>covariant</strong>, then no wildcard with a <strong>lower</strong> bound may be provided as type argument for <code>T</code>.</p>
</li>
<li>
<p>if <code>T</code> is declared on definition site to be <strong>contravariant</strong>, then no wildcard with an <strong>upper</strong> bound may be provided as type argument for <code>T</code>.</p>
</li>
<li>
<p>if <code>T</code> is <strong>invariant</strong>, i.e. neither declared covariant nor declared contravariant on definition site, then any kind of wildcard may be provided as type argument.</p>
<div class="paragraph">
<p>Unbounded wildcards are allowed in all cases.</p>
</div>
</li>
</ol>
</div>
</li>
</ol>
</div>
</div>
</div>
<div id="ex:use-site-declaration-variance" class="exampleblock">
<div class="title">Example 35. Use-site declaration of variance</div>
<div class="content">
<div class="paragraph">
<p>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.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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;
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Function <code>getNameOfFirstPerson</code> 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 <code>List</code>s of any subtype of <code>Person</code>, for example a <code>List&lt;Employee&gt;</code>.
To allow this, its formal parameter has a type of <code>List&lt;? extends Person&gt;</code> instead of <code>List&lt;Person&gt;</code>.
Such use-site variance is useful whenever an invariant type, like <code>List</code> above, is being used in a way such that it can be treated as if it were co- or contravariant.</p>
</div>
<div class="paragraph">
<p>Sometimes, however, we are dealing with types that are inherently covariant or contravariant, for example an <code>ImmutableList</code> 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 <code>getNameOfFirstPerson</code> with a <code>List&lt;Employee&gt;</code>).</p>
</div>
<div class="paragraph">
<p>The solution is to declare the variance on declaration site, as in the following code sample:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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;
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Now we can invoke <code>getNameOfFirstPerson2</code> with a <code>List&lt;Employee&gt;</code> even though the implementor of <code>getNameOfFirstPerson2</code> did not add a
use-site declaration of covariance, because the type <code>ImmutableList</code> is declared to be covariant with respect to its parameter <code>T</code>, and this applies globally
throughout the program.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_members"><a class="anchor" href="#_members"></a><a class="link" href="#_members">5.2. Members</a></h3>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>(overriding, implementation and consumption) is
described in <a href="#_redefinition-of-members">Redefinition of Members</a>.</p>
</div>
<div class="sect3 language-n4js">
<h4 id="_syntax-4"><a class="anchor" href="#_syntax-4"></a><a class="link" href="#_syntax-4">5.2.1. Syntax</a></h4>
<div class="listingblock">
<div class="title">Syntax N4JS member access modifier</div>
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">enum N4JSMemberAccessModifier: private | project | protected | public;
N4MemberDeclaration: N4MethodDeclaration | N4FieldDeclaration | N4GetterDeclaration | N4SetterDeclaration;</code></pre>
</div>
</div>
<div class="sect4">
<h5 id="_properties-3"><a class="anchor" href="#_properties-3"></a><a class="link" href="#_properties-3">5.2.1.1. Properties</a></h5>
<div class="paragraph">
<p>Members share the following properties:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>annotations</code> </dt>
<dd>
<p>Arbitrary annotations, see <a href="#_annotations">Annotations</a> for details.</p>
</dd>
<dt class="hdlist1"><code>accessModifier</code> </dt>
<dd>
<p>N4JS member access modifier: <code>private</code>, <code>project</code>, <code>potected</code>, or <code>public</code>; the latter two can be combined with <code>@Internal</code>; default is <code>project</code> for classes and private interfaces.
For a non-private interface defaults to the interface’s visibility.</p>
</dd>
<dt class="hdlist1"><code>name</code> </dt>
<dd>
<p>The simple name of the member, that is an identifier name (cf. <a href="#_valid-names">Valid Names</a>).</p>
</dd>
<dt class="hdlist1"><code>static</code> </dt>
<dd>
<p>Boolean property to distinguish instance from classifier members, see <a href="#_static-members">Static Members</a>.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>The following pseudo properties are defined via annotations:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>deprecated</code> </dt>
<dd>
<p>Boolean property set to true if annotation <code>@Deprecated</code> is set. <sup class="footnote">[<a id="_footnoteref_29" class="footnote" href="#_footnote_29" title="View footnote.">29</a>]</sup></p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>And we additionally define the following pseudo properties:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>acc</code> </dt>
<dd>
<p>Member access modifier as described in <a href="#_accessibility-of-members">Accessibility of Members</a>, 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.</p>
</dd>
<dt class="hdlist1"><code>owner</code> </dt>
<dd>
<p>Owner classifier of the member.</p>
</dd>
<dt class="hdlist1"><code>typeRef</code> </dt>
<dd>
<p>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).</p>
</dd>
<dt class="hdlist1"><code>assignability</code> </dt>
<dd>
<p>Enumeration, may be one of the following values:</p>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>set</code></dt>
<dd>
<p>Member may only be set, i.e. it could only be used on the left hand side of an assignment.</p>
</dd>
<dt class="hdlist1"><code>get</code></dt>
<dd>
<p>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.</p>
</dd>
<dt class="hdlist1"><code>any</code></dt>
<dd>
<p> 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.</p>
</dd>
</dl>
</div>
</dd>
</dl>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
<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:<br>
</td>
</tr>
</table>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>abstract</code> </dt>
<dd>
<p>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.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>The following pseudo property is set to make fields compatible with properties of an object literal, however it cannot be changed:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>configurable</code> </dt>
<dd>
<p>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.</p>
</dd>
</dl>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_semantics-3"><a class="anchor" href="#_semantics-3"></a><a class="link" href="#_semantics-3">5.2.2. Semantics</a></h4>
<div class="paragraph">
<p>The members of a given classifier <code>C</code> must be named such that the following constraints are met:</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-52"></a><strong>Req. IDE-52:</strong> <a href="#Req-IDE-52">Member Names</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>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 <a href="#compile-time-expressions">Compile-Time Expressions</a>). In particular, string literals, e.g. <code>['myProp']</code>, built-in symbols, e.g. <code>[Symbol.iterator]</code>, and literals of <code>@StringBased</code> enums are all valid computed property names.</p>
</li>
<li>
<p>No two members may have the same name, except one is static and the
other is non-static:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>The member name must be a valid identifier name, see <a href="#_identifier-names-and-identifiers">Identifier Grammar</a>.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>Thus, over<em>loading</em> of methods is not supported <sup class="footnote">[<a id="_footnoteref_30" class="footnote" href="#_footnote_30" title="View footnote.">30</a>]</sup> and no field may have the same name as a method.
However, over<em>riding</em> of methods, getters, and setters are possible, see <a href="#_redefinition-of-members">Redefinition of Members</a>.
Static members may also have the same name as non-static members.<sup class="footnote">[<a id="_footnoteref_31" class="footnote" href="#_footnote_31" title="View footnote.">31</a>]</sup></p>
</div>
<div class="paragraph">
<p>The dollar character <code>$</code> is not allowed for user-defined member identifiers as the dollar sign is used for rewriting private members.</p>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_methods"><a class="anchor" href="#_methods"></a><a class="link" href="#_methods">5.2.3. Methods</a></h4>
<div class="paragraph">
<p>Methods are simply JavaScript functions.
They are defined similarly to methods as proposed in [<a href="#ECMA15a">ECMA15a(p.S13.5)</a>] except for the type information and some modifiers.</p>
</div>
<div class="sect4">
<h5 id="_syntax-5"><a class="anchor" href="#_syntax-5"></a><a class="link" href="#_syntax-5">5.2.3.1. Syntax</a></h5>
<div class="listingblock">
<div class="title">Syntax Method Declaration</div>
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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
;</code></pre>
</div>
</div>
</div>
<div class="sect4 language-n4js">
<h5 id="_properties-4"><a class="anchor" href="#_properties-4"></a><a class="link" href="#_properties-4">5.2.3.2. Properties</a></h5>
<div class="paragraph">
<p>Methods have all the properties of members and the following additional properties can be explicitly defined:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>abstract</code> </dt>
<dd>
<p>Method is declared but not defined.</p>
</dd>
<dt class="hdlist1"><code>typePars</code> </dt>
<dd>
<p>Collection of type parameters of a generic method; empty by default.</p>
</dd>
<dt class="hdlist1"><code>returnTypeRef</code> </dt>
<dd>
<p>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.</p>
</dd>
<dt class="hdlist1"><code>fpars</code> </dt>
<dd>
<p>List of formal parameters, may be left empty.</p>
</dd>
<dt class="hdlist1"><code>body</code> </dt>
<dd>
<p>The body of the method (this is not available in the pure types model)</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>The following pseudo properties are defined via annotations:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>final</code> </dt>
<dd>
<p>Boolean flag set to true if annotation <code>@Final</code> is set.
The flag indicates that method must not be overridden in subclasses; see <a href="#_final-methods">Final Methods</a>.</p>
</dd>
<dt class="hdlist1"><code>declaresOverride</code> </dt>
<dd>
<p>Flag set to true if annotation <code>@Overrides</code> is set. The flag indicates that method must override a method of a superclass; see <a href="#_overriding-of-members">Overriding of Members</a>.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>Additionally, we define the following pseudo properties:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>overrides</code> </dt>
<dd>
<p>True if method overrides a super method or implements an interface method, false otherwise.</p>
</dd>
<dt class="hdlist1"><code>typeRef</code> </dt>
<dd>
<p>Type of the method. This is, in fact, a function type (and not the return type).</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>The following pseudo property is set to make methods compatible with properties of an object literal, however it cannot be changed:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>enumerable</code> </dt>
<dd>
<p>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.</p>
</dd>
</dl>
</div>
</div>
<div class="sect4">
<h5 id="_semantics-4"><a class="anchor" href="#_semantics-4"></a><a class="link" href="#_semantics-4">5.2.3.3. Semantics</a></h5>
<div class="paragraph">
<p>Since methods are ECMAScript functions, all constraints specified in <a href="#_function-type">Function Type</a> apply to methods as well.
This section describes default values and function type conformance which is required for overriding and implementing methods.</p>
</div>
<div class="paragraph">
<p>In addition, method declarations and definitions have to comply with the constraints for naming members of classifiers (cf. <a href="#Req-IDE-52">[Req-IDE-52]</a>)
and with the constraints detailed in the following sections on final methods (<a href="#_final-methods">Final Methods</a>), abstract methods (<a href="#_abstract-methods">Abstract Methods</a> and
method overriding and implementation (<a href="#_overriding-of-members">Overriding of Members</a>, <a href="#_implementation-of-members">Implementation of Members</a>).</p>
</div>
<div class="paragraph">
<p>The following constraints are defined for methods in ECMAScript 6 [<a href="#ECMA15a">ECMA15a(p.207)</a>]</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-53"></a><strong>Req. IDE-53:</strong> <a href="#Req-IDE-53">Method Definition ECMAScript 6</a> (ver. 1)</p>
</div>
<div class="ulist">
<ul>
<li>
<p>It is a Syntax Error if any element of the BoundNames of StrictFormalParameters also occurs in the VarDeclaredNames of FunctionBody.</p>
</li>
<li>
<p>It is a Syntax Error if any element of the BoundNames of StrictFormalParameters also occurs in the LexicallyDeclaredNames of FunctionBody.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="paragraph">
<p>Methods – like functions – define a variable execution environment and therefore provide access to the actual passed-in parameters through the implicit <code>arguments</code> variable inside of their bodies (c.f. <a href="#_arguments-object">Arguments Object</a>).</p>
</div>
<div class="paragraph">
<p>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 <code>v</code>:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {
m(): void {}
}
var v: {function():void} = new C().m;</code></pre>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-54"></a><strong>Req. IDE-54:</strong> <a href="#Req-IDE-54">Method Assignment</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>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 <a href="#_acronyms">Acronyms</a> of a class, their implementation is internal to the class.</p>
</li>
<li>
<p>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:</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>The method’s body can be determined at compile time (i.e., it has been declared <code>@Final</code>) and it lacks usages of <code>this</code> or <code>super</code>. This is true for instance and static methods.</p>
</li>
<li>
<p>The method is the constructor.</p>
</li>
</ol>
</div>
</li>
</ol>
</div>
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
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:
</td>
</tr>
</table>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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</code></pre>
</div>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var v1: {@This(C)function():void} = c.m;
var v2: {@This(C)function():void} = d.m;
v1.call(c);
v2.call(c);</code></pre>
</div>
</div>
<div class="paragraph">
<p>Calling <code>c.m</code> indirectly via <code>v1</code> with <code>c</code> as this object will work.
However, it won’t work for <code>v2</code>: the method is overridden in <code>D</code>, and the method in expects other methods available in <code>D</code> but not in <code>C</code>.
That is, the last call would lead to a runtime error as method <code>f</code> which is called in <code>D.m</code> won’t be available.</p>
</div>
<div class="paragraph">
<p>The same scenario occurs in case of static methods if they are retrieved polymorphically via the variables of type <code>constructor{C}</code>:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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;</code></pre>
</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>The following example shows a problem with breaking the encapsulation of a class.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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)</code></pre>
</div>
</div>
<div class="paragraph">
<p>In <code>D</code>, method <code>g</code> is overridden to accept more types as the original method defined in <code>C</code>.
Calling this new method with receiver type <code>C</code> (as done in the last line) will cause problems, as in <code>D</code> not only <code>f</code> has been adapted but also <code>g</code>.
Eventually, this would lead to a runtime error as well.</p>
</div>
</div>
<div class="sect4">
<h5 id="_final-methods"><a class="anchor" href="#_final-methods"></a><a class="link" href="#_final-methods">5.2.3.4. Final Methods</a></h5>
<div class="paragraph">
<p>By default, methods can be overridden.
To prevent a method from being overridden, it must be annotated with <code>@Final</code>.</p>
</div>
<div class="paragraph">
<p>Of course, a method cannot be declared both abstract and final (cf. <a href="#Req-IDE-46">[Req-IDE-46]</a>).
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.</p>
</div>
<div class="paragraph">
<p>Default methods in interfaces, cf. <a href="#_default-methods-in-interfaces">Default Methods in Interfaces</a>, may also be declared <code>@Final</code>.</p>
</div>
<div class="exampleblock">
<div class="title">Example 36. Final Methods in Interfaces</div>
<div class="content">
<div class="paragraph">
<p>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;</p>
</div>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>a class implementing that interface must not define a method with the same name and</p>
</li>
<li>
<p>a class inheriting a method of that name cannot implement this interface.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>The latter case is illustrated here:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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 {
}</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_abstract-methods"><a class="anchor" href="#_abstract-methods"></a><a class="link" href="#_abstract-methods">5.2.3.5. Abstract Methods</a></h5>
<div class="paragraph">
<p>A method can be declared without defining it, i.e. without providing a method body, and is then called an <em>abstract method</em>.
Such methods must be declared with modifier <code>abstract</code> 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 <a href="#Req-IDE-46">[Req-IDE-46]</a> (see <a href="#_abstract-classes">Abstract Classes</a>).</p>
</div>
<div class="paragraph">
<p>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 <a href="#_implementation-of-members">Implementation of Members</a> about how the default implementation may be mixed in the consumer.</p>
</div>
</div>
<div class="sect4 language-n4js">
<h5 id="_generic-methods"><a class="anchor" href="#_generic-methods"></a><a class="link" href="#_generic-methods">5.2.3.6. Generic Methods</a></h5>
<div class="paragraph">
<p>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 <a href="#_generic-functions">Generic Functions</a>).</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-55"></a><strong>Req. IDE-55:</strong> <a href="#Req-IDE-55">Type variable names for generic methods</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a given generic method <code>M</code> of a class <code>C</code>, the following
constraint must hold:<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></p>
</div>
</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {
&lt;T&gt; foo(p: T p): T { return p;}
};</code></pre>
</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_default-methods-in-interfaces"><a class="anchor" href="#_default-methods-in-interfaces"></a><a class="link" href="#_default-methods-in-interfaces">5.2.4. Default Methods in Interfaces</a></h4>
<div class="paragraph">
<p>If a method declared in an interface defines a body, then this is the so-called <em>default implementation</em> and the method is called a <em>default method</em>.
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 <a href="#_member-consumption">Member Consumption</a>.
Since the implementor is not known, some constraints exist for the body. I.e., no access to super is possible, cf. <a href="#Req-IDE-124">[Req-IDE-124]</a>.</p>
</div>
<div class="paragraph">
<p>In order to declare an interface to provide a default implementation in a definition file, annotation <code>@ProvidesDefaultImplementation</code> can be used, cf. <a href="#Req-IDE-167">[Req-IDE-167]</a>.</p>
</div>
<div class="paragraph">
<p>When a method in an interface is provided with a default implementation, it may even be declared <code>@Final</code>, see <a href="#_final-methods">Final Methods</a>.</p>
</div>
<div class="sect4">
<h5 id="_asynchronous-methods"><a class="anchor" href="#_asynchronous-methods"></a><a class="link" href="#_asynchronous-methods">5.2.4.1. Asynchronous Methods</a></h5>
<div class="paragraph">
<p>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 <a href="#_asynchronous-functions">Asynchronous Functions</a> and <a href="#_asynchronous-arrow-functions">Asynchronous Arrow Functions</a> for details.</p>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_constructors"><a class="anchor" href="#_constructors"></a><a class="link" href="#_constructors">5.2.5. Constructors</a></h4>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>For a given a class <code>C</code>, the constructor is available via two properties:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><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></dt>
<dd>
<p>the explicitly defined constructor (if any).</p>
</dd>
<dt class="hdlist1"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math></dt>
<dd>
<p>the explicit or implicit constructor.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>If <code>C</code> 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.</p>
</div>
<div class="paragraph">
<p>The return type of the constructor of a class <code>C</code> is <code>C</code>.
If <code>C</code> 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:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {
public constructor(s: string) {
// init something
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>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. <a href="#_arguments-object">Arguments Object</a>).</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-56"></a><strong>Req. IDE-56:</strong> <a href="#Req-IDE-56">Defining and Calling Constructors</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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 <code>C</code>, the following conditions
must hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><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 <code>@Override</code>.</p>
</li>
<li>
<p>If a class does not explicitly define a constructor then the constructor’s signature of the superclass constructor is assumed.</p>
</li>
<li>
<p>If a class defines a constructor with formal parameters then this constructor has to be called explicitly in constructors defined in subclasses.</p>
</li>
<li>
<p>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.</p>
</li>
<li>
<p>Constructors may appear in interfaces, but some restrictions apply:</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>constructors in interfaces must not have a body.</p>
</li>
<li>
<p>constructors in interfaces or their containing interface or one of its direct or indirect super interfaces must be annotated with <code>@CovariantConstructor</code>.</p>
</li>
</ol>
</div>
</li>
<li>
<p>A constructor must not have an explicit return type declaration.</p>
</li>
<li>
<p>The implicit return type of a constructor is <code>this?</code>.</p>
</li>
<li>
<p>A constructor must not have any type parameters.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>Properties of object literals may be called <code>constructor</code>.
However they are not recognized as constructors in these cases.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-57"></a><strong>Req. IDE-57:</strong> <a href="#Req-IDE-57">Initialization of Final Fields in the Constructor</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Required attributes must be initialized:<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></p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>Note on syntax: ECMAScript 6 defines constructors similarly, [<a href="#ECMA15a">ECMA15a(p.S13.5)</a>]. In
ECMAScript 6 the super constructor is not called automatically as well.</p>
</div>
<div class="paragraph">
<p>The super literal used in order to call super methods is further
described in <a href="#_the-super-keyword">The super Keyword</a>.</p>
</div>
<div class="sect4">
<h5 id="_structural-this-type-in-constructor"><a class="anchor" href="#_structural-this-type-in-constructor"></a><a class="link" href="#_structural-this-type-in-constructor">5.2.5.1. Structural This Type in Constructor</a></h5>
<div class="paragraph">
<p>The use of a structural this reference as a formal parameter type is possible only in constructors.
This parameter can be annotated with <code>@Spec</code> which causes the compiler to generate initialization code.</p>
</div>
<div class="paragraph">
<p>Simply using <code>this</code> 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.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class A{
public s: string;
public constructor(src: ~~this) {
this.s = src.s;
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Remarks:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The type of the formal parameter <code>~~this</code> refers to the structural field type, see <a href="#_structural-typing">Structural Typing</a> for details on structural typing.
It contains all public fields of the type.</p>
</li>
<li>
<p>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 <code>this</code> is replaced with the actual subclass, but in the case of a <code>super()</code> call the <code>this</code> type of structural formal parameters is replaced with the <code>this</code> type of the superclass,
hence only required fields of the superclass must be present.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class A{
public s: string;
private myPrivateNumber: number;
public constructor(src: ~~this with { x: number; }) {
this.s = src.s;
this.myPrivateNumber = src.x;
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Defining additional members may become a problem if a subclass defines public fields with the same name, as the <code>~~this</code> type will contain these fields in the subclass.
This is marked as an error in the subclass.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-58"></a><strong>Req. IDE-58:</strong> <a href="#Req-IDE-58">Names of additional members of structural this type in constructor</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>If the structural this type is used in a constructor of a class <code>C</code>, 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:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>For any subclass <code>S</code> of <code>C</code>, 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), <code>S</code> 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>:</p>
<div class="paragraph">
<p><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></p>
</div>
</li>
<li>
<p><code>C</code> 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>:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
</ol>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 37. Field name conflicts with structural member name</div>
<div class="content">
<div class="paragraph">
<p>The situation described in <a href="#Req-IDE-58">[Req-IDE-58]</a> is demonstrated in the following code fragment:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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
}</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="spec-constructor"><a class="anchor" href="#spec-constructor"></a><a class="link" href="#spec-constructor">5.2.5.2. @Spec Constructor</a></h5>
<div class="paragraph">
<p>The tedious process of copying the members of the parameter to the fields of the class can be automated via the <code>@Spec</code>
annotation if the argument has <code>~i~this</code> structural initializer field typing. More details about this typing can be
found in <a href="#structural-readWriteInit-field-typing">Structural Read-only, Write-only and Initializer Field Typing</a>. This can be used as shown in the following listing:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class A {
public field: string;
public constructor(@Spec spec: ~i~this) {}
}
let a = new A({field: 'hello'});
console.log(a.field); // prints: hello</code></pre>
</div>
</div>
<div class="paragraph">
<p>The code for initializing the public field of <code>A</code> is automatically generated, thanks to the <code>@Spec</code> annotation being
given in the constructor.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-59"></a><strong>Req. IDE-59:</strong> <a href="#Req-IDE-59">@Spec Constructor</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Annotation <code>@Spec</code> may only appear on a formal parameter of a constructor. Such a formal parameter is then called
<em>@Spec parameter</em> or simply <em>spec parameter</em> and its owning constructor is referred to as a <em>@Spec constructor</em> or
<em>spec constructor</em>. An argument to the spec parameter is called <em>spec object</em>.</p>
</li>
<li>
<p>Only a single formal parameter of a constructor may be annotated with <code>@Spec</code>.</p>
</li>
<li>
<p>If a formal parameter is annotated with <code>@Spec</code>, the parameter’s type must be <code>~i~this</code> (i.e. a use-site
structural initializer field type of <code>this</code>, see <a href="#structural-readWriteInit-field-typing">Structural Read-only, Write-only and Initializer Field Typing</a>).</p>
</li>
<li>
<p>Using the data provided in the spec object, i.e. in the argument to the spec parameter, a spec constructor will
automatically initialize</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>all <strong>owned</strong> data fields and <strong>owned</strong> setters of the containing class, and</p>
</li>
<li>
<p>all data fields and setters from interfaces implemented by the containing class</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>if and only if those members are also part of the spec parameter&#8217;s structural initializer field type.</p>
</div>
</li>
<li>
<p>Fields explicitly added to the spec parameter, e.g. <code>@Spec spec: ~i~this with {name:string}</code>, 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:</p>
<div class="paragraph">
<p><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><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></p>
</div>
</li>
<li>
<p>Even if the <code>@Spec</code> annotation is used, the super constructor must be invoked explicitly (as usual).</p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>It follows from no. 4 above that</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>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),</p>
</li>
<li>
<p>properties provided in the spec object but not defined in the parameter&#8217;s structural initializer field type, are
<em>not</em> used for initialization, even if a (protected or private) field of the same name exists in the class,</p>
</li>
<li>
<p>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).</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>The last of these implications will be detailed further at the end of the coming section.</p>
</div>
<div class="paragraph">
<p><strong>@Spec Constructors and Inheritance</strong></p>
</div>
<div class="paragraph">
<p>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 <strong>and</strong> include
initialization code for them.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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</code></pre>
</div>
</div>
<div class="paragraph">
<p>Public writable fields from implemented interfaces are included as well, i.e. required as property in spec object <strong>and</strong>
initialized by auto-generated code in the <code>@Spec</code> constructor:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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</code></pre>
</div>
</div>
<div class="paragraph">
<p>When having a spec constructor in a class <code>B</code> that extends a super class <code>A</code> without an owned or inherited spec
constructor, it should be noted that the <code>~i~this</code> type will require properties for public writable fields of <code>A</code>,
but the initialization code automatically generated due to the <code>@Spec</code> annotation will <strong>not</strong> initialize those members.
For public writable fields from an interface <code>I</code> implemented by <code>B</code>, however, both a property will be required by
<code>~i~this</code> <strong>and</strong> initialization code will be generated in the <code>@Spec</code> constructor. This is illustrated in the
following code example.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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); // !!</code></pre>
</div>
</div>
<div class="paragraph">
<p>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 <code>@Spec</code> 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-<code>@Spec</code>
constructor of the superclass with <code>super</code>).</p>
</div>
<div class="paragraph">
<p><strong>Special Use Cases</strong></p>
</div>
<div class="paragraph">
<p>The following examples illustrate further details of other use cases of spec constructors.</p>
</div>
<div class="exampleblock">
<div class="title">Example 38. Anonymous Interface in Constructor</div>
<div class="content">
<div class="paragraph">
<p>The base class <code>A</code> in the examples redefines the constructor already defined in <code>N4Object</code>. This is not
generally necessary and is only used here to make the example legible.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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
}
}</code></pre>
</div>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 39. Spec Object and Subclasses</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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!
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The last case (class E) demonstrates a special feature of the typing strategy modifier in combination with the <code>this</code> type, see <a href="#_structural-typing">Structural Typing</a> for details.</p>
</div>
<div class="paragraph">
<p>The constructor in class <code>B</code> contains an error because the super constructor expects all required attributes in <code>A1</code> to be set.
The additional initialization of the required field <code>A1.n</code> as seen in <code>C</code> does not change that expectation.
In this example, the field <code>n</code> should not have been defined as required in the first place.</p>
</div>
<div class="paragraph">
<p>Optional fields like <code>n?</code> in class <code>A2</code> or fields with default values like <code>n=10</code> in class <code>A3</code> are not required to be part of the <code>spec</code> object.</p>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 40. Superfluous Properties in @Spec Constructors</div>
<div class="content">
<div class="paragraph">
<p>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 <em>not</em> used to set non-<math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle></math> fields.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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 } );</code></pre>
</div>
</div>
</div>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1">Restriction when initializing interface fields via @Spec constructor <a id="restriction-interface-field-spec-constructor"></a> </dt>
<dd>
<p>In most cases, interface definitions in <code>n4jsd</code> 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 <code>n4jsd</code> files can initialized via <code>@Spec</code> constructors or not.
In particular, fields of an interface declared in a <code>n4jsd</code> file cannot be initialized via @Spec constructor if the interface</p>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>is a built-in or</p>
</li>
<li>
<p>does not have an <code>@N4JS</code> annotation</p>
</li>
</ol>
</div>
</dd>
</dl>
</div>
<div class="paragraph">
<p>The following example illustrates this restriction.</p>
</div>
<div class="exampleblock">
<div class="title">Example 41. Interface fields that cannot be initialized via @Spec constructors</div>
<div class="content">
<div class="listingblock">
<div class="title">Inf.n4jsd</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">export external interface I {
public m: string;
}
@N4JS
export external interface J {
public n: string;
}</code></pre>
</div>
</div>
<div class="listingblock">
<div class="title">Test.n4js</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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;
--- */</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>In this example, the interface <code>I</code> is defined in the <code>Inf.n4jsd</code> file without the <code>@N4JS</code> annotation. As a result, its field <code>m</code> cannot be initialized via the <code>@Spec</code> constructor and hence the output of <code>console.log(c.m)</code> is <code>undefined</code>. On the other hand, since the interface <code>J</code> is declared with the annotation <code>@N4JS</code>, it is possible to initialize its field <code>n</code> in the <code>@Spec</code> constructor. That&#8217;s why the result of <code>console.log(d.n)</code> is <code>Bye</code>.</p>
</div>
</div>
<div class="sect4">
<h5 id="_callable-constructors"><a class="anchor" href="#_callable-constructors"></a><a class="link" href="#_callable-constructors">5.2.5.3. Callable Constructors</a></h5>
</div>
<div class="sect4">
<h5 id="_covariant-constructors"><a class="anchor" href="#_covariant-constructors"></a><a class="link" href="#_covariant-constructors">5.2.5.4. Covariant Constructors</a></h5>
<div class="paragraph">
<p>Usually, the constructor of a subclass need not be override compatible with the constructor of its super class.
By way of annotation <code>@CovariantConstructor</code> 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 <code>@Spec</code> constructors) or by defining a new constructor with a signature compatible to the inherited constructor.
The same rules as for method overriding apply.</p>
</div>
<div class="paragraph">
<p>The <code>@CovariantConstructor</code> 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 <code>@CovariantConstructor</code> (see <a href="#Req-IDE-60">[Req-IDE-60]</a>).</p>
</div>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="covariant_constructor"></a><strong>Definition:</strong> <a href="#covariant_constructor">Covariant Constructor</a></p>
</div>
<div class="paragraph">
<p>A classifier <code>C</code> is said to <code><em>have a covariant constructor</em></code> if and
only if one of the following applies:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><code>C</code> 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 <code>@CovariantConstructor</code> or <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>C</mi><mi>'</mi></msup></math> has a constructor annotated with <code>@CovariantConstructor</code>.</p>
</li>
<li>
<p><code>C</code> has a directly implemented interface <code>I and `I</code> is annotated with <code>@CovariantConstructor</code> or <code>I</code> has a constructor annotated with <code>@CovariantConstructor</code>.</p>
</li>
<li>
<p><code>C</code> has a direct super class or directly implemented interface that <code><em>has a covariant constructor</em></code> (as defined here).</p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>Note that <code>C</code> does not need to have an owned(!) constructor; also a constructor inherited from a super class can be declared covariant.</p>
</div>
<div class="paragraph">
<p>The following rules apply to covariant constructors.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-60"></a><strong>Req. IDE-60:</strong> <a href="#Req-IDE-60">Covariant Constructors</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Annotation <code>@CovariantConstructor</code> 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.</p>
</li>
<li>
<p>Given a class <code>C</code> 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 <a href="#_covariant-constructors">Covariant Constructors</a>),
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 <code>C</code>,</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p><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>:</p>
<div class="paragraph">
<p><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></p>
</div>
<div class="paragraph">
<p>This constraint corresponds to <a href="#Req-IDE-72">[Req-IDE-72]</a> except for the <code>Override</code> annotation which is not required here.</p>
</div>
</li>
</ol>
</div>
</li>
<li>
<p>Given a classifier <code>C</code> implementing interface <code>I</code> and <code>I</code> has a covariant constructor (owned or inherited, see <a href="#_covariant-constructors">Covariant Constructors</a>), we require</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p><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 <code>C</code>,</p>
</li>
<li>
<p>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</p>
<div class="paragraph">
<p><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></p>
</div>
<div class="paragraph">
<p>This constraint corresponds to <a href="#Req-IDE-74">[Req-IDE-74]</a> except for the <code>@Override</code> annotation, which is not required, here.</p>
</div>
</li>
<li>
<p>Given a classifier <code>C</code> 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 <a href="#_covariant-constructors">Covariant Constructors</a>),
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 <code>C</code> within the context of <code>C</code> 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 <code>M</code> is to be treated as defined in container type <code>T</code>, which means the this-binding is set to <code>T</code>, we can write:</p>
<div class="paragraph">
<p><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></p>
</div>
<div class="paragraph">
<p>This constraint does not correspond to any of the constraints for the redefinition of ordinary members.</p>
</div>
</li>
</ol>
</div>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>The following example demonstrates a use case for covariant constructors.
It shows a small class hierarchy using covariant constructors, <code>Cls</code> and <code>Cls2</code>, together with a helper function <code>createAnother</code> that creates and returns a new instance of the same type as its argument <code>value</code>.</p>
</div>
<div id="ex:covariant_constructors" class="exampleblock">
<div class="title">Example 42. Covariant Constructors</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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());</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>In the code of <a href="#ex:covariant_constructors">Covariant Constructors</a>, we would get an error if we changed the type of parameter <code>p</code> in the constructor of <code>Cls2</code> to some other type that
is not a super type of <code>B</code>, i.e. the type of the corresponding parameter of <code>Cls</code>’s constructor.
If we removed the <code>@CovariantConstructor</code> annotation on <code>Cls</code>, we would get an error in the new expression inside function <code>createAnother</code>.</p>
</div>
<div class="paragraph">
<p>The next example illustrates how to use <code>@CovariantConstructor</code> with interfaces and shows a behavior that might be surprising at first sight.</p>
</div>
<div id="ex:covariant-constructors-in-interfaces" class="exampleblock">
<div class="title">Example 43. Covariant Constructors in Interfaces</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">@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) {}
}</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>Interface <code>I</code> declares a covariant constructor expecting a single parameter of type <code>number</code>.
Even though class <code>C</code> implements <code>I</code>, it does not need to define an owned constructor with such a parameter.
According to <a href="#Req-IDE-60">[Req-IDE-60]</a>, it is enough for <code>C</code> to have a constructor, either owned or inherited, that is override compatible with the one declared by <code>I</code>.
Class <code>C</code> inherits the default constructor from <code>N4Object</code>, which does not have any arguments and is thus override compatible to <code>I</code>’s constructor.</p>
</div>
<div class="paragraph">
<p>In addition, subclasses are now required to have constructors which are override compatible with the constructor of class <code>C</code>, i.e. the one inherited from <code>N4Object</code>.
<a href="#ex:covariant-constructors-in-interfaces">Covariant Constructors in Interfaces</a> shows that this is violated even when repeating the exact same constructor signature from interface <code>I</code>,
because that constructor now appears on the other side of the subtype test during checking override compatibility.</p>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_data-fields"><a class="anchor" href="#_data-fields"></a><a class="link" href="#_data-fields">5.2.6. Data Fields</a></h4>
<div class="paragraph">
<p>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. <code>this.x = 10</code> implicitly defines a field <code>x</code>).
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.</p>
</div>
<div class="sect4">
<h5 id="data-fields-syntax"><a class="anchor" href="#data-fields-syntax"></a><a class="link" href="#data-fields-syntax">5.2.6.1. Syntax</a></h5>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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
;</code></pre>
</div>
</div>
</div>
<div class="sect4">
<h5 id="data-fields-properties"><a class="anchor" href="#data-fields-properties"></a><a class="link" href="#data-fields-properties">5.2.6.2. Properties</a></h5>
<div class="paragraph">
<p>Fields have the following properties which can be explicitly defined:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>declaredOptional</code> </dt>
<dd>
<p>Tells whether the accessor was declared optional.</p>
</dd>
<dt class="hdlist1"><code>typeRef</code> </dt>
<dd>
<p>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>.</p>
</dd>
<dt class="hdlist1"><code>expr</code> </dt>
<dd>
<p>Initializer expression, i.e. sets default value.</p>
</dd>
<dt class="hdlist1"><code>static</code> </dt>
<dd>
<p>Boolean flag set to true if field is a static field.</p>
</dd>
<dt class="hdlist1"><code>const</code> </dt>
<dd>
<p>Boolean flag set to true if field cannot be changed. Note that const fields are automatically static.
Const fields need an initializer. Also see <a href="#_assignment-modifiers">Assignment Modifiers</a>.</p>
</dd>
</dl>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
<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 <em>not</em> 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.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>The following pseudo properties are defined via annotations for setting the values of the property descriptor:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>enumerable</code> </dt>
<dd>
<p>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 <code>@Enumerable(true|false)</code>.
The default value is <math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>true</mtext></mstyle></math>.<sup class="footnote">[<a id="_footnoteref_32" class="footnote" href="#_footnote_32" title="View footnote.">32</a>]</sup></p>
</dd>
<dt class="hdlist1"><code>declaredWriteable</code> </dt>
<dd>
<p>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 <code>@Writeable(true|false)</code>.
The default value is <math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>true</mtext></mstyle></math>.<sup class="footnote">[<a id="_footnoteref_33" class="footnote" href="#_footnote_33" title="View footnote.">33</a>]</sup></p>
</dd>
<dt class="hdlist1"><code>final</code> </dt>
<dd>
<p>Boolean flag making the field read-only, and it must be set in the constructor. Also see <a href="#_assignment-modifiers">Assignment Modifiers</a>.</p>
</dd>
</dl>
</div>
<h6 id="data-fields-derived-values" class="discrete">Derived values for fields</h6>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>readable</code> </dt>
<dd>
<p>Always true for fields.</p>
</dd>
<dt class="hdlist1"><code>abstract</code> </dt>
<dd>
<p>Always false for fields.</p>
</dd>
<dt class="hdlist1"><code>writeable</code> </dt>
<dd>
<p>Set to false if field is declared const or final. In the latter case, it may be set in the constructor (cf. <a href="#_assignment-modifiers">Assignment Modifiers</a>).</p>
</dd>
</dl>
</div>
<div class="sect5">
<h6 id="data-fields-semantics"><a class="anchor" href="#data-fields-semantics"></a><a class="link" href="#data-fields-semantics">5.2.6.2.1. Semantics</a></h6>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-61"></a><strong>Req. IDE-61:</strong> <a href="#Req-IDE-61">Attributes</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For any attribute <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi></math> if a
class <code>C</code>, the following constraints must hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>A required data field must not define an initializer:<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></p>
</li>
<li>
<p>There must be no other member with the same name of a data field <code>f</code>.
In particular, there must be no getter or setter defined with the same name:<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></p>
</li>
</ol>
</div>
<div class="paragraph">
<p>If a subclass should set a different default value, this has to be done in the constructor of the subclass.</p>
</div>
<div class="paragraph">
<p>For the relation of data fields and field accessors in the context of extending classes or implementing interfaces see <a href="#_redefinition-of-members">Redefinition of Members</a>.</p>
</div>
</div>
</div>
</div>
<div class="sect5">
<h6 id="data-fields-type-inference"><a class="anchor" href="#data-fields-type-inference"></a><a class="link" href="#data-fields-type-inference">5.2.6.2.2. Type Inference</a></h6>
<div class="paragraph">
<p>The type of a field is the type of its declaration:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>The type of a field declaration is either the declared type or the inferred type of the initializer expression:</p>
</div>
<div class="paragraph">
<p><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><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><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><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></p>
</div>
<div class="paragraph">
<p>If the type contains type variables they are substituted according to
type parameters which are provided by the reference:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_assignment-modifiers"><a class="anchor" href="#_assignment-modifiers"></a><a class="link" href="#_assignment-modifiers">5.2.6.3. Assignment Modifiers</a></h5>
<div class="paragraph">
<p>Assignment of data fields can be modified by the assignment modifiers <code>const</code> (similar to constant variable declarations, see <a href="#_const">Const</a>) and <code>@Final</code>.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-62"></a><strong>Req. IDE-62:</strong> <a href="#Req-IDE-62">Const Data Fields</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a data field <code>f</code> marked as <code>const</code>, the following constraints must hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>An initializer expression must be provided in the declaration (except in n4jsd files):<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></p>
</li>
<li>
<p>A constant data field is implicitly static and must be accessed only via the classifier type.
It is not possible, therefore, to use the <code>this</code> keyword in the initializer expression of a constant field:<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></p>
</li>
<li>
<p>A constant data field must not be annotated with <code>@Final</code>:<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></p>
</li>
<li>
<p>Constant data fields are not writeable (cf. <a href="#Req-IDE-68">[Req-IDE-68]</a>):<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></p>
</li>
</ol>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-63"></a><strong>Req. IDE-63:</strong> <a href="#Req-IDE-63">Final Data Fields</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a data field <code>f</code> marked as <code>@Final</code>, the following constraints must hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>A final data field must not be modified with <code>const</code> or <code>static</code>:<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></p>
</li>
<li>
<p>A final data field is not writeable:<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><br>
A final field may, however, be set in the constructor.
See <a href="#Req-IDE-68">[Req-IDE-68]</a> for details.</p>
</li>
<li>
<p>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.</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
</ol>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_field-accessors-getter-setter"><a class="anchor" href="#_field-accessors-getter-setter"></a><a class="link" href="#_field-accessors-getter-setter">5.2.6.4. Field Accessors (Getter/Setter)</a></h5>
<div class="paragraph">
<p>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:</p>
</div>
<div class="sect5">
<h6 id="field-acessors-syntax"><a class="anchor" href="#field-acessors-syntax"></a><a class="link" href="#field-acessors-syntax">5.2.6.4.1. Syntax</a></h6>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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;)? ';'?
;</code></pre>
</div>
</div>
<div class="paragraph">
<p>Notes with regard to syntax: Although ECMAScript 6 does not define fields in classes, it defines getter and setter methods similarly (cf. [<a href="#ECMA15a">ECMA15a(p.S13.3, p.p.209)</a>]).</p>
</div>
<div class="exampleblock">
<div class="title">Example 44. Getter and Setter</div>
<div class="content">
<div class="paragraph">
<p>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.</p>
</div>
<div class="listingblock">
<div class="title">Getter Setter</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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 {
// ...
}
}</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect5">
<h6 id="field-acessors-properties"><a class="anchor" href="#field-acessors-properties"></a><a class="link" href="#field-acessors-properties">5.2.6.4.2. Properties</a></h6>
<div class="paragraph">
<p>Properties for field accessors:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>declaredOptional</code> </dt>
<dd>
<p>Tells whether the accessor was declared optional.</p>
</dd>
<dt class="hdlist1"><code>readable</code> </dt>
<dd>
<p>Derived value: true for getters and false for setters.</p>
</dd>
<dt class="hdlist1"><code>writable</code> </dt>
<dd>
<p>Derived value: false for getters and true for setters.</p>
</dd>
</dl>
</div>
</div>
<div class="sect5">
<h6 id="field-accessors-semantics"><a class="anchor" href="#field-accessors-semantics"></a><a class="link" href="#field-accessors-semantics">5.2.6.4.3. Semantics</a></h6>
<div class="paragraph">
<p>There must be no field or method with the same name as a field accessor (follows from <a href="#Req-IDE-52">[Req-IDE-52]</a>). In addition, the following constraints must hold:</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-64"></a><strong>Req. IDE-64:</strong> <a href="#Req-IDE-64">Field Accessors</a> (ver. 1)</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The return type of a getter must not be <code>void</code>.</p>
</li>
<li>
<p>The type of the parameter of a setter must not be <code>void</code>.</p>
</li>
<li>
<p>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 <code>C</code> and a setter <code>S</code> 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
<code>C</code> from one of its super classes, then <code>C</code> 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> <sup class="footnote">[<a id="_footnoteref_34" class="footnote" href="#_footnote_34" title="View footnote.">34</a>]</sup>.</p>
</li>
<li>
<p>A setter must have exactly one formal parameter, i.e. variadic or default modifiers are not allowed.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The same applies to setters, accordingly.</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#Req-IDE-72">[Req-IDE-72]</a>, <a href="#Req-IDE-73">[Req-IDE-73]</a>, and <a href="#Req-IDE-74">[Req-IDE-74]</a> apply to field accessors accordingly (getter / setter overriding).</p>
</li>
</ul>
</div>
<div class="admonitionblock note language-n4js">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
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).<sup class="footnote">[<a id="_footnoteref_35" class="footnote" href="#_footnote_35" title="View footnote.">35</a>]</sup>
</td>
</tr>
</table>
</div>
</div>
</div>
<div class="paragraph">
<p>Getters and setters – like functions – define a variable execution environment and therefore provide access to the actual passed-in parameters through the implicit <code>arguments</code>
variable inside of their bodies (c.f. <a href="#_arguments-object">Arguments Object</a>).</p>
</div>
</div>
</div>
<div class="sect4">
<h5 id="optional-fields"><a class="anchor" href="#optional-fields"></a><a class="link" href="#optional-fields">5.2.6.5. Optional Fields</a></h5>
<div class="paragraph">
<p>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.</p>
</div>
<div class="sect5">
<h6 id="_syntax-6"><a class="anchor" href="#_syntax-6"></a><a class="link" href="#_syntax-6">5.2.6.5.1. Syntax</a></h6>
<div class="paragraph">
<p>To denote a data field or accessor as optional, a question mark is placed right after the name:</p>
</div>
<div class="listingblock">
<div class="title">Syntax of optional fields</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {
public field?: string;
public get getter?(): number {
return 42;
}
public set setter?(value: number) {}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The detailed grammar is given in the sections for data fields, cf. <a href="#data-fields-syntax">Syntax</a>,
and field accessors, cf. <a href="#field-acessors-syntax">Syntax</a>.</p>
</div>
</div>
<div class="sect5">
<h6 id="_semantics-5"><a class="anchor" href="#_semantics-5"></a><a class="link" href="#_semantics-5">5.2.6.5.2. Semantics</a></h6>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>The effect of a field being optional is defined by the following requirement.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-240500"></a><strong>Req. IDE-240500:</strong> <a href="#Req-IDE-240500">Optional Fields</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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).</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>an object literal.</p>
</li>
<li>
<p>a new expression.</p>
</li>
<li>
<p>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.</p>
</li>
<li>
<p>a reference to a const variable if its initializer expression is one of the following:</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>an object literal.</p>
</li>
<li>
<p>a new expression.</p>
</li>
<li>
<p>an instance of a final class (as explained above).</p>
</li>
<li>
<p>an ternary expression</p>
</li>
</ol>
</div>
</li>
</ol>
</div>
<div class="paragraph">
<p>and then</p>
</div>
<div class="ulist">
<ul>
<li>
<p>in cases 1 and 4a, <strong>both</strong> 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>.</p>
</li>
<li>
<p>in cases 2, 3, 4b, and 4c, only <strong>getters</strong> 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.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>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. <code>l = b? trueExpr : falseExpr</code> whose <code>trueExpr</code> or <code>falseExpr</code> possibly recursively contains an expression of the kind mentioned above. In this case, the optionality effect is the more restricted optinality of <code>trueExpr</code> and <code>falseExpr</code>.</p>
</div>
<div class="paragraph">
<p>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).</p>
</div>
</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>The following table summarizes the most common cases and shows how this relates to the different
forms of structural typing.</p>
</div>
<table id="tab:optionalFields" class="tableblock frame-all grid-all spread">
<caption class="title">Table 7. Optional Fields</caption>
<colgroup>
<col style="width: 25%;">
<col style="width: 8.3333%;">
<col style="width: 8.3333%;">
<col style="width: 8.3333%;">
<col style="width: 8.3333%;">
<col style="width: 8.3333%;">
<col style="width: 8.3333%;">
<col style="width: 25.0002%;">
</colgroup>
<tbody>
<tr>
<th class="tableblock halign-left valign-top"></th>
<td class="tableblock halign-center valign-top" colspan="5"><p class="tableblock">&#916;</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Case</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Comment</p></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>~</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>~~</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>~w~</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>~r~</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>~i~</code></p></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"></th>
<td class="tableblock halign-center valign-top" colspan="3"><p class="tableblock">may have setter</p></td>
<td class="tableblock halign-center valign-top" colspan="2"><p class="tableblock">never has setter</p></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock"><code>let x: &#916;C = {};</code></p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">nothing mandatory</p></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock"><code>let x: &#916;C = new D0();</code></p></th>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">2</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">setters mandatory</p></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock"><code>let x: &#916;C = new DG();</code></p></th>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">2</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">setters mandatory</p></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock"><code>let x: &#916;C = new DS();</code></p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">2</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">setters mandatory</p></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock"><code>let x: &#916;C = fooD0();</code></p></th>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">none</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">D0 not final</p></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock"><code>let x: &#916;C = fooSF0();</code></p></th>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">none</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">fooSF0() not nominal</p></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock"><code>let x: &#916;C = fooF0();</code></p></th>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">3</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">setters mandatory</p></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>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:</p>
</div>
<div class="listingblock">
<div class="title">Classes and functions used in table</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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(); }</code></pre>
</div>
</div>
<div class="paragraph">
<p>It follows from the above definitions in Requirements <a href="#Req-IDE-240500">[Req-IDE-240500]</a> that cases 4a and 4b are not
transitive across a chain of several <code>const</code> variables, whereas case 4c is transitive. For example:</p>
</div>
<div class="listingblock">
<div class="title">Transitivity of the use cases of optional fields</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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;</code></pre>
</div>
</div>
<div class="paragraph">
<p>The following example demonstrates how optionality behaves in ternay expressions.</p>
</div>
<div class="listingblock">
<div class="title">Optional fields in ternay expressions</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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 : {}</code></pre>
</div>
</div>
</div>
<div class="sect5">
<h6 id="_background"><a class="anchor" href="#_background"></a><a class="link" href="#_background">5.2.6.5.3. Background</a></h6>
<div class="paragraph">
<p>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.).</p>
</div>
<div class="listingblock">
<div class="title">Problem 1 of optional fields</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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</code></pre>
</div>
</div>
<div class="paragraph">
<p>In the last line of the above example, <code>c.field</code> is actually <code>42</code> but the type systems claims it is of type <code>string</code> and
thus allows accessing member <code>charAt</code> of type <code>string</code> which is undefined at runtime the actual value <code>42</code>.</p>
</div>
<div class="paragraph">
<p>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).</p>
</div>
<div class="listingblock">
<div class="title">Problem 2 of optional fields</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {
public field?: string;
}
class D {}
let c: ~C;
c = new D(); // error: new expression but D is missing setter
c.field = "hello";</code></pre>
</div>
</div>
<div class="paragraph">
<p>In the previous code, if <code>c = new D()</code> were allowed, we would add a new property <code>field</code> to the instance of class
<code>D</code> in the last line, which N4JS aims to avoid in general, unless unsafe language features such as dynamic types
are being employed.</p>
</div>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_static-members"><a class="anchor" href="#_static-members"></a><a class="link" href="#_static-members">5.2.7. Static Members</a></h4>
<div class="paragraph">
<p>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 <code>static</code>.
Since they are members of the type, the <code>this</code> keyword is not bound to instances of the class, but again to the type itself.
This is similar as in ECMAScript 6 ([<a href="#ECMA15a">ECMA15a(p.14.5.15)</a>]).
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.</p>
</div>
<div class="paragraph">
<p>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 <a href="#_static-members-of-interfaces">Static Members of Interfaces</a>).</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-65"></a><strong>Req. IDE-65:</strong> <a href="#Req-IDE-65">Static member not abstract</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a static field accessor or method <code>S</code>, the following constraint must hold:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><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></p>
</li>
</ul>
</div>
</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="sect4">
<h5 id="_access-from-and-to-static-members"><a class="anchor" href="#_access-from-and-to-static-members"></a><a class="link" href="#_access-from-and-to-static-members">5.2.7.1. Access From and To Static Members</a></h5>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-66"></a><strong>Req. IDE-66:</strong> <a href="#Req-IDE-66">Accessing Static Members</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>Let <code>M</code> be a static member of class <code>C</code>. Except for write-access to
fields, which will be explained later, you can access <code>M</code>
via:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The class declaration instance, i.e. the classifier or constructor type, <code>constructor{C}</code>, i.e. <code>C.m</code></p>
</li>
<li>
<p>The class declaration instance of a subtype, i.e. the classifier or constructor type, i.e. <code>D.m</code>, if <code>D</code> is a subclass of <code>C</code>.</p>
</li>
<li>
<p><code>v.m</code>, if <code>v</code> is a variable of type <code>C</code> (i.e. classifier type as defined in <a href="#_constructor-and-classifier-type">Constructor and Classifier Type</a>) or a subtype thereof.</p>
</li>
<li>
<p><code>this.m</code> inside the body of any static method declared in <code>C</code> or any sub-class of <code>C</code>.</p>
</li>
<li>
<p>Via a type variable <code>T</code> which upper bound is a subclassof <code>C</code> e.g., <code>function &lt;T extends C&gt; f(){T.m}</code></p>
</li>
</ol>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-67"></a><strong>Req. IDE-67:</strong> <a href="#Req-IDE-67">Static Member Access</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>It is not possible to access instance members from static members.
This is true in particular for type variables defined by a generic classifier.</p>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-68"></a><strong>Req. IDE-68:</strong> <a href="#Req-IDE-68">Write-access to static data fields and static setter</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For static data fields and static setter <code>f</code> the following constraint must hold:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>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>.</p>
</li>
<li>
<p>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>.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="paragraph">
<p>In the special case of <code>m</code> being a static data field, write-access is only possible via the defining type name <code>C.m</code>.
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.<sup class="footnote">[<a id="_footnoteref_36" class="footnote" href="#_footnote_36" title="View footnote.">36</a>]</sup></p>
</div>
<div class="paragraph">
<p>It is even possible to call a static field accessor or method of a class using dynamic polymorphism, as demonstrated in the following example:</p>
</div>
<div id="ex:Polymorphism_and_static_methods" class="exampleblock">
<div class="title">Example 45. Static members of classes, inheritance and polymorphism</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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"</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>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):</p>
</div>
<div class="exampleblock">
<div class="title">Example 46. Static members in Java</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-java" data-lang="java">// !!! 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)
}
}</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_generic-static-methods"><a class="anchor" href="#_generic-static-methods"></a><a class="link" href="#_generic-static-methods">5.2.7.2. Generic static methods</a></h5>
<div class="paragraph">
<p>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 <a href="#Req-IDE-55">[Req-IDE-55]</a>) does not need to hold for generic static methods.</p>
</div>
</div>
<div class="sect4">
<h5 id="_static-members-of-interfaces"><a class="anchor" href="#_static-members-of-interfaces"></a><a class="link" href="#_static-members-of-interfaces">5.2.7.3. Static Members of Interfaces</a></h5>
<div class="paragraph">
<p>Data fields, field accessors and methods of interfaces may be declared
static. A few restrictions apply:</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-69"></a><strong>Req. IDE-69:</strong> <a href="#Req-IDE-69">Static Members of Interfaces</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>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 <a href="#Req-IDE-66">[Req-IDE-66]</a> above, only the first one applies to static members of interfaces).</p>
</li>
<li>
<p>The <code>this</code> 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 <a href="#Req-IDE-173">[Req-IDE-173]</a>.</p>
</li>
<li>
<p>The <code>super</code> 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. <a href="#Req-IDE-123">[Req-IDE-123]</a>).</p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>Note that the <code>this</code> 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.</p>
</div>
<div class="paragraph">
<p>In general, static members may not be abstract, cf. <a href="#Req-IDE-46">[Req-IDE-46]</a>, which applies here as well.
Static methods and field accessors of interfaces, therefore, always have to provide a body.</p>
</div>
<div class="paragraph">
<p>Static members of interfaces are much more restricted than those of classes.
Compare the following example to <a href="#_polymorphism-and-static-methods">Static Polymorphism</a> for classes above:</p>
</div>
<div class="exampleblock">
<div class="title">Example 47. Static members of interfaces</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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)</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_redefinition-of-members"><a class="anchor" href="#_redefinition-of-members"></a><a class="link" href="#_redefinition-of-members">5.2.8. Redefinition of Members</a></h4>
<div class="paragraph">
<p>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.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-70"></a><strong>Req. IDE-70:</strong> <a href="#Req-IDE-70">Override Compatible</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>A member <code>M</code> is <em>override compatible</em> to a member <code>S</code> if and only if the
following constraints hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The name and static modifiers are equal:<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></p>
</li>
<li>
<p>The metatypes are compatible:</p>
<div class="paragraph">
<p><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><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><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><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></p>
</div>
</li>
<li>
<p>The overridden member must not be declared final:<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></p>
</li>
<li>
<p>Overridden member declared const can only be overridden (redefined) by const members:<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></p>
</li>
<li>
<p>It is not possible to override a non-final / non-const field or a setter with a final / const field:<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></p>
</li>
<li>
<p>It is not possible to override a non-abstract member with an abstract one:<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></p>
</li>
<li>
<p>The types are compatible:</p>
<div class="paragraph">
<p><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><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</p>
</div>
</li>
<li>
<p>The access modifier is compatible:<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></p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-71"></a><strong>Req. IDE-71:</strong> <a href="#Req-IDE-71">Non-Override Declaration</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>If and only if a member <code>M</code> of a class <code>C</code> (extending a class <code>S</code> 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:</p>
</div>
<div class="paragraph">
<p><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><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo></mo></math><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><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><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></p>
</div>
</div>
</div>
<div class="sect4 language-n4js">
<h5 id="_overriding-of-members"><a class="anchor" href="#_overriding-of-members"></a><a class="link" href="#_overriding-of-members">5.2.8.1. Overriding of Members</a></h5>
<div class="paragraph">
<p>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>.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-72"></a><strong>Req. IDE-72:</strong> <a href="#Req-IDE-72">Overriding Members</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>Given a class <code>C</code> 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 <code>M</code> defined in <code>C</code> a member <code>S</code> exists with null
then we call <code>M</code> the overriding member and <code>S</code> the overridden member.
In that case the following constraints must hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><code>S</code> must be accessible from <code>C</code></p>
</li>
<li>
<p><code>M</code> must be override compatible with <code>S</code>:<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></p>
</li>
<li>
<p>If <code>S</code> is a field and <code>M</code> 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 <code>S</code>:</p>
<div class="paragraph">
<p><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><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><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></p>
</div>
</li>
<li>
<p><code>M</code> must be declared as override:<br>
<code>M.override</code></p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>Remarks:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>An overridden method, getter, or setter may called via <code>super</code>.
Note that this is not possible for fields.</p>
</li>
<li>
<p>There is no ’hiding’ of fields as in Java, instead there is field overriding.</p>
</li>
<li>
<p>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.</p>
</li>
<li>
<p>Overriding a field usually makes only sense if the visibility of the field is to be increased.</p>
</li>
</ul>
</div>
</div>
<div class="sect4">
<h5 id="_implementation-of-members"><a class="anchor" href="#_implementation-of-members"></a><a class="link" href="#_implementation-of-members">5.2.8.2. Implementation of Members</a></h5>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="interface_and_class_member_sets"></a><strong>Definition:</strong> <a href="#interface_and_class_member_sets">Interface and Class Member Sets</a></p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="paragraph">
<p>Given a <code>C</code>, 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 <code>C</code>, with</p>
</div>
<div class="paragraph">
<p><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><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></p>
</div>
<div class="paragraph">
<p>Note that these sets already contain only non-private data fields.</p>
</div>
</div>
</div>
<div class="sect5">
<h6 id="_member-consumption"><a class="anchor" href="#_member-consumption"></a><a class="link" href="#_member-consumption">5.2.8.2.1. Member Consumption</a></h6>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="member_consumption_and_implementation"></a><strong>Definition:</strong> <a href="#member_consumption_and_implementation">Member Consumption and Implementation</a></p>
</div>
<div class="paragraph">
<p>A member <code>M</code> defined in an interface <code>I</code> is <em>consumed</em> by an implementor <code>C</code>, 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>.</p>
</div>
<div class="paragraph">
<p>A member <code>M</code> 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. <sup class="footnote">[<a id="_footnoteref_37" class="footnote" href="#_footnote_37" title="View footnote.">37</a>]</sup></p>
</div>
<div class="paragraph">
<p>If the implementor defines the member itself, then the member is implemented rather than consumed.</p>
</div>
<div class="paragraph">
<p>The concrete rules are described in the following;</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-73"></a><strong>Req. IDE-73:</strong> <a href="#Req-IDE-73">Consumption of Interface Members</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>Given a classifier <code>C</code> <sup class="footnote">[<a id="_footnoteref_38" class="footnote" href="#_footnote_38" title="View footnote.">38</a>]</sup>, 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 <code>C</code>, 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 <a href="#interface_and_class_member_sets">[interface_and_class_member_sets]</a>.
A non-static member <code>M</code> 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 (<code>C</code>), if for all other (possible) members <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>M</mi><mi>'</mi></msup></math> of <code>C</code></p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>the following constraints hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The other member’s meta type matches the meta type of the merge candiate:</p>
<div class="paragraph">
<p><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><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></p>
</div>
</li>
<li>
<p>The other member is abstract and not owned by the consumer:</p>
<div class="paragraph">
<p><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><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></p>
</div>
</li>
<li>
<p>The merge candidate’s access modifier is not less than the modifier of the other member:</p>
<div class="paragraph">
<p><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><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></p>
</div>
</li>
<li>
<p>The merge candidate’s type compatible with the other member:</p>
<div class="paragraph">
<p><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><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></p>
</div>
</li>
</ol>
</div>
</div>
</div>
</div>
<div class="sect5 language-n4js">
<h6 id="_member-implementation"><a class="anchor" href="#_member-implementation"></a><a class="link" href="#_member-implementation">5.2.8.2.2. Member Implementation</a></h6>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-74"></a><strong>Req. IDE-74:</strong> <a href="#Req-IDE-74">Implementation of Interface Members</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For any non-static abstract member <code>M</code> defined in an interface <code>I implemented (or extended) by a classifier `C</code>, <code>M</code> must be accessible
from <code>C</code> and one or two member(s) in <code>C</code> must exist which are implementation-compatible with <code>M</code>.
The implementing member(s) must be declared as override if they are directly defined in the consumer.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><code>M</code> must be accessible from <code>C</code>.</p>
</li>
<li>
<p>An implementation-compatible member <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>M</mi><mi>'</mi></msup></math> must exist in <code>C</code>:</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>if <code>M</code> is not a field:</p>
<div class="paragraph">
<p><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><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><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><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></p>
</div>
</li>
<li>
<p>if <code>M</code> 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:</p>
<div class="paragraph">
<p><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><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><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><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><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo></mo></math><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><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><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><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><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></p>
</div>
</li>
</ol>
</div>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>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 <code>abstract</code> 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. <a href="#_abstract-classes">Abstract Classes</a>).</p>
</div>
<div class="paragraph">
<p>Consequences for method implementation:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>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.</p>
</li>
<li>
<p>Methods in an implementor cannot decrease the accessibility of methods from implemented interfaces, that is</p>
<div class="paragraph">
<p><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><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></p>
</div>
</li>
<li>
<p>Methods in the implementor must be a supertype <sup class="footnote">[<a id="_footnoteref_39" class="footnote" href="#_footnote_39" title="View footnote.">39</a>]</sup> of methods from implemented interfaces.
That is to say the implemented methods are override-compatible.</p>
</li>
<li>
<p>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 <em>all</em> 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 <em>resolve</em> the conflict between the implemented (and also inherited) methods.</p>
</li>
<li>
<p>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.</p>
</li>
<li>
<p>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.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>There is currently no separate annotation to indicate that methods are implemented or overridden in order to solve conflicts.
We always use the <code>@Override</code> annotation.</p>
</div>
<div class="exampleblock">
<div class="title">Example 48. Method Consumption</div>
<div class="content">
<div class="paragraph">
<p><a href="#tab:methodConsumption">Consumption of methods</a> shows simple examples of above rules.
Assuming that <code>class C</code> extends super <code>class S</code> and implements interface <code>I1</code> and <code>I2</code>:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C extends S implements I1, I2 {...}</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>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 <code>C</code>.
If the method is defined in class <code>C</code>, and if this method is printed bold, then this means that the method is required to be defined in <code>C</code> in order to solve conflicts.</p>
</div>
<table id="tab:methodConsumption" class="tableblock frame-all grid-all spread">
<caption class="title">Table 8. Consumption of methods</caption>
<colgroup>
<col style="width: 25%;">
<col style="width: 12.5%;">
<col style="width: 12.5%;">
<col style="width: 12.5%;">
<col style="width: 12.5%;">
<col style="width: 12.5%;">
<col style="width: 12.5%;">
</colgroup>
<tbody>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock">Interface <code>I1</code></p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>I1</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>I1</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>I1</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>I1</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>I1</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>I1</sub></em></p></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock">Interface <code>I2</code></p></th>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>I2</sub></em></p></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>I2</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>I2</sub></em></p></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock">class <code>S</code></p></th>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>S</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>S</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>S</sub></em></p></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock">class <code>C</code></p></th>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>C</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><strong>M<sub>C</sub></strong></p></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>C</sub></em></p></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock"><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></p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>I1</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>C</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>C</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>S</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>S</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>C</sub></em></p></td>
</tr>
</tbody>
</table>
<div id="consuming-field-initializers" class="dlist">
<dl>
<dt class="hdlist1">Consuming Field Initializers </dt>
<dd>
<p>Aside from the fields themselves, an implementor <em>always</em> consumes the field initialization if the field is consumed – this is how the consumption is noticed at runtime.</p>
</dd>
</dl>
</div>
<div class="exampleblock">
<div class="title">Example 49. Field and Field Initializer Consumption</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">/* 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
)</code></pre>
</div>
</div>
<div class="paragraph">
<p>We expect the following output (for each field):</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>d.s = "C"</code> : <code>s</code>: is inherited from <code>C</code>, so it is not consumed from <code>I1</code> (or <code>I2</code>).
Consequently, the initializer of <code>s</code> in <code>C</code> is used.</p>
</li>
<li>
<p><code>d.t = "D"</code>: <code>t</code> is defined in <code>D</code>, solving a conflict stemming from the definition of <code>t</code> in <code>I1</code> and <code>I2</code>. Thus, the initializer of <code>t</code> in <code>D</code> is used.</p>
</li>
<li>
<p><code>d.u = "I1"</code> : <code>u</code> is only defined in <code>I1</code>, thus the initializer defined in <code>I1</code> is used.</p>
</li>
<li>
<p><code>d.v = "I2"</code> : <code>v</code> is overridden in <code>I2</code>, so is the field initializer. This is why <code>d.v</code> must be assigned to <code>I2</code> and not <code>I0</code>.</p>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_structural-typing"><a class="anchor" href="#_structural-typing"></a><a class="link" href="#_structural-typing">5.3. Structural Typing</a></h3>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph language-n4js">
<p>Interfaces or classes can be used for this purpose with a <em>typing strategy modifier</em>.
Given a type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math>, the simple <code>~</code> (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.<sup class="footnote">[<a id="_footnoteref_40" class="footnote" href="#_footnote_40" title="View footnote.">40</a>]</sup>
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 <em>~T</em>.</p>
</div>
<div class="paragraph language-n4js">
<p>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 <code>~~</code> (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 <code>~r~</code> or write-only <code>~w~</code> structural field typing.
For initialization blocks, it is even possible to use structural initializer field typing via the <code>~i~</code> operator.</p>
</div>
<div class="sect3 language-n4js">
<h4 id="_syntax-7"><a class="anchor" href="#_syntax-7"></a><a class="link" href="#_syntax-7">5.3.1. Syntax</a></h4>
<div class="paragraph">
<p>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.</p>
</div>
<div id="lst:Structural_Type_Operator_and_References" class="listingblock">
<div class="title">Structural Type Operator and References</div>
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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* '}')?
;</code></pre>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_definition-site-structural-typing"><a class="anchor" href="#_definition-site-structural-typing"></a><a class="link" href="#_definition-site-structural-typing">5.3.2. Definition Site Structural Typing</a></h4>
<div class="paragraph">
<p>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 <code>N4Object</code>.
In the latter case, programmers are enforced to nominal declare the type relation.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-75"></a><strong>Req. IDE-75:</strong> <a href="#Req-IDE-75">Definition Site Structural Typing</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The structurally defined type cannot be used on the right hand side of the <code>instanceof</code> 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></p>
</li>
<li>
<p>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</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>if it is not a subtype of <code>N4Object</code> <sup class="footnote">[<a id="_footnoteref_41" class="footnote" href="#_footnote_41" title="View footnote.">41</a>]</sup>
but it contains all public, non-static members of that type</p>
<div class="paragraph">
<p><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><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><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><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><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><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><br>
or</p>
</div>
</li>
<li>
<p>if it is a subtype of <code>N4Object</code> which explicitly extends or implements the
structurally defined type.</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>A structurally defined type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math> is implicitly derived
from <code>Object</code> if no other type is specified. In particular, a structurally
defined type must not be inherited from</p>
<div class="paragraph">
<p><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></p>
</div>
<div class="paragraph">
<p><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></p>
</div>
</li>
</ol>
</div>
</li>
</ol>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 50. Declaration Site Structural Typing</div>
<div class="content">
<div class="paragraph">
<p>The following snippet demonstrates the effect of definition-site structural types by comparing them to
nominal declared types: <a id="ex:declaration-site-structural-typing"></a></p>
</div>
<div class="listingblock">
<div class="title">Declaration Site Structural Typing</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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</code></pre>
</div>
</div>
<div class="paragraph">
<p>Definition site structural typing may lead to unexpected results. For
example;</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C{}
class ~E extends C{}</code></pre>
</div>
</div>
<div class="paragraph">
<p>It may be unexpected, but <code>E</code> is not a subtype of <code>C</code>, 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., <code>instanceof</code> won’t work with <code>E</code>, while it works
with <code>C</code>!</p>
</div>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_use-site-structural-typing"><a class="anchor" href="#_use-site-structural-typing"></a><a class="link" href="#_use-site-structural-typing">5.3.3. Use-Site Structural Typing</a></h4>
<div class="paragraph">
<p>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: <code>var c : ~C</code>.
The table <a href="#tab:available-fields-of-structural-types">Available Fields of Structural Types</a> 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.</p>
</div>
<table id="tab:available-fields-of-structural-types" class="tableblock frame-all grid-all spread">
<caption class="title">Table 9. Available Fields of Structural Types</caption>
<colgroup>
<col style="width: 16.6666%;">
<col style="width: 16.6666%;">
<col style="width: 16.6666%;">
<col style="width: 16.6666%;">
<col style="width: 16.6666%;">
<col style="width: 16.667%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Property of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math></th>
<th class="tableblock halign-center valign-top"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>X</mtext></mstyle></math></th>
<th class="tableblock halign-center valign-top"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>X</mtext></mstyle></math></th>
<th class="tableblock halign-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></th>
<th class="tableblock halign-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></th>
<th class="tableblock halign-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></th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">public method</p></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi></mi></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi></mi></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi></mi></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi></mi></math></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">public writable field</p></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi></mi></math></p></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi></mi></math></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">public readable field</p></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi></mi></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">writable fields</p></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>Multiple structural typing strategies can be nested when there are multiple use sites, like in the example <a href="#ex:nested-structural-typing-strategies">Nested Structural Typing Strategies</a> below at the locations ST1 and ST2.
In the example, the datafield <code>a.field</code> has the nested structural type <code>~r~ ~i~ A</code> and thus the datafield <code>a.field.df</code> is readable.
Nested structural types are evaluated on the fly when doing subtype checks.</p>
</div>
<div id="ex:nested-structural-typing-strategies" class="exampleblock">
<div class="title">Example 51. Nested Structural Typing Strategies</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class A {
public df : string;
}
interface I&lt;T&gt; {
public field : ~r~T; // ST1
}
var a : ~i~A; // ST2</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>The following example demonstrates the effect of the structural type modifiers:</p>
</div>
<div class="exampleblock">
<div class="title">Example 52. Effect of structural type modifiers on use-site</div>
<div class="content">
<div class="paragraph">
<p>Let’s assume the type defined on the left.
The following <em>pseudo</em> 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:</p>
</div>
<div class="paragraph">
<p>Effect of structural type modifiers on use-site</p>
</div>
<table class="tableblock frame-all grid-all spread">
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
<col style="width: 33.3334%;">
</colgroup>
<tbody>
<tr>
<th class="tableblock halign-center valign-top" colspan="3"><p class="tableblock">Effect of structural type modifiers on use-site</p></th>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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();
}</code></pre>
</div>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var cstructural:~C
class cstructural {
public y;
public f()
public get z():Z
public set z(z:Z)
}
interface ~I {
a;
func();
}</code></pre>
</div>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var cfields:~~C
class cfields {
public y;
public get z():Z
public set z(z:Z)
}
interface ~~I {
a;
}</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">Type</p></th>
<th class="tableblock halign-center valign-top"><p class="tableblock">Structural Type</p></th>
<th class="tableblock halign-center valign-top"><p class="tableblock">Structural Field Type</p></th>
</tr>
</tbody>
</table>
<table class="tableblock frame-all grid-all spread">
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
<col style="width: 33.3334%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var crofields:~r~C
class crofields {
public get y():Y
public get z():Z
}
interface ~r~I {
public get a():A
}</code></pre>
</div>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var cwofields:~w~C
class cwofields {
public set y(y:Y)
public set z(z:Z)
}
interface ~w~I {
public set a(a:A)
}</code></pre>
</div>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var cinitfields:~i~C
class cinitfields {
public get y():Y
public get z():Z
}
interface ~i~I {
public get a():A
}</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">Structural Read-only Field Type</p></th>
<th class="tableblock halign-center valign-top"><p class="tableblock">Structural Write-only Field Type</p></th>
<th class="tableblock halign-center valign-top"><p class="tableblock">Structural Initializer Field Type</p></th>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>Note that even if a type is defined without the structural modifier, it is not possible to use <code>instanceof</code> for variables declared as structural, as shown in the next example:</p>
</div>
<table class="tableblock frame-all grid-all spread">
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
<col style="width: 33.3334%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {..}
interface I {..}
foo(c: C, i: I) {
c instanceof C; // ok
c instanceof I; // ok
}</code></pre>
</div>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {..}
interface I {..}
foo(c: ~C, i: ~I) {
c instanceof C; // error
c instanceof I; // error
}</code></pre>
</div>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {..}
interface I {..}
foo(c: ~~C, i: ~~I) {
c instanceof C; // error
C instanceof I; // error
}</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">Type</p></th>
<th class="tableblock halign-center valign-top"><p class="tableblock">Structural Type</p></th>
<th class="tableblock halign-center valign-top"><p class="tableblock">Structural Field Type</p></th>
</tr>
</tbody>
</table>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-78701"></a><strong>Req. IDE-78701:</strong> <a href="#Req-IDE-78701">Nominal and structural typing spec attributes</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>Within this spec, we define the following attributes of a type reference <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math>:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>If a type is referenced with the structural type modifier <code>~</code> , 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.</p>
</li>
<li>
<p>If a type is referenced with the structural field type modifier <code>~~</code>, 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.</p>
</li>
<li>
<p>If a type is referenced with the structural read-only field type modifier <code>~r~</code>, 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.</p>
</li>
<li>
<p>If a type is referenced with the structural write-only field type modifier <code>~w~</code>, 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.</p>
</li>
<li>
<p>If a type is referenced with the structural initializer field type modifier <code>~i~</code>, 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.</p>
</li>
<li>
<p>We use <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>.</mo><mi>i</mi><mi>s</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> to simply refer any structural typing, i.e.+
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>.</mo><mi>i</mi><mi>s</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>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><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><mo></mo></math>T.refStructuralReadOnlyField \lor T.refStructuralWriteOnlyField || T.refStructuralInitField || T.defStructural$</p>
</li>
<li>
<p>We use <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>.</mo><mi>i</mi><mi>s</mi><mi>N</mi><mi>o</mi><mi>m</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi></math> as the opposite of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>.</mo><mi>i</mi><mi>s</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>, i.e.<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>.</mo><mi>i</mi><mi>s</mi><mi>N</mi><mi>o</mi><mi>m</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi><mo>=</mo><mo>¬</mo><mi>T</mi><mo>.</mo><mi>i</mi><mi>s</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></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>We call the following:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math> the (nominal) type T,</p>
</li>
<li>
<p><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>,</p>
</li>
<li>
<p><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>,</p>
</li>
<li>
<p><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,</p>
</li>
<li>
<p><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</p>
</li>
<li>
<p><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>.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-76"></a><strong>Req. IDE-76:</strong> <a href="#Req-IDE-76">Use-Site Structural Typing</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The structural version of a type is a supertype of the nominal type:<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></p>
</li>
<li>
<p>The structural field version of a type is a supertype of the structural type:<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></p>
</li>
<li>
<p>The structural read-only field version of a type is a supertype of the structural field type:<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></p>
</li>
<li>
<p>The structural write-only field version of a type is a supertype of the structural field type:<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></p>
</li>
<li>
<p>The structural (field) version of a type cannot be used on the right hand side of the <code>instanceof</code> operator:</p>
<div class="paragraph">
<p><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><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><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><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><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><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></p>
</div>
<div class="paragraph">
<p>That is, the following code will always issue an error: <code>x instanceof ~T</code> <sup class="footnote">[<a id="_footnoteref_42" class="footnote" href="#_footnote_42" title="View footnote.">42</a>]</sup>.</p>
</div>
</li>
<li>
<p>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>: <sup class="footnote">[<a id="_footnoteref_43" class="footnote" href="#_footnote_43" title="View footnote.">43</a>]</sup></p>
<div class="paragraph">
<p><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><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><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><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></p>
</div>
</li>
<li>
<p>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>.
Special cases regarding optional fields are described in <a href="#optional-fields">Optional Fields</a>.</p>
<div class="paragraph">
<p><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><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><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><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><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></p>
</div>
</li>
<li>
<p>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 <a href="#optional-fields">Optional Fields</a>.</p>
<div class="paragraph">
<p><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><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><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><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><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></p>
</div>
</li>
<li>
<p>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 <a href="#optional-fields">Optional Fields</a>.</p>
<div class="paragraph">
<p><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><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><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><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><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></p>
</div>
</li>
<li>
<p>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 <code>this</code>.
Special cases regarding optional fields are described in <a href="#optional-fields">Optional Fields</a>.</p>
<div class="paragraph">
<p><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><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><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><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><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><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></p>
</div>
</li>
<li>
<p>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 <code>Object</code>) 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>.</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>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 <code>extends</code>, <code>implements</code> or <code>with</code> in type declarations <sup class="footnote">[<a id="_footnoteref_44" class="footnote" href="#_footnote_44" title="View footnote.">44</a>]</sup>.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>Note that all members of <code>N4Object</code> are excluded.
This implies that extended reflective features (cf. <a href="#_reflection-meta-information">Reflection meta-information</a> ) are not available in the context of structural typing.
The <code>instanceof</code> operator is still working as described in <a href="#_relational-expression">Relational Expression</a>, in that it can be used to check the type of an instance.</p>
</div>
<div class="paragraph">
<p>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>:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>This is only a shortcut for the type inference defined above.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-77"></a><strong>Req. IDE-77:</strong> <a href="#Req-IDE-77">Definition and Use-Site Precedence</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>If a type is structurally typed on both definition and use-site, the rules for
use-site structural typing (<a href="#Req-IDE-76">[Req-IDE-76]</a>) are
applied.</p>
</div>
<div class="exampleblock">
<div class="title">Example 53. Use-Site Structural Typing</div>
<div class="content">
<div class="paragraph">
<p>The following example demonstrates the effect of the structural (field) modifier, used in this case for function parameters.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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</code></pre>
</div>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 54. Structural types variable and instanceof operator</div>
<div class="content">
<div class="paragraph">
<p>It is possible to use a variable typed with a structural version of a type on the left hand side of the <code>instanceof</code> operator, as demonstrated in this example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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);
}
}</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>The following table describes the member availability of <code>X</code> in various
typing scenarios. Such as <code>~~X</code>, <code>~r~X</code>, <code>~w~X</code>, <code>~i~X</code>.</p>
</div>
<table class="tableblock frame-all grid-all spread language-n4js">
<caption class="title">Table 10. Member Availability in various Typing Scenarios</caption>
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 16.6666%;">
<col style="width: 16.6666%;">
<col style="width: 16.6666%;">
<col style="width: 16.6669%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Member of type <em>X</em></th>
<th class="tableblock halign-center valign-top"><code>~~X</code></th>
<th class="tableblock halign-center valign-top"><code>~r~X</code></th>
<th class="tableblock halign-center valign-top"><code>~w~X</code></th>
<th class="tableblock halign-center valign-top"><code>~i~X</code></th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>private m0;</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">&#8201;&#8212;&#8201;</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">&#8201;&#8212;&#8201;</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">&#8201;&#8212;&#8201;</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">&#8201;&#8212;&#8201;</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>public set m1(m) { }</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">write</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">&#8201;&#8212;&#8201;</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">write</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>public get m2() {&#8230;&#8203;}</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">&#8201;&#8212;&#8201;</p></td>
<td class="tableblock halign-center valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>public m3;</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read/write</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">write</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>public m4 = 'init.m4';</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read/write</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">write</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read <em>?</em></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>public m5: any?;</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read<em>?</em>/write</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read<em>?</em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">write</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>?</mi></math></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@Final public m6 = 'init.m6';</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read</p></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@Final public m7;</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read</p></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>public get m8() {&#8230;&#8203;}</code></p></td>
<td class="tableblock halign-center valign-middle" rowspan="2"><p class="tableblock">read/write</p></td>
<td class="tableblock halign-center valign-middle" rowspan="2"><p class="tableblock">read</p></td>
<td class="tableblock halign-center valign-middle" rowspan="2"><p class="tableblock">write</p></td>
<td class="tableblock halign-center valign-middle" rowspan="2"><p class="tableblock">read</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>public set m8(m) { }</code></p></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="structural-readWriteInit-field-typing"><a class="anchor" href="#structural-readWriteInit-field-typing"></a><a class="link" href="#structural-readWriteInit-field-typing">5.3.4. Structural Read-only, Write-only and Initializer Field Typing</a></h4>
<div class="paragraph">
<p>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 <code>@Final</code> 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 <code>@Final</code> annotation are considered to be mandatory in the initializer field typing <code>@Spec</code> constructors.
The already-initialized <code>@Final</code> fields can be either omitted from, or can be re-initialized via, an initializer field typing <code>@Spec</code> style constructor.</p>
</div>
<div class="exampleblock">
<div class="title">Example 55. Subtype relationship between structural field typing</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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) { }
}</code></pre>
</div>
</div>
<div style="page-break-after: always;"></div>
<table class="tableblock frame-all grid-all spread small">
<colgroup>
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2642%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">A1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~A1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~~A1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A2</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A3</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A4</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A5</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~w~A1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~w~A2</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~w~A3</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~w~A4</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~w~A5</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~i~A1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~i~A2</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~i~A3</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A4</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A5</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">A1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~A1</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~~A1</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A1</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A2</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A3</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A4</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A5</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~w~A1</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~w~A2</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~w~A3</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~w~A4</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~w~A5</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~i~A1</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~i~A2</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~i~A3</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A4</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A5</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"></p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_public-setter-annotated-with-code-providesinitializer-code"><a class="anchor" href="#_public-setter-annotated-with-code-providesinitializer-code"></a><a class="link" href="#_public-setter-annotated-with-code-providesinitializer-code">5.3.5. Public Setter Annotated With <code>ProvidesInitializer</code></a></h4>
<div class="paragraph">
<p>Public setters with <code>ProvidesInitializer</code> 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.</p>
</div>
<div class="paragraph">
<p>It is important to note that it is valid to use the <code>ProvidesInitializer</code> annotation for setters in <code>n4js</code> files and not only definition files.</p>
</div>
<div class="exampleblock">
<div class="title">Example 56. Setters with <code>@ProvidesInitializer</code> treated as optional</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_structural-types-with-optional-fields"><a class="anchor" href="#_structural-types-with-optional-fields"></a><a class="link" href="#_structural-types-with-optional-fields">5.3.6. Structural Types With Optional Fields</a></h4>
<div class="paragraph">
<p>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 <a href="#optional-fields">Optional Fields</a>).</p>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_structural-types-with-access-modifier"><a class="anchor" href="#_structural-types-with-access-modifier"></a><a class="link" href="#_structural-types-with-access-modifier">5.3.7. Structural Types With Access Modifier</a></h4>
<div class="paragraph">
<p>The access modifier of the subtype have to provide equal or higher
visibility.</p>
</div>
<div class="exampleblock">
<div class="title">Example 57. Access modifier in structural typing</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_structural-types-with-additional-members"><a class="anchor" href="#_structural-types-with-additional-members"></a><a class="link" href="#_structural-types-with-additional-members">5.3.8. Structural Types With Additional Members</a></h4>
<div class="paragraph">
<p>It is possible to add additional members when structurally referencing a
declared type.</p>
</div>
<div class="sect4">
<h5 id="_syntax-8"><a class="anchor" href="#_syntax-8"></a><a class="link" href="#_syntax-8">5.3.8.1. Syntax</a></h5>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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;</code></pre>
</div>
</div>
<div class="sect5">
<h6 id="_semantics-6"><a class="anchor" href="#_semantics-6"></a><a class="link" href="#_semantics-6">5.3.8.1.1. Semantics</a></h6>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-78"></a><strong>Req. IDE-78:</strong> <a href="#Req-IDE-78">Additional structural members</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>It is only possible to add additional members to a type if use-site structural typing is used.</p>
</div>
<div class="paragraph">
<p>The following constraints must hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>For all additional members defined in a structural type reference, the constraints for member overriding (<a href="#Req-IDE-72">[Req-IDE-72]</a>) apply as well.</p>
</li>
<li>
<p>All additional members have the access modifier set to <code>public</code>.</p>
</li>
<li>
<p>Type variables must not be augmented with additional structural members.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>Additional fields may be declared optional in the same way as fields in classes.
The rules given in <a href="#_structural-types-with-optional-fields">Structural Types With Optional Fields</a> apply accordingly.
Consider the following example:</p>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 58. Additional optional members in structural typing</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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!!</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="exampleblock">
<div class="title">Example 59. Forbidden additional structural members on type variables</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">interface I&lt;T&gt; {
public field : ~T with {prop : int} // error "No additional structural members allowed on type variables."
}</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="exampleblock">
<div class="title">Example 60. Use explicitly defined Interfaces</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">interface ~J {
prop : int;
}
interface II&lt;T extends J&gt; {
}</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_functions"><a class="anchor" href="#_functions"></a><a class="link" href="#_functions">6. Functions</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>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 <a href="#_methods">Methods</a>.</p>
</div>
<div class="sect2 language-n4js">
<h3 id="_function-type"><a class="anchor" href="#_function-type"></a><a class="link" href="#_function-type">6.1. Function Type</a></h3>
<div class="paragraph">
<p>A function type is modeled as <code>Object</code> (see [<a href="#ECMA11a">ECMA11a(p.S13, p.p.98)</a>] in ECMAScript.</p>
</div>
<div class="paragraph">
<p>Function types can be defined by means of;</p>
</div>
<div class="ulist">
<ul>
<li>
<p>A function object (<a href="#_function-object-type">Function-Object-Type</a>).</p>
</li>
<li>
<p>A function type expression (<a href="#_type-expressions">Type Expressions</a>).</p>
</li>
<li>
<p>A function declaration (<a href="#_function-declaration">Function Declaration</a>).</p>
</li>
<li>
<p>A method declaration (<a href="#_methods">Methods</a>).</p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="_properties-5"><a class="anchor" href="#_properties-5"></a><a class="link" href="#_properties-5">6.1.1. Properties</a></h4>
<div class="paragraph">
<p>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:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>typePars</code> </dt>
<dd>
<p>(0-indexed) list of type parameters (i.e. type variables) for generic functions.</p>
</dd>
<dt class="hdlist1"><code>fpars</code> </dt>
<dd>
<p>(0-indexed) list of formal parameters.</p>
</dd>
<dt class="hdlist1"><code>returnType</code> </dt>
<dd>
<p>(possibly inferred) return type (expression) of the function or method.</p>
</dd>
<dt class="hdlist1"><code>name</code> </dt>
<dd>
<p>Name of function or method, may be empty or automatically generated (for messages).</p>
</dd>
<dt class="hdlist1"><code>body</code> </dt>
<dd>
<p>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.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>Additionally, the following pseudo properties for functions are defined:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>thisTypeRef</code> </dt>
<dd>
<p>The this type ref is the type to which the <code>this</code>-keyword would be evaluated
if used inside the function or member. The inference rules are described
in <a href="#_this-keyword">This Keyword</a>.</p>
</dd>
<dt class="hdlist1"><code>fpars</code> </dt>
<dd>
<p>List of formal parameters and the this type ref.
This is only used for sub typing rules.
If <code>this</code> is not used inside the function, then <code>any</code> is set instead of the inferred thisTypeRef to allow for more usages.
The property is computed as follows:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</dd>
</dl>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>name</code> </dt>
<dd>
<p>Name of the parameter.</p>
</dd>
<dt class="hdlist1"><code>type</code> </dt>
<dd>
<p>Type (expression) of the parameter. Note that only parameter types can
be variadic or optional.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>The function definition can be annotated similar to <a href="#_methods">Methods</a> except that the <code>final</code> and <code>abstract</code> modifiers aren’t supported for function declarations.
A function declaration is always final and never abstract.
Also, a function has no property advice set.</p>
</div>
<h4 id="_semantics-7" class="discrete">Semantics</h4>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-79"></a><strong>Req. IDE-79:</strong> <a href="#Req-IDE-79">Function Type</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>Type Given a function type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>F</mi></math>, the following constraints must be true:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>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:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>Only the last parameter of a method may be defined as variadic parameter:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>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:</p>
<div class="paragraph">
<p><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><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><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></p>
</div>
</li>
<li>
<p>If a function explicitly defines a return type, all return
statements must return a type conform to that type:</p>
<div class="paragraph">
<p><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><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo></mo></math><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><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></p>
</div>
</li>
</ol>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="function-type-inference"><a class="anchor" href="#function-type-inference"></a><a class="link" href="#function-type-inference">6.1.2. Type Inference</a></h4>
<div id="function_type_conformance_non_parameterized" class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="function_type_conformance_non_parameterized"></a><strong>Definition:</strong> <a href="#function_type_conformance_non_parameterized">Function Type Conformance Non-Parameterized</a></p>
</div>
<div class="paragraph">
<p><em>For the given non-parameterized function types</em>
<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><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>,<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:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><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><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><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></p>
</li>
<li>
<p>if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>k</mi><mo></mo><mi>n</mi></math>:</p>
<div class="ulist">
<ul>
<li>
<p><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></p>
</li>
<li>
<p><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></p>
</li>
<li>
<p><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></p>
<div class="paragraph">
<p>else (<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>k</mi><mo>&gt;</mo><mi>n</mi></math>):</p>
</div>
</li>
<li>
<p><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></p>
</li>
<li>
<p><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></p>
</li>
<li>
<p><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></p>
</li>
<li>
<p><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></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p><a href="#cdVarianceFunctionChart">Function Variance Chart</a> shows a simple example with the function type conformance relations.</p>
</div>
<div id="cdVarianceFunctionChart" class="imageblock">
<div class="content">
<img src="chapters/06_functions/fig/cdVarianceFunctionChart.svg" alt="cdVarianceFunctionChart">
</div>
<div class="title">Figure 6. Function Variance Chart</div>
</div>
<div class="paragraph">
<p><code>{function()}</code> <math xmlns="http://www.w3.org/1998/Math/MathML"><mo>&lt;</mo><mi>:</mi></math> <code>{function(A)}</code> <math xmlns="http://www.w3.org/1998/Math/MathML"><mo>&lt;</mo><mi>:</mi></math> <code>{function(A, A)}</code> 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.</p>
</div>
<div class="paragraph">
<p>If a function does not define a return type, <code>any</code> is assumed if at least one
of the (indirectly) contained return statements contains an expression.
Otherwise <code>void</code> is assumed. This is also true if there is an error due to
other constraint violations.</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>with</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 61. Function type conformance</div>
<div class="content">
<div class="paragraph">
<p>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 <code>bar</code> 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 <code>bar</code> 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.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">function bar(f1: {function(A,B):C}) { ... }
var f2: {function(A,B):C} = function(p1,p2){...};
bar(f1);</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>The type of <code>this</code> can be explicitly set via the <code>@This</code> annotation.</p>
</div>
<div class="exampleblock">
<div class="title">Example 62. Function Subtyping</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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</code></pre>
</div>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 63. Subtyping with function types</div>
<div class="content">
<div class="paragraph">
<p>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:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js"> {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</code></pre>
</div>
</div>
<div class="paragraph">
<p>The following examples demonstrate the effect of optional and variadic parameters:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">{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 (!)</code></pre>
</div>
</div>
<div class="paragraph">
<p>The following examples demonstrate the effect of optional return types:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">{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</code></pre>
</div>
</div>
<div class="paragraph">
<p>The following examples show the effect of the <code>@This</code> annotation:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">{@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</code></pre>
</div>
</div>
</div>
</div>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="function_type_conformance"></a><strong>Definition:</strong> <a href="#function_type_conformance">Function Type Conformance</a></p>
</div>
<div class="paragraph">
<p>For the given function types<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><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>,<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:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>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>:</p>
<div class="ulist">
<ul>
<li>
<p><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. <a href="#function_type_conformance_non_parameterized">Function Type Conformance Non-Parameterized</a>)</p>
</li>
</ul>
</div>
</li>
<li>
<p>else if<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>:</p>
<div class="ulist">
<ul>
<li>
<p><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. <a href="#function_type_conformance_non_parameterized">Function Type Conformance Non-Parameterized</a> )</p>
<div class="paragraph">
<p>(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 <a href="#function_type_conformance_non_parameterized">Function Type Conformance Non-Parameterized</a>)</p>
</div>
</li>
</ul>
</div>
</li>
<li>
<p>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>:</p>
<div class="ulist">
<ul>
<li>
<p><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)</p>
</li>
<li>
<p>-</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>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><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 <a href="#function_type_conformance_non_parameterized">Function Type Conformance Non-Parameterized</a> 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).</p>
</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="exampleblock">
<div class="title">Example 64. Bounded type variable at co-variant position in function type</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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; }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Method <code>m</code> in <code>Y</code> may return an <code>A</code>, thus breaking the contract of m in <code>X</code>, but only if it is parameterized to do so, which is not allowed for clients of <code>X</code>, only those of <code>Y</code>.
Therefore, the override in the above example is valid.</p>
</div>
</div>
</div>
<div class="paragraph">
<p>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 <a href="#Req-IDE-72">[Req-IDE-72]</a> (applies to method comnsumption and implementation accordingly, see <a href="#Req-IDE-73">[Req-IDE-73]</a> and <a href="#Req-IDE-74">[Req-IDE-74]</a>).
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 <code>void m(B param)</code>, it is valid to override <code>m</code> as <code>void m(A param)</code> in N4JS but not in Java.
In Java this would be handled as method overloading and therefore an <code>@Override</code> annotation on <code>m</code> would produce an error.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-80"></a><strong>Req. IDE-80:</strong> <a href="#Req-IDE-80">Upper and Lower Bound of a Function Type</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:<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></p>
</div>
<div class="paragraph">
<p>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:<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></p>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_autoboxing-of-function-type"><a class="anchor" href="#_autoboxing-of-function-type"></a><a class="link" href="#_autoboxing-of-function-type">6.1.3. Autoboxing of Function Type</a></h4>
<div class="paragraph">
<p>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.</p>
</div>
<div class="listingblock">
<div class="title">Access of Function properties on functions</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">// 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; }</code></pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_arguments-object"><a class="anchor" href="#_arguments-object"></a><a class="link" href="#_arguments-object">6.1.4. Arguments Object</a></h4>
<div class="paragraph">
<p>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 <code>arguments</code> or in the rare case that the function itself is called ’arguments’ [<a href="#ECMA11a">ECMA11a(p.S10.5, p.p.59)</a>].
The argument object has array-like behavior even though it is not of type <code>array</code>:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>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.</p>
</li>
<li>
<p>The <code>length</code> 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.</p>
</li>
<li>
<p>It is possible to store custom values in the arguments object, even outside the original index boundaries.</p>
</li>
<li>
<p>All obtained values from the arguments object are of type <code>any</code>.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>In non-strict ES mode the <code>callee</code> property holds a reference to the function executed [<a href="#ECMA11a">ECMA11a(p.S10.6, p.p.61)</a>].</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-81"></a><strong>Req. IDE-81:</strong> <a href="#Req-IDE-81">Arguments.callee</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>In N4JS and in ES strict mode the use of <code>arguments.callee</code> is prohibited.</p>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-82"></a><strong>Req. IDE-82:</strong> <a href="#Req-IDE-82">Arguments as formal parameter name</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>In N4JS, the formal parameters of the function cannot be named <code>arguments</code>.
This applies to all variable execution environments like field accessors (getter/setter, <a href="#_field-accessors-getter-setter">Field Accessors (Getter/Setter)</a>),
methods (<a href="#_methods">Methods</a>) and constructors (<a href="#_constructor-and-classifier-type">Constructor and Classifier Type</a>), where <code>FormalParameter</code> type is used.</p>
</div>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">// 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;
}</code></pre>
</div>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_ecmascript-5-function-definition"><a class="anchor" href="#_ecmascript-5-function-definition"></a><a class="link" href="#_ecmascript-5-function-definition">6.2. ECMAScript 5 Function Definition</a></h3>
<div class="sect3">
<h4 id="_function-declaration"><a class="anchor" href="#_function-declaration"></a><a class="link" href="#_function-declaration">6.2.1. Function Declaration</a></h4>
<div class="sect4">
<h5 id="_syntax-9"><a class="anchor" href="#_syntax-9"></a><a class="link" href="#_syntax-9">6.2.1.1. Syntax</a></h5>
<div class="paragraph">
<p>A function can be defined as described in [<a href="#ECMA11a">ECMA11a(p.S13, p.p.98)</a>] and additional annotations can be specified.
Since N4JS is based on [<a href="#ECMA15a">ECMA15a</a>], the syntax contains constructs not available in [<a href="#ECMA11a">ECMA11a</a>].
The newer constructs defined only in [<a href="#ECMA15a">ECMA15a</a>] and proposals already implemented in N4JS are described in <a href="#_ecmascript-2015-function-definition">ECMAScript 2015 Function Definition</a> and <a href="#_ecmascript-proposals-function-definition">ECMAScript Proposals Function Definition</a>.</p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
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.
</td>
</tr>
</table>
</div>
<div class="listingblock">
<div class="title">Syntax Function Declaration and Expression</div>
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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;?
;</code></pre>
</div>
</div>
<div class="paragraph">
<p>Properties of the function declaration and expression are described in <a href="#_function-type">Function Type</a>.</p>
</div>
<div class="paragraph">
<p>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>.</p>
</div>
<div class="exampleblock">
<div class="title">Example 65. Function Declaration with Type Annotation</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">// plain JS
function f(p) { return p.length }
// N4JS
function f(p: string): number { return p.length }</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_semantics-8"><a class="anchor" href="#_semantics-8"></a><a class="link" href="#_semantics-8">6.2.1.2. Semantics</a></h5>
<div class="paragraph">
<p>A function defined in a class’s method (or method modifier) builder is a method, see <a href="#_methods">Methods</a> for details and additional constraints.
The metatype of a function definition is function type (<a href="#_function-type">Function Type</a>), as a function declaration is only a different syntax for creating a <code>Function</code> object.
Constraints for function type are described in <a href="#_function-type">Function Type</a>.
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>.</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>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>!</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-83"></a><strong>Req. IDE-83:</strong> <a href="#Req-IDE-83">Function Declaration only on Top-Level</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>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.</p>
</li>
</ol>
</div>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_function-expression"><a class="anchor" href="#_function-expression"></a><a class="link" href="#_function-expression">6.2.2. Function Expression</a></h4>
<div class="paragraph">
<p>A function expression [<a href="#ECMA11a">ECMA11a(p.S11.2.5)</a>] is quite similar to a function declaration.
Thus, most details are explained in <a href="#_ecmascript-5-function-definition">ECMAScript 5 Function Definition</a>.</p>
</div>
<div class="sect4">
<h5 id="function-expression-syntax"><a class="anchor" href="#function-expression-syntax"></a><a class="link" href="#function-expression-syntax">6.2.2.1. Syntax</a></h5>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">FunctionExpression:
({FunctionExpression}
FunctionImpl&lt;Yield=false,YieldIfGenerator=true,Expression=true&gt;
)
;</code></pre>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_semantics-and-type-inference"><a class="anchor" href="#_semantics-and-type-inference"></a><a class="link" href="#_semantics-and-type-inference">6.2.2.2. Semantics and Type Inference</a></h5>
<div class="paragraph">
<p>In general, the inferred type of a function expression simply is the function type as described in <a href="#_function-type">Function Type</a>.
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:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>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.</p>
</li>
<li>
<p>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.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="exampleblock">
<div class="title">Example 66. Inference Of Function Expression’s Signature</div>
<div class="content">
<div class="paragraph">
<p>In general, <code>{function():any}</code> is a subtype of <code>{function():void}</code> (cf. <a href="#_function-type">Function Type</a>).
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:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">function f(cb: {function():void}) { cb() }
f(function() { return 1; });</code></pre>
</div>
</div>
<div class="paragraph">
<p>No error is issued: The type of the function expression actually is inferred to <code>{function():any}</code>, because there is a return statement with an expression.
It is not inferred to <code>{function():void}</code>, even if the formal parameter of <code>f</code> suggests that.
Due to the previously-stated relation <code>{function():any} &lt;: {function():void}</code> this is correct – the client (in this
case function <code>f</code>) works perfectly well even if <code>cb</code> 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!</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_ecmascript-2015-function-definition"><a class="anchor" href="#_ecmascript-2015-function-definition"></a><a class="link" href="#_ecmascript-2015-function-definition">6.3. ECMAScript 2015 Function Definition</a></h3>
<div class="sect3">
<h4 id="_formal-parameters"><a class="anchor" href="#_formal-parameters"></a><a class="link" href="#_formal-parameters">6.3.1. Formal Parameters</a></h4>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>Modifiers of formal parameters such as default or rest are neither evaluated nor rewritten in the transpiler.</p>
</div>
<div class="sect4">
<h5 id="Type_Modifiers_Optional"><a class="anchor" href="#Type_Modifiers_Optional"></a><a class="link" href="#Type_Modifiers_Optional">6.3.1.1. Optional Parameters</a></h5>
<div class="paragraph">
<p>An optional formal parameter can be omitted when calling a function/method.
An omitted parameter has the value <code>undefined</code>.
In case the omitted parameter is variadic, the value is an empty array.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
</div>
<div class="sect4">
<h5 id="Type_Modifiers_Default"><a class="anchor" href="#Type_Modifiers_Default"></a><a class="link" href="#Type_Modifiers_Default">6.3.1.2. Default Parameters</a></h5>
<div class="paragraph">
<p>A default parameter value is specified for a parameter via an equals sign (<code>=</code>).
If a caller doesn’t provide a value for the parameter, the default value is used.</p>
</div>
<div class="paragraph">
<p>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 <code>var = "s"</code>.
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: <code>var : string = "s"</code>.
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 <code>undefined</code>.</p>
</div>
<div class="paragraph">
<p>Formal parameters become default parameters implicitly when they are preceded by an explicit default parameter.
In such cases, the default initializer is <code>undefined</code>.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-14501"></a><strong>Req. IDE-14501:</strong> <a href="#Req-IDE-14501">Default parameters</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>Any normal parameter which is preceded by a default parameter also becomes a default parameter.
Its initializer is <code>undefined</code>.</p>
</div>
</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
</div>
<div class="sect4">
<h5 id="Type_Modifiers_Variadic"><a class="anchor" href="#Type_Modifiers_Variadic"></a><a class="link" href="#Type_Modifiers_Variadic">6.3.1.3. Variadic</a></h5>
<div class="paragraph">
<p>Variadic parameters are also called <em>rest parameters</em>.
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.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-16"></a><strong>Req. IDE-16:</strong> <a href="#Req-IDE-16">Variadic and optional parameters</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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>.</p>
</div>
<div class="paragraph">
<p>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>.</p>
</div>
</div>
</div>
<div class="paragraph">
<p>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 <code>Array&lt;T&gt;</code>.
That is, declaring <code>function(&#8230;&#8203;tags : string)</code> causes <code>tags</code> to be an <code>Array&lt;string&gt;</code> and not just a scalar <code>string</code> value.</p>
</div>
<div class="paragraph">
<p>To make this work at runtime, the compiler will generate code that constructs the <code>parameter</code> from the <code>arguments</code> parameter explicitly passed to the function.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-17"></a><strong>Req. IDE-17:</strong> <a href="#Req-IDE-17">Variadic at Runtime</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
</div>
</div>
<div class="paragraph">
<p>For more constraints on using the variadic modifier, see <a href="#_function-object-type">Function-Object-Type</a>.</p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_generator-functions"><a class="anchor" href="#_generator-functions"></a><a class="link" href="#_generator-functions">6.3.2. Generator Functions</a></h4>
<div class="paragraph">
<p>Generators come together with the <code>yield</code> 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 <code>Generator&lt;TYield,TReturn,TNext&gt;</code> can be controlled by its methods
(cf. [<a href="#ECMA15a">ECMA15a(p.S14.4)</a>], also see [<a href="#Kuizinas14a">Kuizinas14a</a>]).</p>
</div>
<div class="sect4">
<h5 id="generator-functions-syntax"><a class="anchor" href="#generator-functions-syntax"></a><a class="link" href="#generator-functions-syntax">6.3.2.1. Syntax</a></h5>
<div class="paragraph">
<p>Generator functions and methods differ from ordinary functions and methods only in the additional <code>*</code> 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.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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;</code></pre>
</div>
</div>
</div>
<div class="sect4">
<h5 id="generator-functions-semantics"><a class="anchor" href="#generator-functions-semantics"></a><a class="link" href="#generator-functions-semantics">6.3.2.2. Semantics</a></h5>
<div class="paragraph">
<p>The basic idea is to make code dealing with Generators easier to write and more readable without changing their functionality.
Take this example:</p>
</div>
<div id="ex:two-simple-generator-functions" class="exampleblock">
<div class="title">Example 67. Two simple generator functions</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">// 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}</code></pre>
</div>
</div>
<div class="paragraph">
<p>In the example above, two generator functions are declared.
The first declares its return type explicitly whereas the second uses a shorthand form.</p>
</div>
</div>
</div>
<div class="paragraph">
<p>Generator functions and methods return objects of the type <code>Generator&lt;TYield,TReturn,TNext&gt;</code> which is a subtype of the <code>Iterable&lt;TYield&gt;</code> and <code>Iterator&lt;TYield&gt;</code> interfaces.
Moreover, it provides the methods <code>throw(exception:any)</code> and <code>return(value:TNext?)</code> for advanced control of the generator object.
The complete interface of the generator class is given below.</p>
</div>
<div class="listingblock">
<div class="title">The generator class</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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;;
}</code></pre>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-14370"></a><strong>Req. IDE-14370:</strong> <a href="#Req-IDE-14370">Modifier <code>*</code></a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><code>*</code> may be used on declared functions and methods, and for function expressions.</p>
</li>
<li>
<p>A function or method <em>f</em> with a declared return type <em>R</em> that is declared <code>*</code> has an actual return type of <code>Generator&lt;TYield,TReturn,TNext&gt;</code>.</p>
</li>
<li>
<p>A generator function or method can have no declared return type, a shorthand form of a return type or an explicitly declared return type.</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>The explicitly declared return type is of the form <code>Generator&lt;TYield,TReturn,TNext&gt;</code> with the type variables:</p>
<div class="olist lowerroman">
<ol class="lowerroman" type="i">
<li>
<p><em>TYield</em> as the expected type of the yield expression argument,</p>
</li>
<li>
<p><em>TReturn</em> as the expected type of the return expression, and</p>
</li>
<li>
<p><em>TNext</em> as both the return type of the yield expression.</p>
</li>
</ol>
</div>
</li>
<li>
<p>The shorthand form only declares the type of <em>TYield</em> which implicitly translates to <code>Generator&lt;TYield,TReturn,any&gt;</code> as the return type.</p>
<div class="olist lowerroman">
<ol class="lowerroman" type="i">
<li>
<p>The type <em>TReturn</em> is inferred to either <code>undefined</code> or <code>any</code> from the body.</p>
</li>
<li>
<p>In case the declared type is <code>void</code>, actual return type evaluates to <code>Generator&lt;undefined,undefined,any&gt;</code>.</p>
</li>
</ol>
</div>
</li>
<li>
<p>If no return type is declared, both <em>TYield</em> and <em>TReturn</em> are inferred from the body to either <code>any</code> or <code>undefined</code>. <em>TNext</em> is <code>any</code>.</p>
</li>
</ol>
</div>
</li>
<li>
<p>Given a generator function or method <em>f</em> with an actual return type <code>Generator&lt;TYield,TReturn,TNext&gt;</code>:</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>all yield statements in <em>f</em> must have an expression of type <em>TYield</em>.</p>
</li>
<li>
<p>all return statements in <em>f</em> must have an expression of type <em>TReturn</em>.</p>
</li>
</ol>
</div>
</li>
<li>
<p>Return statements in generator functions or methods are always optional.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-14371"></a><strong>Req. IDE-14371:</strong> <a href="#Req-IDE-14371">Modifier <code>yield</code> and <code>yield*</code></a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><code>yield</code> and <code>yield*</code> may only be in body of generator functions or methods.</p>
</li>
<li>
<p><code>yield expr</code> takes only expressions <em>expr</em> of type <em>TYield</em> in a generator function or methods with the actual type <code>Generator&lt;TYield,TReturn,TNext&gt;</code>.</p>
</li>
<li>
<p>The return type of the <code>yield</code> expression is <em>TNext</em>.</p>
</li>
<li>
<p><code>yield* fg()</code> takes only iterators of type <code>Iterator&lt;TYield&gt;</code>, and generator functions or methods <em>fg</em> with the actual return type <code>Generator&lt;? extends TYield,? extends TReturn,? super TNext&gt;</code>.</p>
</li>
<li>
<p>The return type of the <code>yield*</code> expression is <em>any</em>, since a custom iterator could return an entry <code>{done=true,value}</code> and any value for the variable <code>value</code>.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>Similar to <code>async</code> functions, shorthand and explicit form <code>* function():int{};</code> and <code>* function():Generator&lt;int,TResult,any&gt;</code> are equal,
given that the inferred <em>TResult</em> of the former functions equals to <em>TResult</em> in the latter function).
In other words, the return type of generator functions or methods is wrapped when it is not explicitly defined as <code>Generator</code> already.
Thus, whenever a nested generator type is desired, it has to be defined explicitly.
Consider the example below.</p>
</div>
<div class="listingblock">
<div class="title">Type variables with async methods.</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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;
}</code></pre>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_generator-arrow-functions"><a class="anchor" href="#_generator-arrow-functions"></a><a class="link" href="#_generator-arrow-functions">6.3.2.3. Generator Arrow Functions</a></h5>
<div class="paragraph">
<p>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 <a href="https://esdiscuss.org/topic/generator-arrow-functions">ESDiscuss.org</a> and <a href="https://esdiscuss.org/topic/why-do-generator-expressions-return-generators">StackOverflow.com</a>.</p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_arrow-function-expression"><a class="anchor" href="#_arrow-function-expression"></a><a class="link" href="#_arrow-function-expression">6.3.3. Arrow Function Expression</a></h4>
<div class="paragraph">
<p>This is an ECMAScript 6 expression (see [<a href="#ECMA15a">ECMA15a(p.S14.2)</a>]) 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.</p>
</div>
<div class="paragraph">
<p>Along with Assignments, Arrow function expressions have the least precedence, e.g. they serve as the entry point for the expression tree.</p>
</div>
<div class="paragraph">
<p>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 <code>@=></code> stands for an async arrow function (<a href="#_asynchronous-arrow-functions">Asynchronous Arrow Functions</a>).</p>
</div>
<div class="sect4">
<h5 id="arrow-function-expression-syntax"><a class="anchor" href="#arrow-function-expression-syntax"></a><a class="link" href="#arrow-function-expression-syntax">6.3.3.1. Syntax</a></h5>
<div class="paragraph">
<p>The simplified syntax reads like this:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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;</code></pre>
</div>
</div>
</div>
<div class="sect4">
<h5 id="arrow-function-expression-semantics-and-type-inference"><a class="anchor" href="#arrow-function-expression-semantics-and-type-inference"></a><a class="link" href="#arrow-function-expression-semantics-and-type-inference">6.3.3.2. Semantics and Type Inference</a></h5>
<div class="paragraph">
<p>Generally speaking, the semantics are very similar to the function
expressions but the devil’s in the details:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>arguments</code>: Unlike normal function expressions, an arrow function does not introduce an implicit <code>arguments</code> variable (<a href="#_arguments-object">Arguments Object</a>),
therefore any occurrence of it in the arrow function’s body has always the same binding as an occurrence of <code>arguments</code> in the lexical context enclosing the arrow function.</p>
</li>
<li>
<p><code>this</code>: An arrow function does not introduce a binding of its own for the <code>this</code> 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 <code>arguments</code> and <code>this</code> flagged as error (the outer lexical context doesn’t provide definitionsfor them).</p>
</li>
<li>
<p><code>super</code>: As with function expressions in general, whether of the arrow variety or not, the usage of <code>super</code> isn’t allowed in the body of arrow functions.</p>
</li>
</ul>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-84"></a><strong>Req. IDE-84:</strong> <a href="#Req-IDE-84">No This in Top Level Arrow Function in N4JS Mode</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>In N4JS, a top-level arrow function can’t refer to <code>this</code> as there’s no outer lexical context that provides a binding for it.</p>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-85"></a><strong>Req. IDE-85:</strong> <a href="#Req-IDE-85">No Arguments in Top Level Arrow Function</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>In N4JS, a top-level arrow function can’t include usages of <code>arguments</code> in its body, again because of the missing binding for it.</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_ecmascript-proposals-function-definition"><a class="anchor" href="#_ecmascript-proposals-function-definition"></a><a class="link" href="#_ecmascript-proposals-function-definition">6.4. ECMAScript Proposals Function Definition</a></h3>
<div class="sect3">
<h4 id="_asynchronous-functions"><a class="anchor" href="#_asynchronous-functions"></a><a class="link" href="#_asynchronous-functions">6.4.1. Asynchronous Functions</a></h4>
<div class="paragraph">
<p>To improve language-level support for asynchronous code, there exists an ECMAScript proposal <sup class="footnote">[<a id="_footnoteref_45" class="footnote" href="#_footnote_45" title="View footnote.">45</a>]</sup> 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 (<a href="#_asynchronous-methods">Asynchronous Methods</a>) as well
as for function expressions and arrow functions (<a href="#_asynchronous-arrow-functions">Asynchronous Arrow Functions</a>).</p>
</div>
<div class="sect4">
<h5 id="asynchronous-functions-syntax"><a class="anchor" href="#asynchronous-functions-syntax"></a><a class="link" href="#asynchronous-functions-syntax">6.4.1.1. Syntax</a></h5>
<div class="paragraph">
<p>The following syntax rules are extracted from the real syntax rules.
They only display parts relevant to declaring a function or method as
asynchronous.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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</code></pre>
</div>
</div>
<div class="paragraph">
<p>’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 <code>async</code> modifier. This enables the
parser to distinguish between using <code>async</code> as an identifier reference and a
keyword, as shown in the next example.</p>
</div>
<div class="exampleblock">
<div class="title">Example 68. Async as keyword and identifier</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">async <i class="conum" data-value="1"></i><b>(1)</b>
function foo() {}
// vs
async function bar(); <i class="conum" data-value="2"></i><b>(2)</b></code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>In this snippet, the <code>async</code> 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.</td>
</tr>
<tr>
<td><i class="conum" data-value="2"></i><b>2</b></td>
<td>In contrast, <code>async</code> on line 4 is recognized as a modifier declaring the function as asynchronous.</td>
</tr>
</table>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="asynchronous-functions-semantics"><a class="anchor" href="#asynchronous-functions-semantics"></a><a class="link" href="#asynchronous-functions-semantics">6.4.1.2. Semantics</a></h5>
<div class="paragraph">
<p>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:</p>
</div>
<div class="listingblock">
<div class="title">A simple asynchronous function using async/await.</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">// 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;
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The modifier <code>async</code> changes the return type of <code>loadAddress()</code> from <code>string</code> (the declared return type) to <code>Promise&lt;string,?&gt;</code> (the actual return type).
For code inside the function, the return type is still <code>string</code>:
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 <code>Promise&lt;string,?&gt;</code>.
To raise an error, simply throw an exception, its value will become the error value of the returned Promise.</p>
</div>
<div class="paragraph">
<p>If the expression after an <code>await</code> evaluates to a <code>Promise</code>, 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 <code>await</code> evaluates to a non-promise, the value will be simply passed through.
In addition, a warning is shown to indicate the unnecessary <code>await</code> expression.</p>
</div>
<div class="paragraph">
<p>Note how method <code>loadAddress()</code> 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 <code>getDataBase()</code> and <code>loadEntry()</code> 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 <code>loadAddress()</code>.</p>
</div>
<div class="paragraph">
<p>Invoking an async function commonly adopts one of two forms:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>var p: Promise&lt;successType,?&gt; = asyncFn()</code></p>
</li>
<li>
<p><code>await asyncFn()</code></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>These patterns are so common that a warning is available whenever both</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><code>Promise</code> is omitted as expected type; and</p>
</li>
<li>
<p><code>await</code> is also omitted.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>The warning aims at hinting about forgetting to wait for the result, while remaining non-noisy.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-86"></a><strong>Req. IDE-86:</strong> <a href="#Req-IDE-86">Modifier <code>async</code> and <code>await</code></a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><code>async</code> may be used on declared functions and methods as well as for function expressions and arrow functions.</p>
</li>
<li>
<p>A function or method that is declared <code>async</code> can have no declared return type, a shorthand form of a return type or an explicitly declared return type.</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>The explicitly declared return type is of the form <code>Promise&lt;R,E&gt;</code> where <em>R</em> is the type of all return statements in the body, and E is the type of exceptions that are thrown in the body.</p>
</li>
<li>
<p>The shorthand form only declares the type of <em>R</em> which implicitly translates to <code>Promise&lt;R,?&gt;</code> as the actual return type.</p>
</li>
<li>
<p>In case no return type is declared, the type <em>R</em> of <code>Promise&lt;R,?&gt;</code> is inferred from the body.</p>
</li>
</ol>
</div>
</li>
<li>
<p>A function or method <em>f</em> with a declared return type <em>R</em> that is declared <code>async</code> has an actual return type of</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p><code>R</code> if <em>R</em> is a subtype of <code>Promise&lt;?,?&gt;</code>,</p>
</li>
<li>
<p><code>Promise&lt;undefined,?&gt;</code> if <em>R</em> is type <code>void</code>.</p>
</li>
<li>
<p><code>Promise&lt;R,?&gt;</code> in all other cases (i.e. the declared return type <em>R</em> is being wrapped in a <code>Promise</code>).</p>
</li>
</ol>
</div>
</li>
<li>
<p>Return type inference is only performed when no return type is declared.</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>The return type <code>R</code> of <code>Promise&lt;R,?&gt;</code> is inferred either as <code>void</code> or as <code>any</code>.</p>
</li>
</ol>
</div>
</li>
<li>
<p>Given a function or method <em>f</em> that is declared <code>async</code> with a declared return type <em>R</em>, or with a declared return type <code>Promise&lt;R,?&gt;</code>,
all return statements in <em>f</em> must have an expression of type <em>R</em> (and not of type <code>Promise&lt;R,?&gt;</code>).</p>
</li>
<li>
<p><code>await</code> can be used in expressions directly enclosed in an async function, and behaves like a unary operator with the same precedence as <code>yield</code> in ES6.</p>
</li>
<li>
<p>Given an expression <em>expr</em> of type
<em>T</em>, the type of (<code>await</code> <em>expr</em>) is inferred to <em>T</em> if
<em>T</em> is not a Promise, or it is inferred to <em>S</em> if
<em>T</em> is a Promise with a success value of type
<em>S</em>, i.e. <em>T &lt;: Promise&lt;S,?&gt;</em> .</p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>In other words, the return type <em>R</em> of <code>async</code> functions and methods will always be wrapped to <code>Promise&lt;R,?&gt;</code> unless <em>R</em> is a <code>Promise</code> already.
As a consequence, nested <code>Promise</code>s as a return type of a async function or method have to be stated explicitly like <code>Promise&lt;Promise&lt;R,?&gt;,?&gt;</code>.</p>
</div>
<div class="paragraph">
<p>When a type variable <code>T</code> is used to define the the return type of an async function or method, it will always be wrapped.
Consider the example below.</p>
</div>
<div class="exampleblock">
<div class="title">Example 69. Type variables with async methods.</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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;
}</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_asynchronous-arrow-functions"><a class="anchor" href="#_asynchronous-arrow-functions"></a><a class="link" href="#_asynchronous-arrow-functions">6.4.1.3. Asynchronous Arrow Functions</a></h5>
<div class="paragraph">
<p>An <code>await</code> 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.</p>
</div>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_n4js-extended-function-definition"><a class="anchor" href="#_n4js-extended-function-definition"></a><a class="link" href="#_n4js-extended-function-definition">6.5. N4JS Extended Function Definition</a></h3>
<div class="sect3">
<h4 id="_generic-functions"><a class="anchor" href="#_generic-functions"></a><a class="link" href="#_generic-functions">6.5.1. Generic Functions</a></h4>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>In the following listing, a generic function <code>foo</code> is defined that has two type parameters <code>S</code> and <code>T</code>.
Thereby <code>S</code> is used as to declare the parameter type <code>Array&lt;S&gt;</code> and <code>T</code> is used as the return type and to construct the returned value in the function body.</p>
</div>
<div class="listingblock">
<div class="title">Generic Function Definition</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">function &lt;S,T&gt; foo(s: Array&lt;S&gt;): T { return new T(s); }</code></pre>
</div>
</div>
<div class="paragraph">
<p>If a generic type parameter is not used as a formal parameter type or
the return type, a warning is generated.</p>
</div>
</div>
<div class="sect3">
<h4 id="_promisifiable-functions"><a class="anchor" href="#_promisifiable-functions"></a><a class="link" href="#_promisifiable-functions">6.5.2. Promisifiable Functions</a></h4>
<div class="paragraph">
<p>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:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">'function' name '(' arbitraryParameters ',' callbackFunction ')'</code></pre>
</div>
</div>
<div class="paragraph">
<p>Usually the function returns nothing (<code>void</code>).
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’).</p>
</div>
<div class="paragraph">
<p>In order to simplify usage of this pattern, it is possible to mark such a function or method as <code>@Promisifiable</code>.
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 <code>async</code>.
This is particularly useful in N4JS definition files (.n4jsd) to allow using an existing callback-based API from N4JS code with the more convenient <code>await</code>.</p>
</div>
<div class="exampleblock">
<div class="title">Example 70. Promisifiable</div>
<div class="content">
<div class="paragraph">
<p>Given a function with an N4JS signature</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">f(x: int, cb: {function(Error, string)}): void</code></pre>
</div>
</div>
<div class="paragraph">
<p>This method can be annotated with <code>Promisifiable</code> as follows:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">@Promisifiable f(x: int, cb: {function(Error, string)}): void</code></pre>
</div>
</div>
<div class="paragraph">
<p>With this annotation, the function can be invoked in four different
ways:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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</code></pre>
</div>
</div>
<div class="paragraph">
<p>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 <code>f</code> can be promisified using the <code>@Promisify</code> annotation - no callback needs to be provided and instead, a <code>Promise</code> will be returned.
We can either use this promise directly or immediately <code>await</code> on it, as shown in line 3.
The syntax shown in line 4 is merely shorthand for <code>await @Promisify</code>, i.e. the annotation is optional after <code>await</code>.</p>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-87"></a><strong>Req. IDE-87:</strong> <a href="#Req-IDE-87">Promisifiable</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>A function or method <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi></math> can be annotated with <code>@Promisifiable</code> if and only if the following constraints hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>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>).</p>
</li>
<li>
<p>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</p>
<div class="ulist">
<ul>
<li>
<p><code>{function(E, T0, T1, &#8230;&#8203;, Tn): V}</code>, or</p>
</li>
<li>
<p><code>{function(T0, T1, &#8230;&#8203;, Tn): V}</code></p>
<div class="paragraph">
<p>in which <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>E</mi></math> is type <code>Error</code> 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.<br>
Since the return value of the synchronous function call is not available when using <code>@Promisify</code>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>V</mi></math> is recommended to be <code>void</code>, but it can be any type.</p>
</div>
</li>
</ul>
</div>
</li>
<li>
<p>The callback parameter may be optional.<sup class="footnote">[<a id="_footnoteref_46" class="footnote" href="#_footnote_46" title="View footnote.">46</a>]</sup></p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>According to <a href="#Req-IDE-87">[Req-IDE-87]</a>, 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 <code>Error</code>, all other parameters must be of other types.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-88"></a><strong>Req. IDE-88:</strong> <a href="#Req-IDE-88">@Promisify and await with promisifiable functions</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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 <a href="#Req-IDE-87">[Req-IDE-87]</a> can be promisified with <code>Promisify</code> or
used with <code>await</code>, if and only if the following constraints hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Function <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi></math> must be annotated with <code>@Promisifiable</code>.</p>
</li>
<li>
<p>Using <code>@Promisify f()</code> without <code>await</code> returns a promise of type <code>Promise&lt;S,F&gt;</code> where</p>
<div class="ulist">
<ul>
<li>
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi></math> is <code>IterableN&lt;T0,&#8230;&#8203;,Tn&gt;</code> if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi><mo></mo><mn>2</mn></math>, <code>T</code> if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi><mo>=</mo><mn>1</mn></math>, and <code>undefined</code> if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi><mo>=</mo><mn>0</mn></math>.</p>
</li>
<li>
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>F</mi></math> is <code>E</code> if given, <code>undefined</code> otherwise.</p>
</li>
</ul>
</div>
</li>
<li>
<p>Using <code>await @Promisify f()</code> returns a value of type <code>IterableN&lt;T0,&#8230;&#8203;,Tn&gt;</code> if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi><mo></mo><mn>2</mn></math>, of type <code>T</code> if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi><mo>=</mo><mn>1</mn></math>, and of type <code>undefined</code> if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi><mo>=</mo><mn>0</mn></math>.</p>
</li>
<li>
<p>In case of using an <code>await</code>, the annotation can be omitted.<br>
I.e., <code>await @Promisify f()</code> is equivalent to <code>await f()</code>.</p>
</li>
<li>
<p>Only call expressions using f as target can be promisified, in other
words this is illegal:</p>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var pf = @Promisify f; // illegal code!</code></pre>
</div>
</div>
</li>
</ol>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_conversions-and-reflection"><a class="anchor" href="#_conversions-and-reflection"></a><a class="link" href="#_conversions-and-reflection">7. Conversions and Reflection</a></h2>
<div class="sectionbody">
<div class="sect2 language-n4js">
<h3 id="_autoboxing-and-coercing"><a class="anchor" href="#_autoboxing-and-coercing"></a><a class="link" href="#_autoboxing-and-coercing">7.1. Autoboxing and Coercing</a></h3>
<div class="paragraph">
<p>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 <code>string</code>, <code>number</code>, or <code>boolean</code>,
to its corresponding Object type version <code>String</code>, <code>Number</code>, <code>Boolean</code>.
The capital letters in the latter are an essential distinction.</p>
</div>
<div class="paragraph">
<p>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 <code>string</code> to call <code>"abc".length</code>, for example) automatic conversion is applied.</p>
</div>
<div class="paragraph">
<p>Note that N4JS specific primitive types <code>pathselector</code> and <code>i18nkey</code> are handled similarly to <code>string</code>.</p>
</div>
<div class="sect3">
<h4 id="_coercing"><a class="anchor" href="#_coercing"></a><a class="link" href="#_coercing">7.1.1. Coercing</a></h4>
<div class="paragraph">
<p>In [<a href="#ECMA11a">ECMA11a</a>], coercing is defined by means of the abstract specification method <code>ToPrimitive</code> [<a href="#ECMA11a">ECMA11a(p.S9.1)</a>], also see [<a href="#ECMA11a">ECMA11a(p.S9.10)</a>]).
Other conversions, such as <code>ToNumber</code>, are not directly supported but reflected in the typing rules of expressions.</p>
</div>
<div class="paragraph">
<p>We express absence of automatic coercion here by means of subtype
relations:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>and for the N4JS specific types:</p>
</div>
<div class="paragraph">
<p><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><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></p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var bool: boolean = false;
var Bool: Boolean = new Boolean( false );
console.log( bool ? "true" : "false"); // prints "false"
console.log( Bool ? "true" : "false"); // prints "true"!</code></pre>
</div>
</div>
<div class="paragraph">
<p>Conversion between a primitive type to its object-variant is achieved by the <code>new</code> operator.
The <code>valueOf()</code> method converts the object-variant back to a primitive value.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">// 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 );</code></pre>
</div>
</div>
<div class="paragraph">
<p>Conversion of variables of type <code>Object</code> 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 <code>Object</code> 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 <a href="#_auto-conversion-of-class-instances">Auto-Conversion of Class Instances</a></p>
</div>
</div>
<div class="sect3">
<h4 id="_autoboxing-of-primitives"><a class="anchor" href="#_autoboxing-of-primitives"></a><a class="link" href="#_autoboxing-of-primitives">7.1.2. Autoboxing of Primitives</a></h4>
<div class="paragraph">
<p>In [<a href="#ECMA11a">ECMA11a</a>], autoboxing is defined by <code>ToObject</code> [<a href="#ECMA11a">ECMA11a(p.S9.9)</a>].</p>
</div>
<div class="paragraph">
<p>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, <code>"some string value".split(" ");</code> is supported but <code>"some string value".foo=1;</code> will be rejected as String does not allow properties to be added (cf. <code>String</code> vs. <code>String+</code>, see <a href="#_dynamic">[_dynamic]</a>).</p>
</div>
<div class="paragraph">
<p>Autoboxing often leads to problems, in particular in combination with dynamic types – this is why it is not directly supported in N4JS.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var s: String+ = "Hello"; <i class="conum" data-value="1"></i><b>(1)</b>
s.prop = 1;
console.log(s.prop); <i class="conum" data-value="2"></i><b>(2)</b></code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>will produce an error to prevent the following scenario:</td>
</tr>
<tr>
<td><i class="conum" data-value="2"></i><b>2</b></td>
<td>prints "undefined"</td>
</tr>
</table>
</div>
</div>
<div class="sect3">
<h4 id="_autoboxing-of-function-expressions-and-declarations"><a class="anchor" href="#_autoboxing-of-function-expressions-and-declarations"></a><a class="link" href="#_autoboxing-of-function-expressions-and-declarations">7.1.3. Autoboxing of Function Expressions and Declarations</a></h4>
<div class="paragraph">
<p>Function expressions and declarations always define an object of type <code>Function</code>, thus coercing or Autoboxing is not required in case of functions:</p>
</div>
<div class="paragraph">
<p>It is always possible to use a function expression where a <code>Function</code> is required, and to use an object of type <code>Function</code> where a function expression is expected.
This is only possible if the function signatures are subtype-compatible, see <a href="#_function-type">Function Type</a> for details.</p>
</div>
<div class="paragraph">
<p>Still, it is always possible to call members of <code>Function</code>, e.g., <code>function(){}.length()</code>.</p>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_auto-conversion-of-objects"><a class="anchor" href="#_auto-conversion-of-objects"></a><a class="link" href="#_auto-conversion-of-objects">7.2. Auto-Conversion of Objects</a></h3>
<div class="sect3">
<h4 id="_auto-conversion-of-class-instances"><a class="anchor" href="#_auto-conversion-of-class-instances"></a><a class="link" href="#_auto-conversion-of-class-instances">7.2.1. Auto-Conversion of Class Instances</a></h4>
<div class="paragraph">
<p>All classes defined in N4JS modules implicitly subclass <code>N4Object</code>, which is a plain JavaScript Object type.
The same auto-conversion rules defined for JavaScript <code>Object</code> therefore apply to <code>N4Object</code> instances as well.</p>
</div>
<div class="paragraph">
<p>The basic conversion uses the abstract JavaScript function <code>ToPrimitive</code> [<a href="#ECMA11a">ECMA11a(p.S9.1)</a>], which relays on the specification method <code>Object</code> [<a href="#ECMA11a">ECMA11a(p.S8.12.8)</a>].
<code>DefaultValue</code> calls <code>valueOf</code> or <code>toString</code> methods if they are defined by the class (in the <code>methods</code>-builder).</p>
</div>
<div class="paragraph">
<p>Note that according to the [<a href="#ECMA11a">ECMA11a</a>], in most cases, objects are first converted into primitives.
That is, in most cases, no extra hint is passed to <code>DefaultValue</code>. Thus <code>valueOf</code> usually takes precedence over toString as demonstrated in the following example:</p>
</div>
<div class="exampleblock">
<div class="title">Example 71. Auto-Conversion</div>
<div class="content">
<div class="paragraph">
<p>Assume some classes and corresponding instances defined as follows:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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();</code></pre>
</div>
</div>
<div class="paragraph">
<p>Instances of these classes will be converted as demonstrated as follows:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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</code></pre>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_auto-conversion-of-interface-instances"><a class="anchor" href="#_auto-conversion-of-interface-instances"></a><a class="link" href="#_auto-conversion-of-interface-instances">7.2.1.1. Auto-Conversion of Interface Instances</a></h5>
<div class="paragraph">
<p>Instances of interfaces actually are instances of classes at runtime.
The auto-conversion rules described in <a href="#_auto-conversion-of-class-instances">Auto-Conversion of Class Instances</a> are applied to instances declared as instances of interfaces as well.</p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_auto-conversion-of-enum-literals"><a class="anchor" href="#_auto-conversion-of-enum-literals"></a><a class="link" href="#_auto-conversion-of-enum-literals">7.2.2. Auto-Conversion of Enum Literals</a></h4>
<div class="paragraph">
<p>Enumeration values are objects and thus follow the behavior for ECMAScript <code>Object</code> and <code>Function</code>.
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.</p>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_type-cast-and-type-check"><a class="anchor" href="#_type-cast-and-type-check"></a><a class="link" href="#_type-cast-and-type-check">7.3. Type Cast and Type Check</a></h3>
<div class="sect3">
<h4 id="_type-cast"><a class="anchor" href="#_type-cast"></a><a class="link" href="#_type-cast">7.3.1. Type Cast</a></h4>
<div class="literalblock">
<div class="content">
<pre>(IDEBUG-56): Casting to TypeVars</pre>
</div>
</div>
<div class="paragraph">
<p>Type casts are expressed with the cast expression (<code>as</code>), see <a href="#_cast-as-expression">Cast (As) Expression</a> for details.</p>
</div>
<div class="paragraph">
<p>We first define helper rules for the type cast constraints as follows:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-89"></a><strong>Req. IDE-89:</strong> <a href="#Req-IDE-89">Cast Validation At Compile Time</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>Given a type cast expression <code>e</code> 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 <code>T</code>, the
following constraints must hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><code>T</code> must be a classifier, enum, primitive, function type expression, classifier type, type variable, union or intersection type:</p>
<div class="paragraph">
<p><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><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></p>
</div>
</li>
<li>
<p>if <code>S</code> is a subtype of <code>T</code>, the cast is unnecessary and a warning will be generated.</p>
</li>
<li>
<p>if <code>S</code> and <code>T</code> are classes, enums or primitive types, then <code>T</code> must be a subtype of <code>S</code>.
This is also true if <code>T</code> is an interface and the type of <code>S</code> cannot have subtypes, or vice versa.</p>
<div class="paragraph">
<p><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><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><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><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><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></p>
</div>
</li>
<li>
<p>if <code>S</code> is a class, enum or primitive type and <code>T</code> 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 <code>T</code>
of type class, enum or primitive <code>S</code> must be a member of the type hierarchy: <sup class="footnote">[<a id="_footnoteref_47" class="footnote" href="#_footnote_47" title="View footnote.">47</a>]</sup></p>
<div class="paragraph">
<p><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><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></p>
</div>
</li>
<li>
<p>if <code>S</code> is a union or intersection type, then the type cast is valid if it is valid for at least one element of <code>S</code>.</p>
</li>
<li>
<p>if <code>S</code> and <code>T</code> 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: <sup class="footnote">[<a id="_footnoteref_48" class="footnote" href="#_footnote_48" title="View footnote.">48</a>]</sup></p>
<div class="paragraph">
<p><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><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></p>
</div>
</li>
<li>
<p>If <code>T</code> is a union type, then the type cast is valid if it is valid for at least one element of <code>T</code>.</p>
</li>
<li>
<p>If <code>T</code> is an intersection type, then the type cast is valid if it is valid for all elements of <code>T</code>.</p>
</li>
</ol>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
<code>any</code> is a supertype of all other types, thus it is always possible to cast a variable of type <code>any</code> to other (non-composed) types.
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_type-check"><a class="anchor" href="#_type-check"></a><a class="link" href="#_type-check">7.3.2. Type Check</a></h4>
<div class="paragraph">
<p>There are basically two ways of testing the type of a variable: <code>typeof</code> and <code>instanceof</code>.
N4JS supports type comparison via the ECMAScript <code>instanceof</code> operator.
The operator <code>instanceof</code> 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.</p>
</div>
<div class="paragraph">
<p>When used with an N4JS class, <code>instanceof</code> 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.</p>
</div>
<div class="paragraph">
<p><code>typeof</code> only returns a string with the name of the ECMAScript type, which is <code>Object</code> for all class instances.</p>
</div>
<div class="paragraph">
<p>N4JS specific <code>string</code> types, that is <code>pathSelector</code> and <code>i18nkey</code> cannot be tested during runtime.
These types, therefore, must not be used in <code>instanceof</code> 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 <code>instancesof</code> constructs.
For all types for which the evaluation result of <code>instanceof</code> 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 <code>instancesof</code> constructs is also not permitted.</p>
</div>
<div class="paragraph">
<p>In order to avoid errors at runtime, the <code>instanceof</code> operator defines appropriate constraints, see <a href="#_relational-expression">Relational Expression</a> for details.</p>
</div>
<div class="exampleblock">
<div class="title">Example 72. Type Check Example</div>
<div class="content">
<div class="paragraph">
<p>Given the following classes and variable:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">interface I{}
class S{}
class Sub extends S implements I{}
var x = new Sub();</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p><code>typeof x</code> will simply return <code>object</code>. The following table shows the difference between plain JavaScript <code>instanceof</code> and N4JS’s <code>instanceof</code>:</p>
</div>
<table class="tableblock frame-all grid-all spread">
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
<col style="width: 33.3334%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Check</th>
<th class="tableblock halign-center valign-top">JavaScript</th>
<th class="tableblock halign-center valign-top">N4JS</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>x instanceof Sub</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>true</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>true</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>x instanceof S</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>true</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>true</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>x instanceof I</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>false</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>true</code></p></td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_reflection-meta-information"><a class="anchor" href="#_reflection-meta-information"></a><a class="link" href="#_reflection-meta-information">7.4. Reflection meta-information</a></h3>
<div class="paragraph">
<p>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 <code>n4type</code>.
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.</p>
</div>
<div class="paragraph">
<p>This getter is of type <code>N4Class</code> which is a built-in type just like <code>N4Object</code>. It contains the following members:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>fqn</code> </dt>
<dd>
<p>The <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>F</mi><mi>Q</mi><mi>N</mi></math> of the type.</p>
</dd>
<dt class="hdlist1"><code>n4superType</code> </dt>
<dd>
<p>The <code>N4Class</code> of the supertype, may be null if supertype is a not an <code>N4Class</code>.</p>
</dd>
<dt class="hdlist1"><code>allImplementedInterfaces</code> </dt>
<dd>
<p>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)</p>
</dd>
<dt class="hdlist1"><code>get isClass</code> </dt>
<dd>
<p>True if the type is an N4Class.</p>
</dd>
<dt class="hdlist1"><code>get isInterface</code> </dt>
<dd>
<p>True if the type is an N4Interface.</p>
</dd>
</dl>
</div>
<div class="sect3">
<h4 id="_reflection-for-classes"><a class="anchor" href="#_reflection-for-classes"></a><a class="link" href="#_reflection-for-classes">7.4.1. Reflection for Classes</a></h4>
<div class="paragraph">
<p>The meta-information for classes is available by means of <code>N4Object</code>’s static
getter <code>n4type</code>. Since it is static getter, it is actually an instance getter of the constructor of a type.</p>
</div>
<div class="paragraph">
<p>In addition, the static method <code>of</code> in <code>N4Type</code> 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 <code>C</code> without having
the constructor of <code>C</code> available, for example because the constructor is not accessible.</p>
</div>
<div class="exampleblock">
<div class="title">Example 73. Reflection with <code>N4class</code></div>
<div class="content">
<div class="paragraph">
<p>This example demonstrates how these reflective features are accessed:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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);</code></pre>
</div>
</div>
<div class="paragraph">
<p>Assuming this code is defined in file <code>A</code>, this will output</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">A.B
A.B
A.A
A.B
N4BuiltInClasses.N4Class</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>The built-in types <code>N4Object</code> and <code>N4Class</code> are also accessible.
They are not defined in a module, thus their <a href="#_acronyms">FQN</a> returns only their simple name.</p>
</div>
<div class="exampleblock">
<div class="title">Example 74. Reflection with Built-In Types</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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)</code></pre>
</div>
</div>
<div class="paragraph">
<p>Assuming this code is defined in file <code>A</code>, this will output</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">N4Object.n4class.fqn: N4Object
N4Class.n4class.fqn: N4Class
A.n4class.fqn: A.A
A.n4class.n4superType.fqn: N4Object</code></pre>
</div>
</div>
<div class="paragraph">
<p>Note that classes extending <code>Object</code> do not provide the static <code>n4class</code> getter, hat is</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class B extends Object {}
console.log('B.n4class.fqn: ' + B.n4class.fqn)</code></pre>
</div>
</div>
<div class="paragraph">
<p>would issue an error as cannot be resolved.</p>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 75. N4Class.of</div>
<div class="content">
<div class="paragraph">
<p>The type has a method to retrieve the meta-information from instances (i.e. or enumeration literals using )
without using the constructor.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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);</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_reflection-for-interfaces"><a class="anchor" href="#_reflection-for-interfaces"></a><a class="link" href="#_reflection-for-interfaces">7.4.2. Reflection for Interfaces</a></h4>
<div class="paragraph">
<p>The meta-information of an interface <code>X</code> is available via getter <code>n4class</code> defined in the <code>type{X}</code>.
This field is of type <code>N4Class</code> as well.
Since an interface cannot have a super classs, the property <code>n4superTypes</code> will always be empty.
Calling <code>isInterface</code> respectively on the returned <code>N4Class</code> instance will return true.</p>
</div>
</div>
<div class="sect3">
<h4 id="_reflection-for-enumerations"><a class="anchor" href="#_reflection-for-enumerations"></a><a class="link" href="#_reflection-for-enumerations">7.4.3. Reflection for Enumerations</a></h4>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var n: number; var b: boolean; var s: string;</code></pre>
</div>
</div>
<div class="paragraph">
<p>The meta-information for enumerations is available by means of the getter <code>n4class</code>, either statically by using the enumeration type or (in terms of an instance getter) via a literal.
Calling <code>isEnum</code> on the returned <code>N4Class</code> instance will return true.</p>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_conversion-of-primitive-types"><a class="anchor" href="#_conversion-of-primitive-types"></a><a class="link" href="#_conversion-of-primitive-types">7.5. Conversion of primitive types</a></h3>
<div class="paragraph">
<p>Conversion between primitives is given as follows:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var n: number; var b: boolean; var s: string;</code></pre>
</div>
</div>
<table class="tableblock frame-all grid-all spread">
<colgroup>
<col style="width: 11.1111%;">
<col style="width: 11.1111%;">
<col style="width: 44.4444%;">
<col style="width: 33.3334%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-center valign-middle">From</th>
<th class="tableblock halign-center valign-middle">To</th>
<th class="tableblock halign-left valign-middle">Conversion</th>
<th class="tableblock halign-center valign-top">Example</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-center valign-middle"><p class="tableblock"><code>string</code></p></td>
<td class="tableblock halign-center valign-middle"><p class="tableblock"><code>number</code></p></td>
<td class="tableblock halign-left valign-middle"><p class="tableblock"><code>Number&#8230;&#8203;</code></p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">n = Number("42");//42</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-center valign-middle"><p class="tableblock"><code>string</code></p></td>
<td class="tableblock halign-center valign-middle"><p class="tableblock"><code>boolean</code></p></td>
<td class="tableblock halign-left valign-middle"><p class="tableblock"><code>N4Primitives.parseBoolean(&#8230;&#8203;)</code></p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js"> b=N4Primitives.parseBoolean("false");</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-center valign-middle"><p class="tableblock"><code>number</code></p></td>
<td class="tableblock halign-center valign-middle"><p class="tableblock"><code>boolean</code></p></td>
<td class="tableblock halign-left valign-middle"><p class="tableblock"><code>Boolean(&#8230;&#8203;)</code></p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">b=Boolean(17.5); //true</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-center valign-middle"><p class="tableblock"><code>number</code></p></td>
<td class="tableblock halign-center valign-middle"><p class="tableblock"><code>string</code></p></td>
<td class="tableblock halign-left valign-middle"><p class="tableblock"><code>Number.toString()</code></p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">s=42.toString(); //"42"</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-center valign-middle"><p class="tableblock"><code>boolean</code></p></td>
<td class="tableblock halign-center valign-middle"><p class="tableblock"><code>number</code></p></td>
<td class="tableblock halign-left valign-middle"><p class="tableblock"><code>N4Primitives.toNumber(&#8230;&#8203;)</code></p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">n=N4Primitives.toNumber(true);</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-center valign-middle"><p class="tableblock"><code>boolean</code></p></td>
<td class="tableblock halign-center valign-middle"><p class="tableblock"><code>string</code></p></td>
<td class="tableblock halign-left valign-middle"><p class="tableblock"><code>Boolean.toString()</code></p></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js"> s=true.toString();//"true" }</code></pre>
</div>
</div></div></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>Remarks:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>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>. <code>N4Primitives.parseBoolean(x)</code> yields true for <code>x.trim().toLowerCase().equals("true")</code></p>
</li>
<li>
<p>The call to <code>Boolean(..)</code> for the arguments <code>0</code>, <code>-0</code>, <code>null</code>, <code>false</code>, <code>NaN</code>, <code>undefined</code> and <code>""</code> evaluate to <code>false</code>.
All other values evaluate to <code>true</code>.</p>
</li>
<li>
<p><code>Number</code> has several methods for converting a value to string [<a href="#ECMA11a">ECMA11a(p.S15.7.4)</a>]: <code>toExponential()</code>, to <code>Fixed()</code>, <code>toPrecision()</code>.</p>
</li>
<li>
<p>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 <code>N4Primitives.toNumber()</code> yields.</p>
</li>
</ol>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_expressions"><a class="anchor" href="#_expressions"></a><a class="link" href="#_expressions">8. Expressions</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>For all expressions, we define the following pseudo properties:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>containingExpression</code> </dt>
<dd>
<p>The parent expression, in which an expression is contained, may be null.</p>
</dd>
<dt class="hdlist1"><code>containingStatement</code> </dt>
<dd>
<p>The statement in which the expression is (indirectly) contained.</p>
</dd>
<dt class="hdlist1"><code>containingFunctionOrAccessor</code> </dt>
<dd>
<p>The function, method, getter or setter in which the expression is (indirectly) contained, may be null.</p>
</dd>
<dt class="hdlist1"><code>containingClass</code> </dt>
<dd>
<p>The class in which the expression is (indirectly) contained, may be null.</p>
</dd>
<dt class="hdlist1"><code>probableThisTarget</code> </dt>
<dd>
<p>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 <code>T</code>, this usually is the classifier <code>T</code>; in case of static methods, it is the classifier type <code>type{type}</code>.</p>
</dd>
<dt class="hdlist1"><code>container</code> </dt>
<dd>
<p>The direct owner of the expression.
z</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>The expressions and statements are ordered, describing first the constructs available in the 5th edition of ECMA-262, referred to as [<a href="#ECMA11a">ECMA11a</a>] in the following.
It is worth noting that the grammar snippets already use newer constructs in some cases.</p>
</div>
<div class="sect2 language-n4js">
<h3 id="_ecmascript-5-expressions"><a class="anchor" href="#_ecmascript-5-expressions"></a><a class="link" href="#_ecmascript-5-expressions">8.1. ECMAScript 5 Expressions</a></h3>
<div class="paragraph">
<p>N4JS supports the same expressions as ECMAScript.
The semantics are described in [<a href="#ECMA11a">ECMA11a(p.S11)</a>].
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.</p>
</div>
<div class="paragraph">
<p>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., <code>++</code> and <code>+=</code> are deduced to <code>+</code> (and simple assignment).</p>
</div>
<div class="sect3">
<h4 id="_the-this-literal"><a class="anchor" href="#_the-this-literal"></a><a class="link" href="#_the-this-literal">8.1.1. The this Literal</a></h4>
<div class="paragraph">
<p>This section describes the <code>this</code> literal and the semantics of the <code>@This</code> annotation, the type <code>this</code> is described in <a href="#_this-type">This Type</a>.</p>
</div>
<h5 id="_semantics-9" class="discrete">Semantics</h5>
<div class="paragraph">
<p>Semantics are similar to the original ECMAScript this keyword, see [<a href="#ECMA11a">ECMA11a(p.11.1.1, p.p.63)</a>])
Also see [<a href="#West06a">West06a</a>] and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this">MozillaJSRef</a></p>
</div>
<div class="paragraph">
<p>Regarding the location where <code>this</code> may be used, the following restrictions apply:</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-173"></a><strong>Req. IDE-173:</strong> <a href="#Req-IDE-173">Valid location for this literal</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>The literal may not be used in</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>the initializer expression of static data fields in classes.</p>
</li>
<li>
<p>the initializer expression of data fields in interfaces (applies to both static and non-static).</p>
</li>
<li>
<p>static methods of interfaces and static field accessors of interfaces.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>See also <a href="#Req-IDE-69">[Req-IDE-69]</a>.</p>
</div>
</div>
</div>
<div class="paragraph">
<p>Note: <a href="#Req-IDE-173">[Req-IDE-173]</a> also applies for this literals inside arrow expressions in initializers.</p>
</div>
<div class="paragraph">
<p>The use of <code>this</code> is illustrated with some examples as it can often be confusing.
Type inference heuristics and explanations are provided in the next section.</p>
</div>
<div class="exampleblock">
<div class="title">Example 76. This in Unrestricted Mode</div>
<div class="content">
<div class="paragraph">
<p>In unrestricted mode, <code>this</code> 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.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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
}
}</code></pre>
</div>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 77. This in strict mode</div>
<div class="content">
<div class="paragraph">
<p>In strict mode, <code>this</code> is bound to the receiver.
If there is no receiver, it is bound to <code>undefined</code>.
Thus, we will probably get a lot of errors:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">"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
}
}</code></pre>
</div>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 78. This in N4JS mode</div>
<div class="content">
<div class="paragraph">
<p>As in strict mode, <code>this</code> is bound to the receiver and if there is no receiver, it is bound to <code>undefined</code>. So the example above is also true for N4JS mode.
Classes behave slightly differently:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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();
}
}</code></pre>
</div>
</div>
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
In N4JS classes, <code>this</code> is always bound to the instance when used in field initialization.
</td>
</tr>
</table>
</div>
<h5 id="_type-inference-2" class="discrete">Type Inference</h5>
<div class="openblock language-n4js">
<div class="content">
<div class="paragraph">
<p>The type is inferred from the <code>this</code> type is bound to. The inference,
therefore, has to consider the original semantics as described in [<a href="#ECMA11a">ECMA11a(p.10.4., p.10.4.3, p.p.58)</a>].
In ECMAScript the type of this is unfortunately determined by the function call and not by the function definition:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>By default, <code>this</code> is bound to the global object [<a href="#ECMA11a">ECMA11a(p.10.4.1.1)</a>].
Unfortunately it is often unknown what the global object will be at run time (e.g., node.js differs from browsers).</p>
</li>
<li>
<p>If a function is called without a receiver, <code>this</code> is bound to</p>
<div class="ulist">
<ul>
<li>
<p>the global object or</p>
</li>
<li>
<p>to <code>undefined</code> in strict mode.</p>
</li>
</ul>
</div>
</li>
<li>
<p>If a function is called with a receiver,<code>this</code> is bound to the receiver object.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Actually, <code>this</code> is bound to the newly created object if a function is called with the <code>new</code> 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> (<code>apply()</code> etc.), the <code>@This</code> annotation can be used to explicitly set the this type.
This annotation has precedence over otherwise inferred bindings.</p>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-90"></a><strong>Req. IDE-90:</strong> <a href="#Req-IDE-90">Type Inference Heuristic for This-Keyword</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><a id="this-keyword-constraint-1"></a> If the this keyword is used in some function annotated with an annotation <code>@This</code>, the type specified in the annotation is used.
The inferred type is always nominal.</p>
<div class="paragraph">
<p><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><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></p>
</div>
</li>
<li>
<p><a id="this-keyword-constraint-2"></a> If the this keyword is used in some <em>instance</em> method of a classifier or in an <em>instance</em> field initializer,<code>this</code> is bound to the <code>T</code> itself.
If the this keyword is used in some <em>static</em> method of a classifier <code>T</code> or in a <em>static</em> field initializer, the prototype type (or constructor) of the classifier is used, that is <code>type[T]</code>.
In both cases, the target is determined by using the expressions’s pseudo property <code>probableThisTarget</code>.
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 <code>this</code> type in instance methods, and the <code>this</code> type in static methods.</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>In all other cases: Non-strict mode:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
</ol>
</div>
<div class="paragraph">
<p>Strict mode and N4JS mode:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>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. <a href="#Req-IDE-90">[Req-IDE-90]</a> constraints <a href="#this-keyword-constraint-1">1</a> and <a href="#this-keyword-constraint-2">2</a>.).</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-91"></a><strong>Req. IDE-91:</strong> <a href="#Req-IDE-91">Valid Target and Argument for @This Annotation</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The <code>@This</code> 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.</p>
</li>
<li>
<p>The type declared by way of <code>@This(..)</code> an annotation of a method or field accessor must be a subtype of the member’s containing classifier.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-92"></a><strong>Req. IDE-92:</strong> <a href="#Req-IDE-92">Single @This Annotation</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>It is not allowed to use more then one <code>@This(..)</code> annotation on an element.</p>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 79. Effect of Nominal This Type</div>
<div class="content">
<div class="paragraph">
<p>Given the following declaration</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">@This(~Object with {a: string;}) f() {}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Since the this type is always nominal, <code>~ Object</code> becomes <code>Object</code>.
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</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">~this[Object] with {a: string;}</code></pre>
</div>
</div>
<div class="paragraph">
<p>For the sake of simplicity, additional structural members are usually
omitted in error messages, leading to</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">~this[Object]</code></pre>
</div>
</div>
<div class="paragraph">
<p>instead of</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">this[~Object]</code></pre>
</div>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 80. This and Function Declaration</div>
<div class="content">
<div class="paragraph">
<p>This example demonstrates the usage of functions annotated with <code>@This</code>.
By using the argument <code>union{A,B}</code> it is possible to have two completely unrelated classes as the receiver type of the function <code>logger</code>.
To pass an actual object the <code>apply()</code> method of the function is used.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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 ~"</code></pre>
</div>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 81. This and Function Expressions</div>
<div class="content">
<div class="paragraph">
<p>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 <code>@This(B)</code>, access to the receiver of type B is enabled.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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;*"</code></pre>
</div>
</div>
<div class="openblock">
<div class="content">
</div>
</div>
<div class="paragraph">
<p>Note that if a function is called as a constructor function with new, the
type of <code>this</code> can be declared via annotation <code>@This(..)</code>, as shown in the following
snippet:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">@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)</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>Inside the constructor function <code>Box</code>, <code>this</code> is bound to the structural type definition due to the annotation.</p>
</div>
<div class="paragraph">
<p>Inside the nested function <code>area</code>, <code>this</code> is bound to the receiver object (if the function is called like <code>bOk.area()</code>).
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.</p>
</div>
<div class="paragraph">
<p>When calling this function, the type of this is checked against the declared this type, which would cause an error in the first case.</p>
</div>
<div class="paragraph">
<p>The use of the <code>@This</code> annotation is not allowed on methods.</p>
</div>
<div class="admonitionblock tip">
<table>
<tr>
<td class="icon">
<i class="fa icon-tip" title=""></i>
</td>
<td class="content">
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) <strong>class</strong> Rectangle rather then defining the constructor function.
</td>
</tr>
</table>
</div>
</div>
<div class="sect3">
<h4 id="_identifier"><a class="anchor" href="#_identifier"></a><a class="link" href="#_identifier">8.1.2. Identifier</a></h4>
<h5 id="_syntax-10" class="discrete">Syntax</h5>
<div class="paragraph">
<p>Identifiers as expressions are identifier references.
They are defined as follows:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">IdentifierRef &lt;Yield&gt;:
id=[types::IdentifiableElement|BindingIdentifier&lt;Yield&gt;]
;
BindingIdentifier &lt;Yield&gt;:
IDENTIFIER
| &lt;!Yield&gt; 'yield'
| N4Keyword
;</code></pre>
</div>
</div>
<h5 id="_semantics-10" class="discrete">Semantics</h5>
<div class="paragraph">
<p>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. [<a href="#ECMA11a">ECMA11a(p.10.2.2.1GetIdentifierReference, p.p.56)</a>].
The following scopes (aka <em>Lexical Environments</em>) are defined:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>function local; local variables, parameters</p>
</li>
<li>
<p>zero or more function closure in case of nested functions</p>
</li>
<li>
<p>module</p>
</li>
<li>
<p>global</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>These scope are nested as illustrated in <a href="#fig:scopes">Scopes</a>.</p>
</div>
<div class="paragraph">
<p>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 <code>this</code>.
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 <a href="#_property-accessors">Property Accessors</a>).</p>
</div>
<div id="fig:scopes" class="imageblock" style="text-align: center">
<div class="content">
<img src="chapters/08_expressions/fig/scopes.svg" alt="scopes">
</div>
<div class="title">Figure 7. Scopes</div>
</div>
<div class="paragraph">
<p>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 <a href="#_property-accessors">Property Accessors</a>.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-93"></a><strong>Req. IDE-93:</strong> <a href="#Req-IDE-93">Read Access to Identifier</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p><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><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><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><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><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></p>
</div>
</div>
</div>
<h5 id="_type-inference-3" class="discrete">Type Inference</h5>
<div class="paragraph">
<p>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:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_literals"><a class="anchor" href="#_literals"></a><a class="link" href="#_literals">8.1.3. Literals</a></h4>
<div class="paragraph">
<p>cf. [<a href="#ECMA11a">ECMA11a(p.S11.1.3p.63, p.S7.8p.19ff)</a>].</p>
</div>
<h5 id="_type-inference-4" class="discrete">Type Inference</h5>
<div class="paragraph">
<p>The type of a literal can directly be derived from the grammar.
The following axioms are defined for literals:</p>
</div>
<div class="paragraph">
<p><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><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><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><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><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></p>
</div>
<div class="paragraph">
<p>Note that there are no literals specific for <code>pathSelector</code> or <code>i18nkey</code>.</p>
</div>
<div class="sect4">
<h5 id="_integer-literals"><a class="anchor" href="#_integer-literals"></a><a class="link" href="#_integer-literals">8.1.3.1. Integer Literals</a></h5>
<div class="paragraph">
<p>Numeric literals representing integers in the range of JavaScript’s int32 are inferred to the built-in primitive type <code>int</code> instead of <code>number</code>.
The following rules apply:</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-94"></a><strong>Req. IDE-94:</strong> <a href="#Req-IDE-94">Numeric literals</a> (ver. 1)</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Numeric literals with a fraction or using scientific notation, e.g. <code>2.0</code> and <code>2e0</code>, respectively, are always inferred to <code>number</code>, even if they represent integers in the range of int32.</p>
</li>
<li>
<p>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 <code>int</code>.</p>
</li>
<li>
<p>Hexadecimal and octal literals are always interpreted as positive numbers, so all values above <code>0x7fffffff</code> and <code>017777777777</code> lie outside the range of int32 and will thus
be inferred to <code>number</code>; this is an important difference to Java. See below for further elaboration.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>There are differences to numeric literals in Java:</p>
</div>
<table class="tableblock frame-all grid-all spread">
<colgroup>
<col style="width: 25%;">
<col style="width: 25%;">
<col style="width: 12.5%;">
<col style="width: 25%;">
<col style="width: 12.5%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top"></th>
<th class="tableblock halign-center valign-top" colspan="2">Java</th>
<th class="tableblock halign-center valign-top" colspan="2">JavaScript N4JS</th>
</tr>
</thead>
<tbody>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock">Literal</p></th>
<th class="tableblock halign-left valign-top"><p class="tableblock">Value</p></th>
<th class="tableblock halign-left valign-top"><p class="tableblock">Type</p></th>
<th class="tableblock halign-left valign-top"><p class="tableblock">Value</p></th>
<th class="tableblock halign-left valign-top"><p class="tableblock">Type</p></th>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>2147483648</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><em>-2147483648</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>int</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><em>-2147483648</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>int</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>2147483647</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><em>2147483647</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>int</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><em>2147483647</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>int</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>0x7fffffff</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><em>2147483647</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>int</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><em>2147483647</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>int</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>0x80000000</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><em>-2147483648</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>int</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><em>+2147483648</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>number</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>0xffffffff</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><em>-1</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>int</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><em>4294967295</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>number</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>0x100000000</code></p></td>
<th class="tableblock halign-center valign-top" colspan="2"><p class="tableblock">n/a</p></th>
<td class="tableblock halign-left valign-top"><p class="tableblock"><em>4294967296</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>number</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>017777777777</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><em>2147483647</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>int</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><em>2147483647</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>int</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>020000000000</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><em>-2147483648</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>int</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><em>+2147483648</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>number</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>037777777777</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><em>-1</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>int</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><em>4294967295</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>number</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>040000000000</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><em>0</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>int</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><em>4294967296</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>number</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>0100000000000</code></p></td>
<th class="tableblock halign-center valign-top" colspan="2"><p class="tableblock">n/a</p></th>
<td class="tableblock halign-left valign-top"><p class="tableblock"><em>8589934592</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>number</code></p></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>The literals <code>0x100000000</code> and <code>0100000000000</code> produce a syntax error in Java.</p>
</div>
<div class="paragraph">
<p>Until IDE-1881 is complete, all built-in operations always return a <code>number</code> even if all operands are of type <code>int</code>.
For the time being, we therefore interpret <code>-1</code> as a negative integer literal (inferred to <code>int</code>), but <code>-(1)</code> as the negation of a positive integer literal (inferred to <code>number</code>).</p>
</div>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_array-literal"><a class="anchor" href="#_array-literal"></a><a class="link" href="#_array-literal">8.1.4. Array Literal</a></h4>
<h5 id="_syntax-11" class="discrete">Syntax</h5>
<div class="paragraph">
<p>cf [<a href="#ECMA11a">ECMA11a(p.S11.1.4, p.p.63)</a>]</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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;;</code></pre>
</div>
</div>
<h5 id="_type-inference-5" class="discrete">Type Inference</h5>
<div class="paragraph">
<p>In general, an array literal is inferred as <code>Array&lt;T&gt;</code> (similar to the type of <code>new Array()</code>).
The interesting question is the binding of the type variable <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math>.</p>
</div>
<div class="paragraph">
<p>The type of an array padding <em>p</em> is inferred as follows:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>In other languages not supporting union types, the element type is often inferred as the join (<a href="#_acronyms">LCST</a>) 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.</p>
</div>
<div class="paragraph">
<p>Note that <code>typeof [1,2,3]</code> does not return <code>Array&lt;number&gt;</code> (as ECMAScript is not aware of the generic array type), but <code>Object</code>.</p>
</div>
<div class="exampleblock">
<div class="title">Example 82. Array Type Inference</div>
<div class="content">
<div class="paragraph">
<p>The type for all variables declared in this example is inferred to <code>Array&lt;string&gt;</code>:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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;;</code></pre>
</div>
</div>
<div class="paragraph">
<p>Empty array literals are inferred to <code>any</code>, by default.
We are not using <code>Array&lt;?&gt;</code> here because then a typical JavaScript pattern would no longer be supported:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var a = [];
a.push('hello'); <i class="conum" data-value="1"></i><b>(1)</b></code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>This would fail if <code>a</code> and thus <code>[]</code> were inferred to <code>Array&lt;?&gt;</code></td>
</tr>
</table>
</div>
</div>
</div>
<div class="admonitionblock important">
<table>
<tr>
<td class="icon">
<i class="fa icon-important" title=""></i>
</td>
<td class="content">
An important exception; if a type expectation exists for the empty array literal and the expected type is <code>Array&lt;T&gt;</code>, this will be used as the type of the array literal.
</td>
</tr>
</table>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-95"></a><strong>Req. IDE-95:</strong> <a href="#Req-IDE-95">Empty array literal</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>An empty array literal will be inferred as follows:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>If there is a type expectation for the empty array literal and the expected type is <code>Array&lt;T&gt;</code>, for any type <code>T</code>, then the type of the empty array literal will be inferred to <code>Array&lt;T&gt;</code>.</p>
</li>
<li>
<p>Otherwise, the type of the empty array literal will be inferred to <code>Array&lt;any&gt;</code>.</p>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_object-literal"><a class="anchor" href="#_object-literal"></a><a class="link" href="#_object-literal">8.1.5. Object Literal</a></h4>
<div class="paragraph">
<p>In addition to ordinary Javascript object literals, N4JS supports the spread operator within object literals as introduced in [<a href="#ECMA18a">ECMA18a</a>].</p>
</div>
<h5 id="object-literal-syntax" class="discrete">Syntax</h5>
<div class="paragraph">
<p>Cf. [<a href="#ECMA11a">ECMA11a(p.S11.1.5, p.p.65ff)</a>]
The syntax of an object literal is given by:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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;
;</code></pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">import Address from "my/Address";
var simple = {name: "Walter", age: 72, address: new Address()};</code></pre>
</div>
</div>
<div class="sect4">
<h5 id="_properties-6"><a class="anchor" href="#_properties-6"></a><a class="link" href="#_properties-6">8.1.5.1. Properties</a></h5>
<div class="paragraph">
<p>PropertyAssignments have common properties of PropertyNameValuePair, PropertyGetterDeclaration, and PropertySetterDeclaration:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>annotations</code> </dt>
<dd>
<p>The annotations of the property assignment.</p>
</dd>
<dt class="hdlist1"><code>name</code> </dt>
<dd>
<p>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.</p>
</dd>
<dt class="hdlist1"><code>declaredType</code> </dt>
<dd>
<p>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.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>Additionally, we introduce the following pseudo properties to simplify constraints:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>isAccessor</code> </dt>
<dd>
<p>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 <code>IsAccessoprDescriptor</code>.
For a given property assignment <em>p</em> 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>.</p>
</dd>
<dt class="hdlist1"><code>isData</code> </dt>
<dd>
<p>The read-only boolean property.
This is true if the property assignment is a name value pair.
For a given property assignment <em>p</em> 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 <code>isDataDescriptor</code>.
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.</p>
</dd>
</dl>
</div>
<h5 id="properties-semantics" class="discrete">Semantics</h5>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-96"></a><strong>Req. IDE-96:</strong> <a href="#Req-IDE-96">Object literal</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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. [<a href="#ECMA11a">ECMA11a(p.p.66)</a>]:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>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 <a href="#compile-time-expressions">Compile-Time Expressions</a>). In particular, string literals, e.g. <code>['myProp']</code>, built-in symbols, e.g. <code>[Symbol.iterator]</code>, and literals of <code>@StringBased</code> enums are all valid computed property names.</p>
</li>
<li>
<p>Object literal may not have two PropertyNameValuePairs with the same name in strict mode (cf. 4.a [<a href="#ECMA11a">ECMA11a(p.p.66)</a>]):</p>
<div class="paragraph">
<p><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><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><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></p>
</div>
</li>
<li>
<p>Object literal may not have PropertyNameValuePair and <code>PropertyGetterDeclaration</code>/<code>PropertySetterDeclaration</code> with the same name (cf. 4.b/c [<a href="#ECMA11a">ECMA11a(p.p.66)</a>]):</p>
<div class="paragraph">
<p><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><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><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></p>
</div>
</li>
<li>
<p>Object literal may not have multiple <code>PropertyGetterDeclaration</code> or <code>PropertySetterDeclaration</code> with the same name (cf. 4.d [<a href="#ECMA11a">ECMA11a(p.p.66)</a>]):</p>
<div class="paragraph">
<p><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><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><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></p>
</div>
</li>
</ul>
</div>
<div class="quoteblock">
<blockquote>
It is a SyntaxError if the Identifier <code>eval</code> or the Identifier <code>arguments</code> occurs as the Identifier in a <code>PropertySetParameterList</code> of a <code>PropertyAssignment</code> that is contained in strict code or if its
<code>FunctionBody</code> is strict code. [<a href="#ECMA11a">ECMA11a(p.p.66)</a>]
</blockquote>
</div>
<div class="ulist">
<ul>
<li>
<p>If two or more property assignments have the same name (and the previous conditions hold), then the types of these assignments must <em>conform</em>.
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.</p>
</li>
<li>
<p>In N4JS mode, the name of a property must be a valid N4JSIdentifier:</p>
<div class="paragraph">
<p><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><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></p>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-22501"></a><strong>Req. IDE-22501:</strong> <a href="#Req-IDE-22501">Superfluous properties of an object literal</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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 <code>Object</code> 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>.</p>
</div>
<div class="paragraph">
<p>This is true in particular for object literals passed in as arguments of a spec-constructor.</p>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_scoping-and-linking"><a class="anchor" href="#_scoping-and-linking"></a><a class="link" href="#_scoping-and-linking">8.1.5.2. Scoping and linking</a></h5>
<div class="exampleblock">
<div class="title">Example 83. Scoping and linking</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var p = {
f: function() {
console.log("p´s f");
},
b: function() {
this.f();
},
o: {
nested: "Hello"
}
};
p.b();
p.o.nested;</code></pre>
</div>
</div>
<div class="ulist">
<ul>
<li>
<p>Other properties within an object literal property can be accessed using this.
In the expression of property name value pairs, however, <code>this</code> is not be bound to the containing object literal, but usually to undefined or global.</p>
</li>
<li>
<p>The properties of an object literal are accessible from outside.</p>
</li>
<li>
<p>Nested properties of an object literal are also accessible from outside.</p>
</li>
</ul>
</div>
</div>
</div>
<h5 id="type-inference-3" class="discrete">Type Inference</h5>
<div class="paragraph">
<p>An object literal implicitly extends <code>~Object</code>, therefore, object literal types use structural typing.
For details see <a href="#_structural-typing">Structural Typing</a>.
From a type systems point of view, the two variables <code>ol</code> and <code>st</code> below have the same type.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var ol = {
s: "hello",
n: 42
}
var st: ~Object with { s: string; n: number;};</code></pre>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_parenthesized-expression-and-grouping-operator"><a class="anchor" href="#_parenthesized-expression-and-grouping-operator"></a><a class="link" href="#_parenthesized-expression-and-grouping-operator">8.1.6. Parenthesized Expression and Grouping Operator</a></h4>
<div class="paragraph">
<p>The grouping operator is defined here as a parenthesized expression.</p>
</div>
<h5 id="parenthesized-expression-grouping-syntax" class="discrete">Syntax</h5>
<div class="paragraph">
<p>cf. [<a href="#ECMA11a">ECMA11a(p.S11.1.6, p.p.67)</a>]</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">ParenExpression &lt;Yield&gt;: '(' expression=Expression&lt;In=true,Yield&gt; ')';</code></pre>
</div>
</div>
<h5 id="Grouping-Operator-type-inference" class="discrete">Type Inference</h5>
<div class="paragraph">
<p>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:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 84. Parenthesized Expression Type Examples</div>
<div class="content">
<div class="paragraph">
<p>In the following listing, the type of the plain expressions is equivalent to the parenthesized versions:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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);</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_property-accessors"><a class="anchor" href="#_property-accessors"></a><a class="link" href="#_property-accessors">8.1.7. Property Accessors</a></h4>
<h5 id="property-accessor-syntax" class="discrete">Syntax</h5>
<div class="paragraph">
<p>Property accessors in N4JS are based on [<a href="#ECMA11a">ECMA11a(p.S11.2.1, p.p.67ff)</a>].
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.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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]
;</code></pre>
</div>
</div>
<div class="paragraph">
<p>Note that in [<a href="#ECMA11a">ECMA11a</a>], the <code>index access</code> is called <code><em>bracket notation</em></code>.</p>
</div>
<h5 id="property-access-direct" class="discrete">Direct Property Access</h5>
<div class="paragraph">
<p>We define a special case of property access as follows:</p>
</div>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="direct_property_access"></a><strong>Definition:</strong> <a href="#direct_property_access">Direct Property Access</a></p>
</div>
<div class="paragraph">
<p>A property access expression is called <em>direct</em>, iff</p>
</div>
<div class="ulist">
<ul>
<li>
<p>its target is an identifier reference to a class, interface, enum, or the built-in object <code>Symbol</code>, and</p>
</li>
<li>
<p>its property name denotes an <em>owned</em> member of the target classifier (not an inherited, consumed, or polyfilled member) or a literal if the target is an enum.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="paragraph">
<p>As a consequence, a direct property access can only refer to static members.</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {
const field = 'hello';
}
C.field; // direct property access to 'field'
let ctor = C;
ctor.field; // *not* a direct property access to 'field'</code></pre>
</div>
</div>
<div class="paragraph">
<p>Direct property access is the only form of property access allowed in compile-time expressions, cf. <a href="#compile-time-expressions">Compile-Time Expressions</a>.</p>
</div>
<div class="sect4">
<h5 id="properties-1"><a class="anchor" href="#properties-1"></a><a class="link" href="#properties-1">8.1.7.1. Properties</a></h5>
<div class="paragraph">
<p>We define the following properties:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>target</code> </dt>
<dd>
<p>The receiver of the property access.</p>
</dd>
<dt class="hdlist1"><code>index</code> </dt>
<dd>
<p>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).</p>
</dd>
<dt class="hdlist1"><code>property</code> </dt>
<dd>
<p>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).</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>We define the following pseudo properties:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>isDotAccess</code> </dt>
<dd>
<p>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>).</p>
</dd>
<dt class="hdlist1"><code>isIndexAccess</code> </dt>
<dd>
<p>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>.<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.</p>
</dd>
<dt class="hdlist1"><code>name</code> </dt>
<dd>
<p>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.</p>
</dd>
</dl>
</div>
<h5 id="property-acessors-semantics" class="discrete">Semantics</h5>
<div class="paragraph">
<p>The parameterization is part of the property access in case of generic methods.
For generic functions, a parameterized function call is introduced (cf. <a href="#_function-calls">Function Calls</a>).
The constraints are basically similar.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-97"></a><strong>Req. IDE-97:</strong> <a href="#Req-IDE-97">Property Access and Dot Notation</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>If dot notation is used in N4JS mode, the referenced property must exist unless receiver is a dynamic type:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>If dot notation is used and the referenced property exists, then the property must be accessible:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>If dot notation is used and the referenced property exists and this property is a member with a declared <code>@This</code> type (only possible for methods or field accessors),
then the receiver must be a subtype of the declared <code>@This</code> type.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-98"></a><strong>Req. IDE-98:</strong> <a href="#Req-IDE-98">Index Access</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>An index access expression is valid iff one of the following cases applies:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>the receiver is of a dynamic type. In this case, the index may be any expression (need not be a compile-time expression).</p>
</li>
<li>
<p>the receiver is an immediate instance of <code>Object</code>, i.e. it is a subtype of <code>Object</code> and its super types but <strong>not</strong> of any other type including <code>~Object</code> and <code>~~Object</code>.</p>
</li>
<li>
<p>the receiver is of type Array, ArgumentType, string, or String (including their subtypes) <strong>and</strong> the index is an expression of type <code>number</code>.</p>
</li>
<li>
<p>the index expression is a compile-time expression</p>
<div class="ulist">
<ul>
<li>
<p><strong>and</strong> the receiver type defines a member with a name equal to the string representation of the index expression&#8217;s compile-time value<br></p>
</li>
<li>
<p><strong>and</strong> the receiver is not an enum.</p>
</li>
</ul>
</div>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>Although index access is very limited, it is still possible to use immediate instances of <code>Object</code> in terms of a map (but this applies only to index access, not the dot notation):</p>
</div>
<div class="exampleblock">
<div class="title">Example 85. Object as Map</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var map: Object = new Object();
map["Kant"] = "Imperative";
map["Hegel"] = "Dialectic";
map.spinoza = "Am I?"; // error: Couldn't resolve reference to IdentifiableElement 'spinoza'.</code></pre>
</div>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-99"></a><strong>Req. IDE-99:</strong> <a href="#Req-IDE-99">Parameterized Property Access</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The receiver or target must be a function or method:<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></p>
</li>
<li>
<p>The number of type arguments must match the number of type parameters of the generic function or method:<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></p>
</li>
<li>
<p>The type arguments of a parameterized property access expression must be subtypes of the boundaries of the parameters of the called generic method.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>Also see constraints on read (<a href="#Req-IDE-93">[Req-IDE-93]</a>) and write (<a href="#Req-IDE-121">[Req-IDE-121]</a>) access.</p>
</div>
</div>
</div>
<h5 id="type-inference-5" class="discrete">Type Inference</h5>
<div class="paragraph">
<p>Cf. [<a href="#ECMA11a">ECMA11a(p.S11.2.1, p.p.67ff)</a>]</p>
</div>
<div class="paragraph">
<p>We define the following type inferencing rules for property accessors:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The type of an indexed-access expression <em>p</em> is inferred as follows:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>The type of a property access expression is inferred as follows:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>The type of a parameterized access expression <em>p</em> is inferred as follows:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_new-expression"><a class="anchor" href="#_new-expression"></a><a class="link" href="#_new-expression">8.1.8. New Expression</a></h4>
<div class="paragraph">
<p>cf. [<a href="#ECMA11a">ECMA11a(p.S11.2.2, p.p.68)</a>]</p>
</div>
<h5 id="new-expression-syntax" class="discrete">Syntax</h5>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">NewExpression: 'new' callee=MemberExpression&lt;Yield&gt; (-&gt; TypeArguments)?
(=&gt; withArgs?='(' Arguments&lt;Yield&gt;? ')' )?</code></pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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");</code></pre>
</div>
</div>
<h5 id="new-expression-semantics" class="discrete">Semantics</h5>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-100"></a><strong>Req. IDE-100:</strong> <a href="#Req-IDE-100">New expression</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><a id="new-expression-1"></a> 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.</p>
</li>
<li>
<p><a id="new-expression-2"></a> 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,</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p><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></p>
</li>
<li>
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi></math> must not contain any wildcards.</p>
</li>
<li>
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi></math> must not be a type variable.</p>
</li>
</ol>
</div>
</li>
<li>
<p><a id="new-expression-3"></a> 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:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>In particular, it must not refer to a primitive type or a defined
functions (i.e., subtypes of <code>Function</code>) cannot be used in new-expressions in
N4JS.</p>
</div>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>Remarks:</p>
</div>
<div class="paragraph">
<p>to <a href="#new-expression-1">1</a> The type of an abstract class <code>A</code> 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>.</p>
</div>
<div class="paragraph">
<p>to <a href="#new-expression-2">2</a> 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.</p>
</div>
<div class="paragraph">
<p>to <a href="#new-expression-3">3</a> 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.</p>
</div>
<div class="exampleblock">
<div class="title">Example 86. Abstract classes and construction</div>
<div class="content">
<div class="paragraph">
<p>The following examples demonstrates the usage of abstract classes and constructor types, to make the first two constraints more clearer:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">/* 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);</code></pre>
</div>
</div>
</div>
</div>
<h5 id="type-inference-6" class="discrete">Type Inference</h5>
<div class="paragraph">
<p>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:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>For classes, constructors are described in <a href="#_constructor-and-classifier-type">Constructor and Classifier Type</a>.</p>
</div>
<div class="paragraph">
<p>In N4JS it is not allowed to call new on a plain function.
For example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">function foo() {}
var x = new foo();</code></pre>
</div>
</div>
<div class="paragraph">
<p>will issue an error.</p>
</div>
</div>
<div class="sect3">
<h4 id="_function-expression-2"><a class="anchor" href="#_function-expression-2"></a><a class="link" href="#_function-expression-2">8.1.9. Function Expression</a></h4>
<div class="paragraph">
<p>See <a href="#_functions">Functions</a> for details.</p>
</div>
</div>
<div class="sect3">
<h4 id="_function-calls"><a class="anchor" href="#_function-calls"></a><a class="link" href="#_function-calls">8.1.10. Function Calls</a></h4>
<div class="paragraph">
<p>In N4JS, a function call [<a href="#ECMA11a">ECMA11a(p.S11.2.3)</a>] is similar to a method call.
Additionally to the ECMAScript’s CallExpression, a ParameterizedCallExpression is introduced to allow type arguments passed to plain functions.</p>
</div>
<h5 id="_syntax-12" class="discrete">Syntax</h5>
<div class="literalblock">
<div class="content">
<pre>[[function-calls-syntax]]</pre>
</div>
</div>
<div class="paragraph">
<p>Similar to [<a href="#ECMA11a">ECMA11a(p.S11.2.3, p.p.68ff)</a>], a function call is defined as follows:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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;
;</code></pre>
</div>
</div>
<h5 id="function-calls-semantics" class="discrete">Semantics</h5>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-101"></a><strong>Req. IDE-101:</strong> <a href="#Req-IDE-101">Function Call Constraints</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>If less arguments are provided than formal parameters were declared, the missing formal parameters must have been declared optional:<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></p>
</li>
<li>
<p>If more arguments are provided than formal parameters were declared, the last formal parameter must have been declared variadic:<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></p>
</li>
<li>
<p>Types of provided arguments must be subtypes of the formal parameter types:<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></p>
</li>
<li>
<p>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:<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></p>
</li>
</ul>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-102"></a><strong>Req. IDE-102:</strong> <a href="#Req-IDE-102">Parameterized Function Call Constraints</a> (ver. 1)</p>
</div>
<div class="ulist">
<ul>
<li>
<p>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.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Note that (for a limited time), constraints <a href="#Req-IDE-101">[Req-IDE-101]</a> and <a href="#Req-IDE-102">[Req-IDE-102]</a> are not applied if the the type of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>F</mi></math> is <code>Function</code>.
See <a href="#_function-object-type">Function-Object-Type</a>.</p>
</div>
</div>
</div>
<h5 id="type-inference-7" class="discrete">Type Inference</h5>
<div class="paragraph">
<p>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 (<a href="#_methods">Methods</a>) or function declaration (which may be part of a function definition
(<a href="#_function-declaration">Function Declaration</a> or specified via a function type <a href="#_function-type">Function Type</a>) <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>F</mi></math> (via evaluation of <code>MemberExpression</code>.
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:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>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 <a href="#_property-accessors">Property Accessors</a> for semantics and type inference.</p>
</div>
<div class="exampleblock">
<div class="title">Example 87. Generic Method Invocation</div>
<div class="content">
<div class="paragraph">
<p>This examples demonstrate how to explicitly
define the type argument in a method call in case it cannot be inferred
automatically.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {
static &lt;T&gt; foo(p: pathSelector&lt;T&gt;): void {..}
};
C.&lt;my.Address&gt;foo("street.number");</code></pre>
</div>
</div>
<div class="paragraph">
<p>Note that in many cases, the type inferencer should be able to infer the type automatically.
For example, for a method</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">function &lt;T&gt; bar(c: T, p: pathSelector&lt;T&gt;): void {..};</code></pre>
</div>
</div>
<div class="paragraph">
<p>and a function call</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">bar(context, "some.path.selector");
[source,n4js]</code></pre>
</div>
</div>
<div class="paragraph">
<p>the type variable <code>T</code> can be automatically bound to the type of variable <code>context</code>.</p>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_postfix-expression"><a class="anchor" href="#_postfix-expression"></a><a class="link" href="#_postfix-expression">8.1.11. Postfix Expression</a></h4>
<h5 id="postfix-expression-syntax" class="discrete">Syntax</h5>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">PostfixExpression returns Expression: LeftHandSideExpression
(=&gt;({PostfixExpression.expression=current} /* no line terminator here */ op=PostfixOperator))?
;
enum PostfixOperator: inc='++' | dec='--';</code></pre>
</div>
</div>
<h5 id="semantics-and-type-inference" class="discrete">Semantics and Type Inference</h5>
<div class="paragraph">
<p>The type inference and constraints for postfix operators <code>++</code> and <code>--</code>, cf. [<a href="#ECMA11a">ECMA11a(p.S11.3.1, p.p.70)</a>], [<a href="#ECMA11a">ECMA11a(p.S11.3.1, p.p.70)</a>],
are defined similarly to their prefix variants (unary expressions), see <a href="#_unary-expression">Unary Expression</a>.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-103"></a><strong>Req. IDE-103:</strong> <a href="#Req-IDE-103">Postfix Expression Constraints</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>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.</p>
</li>
<li>
<p>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> <em>p</em> and <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>e</mi><mi>t</mi></math> <em>p</em> must be defined.</p>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_unary-expression"><a class="anchor" href="#_unary-expression"></a><a class="link" href="#_unary-expression">8.1.12. Unary Expression</a></h4>
<h5 id="unary-expression-syntax" class="discrete">Syntax</h5>
<div class="paragraph">
<p>We define the following unary operators and expression, similar to [<a href="#ECMA11a">ECMA11a(p.p.70ff)</a>]:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">UnaryExpression returns Expression:
PostfixExpression
| ({UnaryExpression} op=UnaryOperator expression=UnaryExpression);
enum UnaryOperator: delete | void | typeof | inc='++' | dec='--' | pos='+' | neg='-' | inv='$\sim$' | not='!';</code></pre>
</div>
</div>
<h5 id="unary-expression-semantics" class="discrete">Semantics</h5>
<div class="paragraph">
<p>For semantics of the delete operator, see also [<a href="#MozillaJSRef">MozillaJSRef</a>]</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-104"></a><strong>Req. IDE-104:</strong> <a href="#Req-IDE-104">Delete Operator Constraints</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>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).</p>
</li>
<li>
<p>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.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-105"></a><strong>Req. IDE-105:</strong> <a href="#Req-IDE-105">Void Operator Constraints</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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></p>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-106"></a><strong>Req. IDE-106:</strong> <a href="#Req-IDE-106">Typeof Operator Constraints</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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>.</p>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-107"></a><strong>Req. IDE-107:</strong> <a href="#Req-IDE-107">Increment/Decrement Constraints</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>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</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>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> <em>p</em> and <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>e</mi><mi>t</mi></math> <em>p</em> must be defined.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-108"></a><strong>Req. IDE-108:</strong> <a href="#Req-IDE-108">Unary Plus/Minus/Bitwise Not Operator Constraints</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>In N4JS mode, the type T of the expression must be a number:</p>
</li>
</ul>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-109"></a><strong>Req. IDE-109:</strong> <a href="#Req-IDE-109">Logical Not Operator Constraints</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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>.</p>
</div>
</div>
</div>
<h5 id="type-inference-8" class="discrete">Type Inference</h5>
<div class="paragraph">
<p>The following operators have fixed types independent of their operand types:</p>
</div>
<div class="paragraph">
<p><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><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><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><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><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></p>
</div>
</div>
<div class="sect3">
<h4 id="_multiplicative-expression"><a class="anchor" href="#_multiplicative-expression"></a><a class="link" href="#_multiplicative-expression">8.1.13. Multiplicative Expression</a></h4>
<h5 id="multiplicative-expression-syntax" class="discrete">Syntax</h5>
<div class="paragraph">
<p>Cf. [<a href="#ECMA11a">ECMA11a(p.p.73ff)</a>]</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">MultiplicativeExpression returns Expression: UnaryExpression
(=&gt;({MultiplicativeExpression.lhs=current} op=MultiplicativeOperator) rhs=UnaryExpression)*;
enum MultiplicativeOperator: times='*' | div='/' | mod='%';</code></pre>
</div>
</div>
<h5 id="multiplicative-expression-semantics" class="discrete">Semantics</h5>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-110"></a><strong>Req. IDE-110:</strong> <a href="#Req-IDE-110">Multiplicative Expression Constraints</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a given multiplicative expression the following constraints must hold in N4JS mode :</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The types of the operands may be any type:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="paragraph">
<p>If a non-numeric operand is used, the result may be <code>NaN</code> which actually is a number as well.</p>
</div>
<h5 id="_type-inference-6" class="discrete">Type Inference</h5>
<div class="literalblock">
<div class="content">
<pre>[[type-inference-9]]</pre>
</div>
</div>
<div class="paragraph">
<p>The inferred type of a multiplicative expression always is number:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_additive-expression"><a class="anchor" href="#_additive-expression"></a><a class="link" href="#_additive-expression">8.1.14. Additive Expression</a></h4>
<h5 id="additive-expression-syntax" class="discrete">Syntax</h5>
<div class="paragraph">
<p>Cf. [<a href="#ECMA11a">ECMA11a(p.p.75ff)</a>]</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">AdditiveExpression returns Expression: MultiplicativeExpression
(=&gt;({AdditiveExpression.lhs=current} op=AdditiveOperator) rhs=MultiplicativeExpression)*;
enum AdditiveOperator: add='+' | sub='-';</code></pre>
</div>
</div>
<h5 id="additive-expression-semantics" class="discrete">Semantics</h5>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-111"></a><strong>Req. IDE-111:</strong> <a href="#Req-IDE-111">Additive Expression Constraints</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a given additive expression the following constraints must hold in N4JS mode:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The type of the operand can be any type:</p>
</li>
</ul>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>In JavaScript it is possible to subtract two non-numerics, leading to <code>NaN</code>. Also <code>undefined</code> or <code>null</code> may be used. The real difference is what type is to be returned (string or number, see below).</p>
</div>
<div class="sect4">
<h5 id="type-inference-10"><a class="anchor" href="#type-inference-10"></a><a class="link" href="#type-inference-10">8.1.14.1. Type Inference</a></h5>
<div class="paragraph language-n4js">
<p>The type of an additive expression is usually inferred to <code>number</code>, 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 <code>undefined</code> in an additive expression leads to <code>NaN</code> which actually is a number from the type system&#8217;s point of view. Additional analysis may create errors in the latter case though.</p>
</div>
<div class="paragraph">
<p>We first define two helper rules to simplify the addition operator condition:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>The type of an additive expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi></math> is inferred as follows:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>Adding two integers (int) leads to a number, since the result may not be represented as an (JavaScript) int anymore.</p>
</div>
<div class="exampleblock">
<div class="title">Example 88. Type of addition expression</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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"</code></pre>
</div>
</div>
</div>
</div>
<div class="openblock todo">
<div class="content">
<div class="paragraph">
<p>Support new <code>Symbol.toPrimitive</code>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_bitwise-shift-expression"><a class="anchor" href="#_bitwise-shift-expression"></a><a class="link" href="#_bitwise-shift-expression">8.1.15. Bitwise Shift Expression</a></h4>
<h5 id="bitwise-shift-expression-syntax" class="discrete">Syntax</h5>
<div class="literalblock">
<div class="content">
<pre>Cf. +[+&lt;&lt;ECMA11a,ECMA11a(p.p.76f)&gt;&gt;+]+</pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">ShiftExpression returns Expression: AdditiveExpression
(=&gt;({ShiftExpression.lhs=current} op=ShiftOperator rhs=AdditiveExpression))*
;
ShiftOperator returns ShiftOperator:
'&gt;' '&gt;' '&gt;'? // SHR, USHR
| '&lt;' '&lt;' // SHL
;</code></pre>
</div>
</div>
<h5 id="bitwise-shift-expression-semantics" class="discrete">Semantics</h5>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-112"></a><strong>Req. IDE-112:</strong> <a href="#Req-IDE-112">Bitwise Shift Expression Constraints</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</div>
</div>
<h5 id="type-inference-11" class="discrete">Type Inference</h5>
<div class="paragraph">
<p>The type returned by a bitwise shift expression is always <code>number</code>:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>A non-numeric operand is interpreted as 0, except for <code>true</code> which is interpreted as <code>1</code>; or objects implementing the symbol <code>toPrimitive</code>.</p>
</div>
</div>
<div class="sect3">
<h4 id="_relational-expression"><a class="anchor" href="#_relational-expression"></a><a class="link" href="#_relational-expression">8.1.16. Relational Expression</a></h4>
<h5 id="relational-expression-syntax" class="discrete">Syntax</h5>
<div class="paragraph">
<p>Cf. [<a href="#ECMA11a">ECMA11a(p.p.77ff)</a>]</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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';</code></pre>
</div>
</div>
<h5 id="relational-expression-semantics" class="discrete">Semantics</h5>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-113"></a><strong>Req. IDE-113:</strong> <a href="#Req-IDE-113">Greater/Less (Equals) Operator Constraints</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The operands must have the same type and the type must be either a number, string, or boolean:</p>
<div class="paragraph">
<p><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><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><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><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></p>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-114"></a><strong>Req. IDE-114:</strong> <a href="#Req-IDE-114">Instanceof Operator Constraints</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The right operand of the instanceof operator must be a <code>Function</code> <sup class="footnote">[<a id="_footnoteref_49" class="footnote" href="#_footnote_49" title="View footnote.">49</a>]</sup></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>In other words,</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>is contained in the the first type rule, an object type reference <sup class="footnote">[<a id="_footnoteref_50" class="footnote" href="#_footnote_50" title="View footnote.">50</a>]</sup>
or an enum type reference.</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>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 <code>C</code>.
Thus, the <code>instanceof</code> operator cannot be used for structural types.
Use-site structural typing is also not possible since <code>~</code> would be interpreted (by the parser) as a binary operator.</p>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-115"></a><strong>Req. IDE-115:</strong> <a href="#Req-IDE-115">Operator Constraints</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The right operand of the in operator must be an <code>Object</code>:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>In N4JS mode, the left operand is restricted to be of type <code>string</code> or <code>number</code>:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>A special feature of N4JS is support for interface type references in combination with the <code>instance of</code> operator.
The compiler rewrites the code to make this work.</p>
</div>
<div class="exampleblock">
<div class="title">Example 89. <code>instanceof</code> with Interface</div>
<div class="content">
<div class="paragraph">
<p>The following example demonstrates the use of the operator with an interface.
This is, of course, not working in pure ECMAScript.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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())</code></pre>
</div>
</div>
<div class="paragraph">
<p>This will print out</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">A is instance of I
B is instance of I</code></pre>
</div>
</div>
</div>
</div>
<h5 id="type-inference-12" class="discrete">Type Inference</h5>
<div class="paragraph">
<p>The type of a relational expression always is <code>boolean</code>;</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_equality-expression"><a class="anchor" href="#_equality-expression"></a><a class="link" href="#_equality-expression">8.1.17. Equality Expression</a></h4>
<h5 id="equality-expression-syntax" class="discrete">Syntax</h5>
<div class="paragraph">
<p>Cf. [<a href="#ECMA11a">ECMA11a(p.p.80ff)</a>]</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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='!=';</code></pre>
</div>
</div>
<h5 id="equality-expression-semantics" class="discrete">Semantics</h5>
<div class="paragraph">
<p>There are no hard constraints defined for equality expressions.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">// with
class A{} class B extends A{}
function isFirst(ar: Array&lt;? extends A&gt;, b: B): boolean {
return b === ar[0]
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>the type of array elements is <code>? extends A</code>.<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.</p>
</div>
<h5 id="type-inference-13" class="discrete">Type Inference</h5>
<div class="paragraph">
<p>The inferred type of an equality expression always is <code>boolean</code>.</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_binary-bitwise-expression"><a class="anchor" href="#_binary-bitwise-expression"></a><a class="link" href="#_binary-bitwise-expression">8.1.18. Binary Bitwise Expression</a></h4>
<h5 id="binary-bitwise-expression-syntax" class="discrete">Syntax</h5>
<div class="paragraph">
<p>Cf. [<a href="#ECMA11a">ECMA11a(p.p.82ff)</a>]</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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)*;</code></pre>
</div>
</div>
<h5 id="binary-bitwise-expression-semantics" class="discrete">Semantics</h5>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-116"></a><strong>Req. IDE-116:</strong> <a href="#Req-IDE-116">Bitwise Bitwise Expression Constraints</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The types of the operands must be both number.</p>
</li>
</ul>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</div>
</div>
<h5 id="type-inference-14" class="discrete">Type Inference</h5>
<div class="paragraph">
<p>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>:</p>
</div>
<div class="openblock">
<div class="content">
</div>
</div>
</div>
<div class="sect3">
<h4 id="_binary-logical-expression"><a class="anchor" href="#_binary-logical-expression"></a><a class="link" href="#_binary-logical-expression">8.1.19. Binary Logical Expression</a></h4>
<h5 id="binary-logical-expression-syntax" class="discrete">Syntax</h5>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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)*;</code></pre>
</div>
</div>
<h5 id="binary-logical-expression-semantics" class="discrete">Semantics</h5>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-117"></a><strong>Req. IDE-117:</strong> <a href="#Req-IDE-117">Binary Logical Expression Constraints</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>In N4JS mode <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>L</mi></math> must not be <code>undefined</code> or <code>null</code>.</p>
</li>
</ul>
</div>
</div>
</div>
<h5 id="type-inference-15" class="discrete">Type Inference</h5>
<div class="paragraph">
<p>The evaluation relies on ECMAScript’s abstract operation <code>ToBoolean</code> [<a href="#ECMA11a">ECMA11a(p.p.43)</a>].
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 <code>boolean</code> ((cf. [<a href="#ECMA11a">ECMA11a(p.S11.11., p.p.83ff)</a>] ).
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 <code>null</code>
either or <code>undefined</code>, which is not allowed in N4JS mode.</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_conditional-expression"><a class="anchor" href="#_conditional-expression"></a><a class="link" href="#_conditional-expression">8.1.20. Conditional Expression</a></h4>
<h5 id="conditional-expression-syntax" class="discrete">Syntax</h5>
<div class="paragraph">
<p>Cf. [<a href="#ECMA11a">ECMA11a(p.S11.12, p.p.84)</a>]</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">ConditionalExpression returns Expression: LogicalORExpression
(=&gt; ({ConditionalExpression.expression=current} '?') trueExpression=AssignmentExpression ':' falseExpression=AssignmentExpression)?;
ConditionalExpressionNoIn returns Expression: LogicalORExpressionNoIn
(=&gt; ({ConditionalExpression.expression=current} '?') trueExpression=AssignmentExpression ':' falseExpression=AssignmentExpressionNoIn)?;</code></pre>
</div>
</div>
<h5 id="conditional-expression-semantics" class="discrete">Semantics</h5>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-118"></a><strong>Req. IDE-118:</strong> <a href="#Req-IDE-118">Conditional Expression Constraints</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a given conditional expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi></math> with</p>
</div>
<div class="paragraph">
<p><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><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><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></p>
</div>
<div class="paragraph">
<p>the following constraints must hold:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>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<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>.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>There are no specific constraints defined for the condition.
The ECMAScript operation <code>ToBoolean</code> [<a href="#ECMA11a">ECMA11a(p.S9.2, p.p.43)</a>] is used to convert any type to boolean.</p>
</div>
</div>
</div>
<h5 id="type-inference-16" class="discrete">Type Inference</h5>
<div class="paragraph">
<p>The inferred type of a conditional expression is the union of the true and false expression (cf. [<a href="#ECMA11a">ECMA11a(p.S11.12, p.p.84)</a>] ():</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 90. Type of Conditional Expressions</div>
<div class="content">
<div class="paragraph">
<p>Given the following declarations:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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;</code></pre>
</div>
</div>
<div class="paragraph">
<p>Then the type of the following conditional expression is inferred as noted in the comments:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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;}</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_assignment-expression"><a class="anchor" href="#_assignment-expression"></a><a class="link" href="#_assignment-expression">8.1.21. Assignment Expression</a></h4>
<h5 id="assignment-expression-syntax" class="discrete">Syntax</h5>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">AssignmentExpression &lt;In, Yield&gt;:
lhs=Expression op=AssignmentOperator rhs=AssignmentExpression&lt;In,Yield&gt;
;
AssignmentOperator:
'='
| '*=' | '/=' | '%=' | '+=' | '-='
| '&lt;&lt;=' | '&gt;&gt;=' | '&gt;&gt;&gt;='
| '&amp;=' | '^=' | '|='
;</code></pre>
</div>
</div>
<h5 id="assignment-expression-semantics" class="discrete">Semantics</h5>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-119"></a><strong>Req. IDE-119:</strong> <a href="#Req-IDE-119">Simple Assignment</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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</p>
</div>
<div class="paragraph">
<p><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></p>
</div>
<div class="paragraph">
<p>the following constraints must hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><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></p>
<div class="paragraph">
<p>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,</p>
</div>
<div class="openblock">
<div class="content">
</div>
</div>
</li>
</ol>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-120"></a><strong>Req. IDE-120:</strong> <a href="#Req-IDE-120">Compound Assignment</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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 <code>+=</code>, both, left and right must be subtypes of <code>number</code>.<br>
For operator <code>+=</code>,</p>
</div>
<div class="ulist">
<ul>
<li>
<p>if the left-hand side is a <code>number</code>, 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 <code>number</code> (and not a <code>boolean</code>) here in order to avoid unexpected results.</p>
</li>
<li>
<p>if the left-hand side is a <code>string</code>, 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 <code>any</code> type.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The expected type for the left-hand side is <code>union{number,string}</code>.</p>
</div>
<div class="paragraph">
<p>The basic idea behind these constraints is that the type of the left-hand side is not to be changed by the compound assignment.</p>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-121"></a><strong>Req. IDE-121:</strong> <a href="#Req-IDE-121">Write Acccess</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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></p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>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 <code>const</code> or the <code>@Final</code> annotation.
See <a href="#_assignment-modifiers">Assignment Modifiers</a>(data fields) and <a href="#_const">Const</a> (const variables) for details.</p>
</div>
<div class="paragraph">
<p>Also see <a href="#Req-IDE-93">[Req-IDE-93]</a> for read access constraint.</p>
</div>
<div class="paragraph">
<p>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 <a href="#_array-and-object-destructuring">Array and Object Destructuring</a> for details.</p>
</div>
</div>
</div>
<h5 id="type-inference-17" class="discrete">Type Inference</h5>
<div class="paragraph">
<p>Similarly to [<a href="#ECMA11a">ECMA11a(p.S11.1, p.p.84ff)</a>], we define type inference for simple assignment (<code>=</code>) and compound assignment (<code>op=</code>) individually.</p>
</div>
<div class="paragraph">
<p>The type of the assignment is simply the type of the right-hand side:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>Compound assignments are reduced to the former by splitting an operator <code>@=</code>, in which <code>@</code> is a simple operator,
into a simple operator expression with operator <code>@</code> and a simple assignment <code>=</code>.
Since the type of the latter is the right-hand side, we can define:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_comma-expression"><a class="anchor" href="#_comma-expression"></a><a class="link" href="#_comma-expression">8.1.22. Comma Expression</a></h4>
<h5 id="comma-expression-syntax" class="discrete">Syntax</h5>
<div class="paragraph">
<p>Cf. [<a href="#ECMA11a">ECMA11a(p.S11.14, p.p.85)</a>]</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">CommaExpression &lt;In, Yield&gt;:
exprs+=AssignmentExpression&lt;In,Yield&gt; ',' exprs+=AssignmentExpression&lt;In,Yield&gt;
(',' exprs+=AssignmentExpression&lt;In,Yield&gt;)*
;</code></pre>
</div>
</div>
<h5 id="comma-expression-semantics" class="discrete">Semantics</h5>
<div class="paragraph">
<p>All expressions will be evaluated even though only the value of the last expression will be the result.</p>
</div>
<div class="exampleblock">
<div class="title">Example 91. Comma Expression</div>
<div class="content">
<div class="paragraph">
<p>Assignment expressions preceed comma expressions:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var b: boolean;
b = (12, 34, true); // ok, b=true
b = 12, 34, true ; // error, b=12 is invalid</code></pre>
</div>
</div>
</div>
</div>
<h5 id="type-inference-18" class="discrete">Type Inference</h5>
<div class="paragraph">
<p>Cf. [<a href="#ECMA11a">ECMA11a(p.S11.14, p.p.85)</a>]</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_ecmascript-6-expressions"><a class="anchor" href="#_ecmascript-6-expressions"></a><a class="link" href="#_ecmascript-6-expressions">8.2. ECMAScript 6 Expressions</a></h3>
<div class="sect3">
<h4 id="_the-super-keyword"><a class="anchor" href="#_the-super-keyword"></a><a class="link" href="#_the-super-keyword">8.2.1. The super Keyword</a></h4>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">SuperLiteral: {SuperLiteral} 'super';</code></pre>
</div>
</div>
<div class="paragraph">
<p>Apart from the use of keyword <code>super</code> in wildcards of type expressions (cf. <a href="#_type-expressions">Type Expressions</a>),
there are two use cases for keyword <code>super</code>: super member access and super constructor calls.</p>
</div>
<div class="exampleblock">
<div class="title">Example 92. Super Keyword</div>
<div class="content">
<div class="paragraph">
<p>Two use cases for keyword super:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class B extends A {
constructor() {
// super call
super();
}
@Override
m();: void {
// super member access
super.m();
}
}</code></pre>
</div>
</div>
</div>
</div>
<h5 id="super-keyword-semantics" class="discrete">Semantics</h5>
<div class="paragraph">
<p><code>super</code> can be used to access the supertype’s constructor, methods, getters and setters.
The supertype is defined lexically, which is different from how <code>this</code> works.<sup class="footnote">[<a id="_footnoteref_51" class="footnote" href="#_footnote_51" title="View footnote.">51</a>]</sup></p>
</div>
<div class="paragraph">
<p>Note that in [<a href="#ECMA15a">ECMA15a</a>] Chapter 12.3.5 <code>The Super Keyword</code>, <code>super</code> is defined as a keyword but the syntax and semantics are defined in conjunction of member access.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-122"></a><strong>Req. IDE-122:</strong> <a href="#Req-IDE-122">Type of Super is Always Nominal</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>The type referenced with the super literal is always nominal.
This is a consequence of references to types in extend clauses to be nominal.</p>
</div>
<div class="paragraph">
<p><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></p>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-123"></a><strong>Req. IDE-123:</strong> <a href="#Req-IDE-123">Access Super Constructor with Super Literal</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The super constructor access must be a call expression:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>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>:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>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:</p>
<div class="paragraph">
<p><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></p>
</div>
</li>
<li>
<p>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>.</p>
<div class="paragraph">
<p>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:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>Then, the following constraint must hold: <sup class="footnote">[<a id="_footnoteref_52" class="footnote" href="#_footnote_52" title="View footnote.">52</a>]</sup></p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
</ol>
</div>
<div class="paragraph">
<p>Further constraints with regard to super constructor calls are described in <a href="#_constructor-and-classifier-type">Constructor and Classifier Type</a>.</p>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-124"></a><strong>Req. IDE-124:</strong> <a href="#Req-IDE-124">Access Super Member with Super Literal</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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></p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The super literal must be the receiver of a method call (cf. remarks below):</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>The super literal is used in a method or field accessor of a class:</p>
</li>
</ol>
</div>
<div class="paragraph">
<p><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:</p>
</div>
<div class="paragraph">
<p>+
<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).</p>
</div>
<div class="paragraph">
<p>+</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-125"></a><strong>Req. IDE-125:</strong> <a href="#Req-IDE-125">Super Literal Usage</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For super literals, either <a href="#Req-IDE-123">[Req-IDE-123]</a> or <a href="#Req-IDE-124">[Req-IDE-124]</a> must hold, no other usage
is allowed.</p>
</div>
<div class="paragraph">
<p>Consequences:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Since fields cannot be overridden (except for changing the access modifier), it is not possible nor allowed to access a field via <code>super</code>.</p>
</li>
<li>
<p>Super literals must not be used with index access (e.g., <code>super["foo"]</code>)</p>
</li>
<li>
<p>It is not possible to chain super keywords. That is, it is not possible to call <code>super.super.m()</code>.</p>
</li>
<li>
<p>It is not allowed to use the super literal in interfaces or non-methods/accessors.</p>
</li>
<li>
<p>Super cannot be used to call an overridden method of an implemented method from the overriding method in the implementing class.</p>
</li>
<li>
<p>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.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="exampleblock">
<div class="title">Example 93. Super Call in Static Methods</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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(); <i class="conum" data-value="1"></i><b>(1)</b>
super.bar(); <i class="conum" data-value="2"></i><b>(2)</b>
}
}
B.bar();</code></pre>
</div>
</div>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>The receiver (which is similar to the this-binding in ECMAScript) is changed to <code>A</code>.</td>
</tr>
<tr>
<td><i class="conum" data-value="2"></i><b>2</b></td>
<td>Using super, the receiver is preserved, i.e. <code>B</code>.</td>
</tr>
</table>
</div>
</div>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_ecmascript-7-expressions"><a class="anchor" href="#_ecmascript-7-expressions"></a><a class="link" href="#_ecmascript-7-expressions">8.3. ECMAScript 7 Expressions</a></h3>
<div class="sect3">
<h4 id="_await-expression"><a class="anchor" href="#_await-expression"></a><a class="link" href="#_await-expression">8.3.1. Await Expression</a></h4>
<div class="paragraph">
<p>In N4JS, <code>await</code> is implemented as a unary operator with the same precedence as <code>yield</code> in ECMAScript 6.</p>
</div>
<div class="paragraph">
<p>Constraints governing the use of <code>await</code> are given together with those for <code>async</code> in <a href="#_asynchronous-functions">Asynchronous Functions</a>.</p>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_n4js-specific-expressions"><a class="anchor" href="#_n4js-specific-expressions"></a><a class="link" href="#_n4js-specific-expressions">8.4. N4JS Specific Expressions</a></h3>
<div class="sect3">
<h4 id="_class-expression"><a class="anchor" href="#_class-expression"></a><a class="link" href="#_class-expression">8.4.1. Class Expression</a></h4>
<div class="paragraph">
<p>A class expression in N4JS is similar to a class expression in ECMAScript 6 [<a href="#ECMA15a">ECMA15a(p.14.5)</a>].</p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
Class expressions are not part of version 0.3
</td>
</tr>
</table>
</div>
<h5 id="class-expression-syntax" class="discrete">Syntax</h5>
<div class="paragraph">
<p>See <a href="#_classes">Classes</a>.</p>
</div>
<h5 id="class-expression-semantics-type-inference" class="discrete">Semantics and Type Inference</h5>
<div class="paragraph">
<p>The inferred type of a class expression simply is the class type as described in <a href="#_constructor-and-classifier-type">Constructor and Classifier Type</a>.</p>
</div>
</div>
<div class="sect3">
<h4 id="_cast-as-expression"><a class="anchor" href="#_cast-as-expression"></a><a class="link" href="#_cast-as-expression">8.4.2. Cast (As) Expression</a></h4>
<h5 id="cast-as-expression-syntax" class="discrete">Syntax</h5>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">CastExpression &lt;Yield&gt; returns Expression: expression=Expression 'as' targetTypeRef=TypeRefForCast;
TypeRefForCast returns StaticBaseTypeRef:
ParameterizedTypeRef
| ThisTypeRef
| ConstructorTypeRef
| ClassifierTypeRef
| FunctionTypeExpression
| UnionTypeExpression
| IntersectionTypeExpression</code></pre>
</div>
</div>
<div class="sect4">
<h5 id="cast-as-expression-semantics-type-inference"><a class="anchor" href="#cast-as-expression-semantics-type-inference"></a><a class="link" href="#cast-as-expression-semantics-type-inference">8.4.2.1. Semantics and Type Inference</a></h5>
<div class="paragraph">
<p>The inferred type of the type cast expression is the target type:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>An error is issued if the cast is either unnecessary or cannot succeed.
See further details in <a href="#_type-cast">Type Cast</a>.</p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="Import_Calls"><a class="anchor" href="#Import_Calls"></a><a class="link" href="#Import_Calls">8.4.3. Import Calls</a></h4>
<div class="paragraph">
<p>Import calls as specified by the corresponding <a href="https://github.com/tc39/proposal-dynamic-import">ECMA TC39 proposal</a> are
available in N4JS. Such an import call has the form</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">import(moduleSpecifier)</code></pre>
</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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 <code>import * as N+ from "&#8230;&#8203;"</code>, i.e. compile-time imports that do not
require type information of the module imported from, see <a href="#Dynamic_Imports">Dynamic Imports</a>, and stems from the term "dynamic type"
(see <a href="#Type_Modifiers_Dynamic">Dynamic</a>). To avoid confusion, we will usually avoid referring to import calls as a "dynamic
import".</p>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="compile-time-expressions"><a class="anchor" href="#compile-time-expressions"></a><a class="link" href="#compile-time-expressions">8.5. Compile-Time Expressions</a></h3>
<div class="paragraph">
<p>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: <code>5 / 0</code> is a valid ECMAScript expression (evaluating
to <code>NaN</code>) 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.</p>
</div>
<div class="paragraph">
<p>The value a compile-time expression evaluates to at compile-time is called <em>compile-time value</em>. So, an expression has a compile-time
value if and only if it is a compile-time expression.</p>
</div>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="compile_time_expression"></a><strong>Definition:</strong> <a href="#compile_time_expression">Compile-Time Expression</a></p>
</div>
<div class="paragraph">
<p>The following expressions are called compile-time expressions:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>undefined</code> (but not <code>NaN</code> or <code>Infinity</code>).</p>
</li>
<li>
<p>the <code>null</code> literal.</p>
</li>
<li>
<p>all boolean, numeric, and string literals.</p>
</li>
<li>
<p>template string literals, iff all embedded expressions are compile-time expressions.</p>
</li>
<li>
<p>a parenthesis expression, iff its nested expression is a compile-time expression.</p>
</li>
<li>
<p>unary expressions in case of the following operators:</p>
<div class="ulist">
<ul>
<li>
<p><code>!</code> iff the operand is a compile-time expression and evaluates to a boolean value.</p>
</li>
<li>
<p><code>+</code> iff the operand is a compile-time expression and evaluates to a numeric value.</p>
</li>
<li>
<p><code>-</code> iff the operand is a compile-time expression and evaluates to a numeric value.</p>
</li>
<li>
<p><code>void</code>.</p>
</li>
</ul>
</div>
</li>
<li>
<p>binary expressions in case of the following operators:</p>
<div class="ulist">
<ul>
<li>
<p><code>+</code> iff both operands are compile-time expressions and</p>
<div class="ulist">
<ul>
<li>
<p>both evaluate to numeric values, or</p>
</li>
<li>
<p>at least one evaluates to a string value.</p>
</li>
</ul>
</div>
</li>
<li>
<p><code>-</code>, <code>*</code> iff both operands are compile-time expressions and evaluate to numeric values.</p>
</li>
<li>
<p><code>/</code>, <code>%</code> 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 <code>NaN</code> is not a supported compile-time value).</p>
</li>
<li>
<p><code>&amp;&amp;</code>, <code>||</code> iff both operands are compile-time expressions and evaluate to boolean values.</p>
</li>
</ul>
</div>
</li>
<li>
<p>a tertiary conditional expression, iff the first operand is a compile-time expression evaluating to a boolean value B and</p>
<div class="ulist">
<ul>
<li>
<p>in case B is true, the second operand is a compile-time expression.</p>
</li>
<li>
<p>in case B is false, the third operand is a compile-time expression.</p>
</li>
</ul>
</div>
</li>
<li>
<p>an identifier reference to a const variable, iff its initializer expression is a compile-time expression.</p>
</li>
<li>
<p>a property access expression, iff it is direct (see <a href="#property-access-direct">Direct Property Access</a>) and refers to</p>
<div class="ulist">
<ul>
<li>
<p>a built-in symbol, e.g. <code>Symbol.iterator</code>,</p>
</li>
<li>
<p>a literal of a <code>@StringBased</code> enum, or</p>
</li>
<li>
<p>a const field with a compile-time initializer expression.</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>In all other cases, the expression is not a compile-time expression.</p>
</div>
</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_statements"><a class="anchor" href="#_statements"></a><a class="link" href="#_statements">9. Statements</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>For all statements, we define the following pseudo properties:</p>
</div>
<div class="openblock language-n4js">
<div class="content">
<div class="dlist">
<dl>
<dt class="hdlist1"><code>containingFunction</code> </dt>
<dd>
<p>The function or method in which the statement is (indirectly) contained, this may be null.</p>
</dd>
<dt class="hdlist1"><code>containingClass</code> </dt>
<dd>
<p>The class in which the statement is (indirectly) contained, this may be null.</p>
</dd>
</dl>
</div>
</div>
</div>
<div class="paragraph">
<p>The expressions and statements are ordered, at first describing the constructs available in the 5th edition of ECMA-262
referred to as [<a href="#ECMA11a">ECMA11a</a>] in the following.
The grammar snippets already use newer constructs in some cases.</p>
</div>
<div class="sect2 language-n4js">
<h3 id="_ecmascript-5-statements"><a class="anchor" href="#_ecmascript-5-statements"></a><a class="link" href="#_ecmascript-5-statements">9.1. ECMAScript 5 Statements</a></h3>
<div class="paragraph">
<p>N4JS supports the same statements as
ECMAScript. Some of these statements are enhanced with annotations <a href="#_annotations">Annotations</a> and type information.</p>
</div>
<div class="paragraph">
<p>Although some statements may return a value which can be used via certain constructs such as <code>eval</code>), no type is inferred for any statement.
The compiler will always create a warning if a statement is used instead of an expression.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>All syntax definitions taken from [<a href="#ECMA11a">ECMA11a</a>] 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 [<a href="#ECMA11a">ECMA11a</a>] is to be used.</p>
</div>
<div class="sect3">
<h4 id="_function-or-field-accessor-bodies"><a class="anchor" href="#_function-or-field-accessor-bodies"></a><a class="link" href="#_function-or-field-accessor-bodies">9.1.1. Function or Field Accessor Bodies</a></h4>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-126"></a><strong>Req. IDE-126:</strong> <a href="#Req-IDE-126">Dead Code</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For all statements in a function or field accessor (getter/setter) body, the following constraints must hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>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.</p>
</li>
</ol>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_variable-statement"><a class="anchor" href="#_variable-statement"></a><a class="link" href="#_variable-statement">9.1.2. Variable Statement</a></h4>
<h5 id="variable-statement-syntax" class="discrete">Syntax</h5>
<div class="paragraph">
<p>A var statement can declare the type of the variable with a type
reference. This is described with the following grammar similar to
[<a href="#ECMA11a">ECMA11a(p.S12.2, p.p.87)</a>]:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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;)?
)
;</code></pre>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 94. Variable Statement</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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</code></pre>
</div>
</div>
</div>
</div>
<h5 id="variable-statement-semantics" class="discrete">Semantics</h5>
<div class="paragraph">
<p>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 (<a href="#_export-statement">Export Statement</a>) and constant statement and declaration (<a href="#_const">Const</a>).</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-127"></a><strong>Req. IDE-127:</strong> <a href="#Req-IDE-127">Variable declaration</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a given variable declaration <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>d</mi></math>, the following constraints must hold:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The type of the initializer expression must conform to the declared type:</p>
<div class="paragraph">
<p><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><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></p>
</div>
</li>
<li>
<p>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.<br>
To clarify: <em>should not</em> means that only a warning will be produced.</p>
</li>
</ul>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">// 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; }();</code></pre>
</div>
</div>
<div class="paragraph">
<p>The variable statement may contain array or object destructuring patterns, see <a href="#_array-and-object-destructuring">Array and Object Destructuring</a> for details.</p>
</div>
</div>
</div>
<h5 id="variable-statement-type-inference" class="discrete">Type Inference</h5>
<div class="paragraph">
<p>The type of a variable is the type of its declaration:</p>
</div>
<div class="openblock">
<div class="content">
</div>
</div>
<div class="paragraph">
<p>The type of a variable declaration is either the declared type or the inferred type of the initializer expression:</p>
</div>
<div class="openblock">
<div class="content">
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_if-statement"><a class="anchor" href="#_if-statement"></a><a class="link" href="#_if-statement">9.1.3. If Statement</a></h4>
<h5 id="if-statement-syntax" class="discrete">Syntax</h5>
<div class="paragraph">
<p>Cf. [<a href="#ECMA11a">ECMA11a(p.S12.5, p.p.89)</a>]</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">IfStatement &lt;Yield&gt;:
'if' '(' expression=Expression&lt;In=true,Yield&gt; ')'
ifStmt=Statement&lt;Yield&gt;
(=&gt; 'else' elseStmt=Statement&lt;Yield&gt;)?;</code></pre>
</div>
</div>
<h5 id="if-statement-semantics" class="discrete">Semantics</h5>
<div class="paragraph">
<p>There are no specific constraints defined for the condition, the ECMAScript operation <code>ToBoolean</code> [<a href="#ECMA11a">ECMA11a(p.S9.2, p.p.43)</a>] is used to convert any type to boolean.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-128"></a><strong>Req. IDE-128:</strong> <a href="#Req-IDE-128">If Statement</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>In N4JS, the expression of an if statement must not evaluate to <code>void</code>.
If the expressions is a function call in particular, the called function must not be declared to return <code>void</code>.</p>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_iteration-statements"><a class="anchor" href="#_iteration-statements"></a><a class="link" href="#_iteration-statements">9.1.4. Iteration Statements</a></h4>
<h5 id="iterations-statements-syntax" class="discrete">Syntax</h5>
<div class="paragraph">
<p>Cf. [<a href="#ECMA11a">ECMA11a(p.S12.6, p.p.90ff)</a>]</p>
</div>
<div class="paragraph">
<p>The syntax already considers the for-of style described in <a href="#_for-of-statement"><code>for &#8230;&#8203; of</code> statement</a>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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;</code></pre>
</div>
</div>
<div class="paragraph">
<p>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 <code>VariableStatement</code>s as described in <a href="#_variable-statement">Variable Statement</a>, the declared variables can be type annotated.</p>
</div>
<div class="admonitionblock tip">
<table>
<tr>
<td class="icon">
<i class="fa icon-tip" title=""></i>
</td>
<td class="content">
Using for-in is not recommended, instead <code>_each</code> should be used.
</td>
</tr>
</table>
</div>
<h5 id="iterations-statements-semantics" class="discrete">Semantics</h5>
<div class="paragraph">
<p>There are no specific constraints defined for the condition, the
ECMAScript operation <code>ToBoolean</code> [<a href="#ECMA11a">ECMA11a(p.S9.2, p.p.43)</a>] is used to convert any type to boolean.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-129"></a><strong>Req. IDE-129:</strong> <a href="#Req-IDE-129">For-In-Statement Constraints</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a given <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi></math> the following conditions must hold:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The type of the expression must be conform to object:<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></p>
</li>
<li>
<p>Either a new loop variable must be declared or an rvalue must be provided as init expression:<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></p>
</li>
<li>
<p>The type of the loop variable must be a string (or a super type of string, i.e. any):</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_return-statement"><a class="anchor" href="#_return-statement"></a><a class="link" href="#_return-statement">9.1.5. Return Statement</a></h4>
<h5 id="return-statement-syntax" class="discrete">Syntax</h5>
<div class="paragraph">
<p>The returns statement is defined as in [<a href="#ECMA11a">ECMA11a(p.S12.9, p.p.93)</a>] with</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">ReturnStatement &lt;Yield&gt;:
'return' (expression=Expression&lt;In=true,Yield&gt;)? Semi;</code></pre>
</div>
</div>
<h5 id="return-statement-semantics" class="discrete">Semantics</h5>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-130"></a><strong>Req. IDE-130:</strong> <a href="#Req-IDE-130">Return statement</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Expected type of expression in a return statement must be a sub type of the return type of the enclosing function:</p>
<div class="openblock">
<div class="content">
</div>
</div>
<div class="paragraph">
<p>Note that the expression may be evaluated to <code>void</code>.</p>
</div>
</li>
<li>
<p>If enclosing function is declared to return <code>void</code>, then either</p>
<div class="ulist">
<ul>
<li>
<p>no return statement must be defined</p>
</li>
<li>
<p>return statement has no expression</p>
</li>
<li>
<p>type of expression of return statement is <code>void</code></p>
</li>
</ul>
</div>
</li>
<li>
<p>If enclosing function is declared to to return a type different from <code>void</code>, then</p>
<div class="ulist">
<ul>
<li>
<p>all return statements must have a return expression</p>
</li>
<li>
<p>all control flows must either end with a return or throw statement</p>
</li>
</ul>
</div>
</li>
<li>
<p>Returns statements must be enclosed in a function.
A return statement, for example, must not be a top-level statement.</p>
</li>
</ol>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_with-statement"><a class="anchor" href="#_with-statement"></a><a class="link" href="#_with-statement">9.1.6. With Statement</a></h4>
<h5 id="with-statement-syntax" class="discrete">Syntax</h5>
<div class="paragraph">
<p>The with statement is not allowed in N4JS, thus an error is issued.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">WithStatement &lt;Yield&gt;:
'with' '(' expression=Expression&lt;In=true,Yield&gt; ')'
statement=Statement&lt;Yield&gt;;</code></pre>
</div>
</div>
<h5 id="with-statement-semantics" class="discrete">Semantics</h5>
<div class="paragraph">
<p>N4JS is based on strict mode and the with statement is not allowed in
strict mode, cf. [<a href="#ECMA11a">ECMA11a(p.S12.10.1, p.p.94)</a>].</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-131"></a><strong>Req. IDE-131:</strong> <a href="#Req-IDE-131">With Statement</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>With statements are not allowed in N4JS or strict mode.</p>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_switch-statement"><a class="anchor" href="#_switch-statement"></a><a class="link" href="#_switch-statement">9.1.7. Switch Statement</a></h4>
<h5 id="switch-statement-syntax" class="discrete">Syntax</h5>
<div class="paragraph">
<p>Cf. [<a href="#ECMA11a">ECMA11a(p.S12.11, p.p.94ff)</a>]</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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;)*;</code></pre>
</div>
</div>
<h5 id="switch-statement-semantics" class="discrete">Semantics</h5>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-132"></a><strong>Req. IDE-132:</strong> <a href="#Req-IDE-132">Switch Constraints</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a given switch statement <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi></math>, the following constraints must hold:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>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 <a href="#_equality-expression">Equality Expression</a>.</p>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_throw-try-and-catch-statements"><a class="anchor" href="#_throw-try-and-catch-statements"></a><a class="link" href="#_throw-try-and-catch-statements">9.1.8. Throw, Try, and Catch Statements</a></h4>
<h5 id="throw-try-catch-syntax" class="discrete">Syntax</h5>
<div class="paragraph">
<p>Cf. [<a href="#ECMA11a">ECMA11a(p.S12.13/14, p.p.96ff)</a>]</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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;;</code></pre>
</div>
</div>
<div class="paragraph">
<p>There must be not type annotation for the catch variable, as this would lead to the wrong assumption that a type can be specified.</p>
</div>
<h5 id="throw-try-catch-type-inference" class="discrete">Type Inference</h5>
<div class="paragraph">
<p>The type of the catch variable is always assumed to be <code>any</code>.</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_debugger-statement"><a class="anchor" href="#_debugger-statement"></a><a class="link" href="#_debugger-statement">9.1.9. Debugger Statement</a></h4>
<h5 id="debugger-statement-syntax" class="discrete">Syntax</h5>
<div class="paragraph">
<p>Cf. [<a href="#ECMA11a">ECMA11a(p.S12.15, p.p.97ff)</a>])</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">DebuggerStatement: {DebuggerStatement} 'debugger' Semi;</code></pre>
</div>
</div>
<h5 id="debugger-statement--semantics" class="discrete">Semantics</h5>
<div class="paragraph">
<p>na</p>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_ecmascript-6-statements"><a class="anchor" href="#_ecmascript-6-statements"></a><a class="link" href="#_ecmascript-6-statements">9.2. ECMAScript 6 Statements</a></h3>
<div class="paragraph">
<p>N4JS export and import statements are similar to ES6 with some minor d ifferences which are elaborated on below.</p>
</div>
<div class="sect3">
<h4 id="_let"><a class="anchor" href="#_let"></a><a class="link" href="#_let">9.2.1. Let</a></h4>
<div class="paragraph">
<p>Cf. [<a href="#ECMA11a">ECMA11a(p.13.2.1)</a>], also <a href="http://www.2ality.com/2015/02/es6-scoping.html">Rauschmayer, 2ality: <strong>Variables and scoping in ECMAScript 6</strong></a></p>
</div>
</div>
<div class="sect3">
<h4 id="_const"><a class="anchor" href="#_const"></a><a class="link" href="#_const">9.2.2. Const</a></h4>
<div class="paragraph">
<p>Cf. [<a href="#ECMA15a">ECMA15a(p.13.2.1)</a>], also <a href="http://www.2ality.com/2015/02/es6-scoping.html">Rauschmayer, 2ality: <strong>Variables and scoping in ECMAScript 6</strong></a></p>
</div>
<div class="paragraph">
<p>Additionally to the <code>var</code> statement, the <code>const</code> 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.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">ConstStatement returns VariableStatement: 'const' varDecl+=ConstDeclaration ( ',' varDecl+=ConstDeclaration )* Semi;
ConstDeclaration returns VariableDeclaration: typeRef=TypeRef? name=IDENTIFIER const?='=' expression=AssignmentExpression;</code></pre>
</div>
</div>
<h5 id="const-semantics" class="discrete">Semantics</h5>
<div class="paragraph">
<p>A const variable statement is more or less a normal variable statement (see <a href="#_variable-statement">Variable Statement</a>), except that all variables declared by that
statement are not writable (cf. <a href="#Req-IDE-121">[Req-IDE-121]</a>).
This is similar to constant data fields (cf. <a href="#_assignment-modifiers">Assignment Modifiers</a>).</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-133"></a><strong>Req. IDE-133:</strong> <a href="#Req-IDE-133">Writability of const variables</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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></p>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_for-of-statement"><a class="anchor" href="#_for-of-statement"></a><a class="link" href="#_for-of-statement">9.2.3. <code>for &#8230;&#8203; of</code> statement</a></h4>
<div class="paragraph">
<p>ES6 introduced a new form of <code>for</code> statement: <code>for &#8230;&#8203; of</code> to iterate over the elements of an <code>Iterable</code>, cf. <a href="#_iterablen">[_iterablen]</a>.</p>
</div>
<h5 id="for-of-statement-syntax" class="discrete">Syntax</h5>
<div class="paragraph">
<p>See <a href="#_iteration-statements">Iteration Statements</a></p>
</div>
<h5 id="for-of-statement-semantics" class="discrete">Semantics</h5>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-134"></a><strong>Req. IDE-134:</strong> <a href="#Req-IDE-134">for &#8230;&#8203; of statement</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a given <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi></math> the following conditions must hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The value provided after <code>of</code> in a <code>for &#8230;&#8203; of</code> statement must be a subtype of <code>Iterable&lt;?&gt;</code>.</p>
</li>
<li>
<p>Either a new loop variable must be declared or an rvalue must be provided as init expression:<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></p>
</li>
<li>
<p>If a new variable <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>v</mi></math> is declared before <code>of</code> 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 <code>Iterable&lt;?extendsT&gt;</code>.
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 <code>of</code>.</p>
</li>
<li>
<p>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 <code>of</code> must be a subtype of <code>Iterable&lt;?extendsT&gt;</code>.</p>
</li>
</ol>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
<code>Iterable</code> is structurally typed on definition-site so non-N4JS types can meet the above requirements by simply implementing the only method in interface <code>Iterable</code> (with a correct return type).
</td>
</tr>
</table>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
The first of the above constraints (the type required by the ’of’ part in a <code>for &#8230;&#8203; of</code> loop is <code>Iterable</code>) 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 <code>Iterator</code> or accept both <code>Iterator</code> an or <code>Iterable</code>.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>Requiring an <code>Iterable</code> and not accepting a plain <code>Iterator</code> seems to be the final decision (as of Dec. 2014).
For reference, see abstract operations <code>GetIterator</code> in [<a href="#ECMA15a">ECMA15a(p.S7.4.2)</a>] and "CheckIterable" [<a href="#ECMA15a">ECMA15a(p.S7.4.1)</a>] and their
application in "ForIn/OfExpressionEvaluation" [<a href="#ECMA15a">ECMA15a(p.S13.6.4.8)</a>] and <code>CheckIterable</code> and their application in <code>ForIn/OfExpressionEvaluation</code>.
See also a related blog post <sup class="footnote">[<a id="_footnoteref_53" class="footnote" href="#_footnote_53" title="View footnote.">53</a>]</sup> that is kept up to date with changes to ECMAScript 6:</p>
</div>
<div class="quoteblock">
<blockquote>
<em>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.</em>
</blockquote>
</div>
<div class="paragraph">
<p>An array or object destructuring pattern may be used left of the <code>of</code>.
This is used to destructure the elements of the <code>Iterable</code> on the right-hand side (not the <code>Iterable</code> itself).
For detais, see <a href="#_array-and-object-destructuring">Array and Object Destructuring</a>.</p>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_import-statement"><a class="anchor" href="#_import-statement"></a><a class="link" href="#_import-statement">9.2.4. Import Statement</a></h4>
<div class="paragraph">
<p>Cf. ES6 import [<a href="#ECMA15a">ECMA15a(p.15.2.2)</a>], see also <a href="https://babeljs.io/docs/usage/modules/" class="bare">https://babeljs.io/docs/usage/modules/</a></p>
</div>
<h5 id="import-statement-syntax" class="discrete">Syntax</h5>
<div class="paragraph">
<p>The grammar of import declarations is defined as follows:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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;</code></pre>
</div>
</div>
<div class="paragraph">
<p>These are the properties of import declaration which can be specified by the user:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>annotations</code> </dt>
<dd>
<p>Arbitrary annotations, see <a href="#_annotations">Annotations</a> and below for details.</p>
</dd>
<dt class="hdlist1"><code>importSpecifiers</code> </dt>
<dd>
<p>The elements to be imported with their names.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>Also see compilation as described in <a href="#_modules">Modules</a>, for semantics see <a href="#import-statement-semantics">Import Statement Semantics</a>.</p>
</div>
<div class="exampleblock">
<div class="title">Example 95. Import</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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"</code></pre>
</div>
</div>
</div>
</div>
<h5 id="import-statement-semantics" class="discrete">Semantics</h5>
<div class="paragraph">
<p>Import statements are used to import identifiable elements from another module.
Identifiable elements are</p>
</div>
<div class="ulist">
<ul>
<li>
<p>types (via their type declaration), in particular</p>
<div class="ulist">
<ul>
<li>
<p>classifiers (classes, interfaces)</p>
</li>
<li>
<p>functions</p>
</li>
</ul>
</div>
</li>
<li>
<p>variables and constants.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The module to import from is identified by the string literal following keyword <code>from</code>.
This string must be a valid</p>
</div>
<div class="ulist">
<ul>
<li>
<p>complete module specifier <sup class="footnote">[<a id="_footnoteref_54" class="footnote" href="#_footnote_54" title="View footnote.">54</a>]</sup>:</p>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js"> import {A} from "ProjectA/a/b/c/M"</code></pre>
</div>
</div>
</li>
<li>
<p>plain module specifier:</p>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js"> import {A} from "a/b/c/M"</code></pre>
</div>
</div>
</li>
<li>
<p>or project name only, assuming the project defines a main module in its <code>package.json</code> file (using the <code>mainModule</code> package.json property, see <a href="#package-json-mainModule">mainModule</a>):</p>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js"> import {A} from "ProjectA"</code></pre>
</div>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>For choosing the element to import, there are the exact same options as in ECMAScript6:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>named imports select one or more elements by name, optionally introducing a local alias:</p>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js"> import {C} from "M"
import {D as MyD} from "M"
import {E, F as MyF, G, H} from "M"</code></pre>
</div>
</div>
</li>
<li>
<p>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:</p>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js"> import * as N from "M"
var c: N.C = new N.C();</code></pre>
</div>
</div>
</li>
<li>
<p>default imports select whatever element was exported by the remote module as the default (there can be at most one default export per module):</p>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js"> import C from "M"</code></pre>
</div>
</div>
</li>
<li>
<p>namespace imports provide access to the default export:</p>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js"> import * as N from "M"
var c: N.default = new N.default();</code></pre>
</div>
</div>
</li>
</ul>
</div>
<div class="openblock">
<div class="content">
<div class="paragraph">
<p>The following constraints are defined on a (non-dynamic) import statement <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi></math>:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The imported module needs to be accessible from the current project.</p>
</li>
<li>
<p>The imported declarations need to be accessible from the current module.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>For named imports, the following constraints must hold:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>No declaration must be imported multiple times, even if aliases are used.</p>
</li>
<li>
<p>The names must be unique in the module. They must not conflict with each other or locally declared variables, types, or functions.</p>
</li>
<li>
<p>Declarations imported via named imports are accessible only via used name (or alias) and not via original name directly.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>For wildcard imports, the following constraints must hold:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Only one namespace import can be used per (target) module, even if different namespace name is used.</p>
</li>
<li>
<p>The namespace name must be unique in the module. They must not conflict with each other or locally declared variables, types, or functions.</p>
</li>
<li>
<p>Declarations imported via namespace import are accessible via namespace only and not with original name directly.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>For namespace imports, the following constraints must hold:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>If the referenced module is a plain <code>js</code> file, a warning will be created to use the dynamic import instead.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>For default imports, the following constraints must hold:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The referenced module must have a default export.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Cross-cutting constraints:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>No declaration can be imported via named import and namespace import at the same time.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 96. Imports</div>
<div class="content">
<div class="paragraph">
<p>Imports cannot be duplicated:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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</code></pre>
</div>
</div>
<div class="paragraph">
<p>Given element cannot be imported multiple times:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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</code></pre>
</div>
</div>
<div style="page-break-after: always;"></div>
<div class="paragraph">
<p>Names used in imports must not not conflict with each other or local
declarations:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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</code></pre>
</div>
</div>
<div class="paragraph">
<p>Using named imports, aliases and namespaces allows to refer to mulitple
types of the same name such as <code>A/A</code>, <code>B/A</code> and <code>C/A</code>:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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</code></pre>
</div>
</div>
<div style="page-break-after: always;"></div>
<div class="paragraph">
<p>If a declaration has been imported with an alias or namespace, it is not
accessible via its original name:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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</code></pre>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="Dynamic_Imports"><a class="anchor" href="#Dynamic_Imports"></a><a class="link" href="#Dynamic_Imports">9.2.4.1. Dynamic Imports</a></h5>
<div class="paragraph">
<p>N4JS extends the ES6 module import in order that modules without a <code>n4jsd</code> or <code>n4js</code> file (plain <code>js</code> modules) can be imported.
This is done by adding <code>+</code> 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 <a href="#Import_Calls">Import Calls</a>, which are sometimes referred to as "dynamic import" as well.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-136"></a><strong>Req. IDE-136:</strong> <a href="#Req-IDE-136">Dynamic Import</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><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 <code>n4js</code> file.</p>
</li>
<li>
<p>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 <code>n4jsd</code> file, a warning is
to be created.</p>
</li>
<li>
<p>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.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>These constraints define the error level when using dynamic import: we receive no error for <code>js</code>, a warning for <code>n4jsd</code>, and an error for <code>n4js</code> files.
The idea behind these distinct error levels is as follows:<br>
If only a plain <code>js</code> file is available, using the dynamic import is the only way to access elements from the <code>js</code> module.
This might be an unsafe way, but it allows the access and simplifies the first steps.
An <code>n4jsd</code> file may then be made available either by the developer using the <code>js</code> 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 <code>n4jsd</code> file and a <code>js</code> file still must be provided by the user.
Having an <code>n4js</code> file is a completely different story; no <code>n4jsd</code> file is required, no <code>js</code> file is needed
(since the transpiler creates one from the <code>n4js</code> file) and there is absolutely no reason to use the module dynamically.</p>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_immutabilaty-of-imports"><a class="anchor" href="#_immutabilaty-of-imports"></a><a class="link" href="#_immutabilaty-of-imports">9.2.4.2. Immutabilaty of Imports</a></h5>
<div class="paragraph">
<p>Imports create always immutable bindings, c.f.
[<a href="#ECMA15a">ECMA15a(p.8.1.1.5)</a>]
<a href="http://www.ecma-international.org/ecma-262/6.0/index.html#sec-createimportbinding" class="bare">http://www.ecma-international.org/ecma-262/6.0/index.html#sec-createimportbinding</a></p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-137"></a><strong>Req. IDE-137:</strong> <a href="#Req-IDE-137">Immutable Import</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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</p>
</div>
<div class="ulist">
<ul>
<li>
<p><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),</p>
</li>
<li>
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi></math> as a direct argument of a postfix operator (<code>i++</code>/<code>i--</code>),</p>
</li>
<li>
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi></math> as a direct argument of a <code>delete</code> operator,</p>
</li>
<li>
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi></math> as a direct argument of the <code>increment</code> or <code>decrement</code> unary operator (<code>i++</code>/<code>i--</code>)</p>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_export-statement"><a class="anchor" href="#_export-statement"></a><a class="link" href="#_export-statement">9.2.5. Export Statement</a></h4>
<div class="paragraph">
<p>Cf. ES6 import [<a href="#ECMA15a">ECMA15a(p.15.2.3)</a>]</p>
</div>
<h5 id="export-statement-syntax" class="discrete">Syntax</h5>
<div class="paragraph">
<p>Grammar of export declarations is defined as follows:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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
;</code></pre>
</div>
</div>
<div class="paragraph">
<p>These are the properties of export declaration, which can be specified by the user:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>exportedElement</code> </dt>
<dd>
<p>The element to be exported, can be a declaration or a variable/const statement.</p>
</dd>
</dl>
</div>
<div class="exampleblock">
<div class="title">Example 97. Export</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">export public class A{}
export interface B{}
export function foo() {}
export var a;
export const c="Hello";</code></pre>
</div>
</div>
</div>
</div>
<h5 id="export-statement-semantics" class="discrete">Semantics</h5>
<div class="paragraph">
<p>With regard to type inference, export statements are not handled at all.
Only the exported element is inferred and the <code>export</code> keyword is ignored.</p>
</div>
<div class="paragraph">
<p>In order to use types defined in other compilation units, these types have to be explicitly imported with an import statement.</p>
</div>
<div class="paragraph">
<p>Imported namespaces cannot be exported.</p>
</div>
<div class="paragraph">
<p>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. <a href="#_import-statement">Import Statement</a>).</p>
</div>
<div class="paragraph">
<p>Some special components (runtime environment and libraries, cf. <a href="#_runtime-environment-and-runtime-libraries">Runtime Environment and Runtime Libraries</a>, may export elements globally.
This is done by annotating the export (or the whole module) with <code>@Global</code>, see <a href="#_global-definitions">Global Definitions</a> for details.</p>
</div>
<div class="paragraph">
<p>By adding <code>default</code> after the keyword <code>export</code>, the identifiable element can be exported as ’the default’.
This can then be imported from other modules via default imports (see <a href="#_import-statement">Import Statement</a>).</p>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_annotations"><a class="anchor" href="#_annotations"></a><a class="link" href="#_annotations">10. Annotations</a></h2>
<div class="sectionbody">
<div class="sect2 language-n4js">
<h3 id="_introduction-2"><a class="anchor" href="#_introduction-2"></a><a class="link" href="#_introduction-2">10.1. Introduction</a></h3>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="sect3">
<h4 id="_syntax-13"><a class="anchor" href="#_syntax-13"></a><a class="link" href="#_syntax-13">10.1.1. Syntax</a></h4>
<div class="paragraph">
<p>Annotations are used similarly as in Java (although new annotations cannot be defined by the user).
They are formally defined as follows:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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
;</code></pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_properties-7"><a class="anchor" href="#_properties-7"></a><a class="link" href="#_properties-7">10.1.2. Properties</a></h4>
<div class="paragraph">
<p>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>.</p>
</div>
<div style="page-break-after: always;"></div>
</div>
<div class="sect3">
<h4 id="_element-specific-annotations"><a class="anchor" href="#_element-specific-annotations"></a><a class="link" href="#_element-specific-annotations">10.1.3. Element-Specific Annotations</a></h4>
<div class="paragraph">
<p>The following annotations are element-specific and are explained in the corresponding sections:</p>
</div>
<table class="tableblock frame-all grid-all spread">
<caption class="title">Table 11. Element-Specific Annotations</caption>
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
<col style="width: 33.3334%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Annotation</th>
<th class="tableblock halign-center valign-top">Element Types</th>
<th class="tableblock halign-left valign-top">Section</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@Internal</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>TypeDefiningElement, Member, Function, Export</code></p></td>
<td class="tableblock halign-left valign-top"><div><div class="paragraph">
<p><a href="#_access-control">Access Control</a></p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@Undefined</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Variable</code></p></td>
<td class="tableblock halign-left valign-top"><div><div class="paragraph">
<p><a href="#_undefined-type">Undefined Type</a></p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@StringBased</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Enum</code></p></td>
<td class="tableblock halign-left valign-top"><div><div class="paragraph">
<p><a href="#_string-based-enums">String-Based Enums</a></p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@Final</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Class, Member</code></p></td>
<td class="tableblock halign-left valign-top"><div><div class="paragraph">
<p><a href="#_final-methods">Final Methods</a></p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@Spec</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>FPar</code></p></td>
<td class="tableblock halign-left valign-top"><div><div class="paragraph">
<p><a href="#Req-IDE-59">[Req-IDE-59]</a></p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@Override</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Method</code></p></td>
<td class="tableblock halign-left valign-top"><div><div class="paragraph">
<p><a href="#_redefinition-of-members">Redefinition of Members</a></p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@Promisifiable</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Function</code></p></td>
<td class="tableblock halign-left valign-top"><div><div class="paragraph">
<p><a href="#_promisifiable-functions">Promisifiable Functions</a></p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@Promisify</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>CallExpression</code></p></td>
<td class="tableblock halign-left valign-top"><div><div class="paragraph">
<p><a href="#_promisifiable-functions">Promisifiable Functions</a></p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@This</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Function</code></p></td>
<td class="tableblock halign-left valign-top"><div><div class="paragraph">
<p><a href="#_this-keyword">This Keyword</a></p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@N4JS</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Class, Export Statement</code></p></td>
<td class="tableblock halign-left valign-top"><div><div class="paragraph">
<p><a href="#_external-declarations">External Declarations</a></p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@IgnoreImplementation</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Script, ExportDeclaration, ExportableElement</code></p></td>
<td class="tableblock halign-left valign-top"><div><div class="paragraph">
<p><a href="#_external-declarations">External Declarations</a></p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@Global</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>External Declaration</code></p></td>
<td class="tableblock halign-left valign-top"><div><div class="paragraph">
<p><a href="#_global-definitions">Global Definitions</a></p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@ProvidedByRuntime</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>External Declaration</code></p></td>
<td class="tableblock halign-left valign-top"><div><div class="paragraph">
<p><a href="#_runtime-definitions">Runtime Definitions</a></p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@TestAPI</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>TypeDefiningElement, Member</code></p></td>
<td class="tableblock halign-left valign-top"><div><div class="paragraph">
<p><a href="#_test-support">Test Support</a></p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@Polyfill</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Class</code></p></td>
<td class="tableblock halign-left valign-top"><div><div class="paragraph">
<p><a href="#_polyfill-definitions">Polyfill Definitions</a></p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@StaticPolyfill</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Class</code></p></td>
<td class="tableblock halign-left valign-top"><div><div class="paragraph">
<p><a href="#_static-polyfill-definitions">Static Polyfill Definitions</a></p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@StaticPolyfillAware</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Script</code></p></td>
<td class="tableblock halign-left valign-top"><div><div class="paragraph">
<p><a href="#_static-polyfill-definitions">Static Polyfill Definitions</a></p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@StaticPolyfillModule</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Script</code></p></td>
<td class="tableblock halign-left valign-top"><div><div class="paragraph">
<p><a href="#_static-polyfill-definitions">Static Polyfill Definitions</a></p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@Transient</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Field</code></p></td>
<td class="tableblock halign-left valign-top"><div><div class="paragraph">
<p><sup class="footnote">[<a id="_footnoteref_1" class="footnote" href="#_footnote_1" title="View footnote.">1</a>]</sup></p>
</div>
<div id="footnotes">
<hr>
<div class="footnote" id="_footnote_1">
<a href="#_footnoteref_1">1</a>. intended for internal use only; will be removed
</div>
</div></div></td>
</tr>
</tbody>
</table>
</div>
<div class="sect3">
<h4 id="_general-annotations"><a class="anchor" href="#_general-annotations"></a><a class="link" href="#_general-annotations">10.1.4. General Annotations</a></h4>
<div class="sect4">
<h5 id="_idebug"><a class="anchor" href="#_idebug"></a><a class="link" href="#_idebug">10.1.4.1. IDEBUG</a></h5>
<div class="paragraph">
<p><code>@IDEBUG</code> is an annotation similar to Java’s <code>@SuppressWarnings</code>.
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.</p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="idebug-syntax"><a class="anchor" href="#idebug-syntax"></a><a class="link" href="#idebug-syntax">10.1.5. Syntax</a></h4>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">'@IDEBUG' '(' bugID = INT ',' errorMessage=STRING ')'</code></pre>
</div>
</div>
<div class="paragraph">
<p>The annotation is defined transitively and repeatable on script, type declaration, function and method level.</p>
</div>
<div class="sect4">
<h5 id="_semantics-11"><a class="anchor" href="#_semantics-11"></a><a class="link" href="#_semantics-11">10.1.5.1. Semantics</a></h5>
<div class="paragraph">
<p>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 <code></code> (three dots as a single character, created by Eclipse to abbreviate messages), then the error’s message text must start with the specified text.</p>
</div>
<div class="paragraph">
<p>If no matching error is found, the annotation itself will issue an error.</p>
</div>
<div id="ex:IDEBUG" class="exampleblock">
<div class="title">Example 98. IDEBUG Example</div>
<div class="content">
<div class="paragraph">
<p>In the following code snippet, two errors are to be transformed to warnings.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">export class TestDataBridge with IModuleTest {
@IDEBUG(166, "{function(number):void} is not a subtype of {function(T):void}.") <i class="conum" data-value="1"></i><b>(1)</b>
@IDEBUG(91, "Incorrect number of arguments: expected 1, got 2.") <i class="conum" data-value="2"></i><b>(2)</b>
@Override public run(): void {
var foo = new Foo(),
cb = function(val: number): void {},
db = DataBridge.&lt;number&gt;bind(foo, "bar");
db.add(cb); <i class="conum" data-value="3"></i><b>(3)</b>
Assert.isTrue(called);
}
}</code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>The annotation transforms the error <code>{function(number):void} is not a subtype of {function(T):void}</code> into a warning with the following text:</td>
</tr>
<tr>
<td><i class="conum" data-value="2"></i><b>2</b></td>
<td>This annotation was proposed as a workaround for <a href="https://github.com/NumberFour/n4js/issues/91">IDEBUG-91</a> which has been fixed.<br>
No error message is produced and an error will be issued on this line instead:</td>
</tr>
<tr>
<td><i class="conum" data-value="3"></i><b>3</b></td>
<td>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.</td>
</tr>
</table>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_suppress-warnings"><a class="anchor" href="#_suppress-warnings"></a><a class="link" href="#_suppress-warnings">10.1.5.2. Suppress Warnings</a></h5>
<div class="admonitionblock tip">
<table>
<tr>
<td class="icon">
<i class="fa icon-tip" title=""></i>
</td>
<td class="content">
This is not part of the current version
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_declaration-of-annotations"><a class="anchor" href="#_declaration-of-annotations"></a><a class="link" href="#_declaration-of-annotations">10.2. Declaration of Annotations</a></h3>
<div class="admonitionblock tip">
<table>
<tr>
<td class="icon">
<i class="fa icon-tip" title=""></i>
</td>
<td class="content">
This is not part of the current version
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_extended-fetaures"><a class="anchor" href="#_extended-fetaures"></a><a class="link" href="#_extended-fetaures">11. Extended Fetaures</a></h2>
<div class="sectionbody">
<div class="sect2 language-n4js">
<h3 id="_array-and-object-destructuring"><a class="anchor" href="#_array-and-object-destructuring"></a><a class="link" href="#_array-and-object-destructuring">11.1. Array and Object Destructuring</a></h3>
<div class="paragraph">
<p>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 <sup class="footnote">[<a id="_footnoteref_55" class="footnote" href="#_footnote_55" title="View footnote.">55</a>]</sup>.</p>
</div>
<div class="sect3">
<h4 id="_syntax-14"><a class="anchor" href="#_syntax-14"></a><a class="link" href="#_syntax-14">11.1.1. Syntax</a></h4>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-ebnf" data-lang="ebnf">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) ']'
;</code></pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_semantics-12"><a class="anchor" href="#_semantics-12"></a><a class="link" href="#_semantics-12">11.1.2. Semantics</a></h4>
<div class="paragraph">
<p>The following example declares four variables <code>a</code>, <code>b</code>, <code>x</code>, and <code>prop2</code>. Variables <code>a</code> and <code>x</code> will have the value <code>hello</code>, whereas <code>b</code> and <code>prop2</code> will have value 42.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var [a,b] = ["hello", 42];
var {prop1:x, prop2} = {prop1:"hello", prop2:42};</code></pre>
</div>
</div>
<div class="paragraph">
<p>In the case of <code>prop2</code>, 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 <code>single name binding</code>).</p>
</div>
<div class="paragraph">
<p>One of the most useful use cases of destructuring is in a <code>for..of</code> loop.
Take this example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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</code></pre>
</div>
</div>
<div class="paragraph">
<p>Array and object destructuring pattern can appear in many different places:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>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 <em>destructuring binding</em>; see <a href="#_variable-statement">Variable Statement</a>).</p>
</li>
<li>
<p>On the left-hand side of an assignment expression (the assignment expression is then called <em>destructuring assignment</em>; see <a href="#_assignment-expression">Assignment Expression</a>).</p>
</li>
<li>
<p>In a <code>for..in</code> or <code>for..of</code> loop on the left side of the <code>in</code>/<code>of</code> (see <a href="#_for-of-statement"><code>for &#8230;&#8203; of</code> statement</a>).</p>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
It can also be used in plain statements, but then we actually have one of the above two use cases.
</td>
</tr>
</table>
</div>
</li>
<li>
<p>With lists of formal parameters or function arguments (not supported yet).</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>For further details on array and object destructuring please refer to the ECMAScript 6 specification - [<a href="#ECMA15a">ECMA15a</a>].</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var {x: someTypeOrNewVar} = ol</code></pre>
</div>
</div>
<div class="paragraph">
<p>could either mean that a new variable <code>someTypeOrNewVar</code> is declared and <code>ol.x</code> is assigned to it, or that a new variable <code>x</code> is declared with type <code>someTypeOrNewVar</code>.
The longer form would look like this:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var {x: x: someType} = ol</code></pre>
</div>
</div>
<div class="paragraph">
<p>We can make this more readable:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var {propOfOl: newVar: typeOfNewVar} = ol</code></pre>
</div>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_dependency-injection"><a class="anchor" href="#_dependency-injection"></a><a class="link" href="#_dependency-injection">11.2. Dependency Injection</a></h3>
<div class="paragraph">
<p>This chapter describes <a href="#_acronyms">DI</a> mechanisms for N4JS.
This includes compiler, validation and language extensions that allow to achieve DI mechanisms built in into the N4JS language and IDE.</p>
</div>
<div class="paragraph">
<p>N4JS <a href="#_acronyms">DI</a> 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.</p>
</div>
<div id="fig-di-terms" class="imageblock" style="text-align: center">
<div class="content">
<img src="chapters/11_extFeatures/fig/diBasicTerms.png" alt="diBasicTerms">
</div>
<div class="title">Figure 8. DI Basic Terms</div>
</div>
<div class="paragraph">
<p><em>key: pass the dependency instead of letting the client create or find it</em></p>
</div>
<div class="paragraph">
<p>Core terms</p>
</div>
<div class="ulist">
<ul>
<li>
<p><strong>Service</strong> - A set of APIs describing the functionality of the service.</p>
</li>
<li>
<p><strong>Service Implementation</strong>s - One or more implementations of given service API.</p>
</li>
<li>
<p><strong>Client</strong> - Consumer of a given functionality, uses the given <strong>Service Implementation</strong>.</p>
</li>
<li>
<p><strong>Injector</strong> - Object providing <strong>Service Implementation</strong> of a specific <strong>Service</strong>, according to configuration.</p>
</li>
<li>
<p><strong>Binding</strong> - Part of configuration describing which interface implementing a subtype will be injected, when a given interface is requested.</p>
</li>
<li>
<p><strong>Provider</strong> - Factory used to create instances of a given <strong>Service Implementation</strong> or its sub-components, can be a method.</p>
</li>
<li>
<p><strong>Injection Point</strong> - Part of the user’s code that will have the given dependency injected. This is usually fields, method parameters, constructor parameters etc.</p>
</li>
<li>
<p><strong>DI configuration</strong> - 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.</p>
</li>
<li>
<p><strong>di wiring</strong> - The code responsible for creating user objects.
These are injectors, type factories/providers, fields initiators etc.</p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="_di-components-and-injectors"><a class="anchor" href="#_di-components-and-injectors"></a><a class="link" href="#_di-components-and-injectors">11.2.1. DI Components and Injectors</a></h4>
<div class="paragraph">
<p>N4JS’ <a href="#_dependency-injection">Dependency Injection</a> systems is based on the notion of <a href="#_acronyms">DIC</a>.</p>
</div>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="di_component"></a><strong>Definition:</strong> <a href="#di_component">DI Component</a></p>
</div>
<div class="paragraph">
<p>A <a href="#_acronyms">DIC</a> is a N4Class annotated with <code>@GenerateInjector</code>.</p>
</div>
</div>
</div>
<div class="paragraph">
<p>This annotation causes an <em>injector</em> to be created for (and associated to) the <a href="#_acronyms">DI</a>.
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 <code>@WithParent</code> to ensure that it is always used with a proper parent.</p>
</div>
<div class="paragraph">
<p><em>Injector</em> is the main object of DI mechanisms responsible for creating object graphs of the application.
At runtime, injectors are instances of <code>N4Injector</code>.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-138"></a><strong>Req. IDE-138:</strong> <a href="#Req-IDE-138">DI Component and Injector</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>The following constraints must hold for a class <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi></math> marked as DIC:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>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 <code>GenerateInjector</code>.</p>
</li>
<li>
<p>If a parent <a href="#_dicomponent-relations">DIC</a> <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi></math> is specified via <code>WithParent</code>, then <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi></math> must be a DIC as well.</p>
</li>
<li>
<p>The injector associated to a DIC is of type <code>N4Injector</code>. It can be retrieved via <code>N4Injector.of(DIC)</code> in which <code>DIC</code> is the <code>DIC</code>.</p>
</li>
<li>
<p>Injectors associated to DIC a are DI-singletons (cf. <a href="#_singleton-scope">Singleton Scope</a>).
Two calls to <code>N4Injector.of(DIC)</code> are different (as different DIC are assumed).</p>
</li>
</ol>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-139"></a><strong>Req. IDE-139:</strong> <a href="#Req-IDE-139">Injection Phase</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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 <em>injection phase</em>.
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:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Root objects are created by one of the following mechanisms:</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>Any class or interface can be created as root objects via an injector associated to a DIC:<br>
<code>var x: X = N4Injector.of(DIC).create(X);</code><br>
in which <code>DIC</code> is a DIC.</p>
<div class="paragraph">
<p>Of course, an appropriate binding must exist. <sup class="footnote">[<a id="_footnoteref_56" class="footnote" href="#_footnote_56" title="View footnote.">56</a>]</sup></p>
</div>
</li>
<li>
<p>If a type has the injector being injected, e.g. via field injection <code>@Inject injector: N4Injector;</code>, then this injector can be used anytime in the control flow to create
a new root object similar as above (using <code>create</code> method).</p>
</li>
<li>
<p>If a provider has been injected (i.e. an instance of <code>{N4Provider}</code>), then its <code>get()</code> method can be used to create a root object causing a new injection phase to take place.</p>
</li>
</ol>
</div>
</li>
<li>
<p>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 <a href="#Req-IDE-143">[Req-IDE-143]</a> . For all arguments the injection phase constraints have to hold as well.</p>
</li>
<li>
<p>All fields of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi></math>, including <em>inherited</em> once, marked as injection points are set by the injector.
For all fields the injection phase constraints have to hold as well.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>The injector may use a provider method (of a binder) to create nested instances.</p>
</div>
<div class="paragraph">
<p>The injector is configured with <em>Binders</em> and it tracks <em>Bindings</em> between types (<a href="#_binders-and-bindings">Binders and Bindings</a>).
An N4JS developer normally would not interact with this object directly except when defining an entry-point to his application.
<em>Injector</em>s are configured with <em>Binder</em>s which contain explicit <em>Binding</em>s defined by an N4JS developer.
A set of these combined with <em>implicit bindings</em> creates the <em>di configuration</em> used by a given injector.
To configure given <em>Injector</em>s with given <em>Binder</em>(s) use <code>@UseBinder</code> annotation.</p>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_dicomponent-relations"><a class="anchor" href="#_dicomponent-relations"></a><a class="link" href="#_dicomponent-relations">11.2.1.1. DIComponent Relations</a></h5>
<div class="paragraph">
<p>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 <em>change</em> targets used by a parent.
The final circumstance is local to the child and is referred to as <em>rebinding</em>.
For more information about bindings see <a href="#_binders-and-bindings">Binders and Bindings</a>.
A Child-Parent relation is expressed by the <code>@WithParentInjector</code> 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 <code>@WithParentInjector</code>) when creating the child injector
(injector of the DIComponent annotated with <code>@WithParentInjector</code>).</p>
</div>
<div class="exampleblock">
<div class="title">Example 99. Simple DIComponents Relation</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">@GenerateInjector
class ParentDIComponent{}
@GenerateInjector
@WithParentInjector(ParentDIComponent)
class ChildDIComponent{}
var parentInejctor = N4Inejctor.of(ParentDiCompoennt);
var childInjector = N4Inejctor.of(ChildDIComponent, parentInjector);</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>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, <em>compatible</em> with its parents.</p>
</div>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="compatible_dicomponent"></a><strong>Definition:</strong> <a href="#compatible_dicomponent">Compatible DIComponent</a></p>
</div>
<div class="paragraph">
<p>A given DIComponent is compatible with another DIComponent if it has bindings for all keys in other component bindings.</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
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 <strong>not</strong> 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.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>A complex Child-Parent relation between components is depicted in <a href="#fig-complex-dicomponents-relations">Complex DIComponents Relations</a> and <a href="#ex:complex-dicomponents-relations">Complex DIComponents Relations</a> below.</p>
</div>
<div id="fig-complex-dicomponents-relations" class="imageblock" style="text-align: center">
<div class="content">
<img src="chapters/11_extFeatures/fig/diagDICParentChild.svg" alt="diagDICParentChild">
</div>
<div class="title">Figure 9. Complex DIComponents Relations</div>
</div>
</div>
</div>
<div id="ex:complex-dicomponents-relations" class="exampleblock">
<div class="title">Example 100. Complex DIComponents Relations</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">@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</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_binders-and-bindings"><a class="anchor" href="#_binders-and-bindings"></a><a class="link" href="#_binders-and-bindings">11.2.2. Binders and Bindings</a></h4>
<div class="paragraph">
<p><em>Binder</em> allows an N4JS developer to (explicitly) define a set of <em>Binding</em>s that will be used by an <em>Injector</em> configured with a given <em>Binder</em>.
There are two ways for <em>Binder</em> to define <em>Binding</em>s: <code>@Bind</code> (<a href="#_n4js-di-bind">N4JS DI @Bind</a>) annotations and a method annotated with <code>@Provides</code>.</p>
</div>
<div class="paragraph">
<p><em>Binder</em> is declared by annotating a class with the <code>@Binder</code> annotation.</p>
</div>
<div class="paragraph">
<p>A <em>Binding</em> is part of a configuration that defines which instance of
what type should be injected into an <em>injection point</em> (<a href="#_injection-points">Injection Points</a>) with an expected type.</p>
</div>
<div class="paragraph">
<p><em>Provider Method</em> is essentially a <em>factory method</em> that is used to create an instance of a type.
N4JS allows a developer to declare those methods (see <a href="#_n4js-di-provides">N4JS DI @Provides</a>) which gives them a hook in instance creation process.
Those methods will be used when creating instances by the <em>Injector</em> configured with the corresponding <em>Binder</em>.
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).</p>
</div>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="binding"></a><strong>Definition:</strong> <a href="#binding">Binding</a></p>
</div>
<div class="paragraph">
<p>A <em>binding</em> 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.</p>
</div>
<div class="paragraph">
<p>A <em>binding</em> is called <em>explicit</em> if it is declared in the code, i.e. via <code>@Bind</code>
annotation or <code>@Provides</code> annotation).</p>
</div>
<div class="paragraph">
<p>A <em>binding</em> is called <em>implicit</em> 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>.</p>
</div>
<div class="paragraph">
<p>A provider method <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi></math> (in the binder) defines a binding</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>(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>).</p>
</div>
<div class="paragraph">
<p>For simplification, we define:</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
<div class="paragraph">
<p>and</p>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-140"></a><strong>Req. IDE-140:</strong> <a href="#Req-IDE-140">Bindings</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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: <sup class="footnote">[<a id="_footnoteref_57" class="footnote" href="#_footnote_57" title="View footnote.">57</a>]</sup></p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><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.</p>
</li>
<li>
<p><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.</p>
</li>
<li>
<p>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.</p>
</li>
<li>
<p><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!</p>
</li>
<li>
<p><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</p>
</li>
<li>
<p>If during injection phase no binding for a given key is found, an <code>DIUnsatisfiedBindingError</code> is thrown.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-141"></a><strong>Req. IDE-141:</strong> <a href="#Req-IDE-141">Transitive Bindings</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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>).</p>
</div>
<div class="paragraph">
<p>N4JS <a href="#_acronyms">DI</a> 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 <sup class="footnote">[<a id="_footnoteref_58" class="footnote" href="#_footnote_58" title="View footnote.">58</a>]</sup>.
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.</p>
</div>
<div class="paragraph">
<p>While direct binding overriding or rebinding is not allowed, <em>Injector</em> can be configured in a way where one type can be separately bound to different types with implicit binding,
<em>explicit binding</em> and in bindings of the child injectors.
<em>Binding precedence</em> is a mechanism of <em>Injector</em> selecting a binding use for a type.
It operates in the following order:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Try to use explicit binding, if this is not available:</p>
</li>
<li>
<p>Try to delegate to parent injectors (order of lookup is not guaranteed, first found is selected). If this is not available then:</p>
</li>
<li>
<p>Try to use use implicit binding, which is simply to attempt to create the instance.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>If no binding for a requested type is available an error will be thrown.</p>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_injection-points"><a class="anchor" href="#_injection-points"></a><a class="link" href="#_injection-points">11.2.3. Injection Points</a></h4>
<div class="paragraph">
<p>By <em>injection point</em> we mean a place in the source code which, at runtime, will be expected to hold a reference to a particular type instance.</p>
</div>
<div class="sect4">
<h5 id="_field-injection"><a class="anchor" href="#_field-injection"></a><a class="link" href="#_field-injection">11.2.3.1. Field Injection</a></h5>
<div class="paragraph">
<p>In its simplest form, this is a class field annotated with <code>@Inject</code> 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 <em>Field Injection</em>.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-142"></a><strong>Req. IDE-142:</strong> <a href="#Req-IDE-142">Field Injection</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>The injector will inject the
following fields:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>All directly contained fields annotated with <code>@Inject</code>.</p>
</li>
<li>
<p>All inherited fields annotated with <code>@Inject</code>.</p>
</li>
<li>
<p>The injected fields will be created by the injector and their fields will be injected as well.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 101. Simple Field Injection</div>
<div class="content">
<div class="paragraph">
<p><a href="#ex:field-injection">Simple Field Injection</a> demonstrates simple field injection using default bindings.
Note that all inherited fields (i.e. <code>A.xInA</code>) are injected and also fields in injected fields (i.e. <code>x.y</code>)</p>
</div>
<div id="ex:field-injection" class="listingblock">
<div class="title">Simple Field Injection</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_constructor-injection"><a class="anchor" href="#_constructor-injection"></a><a class="link" href="#_constructor-injection">11.2.3.2. Constructor Injection</a></h5>
<div class="paragraph">
<p>Parameters of the constructor can also be injected, in which case this is usually referred to as <em>Constructor Inejction</em>.
This is similar to <em>Method Injection</em> and while constructor injection is supported in N4JS, method injection is not (see remarks below).</p>
</div>
<div class="paragraph">
<p>When a constructor is annotated with <code>@Inject</code> 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 <code>@Inject</code> that has variadic parameters, a validation error will be reported.
When a class’s constructor is annotated with <code>@Inject</code> 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 <code>@Inject</code> annotations for any subclasses, a validation warning will
be reported.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-143"></a><strong>Req. IDE-143:</strong> <a href="#Req-IDE-143">Constructor Injection</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>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.</p>
</li>
<li>
<p>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.</p>
</li>
</ol>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_method-injection"><a class="anchor" href="#_method-injection"></a><a class="link" href="#_method-injection">11.2.3.3. Method Injection</a></h5>
<div class="paragraph">
<p>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.</p>
</div>
<div class="sect5">
<h6 id="_provider"><a class="anchor" href="#_provider"></a><a class="link" href="#_provider">11.2.3.3.1. Provider</a></h6>
<div class="paragraph">
<p><em>Provider</em> is essentially a <em>factory</em> for a given type.
By injecting an <code>N4Provider</code> 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.</p>
</div>
<div class="paragraph">
<p><code>N4Provider</code> 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 <code>N4Provider</code> interface has one single public method: <code>public T get()</code> which should be invoked from the client code when a new instance of the dependent type is required.
Unlike any other unbound interfaces, the <code>N4Provider</code> can be injected without any explicit binding.</p>
</div>
<div class="paragraph">
<p>The following snippet demonstrates the usage of <code>N4Provider</code>:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>It is important to note that the <code>N4Provider</code> 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 <code>N4Provider</code>s.</p>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_n4js-di-life-cycle-and-scopes"><a class="anchor" href="#_n4js-di-life-cycle-and-scopes"></a><a class="link" href="#_n4js-di-life-cycle-and-scopes">11.2.4. N4JS DI Life Cycle and Scopes</a></h4>
<div class="paragraph">
<p><a href="#_acronyms">DI</a> 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 <code>N4JSProvider</code> and <code>Binder@Provides</code> methods.</p>
</div>
<div class="sect4">
<h5 id="_injection-cylces"><a class="anchor" href="#_injection-cylces"></a><a class="link" href="#_injection-cylces">11.2.4.1. Injection Cylces</a></h5>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="injection_cycle"></a><strong>Definition:</strong> <a href="#injection_cycle">Injection Cycle</a></p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><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></p>
</li>
<li>
<p><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></p>
</li>
</ol>
</div>
<div class="paragraph">
<p>One cycle in this graph is an injection cycle.</p>
</div>
</div>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<table class="tableblock frame-none grid-rows spread">
<colgroup>
<col style="width: 57.1428%;">
<col style="width: 42.8572%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-bottom"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class A { @Inject b: B; }
class B { @Inject a: A; }</code></pre>
</div>
</div></div></td>
<td class="tableblock halign-center valign-top"><div><div id="fig-field-cycle" class="imageblock" style="text-align: center">
<div class="content">
<img src="chapters/11_extFeatures/fig/injectionGraph_cycleField.svg" alt="injectionGraph cycleField">
</div>
<div class="title">Figure 10. Field Cycle</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-bottom"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C { @Inject constructor(d: D) {} }
class D { @Inject c: C; }</code></pre>
</div>
</div></div></td>
<td class="tableblock halign-center valign-top"><div><div id="fig-ctor-field" class="imageblock" style="text-align: center">
<div class="content">
<img src="chapters/11_extFeatures/fig/injectionGraph_cycleCtorField.svg" alt="injectionGraph cycleCtorField">
</div>
<div class="title">Figure 10. Ctor Field Cycle</div>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-bottom"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class E { @Inject constructor(f: F) {} }
class F { @Inject constructor(e: E) {} }</code></pre>
</div>
</div></div></td>
<td class="tableblock halign-center valign-top"><div><div id="fig-ctor-cycle" class="imageblock" style="text-align: center">
<div class="content">
<img src="chapters/11_extFeatures/fig/injectionGraph_cycleCtor.svg" alt="injectionGraph cycleCtor">
</div>
<div class="title">Figure 10. Ctor Cycle</div>
</div></div></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>The injector needs to detect these cycles and resolve them.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-144"></a><strong>Req. IDE-144:</strong> <a href="#Req-IDE-144">Resolution of Injection Cycles</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>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.</p>
</li>
<li>
<p>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.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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;
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The actual instance is created in line 10 via <code>createInstance</code>.
This function then takes scopes into account.
The <code>createdInstancesPerType</code> 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. <a href="https://jersey.java.net/documentation/latest/ioc.html">Jersey custom scopes</a> and <a href="https://github.com/google/guice/wiki/CustomScopes">Guice custom scopes</a>, may be added in the future.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
</div>
<div class="sect4">
<h5 id="_default-scope"><a class="anchor" href="#_default-scope"></a><a class="link" href="#_default-scope">11.2.4.2. Default Scope</a></h5>
<div class="paragraph">
<p>The default scope always creates a new instance.</p>
</div>
</div>
<div class="sect4">
<h5 id="_singleton-scope"><a class="anchor" href="#_singleton-scope"></a><a class="link" href="#_singleton-scope">11.2.4.3. Singleton Scope</a></h5>
<div class="paragraph">
<p>The singleton scope (per injector) creates one instance (of the type with <code>@Singleton</code> scope) per injector, which is then shared between clients.</p>
</div>
<div class="paragraph">
<p>The injector will preserve a single instance of the type of <code>S</code> and will provide it to all injection points where type of <code>S</code> is used.
Assuming nested injectors without any declared binding where the second parameter is <code>S</code>, the same preserved singleton instance will be available for all nested injectors at all injection points as well.</p>
</div>
<div class="paragraph">
<p>The singleton preservation behavior changes when explicit bindings are declared for type <code>S</code> on the nested injector level.
Let&#8217;s assume that the type <code>S</code> exists and the type is annotated with <code>@Singleton</code>.
Furthermore, there is a declared binding where the binding&#8217;s second argument is <code>S</code>.
In that case, unlike in other dependency injection frameworks, nested injectors may preserve a singleton for itself and all descendant injectors with <code>@Bind</code> annotation.
In this case, the preserved singleton at the child injector level will be a different instance than the one at the parent injectors.</p>
</div>
<div class="paragraph">
<p>The tables below depict the expected runtime behavior of singletons used at different injector levels.
Assume the following are injectors: <code>C</code>, <code>D</code>, <code>E</code>, <code>F</code> and <code>G</code>. Injector <code>C</code> is the top most injector and its nesting injector <code>D</code>, hence injector <code>C</code> is the parent of the injector <code>D</code>.
Injector <code>D</code> is nesting <code>E</code> and so on.
The most nested injector is <code>G</code>. Let&#8217;s assume <code>J</code> is an interface, class <code>U</code> implements interface <code>J</code> and class <code>V</code> extends class <code>U</code>.
Finally assume both <code>U</code> and <code>V</code> are annotated with <code>@Singleton</code> at definition-site.</p>
</div>
<div class="paragraph">
<p><a href="#tab:diNoBindings">DI No Bindings</a> depicts the singleton preservation for nested injectors without any bindings.
All injectors use the same instance from a type.
Type <code>J</code> is not available at all since it is not bound to any concrete implementation:</p>
</div>
<table id="tab:diNoBindings" class="tableblock frame-all grid-all spread">
<caption class="title">Table 12. DI No Bindings</caption>
<colgroup>
<col style="width: 28.5714%;">
<col style="width: 14.2857%;">
<col style="width: 14.2857%;">
<col style="width: 14.2857%;">
<col style="width: 14.2857%;">
<col style="width: 14.2858%;">
</colgroup>
<tbody>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">Binding</p></th>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">Injector nesting (<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>&gt;</mo></math>)</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock">C</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">D</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">E</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">F</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">G</p></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">J</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>N</mi><mi>a</mi><mi>N</mi></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>N</mi><mi>a</mi><mi>N</mi></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>N</mi><mi>a</mi><mi>N</mi></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>N</mi><mi>a</mi><mi>N</mi></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>N</mi><mi>a</mi><mi>N</mi></math></p></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">U</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></p></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">V</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></p></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p><a href="#tab:diTransitiveBindings">DI Transitive Bindings</a> is configured by explicit bindings. At the root injector level, type <code>J</code> is bound to type <code>U</code>.
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 <code>U</code>.
At injector level <code>C</code>, <code>D</code> and <code>E</code>, the same instance is used for type <code>J</code> which is type <code>U</code> at runtime.
At injector level <code>E</code> there is an additional binding from type <code>U</code> to type <code>V</code> that overrules the binding declared at the root injector level.
With this binding, each places where <code>J</code> is declared, type <code>U</code> is used at runtime.</p>
</div>
<div class="paragraph">
<p>Furthermore, since <code>V</code> is declared as a singleton, both injector <code>F</code> and <code>G</code> are using a shared singleton instance of type <code>V</code>.
Finally, for type <code>V</code>, injector <code>C</code>, <code>D</code> and <code>E</code> should use a separate instance of <code>V</code> other than injector level <code>F</code> and <code>G</code> because <code>V</code> is preserved at injector level <code>F</code> with the <code>U</code> <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo></math> <code>V</code> binding.</p>
</div>
<table id="tab:diTransitiveBindings" class="tableblock frame-all grid-all spread">
<caption class="title">Table 13. DI Transitive Bindings</caption>
<colgroup>
<col style="width: 28.5714%;">
<col style="width: 14.2857%;">
<col style="width: 14.2857%;">
<col style="width: 14.2857%;">
<col style="width: 14.2857%;">
<col style="width: 14.2858%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-center valign-top">Binding</th>
<th class="tableblock halign-center valign-top">J &#8594; U</th>
<th class="tableblock halign-center valign-top"></th>
<th class="tableblock halign-center valign-top"></th>
<th class="tableblock halign-center valign-top">U &#8594; V</th>
<th class="tableblock halign-center valign-top"></th>
</tr>
</thead>
<tbody>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">Injector nesting (&gt;)</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock">C</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">D</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">E</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">F</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">G</p></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">J</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></p></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">U</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></p></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">V</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>1</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>1</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>1</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></p></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p><a href="#tab:diReBinding">DI Re - Binding</a> depicts the singleton behaviour but unlike the above
table, the bindings are declared for the interface <code>J</code>.</p>
</div>
<table id="tab:diReBinding" class="tableblock frame-all grid-all spread">
<caption class="title">Table 14. DI Re - Binding</caption>
<colgroup>
<col style="width: 28.5714%;">
<col style="width: 14.2857%;">
<col style="width: 14.2857%;">
<col style="width: 14.2857%;">
<col style="width: 14.2857%;">
<col style="width: 14.2858%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-center valign-top">Binding</th>
<th class="tableblock halign-center valign-top">J &#8594; U</th>
<th class="tableblock halign-center valign-top"></th>
<th class="tableblock halign-center valign-top"></th>
<th class="tableblock halign-center valign-top">J &#8594; V</th>
<th class="tableblock halign-center valign-top"></th>
</tr>
</thead>
<tbody>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">Injector nesting (<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>&gt;</mo></math>)</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock">C</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">D</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">E</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">F</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">G</p></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">J</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></p></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">U</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></p></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">V</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>1</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>1</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>1</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></p></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p><a href="#tab:diChildBinding">DI Child Binding</a> describes the singleton behavior when both bindings are configured at child injector levels but not the root injector level.</p>
</div>
<table id="tab:diChildBinding" class="tableblock frame-all grid-all spread">
<caption class="title">Table 15. DI Child Binding</caption>
<colgroup>
<col style="width: 28.5714%;">
<col style="width: 14.2857%;">
<col style="width: 14.2857%;">
<col style="width: 14.2857%;">
<col style="width: 14.2857%;">
<col style="width: 14.2858%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-center valign-top">Binding</th>
<th class="tableblock halign-center valign-top"></th>
<th class="tableblock halign-center valign-top">U <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo></math> V</th>
<th class="tableblock halign-center valign-top"></th>
<th class="tableblock halign-center valign-top">J <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo></math> U</th>
<th class="tableblock halign-center valign-top"></th>
</tr>
</thead>
<tbody>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">Injector nesting (<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>&gt;</mo></math>)</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock">C</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">D</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">E</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">F</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">G</p></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">J</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>N</mi><mi>a</mi><mi>N</mi></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>N</mi><mi>a</mi><mi>N</mi></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>N</mi><mi>a</mi><mi>N</mi></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></p></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">U</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>1</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>U</mi><mn>0</mn></msub></math></p></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">V</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>1</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>V</mi><mn>0</mn></msub></math></p></td>
</tr>
</tbody>
</table>
</div>
<div class="sect4">
<h5 id="_per-injection-chain-singleton"><a class="anchor" href="#_per-injection-chain-singleton"></a><a class="link" href="#_per-injection-chain-singleton">11.2.4.4. Per Injection Chain Singleton</a></h5>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>Assume a provider declared as</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var pb: Provider&lt;B&gt;;</code></pre>
</div>
</div>
<div class="paragraph">
<p>to be available:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">@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</code></pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">@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</code></pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">// 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</code></pre>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_validation-of-callsites-targeting-n4injector-methods"><a class="anchor" href="#_validation-of-callsites-targeting-n4injector-methods"></a><a class="link" href="#_validation-of-callsites-targeting-n4injector-methods">11.2.5. Validation of callsites targeting N4Injector methods</a></h4>
<div class="paragraph">
<p>Terminology for this section:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>a value is <strong>injectable</strong> if it</p>
<div class="ulist">
<ul>
<li>
<p>either conforms to a user-defined class or interface (a non-parameterized one, that is),</p>
</li>
<li>
<p>or conforms to Provider-of-T where T is injectable itself.</p>
</li>
</ul>
</div>
</li>
<li>
<p>a classifier declaring injected members is said to <strong>require injection</strong></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>To better understand the validations in effect for callsites targeting</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">N4Injector.of(ctorOfDIC: constructor{N4Object}, parentDIC: N4Injector?, ...providedBinders: N4Object)</code></pre>
</div>
</div>
<div class="paragraph">
<p>we can recap that at runtime:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The first argument denotes a DIC constructor.</p>
</li>
<li>
<p>The second (optional) argument is an injector.</p>
</li>
<li>
<p>Lastly, the purpose of <code>providedBinders</code> is as follows:</p>
<div class="ulist">
<ul>
<li>
<p>The DIC above is marked with one or more <code>@UseBinder</code>.</p>
</li>
<li>
<p>Some of those binders may require injection.</p>
</li>
<li>
<p>Some of those binders may have constructor(s) taking parameters.</p>
</li>
<li>
<p>The set of binders described above should match the providedBinders.</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>Validations in effect for <code>N4Injector.create(type{T} ctor)</code> callsites:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>type{T}</code> should be injectable (in particular, it may be an <code>N4Provider</code>).</p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="_n4js-di-annotations"><a class="anchor" href="#_n4js-di-annotations"></a><a class="link" href="#_n4js-di-annotations">11.2.6. N4JS DI Annotations</a></h4>
<div class="paragraph">
<p>Following annotations describe API used to configure N4JSDI.</p>
</div>
<div class="sect4">
<h5 id="_n4js-di-generateinjector"><a class="anchor" href="#_n4js-di-generateinjector"></a><a class="link" href="#_n4js-di-generateinjector">11.2.6.1. N4JS DI @GenerateInjector</a></h5>
<table class="tableblock frame-none grid-none spread">
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
<col style="width: 33.3334%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">name</dt>
<dd>
<p>@GenerateInjector</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">targets</dt>
<dd>
<p>N4Class</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">retention policy</dt>
<dd>
<p>RUNTIME</p>
</dd>
</dl>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">transitive</dt>
<dd>
<p>NO</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">repeatable</dt>
<dd>
<p>NO</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">arguments</dt>
<dd>
<p>NO</p>
</dd>
</dl>
</div></div></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p><code>@GenerateInjector</code> 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.</p>
</div>
</div>
<div class="sect4">
<h5 id="_n4js-di-withparentinjector"><a class="anchor" href="#_n4js-di-withparentinjector"></a><a class="link" href="#_n4js-di-withparentinjector">11.2.6.2. N4JS DI @WithParentInjector</a></h5>
<table class="tableblock frame-none grid-none spread">
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
<col style="width: 33.3334%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">name</dt>
<dd>
<p>@WithParentInjector</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">targets</dt>
<dd>
<p>N4Class</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">retention policy</dt>
<dd>
<p>RUNTIME</p>
</dd>
</dl>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">transitive</dt>
<dd>
<p>NO</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">repeatable</dt>
<dd>
<p>NO</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">arguments</dt>
<dd>
<p>TypeRef</p>
</dd>
</dl>
</div></div></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p><code>@WithParentInjector</code> marks given <em>injector</em> as depended on other <em>injector</em>.
The depended <em>injector</em> may use provided <em>injector</em> to create instances of objects required in its object graph.</p>
</div>
<div class="paragraph">
<p>Additional <em>WithParentInjector</em> constraints:</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-145"></a><strong>Req. IDE-145:</strong> <a href="#Req-IDE-145">DI WithParentInjector</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Allowed only on <code>N4ClassDeclarations</code> annotated with <code>@GenerateInjector</code>.</p>
</li>
<li>
<p>Its parameter can only be <code>N4ClassDeclarations</code> annotated with .</p>
</li>
</ol>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_n4js-di-usebinder"><a class="anchor" href="#_n4js-di-usebinder"></a><a class="link" href="#_n4js-di-usebinder">11.2.6.3. N4JS DI @UseBinder</a></h5>
<table class="tableblock frame-none grid-none spread">
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
<col style="width: 33.3334%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">name</dt>
<dd>
<p>@UseBinder</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">targets</dt>
<dd>
<p>N4Class</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">retention policy</dt>
<dd>
<p>RUNTIME</p>
</dd>
</dl>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">transitive</dt>
<dd>
<p>NO</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">arguments</dt>
<dd>
<p>TypeRef</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">arguments are optional</dt>
<dd>
<p>NO</p>
</dd>
</dl>
</div></div></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p><code>@UseBinder</code> describes <em>Binder</em> to be used (configure) target <em>Injector</em>.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-146"></a><strong>Req. IDE-146:</strong> <a href="#Req-IDE-146">DI UseInjector</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Allowed only on <code>N4ClassDeclarations</code> annotated with <code>@GenerateInjector</code>.</p>
</li>
<li>
<p>Its parameter can only be <code>N4ClassDeclarations</code> annotated with <code>@Binder</code>.</p>
</li>
</ol>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_n4js-di-binder"><a class="anchor" href="#_n4js-di-binder"></a><a class="link" href="#_n4js-di-binder">11.2.6.4. N4JS DI @Binder</a></h5>
<table class="tableblock frame-none grid-none spread">
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
<col style="width: 33.3334%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">name</dt>
<dd>
<p>@Binder</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">targets</dt>
<dd>
<p>N4Class</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">retention policy</dt>
<dd>
<p>RUNTIME</p>
</dd>
</dl>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">transitive</dt>
<dd>
<p>NO</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">repeatable</dt>
<dd>
<p>NO</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">arguments</dt>
<dd>
<p>NONE</p>
</dd>
</dl>
</div></div></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p><code>@Binder</code> defines a list of bind configurations.
That can be either <code>@Bind</code> annotations on <code>@Binder</code> itself or its factory methods annotated with <code>@Provides</code>.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-147"></a><strong>Req. IDE-147:</strong> <a href="#Req-IDE-147">DI binder</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Target <code>N4ClassDeclaration</code> must not be <em>abstract</em>.</p>
</li>
<li>
<p>Target <code>N4ClassDeclaration</code> must not be annotated with <code>@GenerateInjector</code>.</p>
</li>
<li>
<p>Target class cannot have <em>injection points</em>.</p>
</li>
</ol>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_n4js-di-bind"><a class="anchor" href="#_n4js-di-bind"></a><a class="link" href="#_n4js-di-bind">11.2.6.5. N4JS DI @Bind</a></h5>
<table class="tableblock frame-none grid-none spread">
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
<col style="width: 33.3334%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">name</dt>
<dd>
<p>@Bind</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">targets</dt>
<dd>
<p>N4ClassDeclaration</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">retention policy</dt>
<dd>
<p>RUNTIME</p>
</dd>
</dl>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">transitive</dt>
<dd>
<p>NO</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">arguments</dt>
<dd>
<p>TypeRef key, TypeRef target</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">arguments are optional</dt>
<dd>
<p>NO</p>
</dd>
</dl>
</div></div></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>Defines <em>binding</em> between type and subtype that will be used by injector when configured with target <a href="#_n4js-di-binder">N4JS DI @Binder</a>.
See also <a href="#_validation-of-callsites-targeting-n4injector-methods">Validation of callsites targeting N4Injector methods</a> for description of injectable types.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-148"></a><strong>Req. IDE-148:</strong> <a href="#Req-IDE-148">DI Bind</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Allowed only on <code>N4ClassDeclarations</code> that are annotated with <code>@Binder</code>(<a href="#_n4js-di-binder">N4JS DI @Binder</a>).</p>
</li>
<li>
<p>Parameters are instances of one of the values described in <a href="#_validation-of-callsites-targeting-n4injector-methods">Validation of callsites targeting N4Injector methods</a>.</p>
</li>
<li>
<p>The second parameter must be a subtype of the first one.</p>
</li>
</ol>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_n4js-di-provides"><a class="anchor" href="#_n4js-di-provides"></a><a class="link" href="#_n4js-di-provides">11.2.6.6. N4JS DI @Provides</a></h5>
<table class="tableblock frame-none grid-none spread">
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
<col style="width: 33.3334%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">name</dt>
<dd>
<p>@Provides</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">targets</dt>
<dd>
<p>N4MethodDeclaration</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">retention policy</dt>
<dd>
<p>RUNTIME</p>
</dd>
</dl>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">transitive</dt>
<dd>
<p>NO</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">repeatable</dt>
<dd>
<p>NO</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">arguments</dt>
<dd>
<p>NONE</p>
</dd>
</dl>
</div></div></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p><code>@Provides</code> marks <em>factory method</em> to be used as part <a href="#AC">DI</a>.
This is treated as <em>explicit binding</em> between declared return type and actual return type.
This method is expected to be part of the <code>@Binder</code>.
Can be used to implement custom scopes.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-149"></a><strong>Req. IDE-149:</strong> <a href="#Req-IDE-149">DI Provides</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Allowed only on <code>N4MethodDeclarations</code> that are part of a classifier annotated with <code>@Binder</code>.</p>
</li>
<li>
<p>Annotated method declared type returns instance of one of the types described in <em>injectable values</em> <a href="#_validation-of-callsites-targeting-n4injector-methods">Validation of callsites targeting N4Injector methods</a>.</p>
</li>
</ol>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_n4js-di-inject"><a class="anchor" href="#_n4js-di-inject"></a><a class="link" href="#_n4js-di-inject">11.2.6.7. N4JS DI @Inject</a></h5>
<table class="tableblock frame-none grid-none spread">
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
<col style="width: 33.3334%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">name</dt>
<dd>
<p>@Inject</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">targets</dt>
<dd>
<p>N4Field, N4Method, constructor</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">retention policy</dt>
<dd>
<p>RUNTIME</p>
</dd>
</dl>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">transitive</dt>
<dd>
<p>NO</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">repeatable</dt>
<dd>
<p>NO</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">arguments</dt>
<dd>
<p>NO</p>
</dd>
</dl>
</div></div></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p><code>@Inject</code> 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 <a href="#_injection-points">Injection Points</a> for more information.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-150"></a><strong>Req. IDE-150:</strong> <a href="#Req-IDE-150">DI Inject</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Injection point bindings need to be resolvable.</p>
</li>
<li>
<p>Binding for given type must not be duplicated.</p>
</li>
<li>
<p>Annotated types must be instances of one of the types described in <a href="#_validation-of-callsites-targeting-n4injector-methods">Validation of callsites targeting N4Injector methods</a>.</p>
</li>
</ol>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_n4js-di-singleton"><a class="anchor" href="#_n4js-di-singleton"></a><a class="link" href="#_n4js-di-singleton">11.2.6.8. N4JS DI @Singleton</a></h5>
<table class="tableblock frame-none grid-none spread">
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
<col style="width: 33.3334%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">name</dt>
<dd>
<p>@Singleton</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">targets</dt>
<dd>
<p>N4Class</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">retention policy</dt>
<dd>
<p>RUNTIME</p>
</dd>
</dl>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">transitive</dt>
<dd>
<p>NO</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">repeatable</dt>
<dd>
<p>NO</p>
</dd>
</dl>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="dlist">
<dl>
<dt class="hdlist1">arguments</dt>
<dd>
<p>NO</p>
</dd>
</dl>
</div></div></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>In the case of annotating a class <code>S</code> with <code>@Singleton</code> on the definition-site, the singleton scope will be used as described in <a href="#_singleton-scope">Singleton Scope</a>.</p>
</div>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_test-support"><a class="anchor" href="#_test-support"></a><a class="link" href="#_test-support">11.3. Test Support</a></h3>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>In order to enable tests for private methods, test projects may define which project they are testing.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-151"></a><strong>Req. IDE-151:</strong> <a href="#Req-IDE-151">Test API methods and types</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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 <code>@TestAPI</code>.
There are no constraints defined for that annotation at the moment.</p>
</div>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_polyfill-definitions"><a class="anchor" href="#_polyfill-definitions"></a><a class="link" href="#_polyfill-definitions">11.4. Polyfill Definitions</a></h3>
<div class="paragraph">
<p>In plain JavaScript, so called <em>polyfill</em> (or sometimes called <em>shim</em>) 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 <code>@Polyfill</code> or <code>@StaticPolyfill</code>.
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 <code>@Final</code>.</p>
</div>
<div class="paragraph">
<p>We distinguish two flavours of polyfill classes: runtime and static.</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Runtime polyfilling covers type enrichment for runtime libraries.
For type modifications the annotation <code>@Polyfill</code> is used.</p>
</li>
<li>
<p>Static polyfilling covers code modifications for adapting generated code.
The annotation <code>@StaticPolyfill</code> denotes a polyfill in ordinary code, which usually provides executable implementations.</p>
</li>
</ul>
</div>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="polyfill_class"></a><strong>Definition:</strong> <a href="#polyfill_class">Polyfill Class</a></p>
</div>
<div class="paragraph">
<p>A <em>polyfill class</em> (or simply <em>polyfill</em>) 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 <em>filled</em> class and the modification <em>filling</em>.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-152"></a><strong>Req. IDE-152:</strong> <a href="#Req-IDE-152">Polyfill Class</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a polyfill class <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi></math> annotated with <code>@Polyfill</code> or <code>@StaticPolyfill</code>, 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:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><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:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p><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):<br></p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>Both the polyfill and filled class must be top-level declarations (i.e., no class expression):<br></p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi></math> must not implement any interfaces:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p><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:<br></p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>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:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p><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):</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>All constraints related to member redefinition (cf. <a href="#_redefinition-of-members">Redefinition of Members</a>) have to hold.
In the case of polyfills, this is true for constructors (cf. <a href="#Req-IDE-72">[Req-IDE-72]</a>) and private members.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_runtime-polyfill-definitions"><a class="anchor" href="#_runtime-polyfill-definitions"></a><a class="link" href="#_runtime-polyfill-definitions">11.4.1. Runtime Polyfill Definitions</a></h4>
<div class="paragraph">
<p>(Runtime) Libraries often do not provide completely new types but modify existing types.
The ECMA-402 Internationalization Standard [<a href="#ECMA12a">ECMA12a</a>], for example, changes methods of the built-in class <code>Date</code> to be timezone aware.
Other scenarios include new functionality provided by browsers which are not part of an official standard yet.
Even ECMAScript 6 [<a href="#ECMA15a">ECMA15a</a>] extends the predecessor [<a href="#ECMA11a">ECMA11a</a>] in terms of new methods (or new method parameters) added to existing types (it also adds completely new classes and features, of course).</p>
</div>
<div class="paragraph">
<p>Runtime polyfills are only applicable to runtime libraries or environments and thus are limited to n4jsd files.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-153"></a><strong>Req. IDE-153:</strong> <a href="#Req-IDE-153">Runtime Polyfill Class</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a runtime-polyfill class <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi></math> annotated with <code>@Polyfill</code>, 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 <a href="#Req-IDE-152">[Req-IDE-152]</a>:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Both the polyfill and filled class are provided by the runtime
(annotated with <code>@ProvidedByRuntime</code>): <sup class="footnote">[<a id="_footnoteref_59" class="footnote" href="#_footnote_59" title="View footnote.">59</a>]</sup></p>
</li>
</ol>
</div>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-154"></a><strong>Req. IDE-154:</strong> <a href="#Req-IDE-154">Applying Polyfills</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>No member must be filled by more than one polyfill.</p>
</li>
</ol>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_static-polyfill-definitions"><a class="anchor" href="#_static-polyfill-definitions"></a><a class="link" href="#_static-polyfill-definitions">11.4.2. Static Polyfill Definitions</a></h4>
<div class="paragraph">
<p>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 <a href="#_runtime-polyfill-definitions">Runtime Polyfill Definitions</a> 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.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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 <code>@StaticPolyfillAware</code>.
The filling types must also be annotated with <code>@StaticPolyfill</code> and be contained in a different module with same specifier but annotated with <code>@StaticPolyfillModule</code>.
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.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>In order to support efficient transpilation, the following constraint must hold in addition to constraints:</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-155"></a><strong>Req. IDE-155:</strong> <a href="#Req-IDE-155">Static Polyfill Layout</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a static polyfill class <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>P</mi></math> annotated with <code>@StaticPolyfill</code>, 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 <a href="#Req-IDE-152">[Req-IDE-152]</a>:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><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:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>Both the static polyfill and the filled class are part of the same project:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>The filled class must be contained in a module annotated with <code>@StaticPolyfillAware</code>:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>The static polyfill and the filled type must both be declared in an n4js file or both in an n4jsd file.</p>
</li>
<li>
<p>The filling class must be contained in a module annotated with <code>@StaticPolyfillModule</code>:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>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:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
</ol>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-156"></a><strong>Req. IDE-156:</strong> <a href="#Req-IDE-156">Restrictions on static polyfilling</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>All top-level elements are static polyfills:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
<li>
<p>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.</p>
</li>
<li>
<p>It is an error if two static polyfill modules for the same filled module exist in the same project:</p>
<div class="openblock">
<div class="content">
<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>
</div>
</div>
</li>
</ol>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 102. Static polyfill</div>
<div class="content">
<div class="paragraph">
<p><a href="#ex:staticpolyfill-genmod">Static Polyfill, Genmod</a> shows an example of generated code.
<a href="#ex:staticpolyfill-polyfillmod">Static Polyfill, Polyfillmod</a> demonstrates the static polyfill.</p>
</div>
<div class="paragraph">
<p>Note that the containing project has two source folders configured:<br>
<code>Project/src/n4js</code> and <code>Project/src/n4jsgen</code>.</p>
</div>
<div id="ex:staticpolyfill-polyfillmod" class="listingblock">
<div class="title">Static Polyfill, Polyfillmod</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">@@StaticPolyfillAware
export public class A {
constructor() {...}
m1(): void{...}
}
export public class B {
constructor() {...}
m2(): void{...}
}</code></pre>
</div>
</div>
<div id="ex:staticpolyfill-genmod" class="listingblock">
<div class="title">Static Polyfill, Genmod</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">@@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
}</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_transpiling-static-polyfilled-classes"><a class="anchor" href="#_transpiling-static-polyfilled-classes"></a><a class="link" href="#_transpiling-static-polyfilled-classes">11.4.3. Transpiling static polyfilled classes</a></h4>
<div class="paragraph">
<p>Transpiling static polyfilled classes encounters the special case that two different <code>n4js</code> 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:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Current file to transpile is <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi></math></p>
</li>
<li>
<p>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</p>
<div class="ulist">
<ul>
<li>
<p>search for a second file <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>G</mi></math> with same qualified name:<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></p>
</li>
<li>
<p>If <math xmlns="http://www.w3.org/1998/Math/MathML"><mo></mo><mi>G</mi></math>, then</p>
<div class="ulist">
<ul>
<li>
<p>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></p>
</li>
<li>
<p>conventionally transpile <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>M</mi><mi>'</mi></msup></math></p>
</li>
</ul>
</div>
</li>
<li>
<p>else conventionally transpile <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi></math></p>
</li>
</ul>
</div>
</li>
<li>
<p>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>,</p>
<div class="ulist">
<ul>
<li>
<p>then <em>do nothing</em>. (Transpilation will be triggered for filled type separately.)</p>
</li>
</ul>
</div>
</li>
<li>
<p>else, conventionally transpile <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi></math></p>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_components"><a class="anchor" href="#_components"></a><a class="link" href="#_components">12. Components</a></h2>
<div class="sectionbody">
<div class="sect2 language-n4js">
<h3 id="_overview-2"><a class="anchor" href="#_overview-2"></a><a class="link" href="#_overview-2">12.1. Overview</a></h3>
<div class="paragraph">
<p>For modularization purposes, N4JS code is managed in so-called <em>components</em>. These components correspond to what
node and npm call <em>packages</em>, what OSGi calls <em>bundle</em>, and what Eclipse calls <em>project</em>.</p>
</div>
<div class="paragraph">
<p>N4JS distinguishes several types of such components. <a href="#fig-cmpd_components_in_n4js">Overview of N4JS Components (OLD)</a> shows the N4JS component types
described in detail in this chapter. <a href="#fig-cmp_components">Overview of N4JS Components and Dependencies (NEW)</a> shows a recently updated diagram, which is not yet fully
reflected in the implementation.</p>
</div>
<div id="fig-cmpd_components_in_n4js" class="imageblock" style="text-align: center">
<div class="content">
<img src="chapters/12_components/fig/cmpd_components_in_n4js.svg" alt="cmpd components in n4js">
</div>
<div class="title">Figure 10. Overview of N4JS Components (OLD)</div>
</div>
<div id="fig-cmp_components" class="imageblock" style="text-align: center">
<div class="content">
<img src="chapters/12_components/fig/cmp_components.svg" alt="cmp components">
</div>
<div class="title">Figure 11. Overview of N4JS Components and Dependencies (NEW)</div>
</div>
<div class="paragraph">
<p>The following types of components can only be created by internal developers:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1">Runtime Environment</dt>
<dd>
<p>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).</p>
</dd>
<dt class="hdlist1">Runtime Library</dt>
<dd>
<p>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.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>The following components can be created by external developers:</p>
</div>
<div id="App" class="dlist">
<dl>
<dt class="hdlist1">App</dt>
<dd>
<p>User-written N4JS applications.</p>
</dd>
<dt class="hdlist1">Processor</dt>
<dd>
<p>User-written N4JS code running server-side on the N4 platform. Not implemented yet.</p>
</dd>
<dt class="hdlist1">Library</dt>
<dd>
<p>User-written libraries used by apps, processors, or other libraries.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>The available component types are described in more detail in <a href="#Component_Types">Component Types</a>.</p>
</div>
<div class="paragraph">
<p>A component corresponds to a single project in the N4JS IDE. Generally, it contains the following:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1">Package.json File</dt>
<dd>
<p>The <code>package.json</code> file describing the contents, dependencies and metadata.</p>
</dd>
<dt class="hdlist1">Resources</dt>
<dd>
<p>Resources such as images, data files etc.</p>
</dd>
<dt class="hdlist1">Sources</dt>
<dd>
<p>Source files containing either N4JS code or plain Javascript. The actual code used in a project.</p>
</dd>
<dt class="hdlist1">Output Code</dt>
<dd>
<p>Compiled and possibly adjusted (e.g. minified, concatenated) versions of the N4JS source files and
plain Javascript files.</p>
</dd>
<dt class="hdlist1">Tests</dt>
<dd>
<p>Optional test sources and compiled output code.</p>
</dd>
<dt class="hdlist1">Source Maps</dt>
<dd>
<p>Optional source maps.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>Components contain modules. <a href="#fig-component_content">Content of a Component</a> describes what can be contained in a component.</p>
</div>
<div id="fig-component_content" class="imageblock" style="text-align: center">
<div class="content">
<img src="chapters/12_components/fig/cmpd_component_content.svg" alt="cmpd component content">
</div>
<div class="title">Figure 12. Content of a Component</div>
</div>
<div class="paragraph">
<p>At both compile time and runtime, all components defined as dependency have to be available. Since dependencies
are defined in <code>package.json</code> files in a form compliant to node/npm, this can be fully managed by npm (or yarn).</p>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="Component_Types"><a class="anchor" href="#Component_Types"></a><a class="link" href="#Component_Types">12.2. Component Types</a></h3>
<div class="paragraph">
<p>Different N4JS component types are described in this section.</p>
</div>
<div class="sect3">
<h4 id="_libraries"><a class="anchor" href="#_libraries"></a><a class="link" href="#_libraries">12.2.1. Libraries</a></h4>
<div class="paragraph">
<p>A library is a user project providing modules with declaration.</p>
</div>
</div>
<div class="sect3">
<h4 id="_runtime-environment-and-runtime-libraries"><a class="anchor" href="#_runtime-environment-and-runtime-libraries"></a><a class="link" href="#_runtime-environment-and-runtime-libraries">12.2.2. Runtime Environment and Runtime Libraries</a></h4>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
runtime environments are not fully implemented at this time (see GH-1291).
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>Runtime environments and libraries define globally available elements (types, variables, functions) provided by the JavaScript engine.
Both must contain <em>only</em> definition files (n4jsd) of which all elements are marked as <code>@ProvidedByRuntime</code> (<a href="#_runtime-definitions">Runtime Definitions</a>) and <code>@Global</code> (<a href="#_global-definitions">Global Definitions</a>).</p>
</div>
<div class="paragraph">
<p>Other projects may refer to <em>multiple</em> runtime libraries in their <code>package.json</code> file via the property <a href="#package-json-requiredRuntimeLibraries">requiredRuntimeLibraries</a>.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>Typical runtime environments are ES5 or ES6, typical runtime libraries are DOM or HTML.</p>
</div>
<div class="paragraph">
<p>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 <em>polyfil</em>.
Other adaptations, such as enabling ECMSScript 6 object types in ECMAScript 5 environments, are known as <em>shim</em>.
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.</p>
</div>
</div>
<div class="sect3">
<h4 id="_tests"><a class="anchor" href="#_tests"></a><a class="link" href="#_tests">12.2.3. Tests</a></h4>
<div class="paragraph">
<p>Tests are special projects which contain tests for other projects.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-157"></a><strong>Req. IDE-157:</strong> <a href="#Req-IDE-157">Test Project</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Tests have full access to the tested project including elements with <code>project</code> visibility.</p>
</li>
<li>
<p>Only other test projects can depend on tests project.
In other words, other components must not depend on test components.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>In a test project, the tested projects can be specified via <code>testee</code>.</p>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_type-definitions"><a class="anchor" href="#_type-definitions"></a><a class="link" href="#_type-definitions">12.2.4. Type Definitions</a></h4>
<div class="paragraph">
<p>Projects of type "definition" are special projects which only provide type information for another so-called <em>implementation project</em>, which only provides executable JS files.</p>
</div>
<div class="paragraph">
<p>Generally, client projects that depend on a given <em>implementation project</em> 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.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-GH-821002"></a><strong>Req. GH-821002:</strong> <a href="#Req-GH-821002">Type Definition Project Configuration</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For type definition projects, the following constraints must hold true with regard to their project configuration:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>They must declare their <em>implementation project</em> via the <a href="#package-json-definesPackage">definesPackage</a> property in their <code>package.json</code> file.</p>
</li>
<li>
<p>They must not declare an output folder.</p>
</li>
</ol>
</div>
</div>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="package-json"><a class="anchor" href="#package-json"></a><a class="link" href="#package-json">12.3. Package.json File</a></h3>
<div class="paragraph">
<p>A folder is a "component" if and only if it contains a <code>package.json</code> 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.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="sect3">
<h4 id="_basic-properties"><a class="anchor" href="#_basic-properties"></a><a class="link" href="#_basic-properties">12.3.1. Basic Properties</a></h4>
<div class="paragraph">
<p>The following standard <code>package.json</code> properties are used by N4JS tooling. Unless otherwise
noted, all these properties have the exact same format and meaning as usual in <code>package.json</code>
files.</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1">name </dt>
<dd>
<p>Used as the globally unique identifier of the component.</p>
</dd>
<dt class="hdlist1">version </dt>
<dd>
<p>The component&#8217;s version.</p>
</dd>
</dl>
</div>
<div id="package-json-dependencies" class="dlist">
<dl>
<dt class="hdlist1">dependencies </dt>
<dd>
<p>List of components required at runtime and compile time.</p>
</dd>
</dl>
</div>
<div id="package-json-devDependencies" class="dlist">
<dl>
<dt class="hdlist1">devDependencies </dt>
<dd>
<p>List of components required at compile time only.</p>
</dd>
<dt class="hdlist1">main </dt>
<dd>
<p>Path relative to the component&#8217;s root folder, pointing to a <code>.js</code> file
located in a source container (the <code>.js</code> 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 <code>index.js</code> located in that folder. If this property denotes a file
other than a <code>.js</code> file, it will be ignored. In particular, it cannot be
used for <code>.n4js</code> files; in that case, property "mainModule" has to be used
(see below).</p>
</dd>
<dt class="hdlist1">workspaces </dt>
<dd>
<p>(array of strings) Property used by package management tool <code>yarn</code> 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
<a href="https://yarnpkg.com/lang/en/docs/workspaces">here</a>.
In N4JS, a project is called a "yarn workspace root" if and only if its
<code>package.json</code> 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.</p>
</dd>
</dl>
</div>
</div>
<div class="sect3">
<h4 id="_n4js-properties"><a class="anchor" href="#_n4js-properties"></a><a class="link" href="#_n4js-properties">12.3.2. N4JS Properties</a></h4>
<div class="paragraph">
<p>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:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1">projectType</dt>
<dd>
<p>(string) Must be one of the following strings:</p>
<div class="dlist">
<dl>
<dt class="hdlist1">application</dt>
<dd>
<p>An application. See <a href="#App">[App]</a>.</p>
</dd>
<dt class="hdlist1">library</dt>
<dd>
<p>A library. See <a href="#_libraries">Libraries</a>.</p>
</dd>
<dt class="hdlist1">processor</dt>
<dd>
<p>For processors running server-side on the N4 platform. Not implemented yet.</p>
</dd>
<dt class="hdlist1">test</dt>
<dd>
<p>An N4JS project containing tests for one or more other N4JS projects specified
via property "testedProjects".</p>
</dd>
<dt class="hdlist1">api</dt>
<dd>
<p>For N4JS projects that contain only API (in <code>.n4jsd</code> 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).</p>
</dd>
<dt class="hdlist1">runtimeEnvironment</dt>
<dd>
<p>Runtime environments. See <a href="#Runtime Environment Resolution">[Runtime Environment Resolution]</a>.
NOTE: runtime environments are not fully implemented at this time (see GH-1291).</p>
</dd>
<dt class="hdlist1">runtimeLibrary</dt>
<dd>
<p>Runtime libraries. See <a href="#_runtime-environment-and-runtime-libraries">Runtime Environment and Runtime Libraries</a>.</p>
</dd>
<dt class="hdlist1">validation</dt>
<dd>
<p>A project in which <code>.n4js</code> files are only being validated, not transpiled. This is used for projects
that are implemented in terms of <code>.js</code> files but that also provide type information in terms of <code>.n4jsd</code> files.</p>
</dd>
<dt class="hdlist1">plainjs</dt>
<dd>
<p>A project which only contains <code>.js</code> 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.</p>
</dd>
</dl>
</div>
</dd>
<dt class="hdlist1">vendorId</dt>
<dd>
<p>(string) Globally unique identifier for the component&#8217;s vendor.
Used for the <code>@Internal</code> accessibility modifier.</p>
</dd>
<dt class="hdlist1">vendorName</dt>
<dd>
<p>(string) Human-readable name of the component&#8217;s vendor. Used only for informational
purposes.</p>
</dd>
<dt class="hdlist1">output</dt>
<dd>
<p>(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 <code>.js</code> files created for each <code>.n4js</code> file.</p>
</dd>
<dt class="hdlist1">sources</dt>
<dd>
<p>(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 <code>.n4js</code> source files to be compiled.</p>
</dd>
<dt class="hdlist1">moduleFilters</dt>
<dd>
<p>(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.</p>
<div class="paragraph">
<p>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).</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1">noValidate</dt>
<dd>
<p>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.</p>
</dd>
</dl>
</div>
</dd>
</dl>
</div>
<div class="exampleblock">
<div class="title">Example 103. Module Filters</div>
<div class="content">
<div class="paragraph">
<p>A simple and a source-container-specific module filter in the <code>n4js</code> section of a package.json file.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-json" data-lang="json">"moduleFilters": {
"noValidate": [
"abc*",
{
"module": "xyz*",
"sourceContainer": "src/n4js"
}
]
}</code></pre>
</div>
</div>
</div>
</div>
<div id="package-json-mainModule" class="dlist">
<dl>
<dt class="hdlist1">mainModule</dt>
<dd>
<p>(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 <code>from</code> states only the project name and not the complete module specifier (see <a href="#import-statement-semantics">Import Statement Semantics</a>).
If this property is defined, top-level property <code>main</code> will be ignored.</p>
</dd>
<dt class="hdlist1">testedProjects</dt>
<dd>
<p> (array) List of N4JS components being tested by this project.
<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.</p>
</dd>
</dl>
</div>
<div id="package-json-implementationId" class="dlist">
<dl>
<dt class="hdlist1">implementationId</dt>
<dd>
<p>(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 <a href="#API and Implementation Components">[API and Implementation Components]</a>.</p>
<div class="paragraph">
<p>Only projects of type "application", "processor", "library", "api" or "validation" may declare this property.</p>
</div>
</dd>
</dl>
</div>
<div id="package-json-implementedProjects" class="dlist">
<dl>
<dt class="hdlist1">implementedProjects</dt>
<dd>
<p>(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
<a href="#API and Implementation Components">[API and Implementation Components]</a>. Only components of type "application", "processor", "library", "api"
or "validation" may declare this property.</p>
</dd>
</dl>
</div>
<div id="package-json-requiredRuntimeLibraries" class="dlist">
<dl>
<dt class="hdlist1">requiredRuntimeLibraries</dt>
<dd>
<p>(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 <code>dependencies</code> or <code>devDependencies</code>.</p>
</dd>
<dt class="hdlist1">extendedRuntimeEnvironment</dt>
<dd>
<p>(string) The name of the runtime environment project that is extended by this component. Only components of
type "runtime environment" may declare this property.</p>
</dd>
<dt class="hdlist1">providedRuntimeLibraries</dt>
<dd>
<p>(array) The list of runtime library components that are provided by this component. Only components of
type "runtime environment" may declare this property.</p>
</dd>
</dl>
</div>
<div id="package-json-definesPackage" class="dlist">
<dl>
<dt class="hdlist1">definesPackage</dt>
<dd>
<p>(string) The name of the package this component provides type definitions for. Only components of project type "definition" may declare this property.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>All properties described above are optional. The following default values apply:</p>
</div>
<table class="tableblock frame-all grid-all spread">
<colgroup>
<col style="width: 50%;">
<col style="width: 50%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Property</strong></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Default Value</strong></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">name</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">name of the folder containing the <code>package.json</code> file</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">version</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">"0.0.1"</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">projectType</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">"plainjs"</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">vendorId</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">"vendor.default"</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">mainModule</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">"index"</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">output</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">"."</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">sources</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">a single source-container of type "source" with path "." (except for yarn workspace roots, see below)</p></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>All other properties are undefined if not given in the <code>package.json</code> 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.</p>
</div>
<div class="exampleblock">
<div class="title">Example 104. A package.json file with N4JS-specific properties.</div>
<div class="content">
<div class="paragraph">
<p>The following example illustrates how to use the N4JS-related package.json properties.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">{
"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"
]
}
}</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_constraints"><a class="anchor" href="#_constraints"></a><a class="link" href="#_constraints">12.3.3. Constraints</a></h4>
<div class="paragraph">
<p>The following constraints apply.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-158"></a><strong>Req. IDE-158:</strong> <a href="#Req-IDE-158">GeneralConstraints</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>There must be an output directory specified so the compiler(s) can run.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-159"></a><strong>Req. IDE-159:</strong> <a href="#Req-IDE-159">Paths</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>Paths Paths are constrained in the following way:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>A path cannot appear more than one time within a source fragment type (same applies to paths in the resources section).</p>
</li>
<li>
<p>A path cannot be used in different source fragment types at same times.</p>
</li>
<li>
<p>A path can only be declared exclusively in one of the sections Output, Libraries, Resources or Sources.</p>
</li>
<li>
<p>A path must not contain wild cards.</p>
</li>
<li>
<p>A path has to be relative to the project path.</p>
</li>
<li>
<p>A path has to point to folder.</p>
</li>
<li>
<p>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).</p>
</li>
</ol>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-160"></a><strong>Req. IDE-160:</strong> <a href="#Req-IDE-160">Module Specifiers</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>Module Specifiers are constrained in the following
way:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Within a module filter type no duplicate specifiers are allowed.</p>
</li>
<li>
<p>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</p>
</li>
<li>
<p>A module specifier can be constrained to be applied only to a certain source container.</p>
</li>
<li>
<p>A module specifier is allowed to contain wildcards but it must resolve to some existing files in the project</p>
</li>
</ol>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-161"></a><strong>Req. IDE-161:</strong> <a href="#Req-IDE-161">Module Specifier Wildcard Constraints</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>All path patterns are case sensitive.</p>
</li>
<li>
<p><code>**</code> all module specifiers will be matched.</p>
</li>
<li>
<p><code>**/*</code> all module specifiers will be matched.</p>
</li>
<li>
<p><code>test/A??</code> matches all module specifiers whose qualified name consists of two segments where the first part matches test and the second part starts with an <code>A</code> and then two more characters.</p>
</li>
<li>
<p><code>**/test/**/XYZ</code> - matches all module specifiers whose qualified name contains a segment that matches test and the last segment ends with an ’XYZ’.</p>
</li>
<li>
<p>A module specifier wild card isn’t allowed to contain <code>***</code>.</p>
</li>
<li>
<p>A module specifier wild card isn’t allowed to contain relative navigation.</p>
</li>
<li>
<p>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).</p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>Examples of using external source fragments and filters are given in <a href="#_implementation-of-external-declarations">Implementation of External Declarations</a>, see <a href="#external-definitions-and-implementations">External Definitions and Their Implementations</a>.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-GH-821001"></a><strong>Req. GH-821001:</strong> <a href="#Req-GH-821001">Dependencies to Definition Projects</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>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.</p>
</li>
</ol>
</div>
</div>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_support-for-npm-scopes"><a class="anchor" href="#_support-for-npm-scopes"></a><a class="link" href="#_support-for-npm-scopes">12.4. Support for NPM Scopes</a></h3>
<div class="paragraph">
<p>NPM supports a namespace concept for npm packages. Such namespaces are called "scopes". For details see
<a href="https://docs.npmjs.com/misc/scope" class="bare">https://docs.npmjs.com/misc/scope</a> and <a href="https://docs.npmjs.com/getting-started/scoped-packages" class="bare">https://docs.npmjs.com/getting-started/scoped-packages</a>.
In N4JS, this is supported too.</p>
</div>
<div class="paragraph">
<p>Terminology:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>A project&#8217;s <em>plain project name</em> is its name without mentioning the project&#8217;s scope (if any),
e.g. <code>myProject</code>.</p>
</li>
<li>
<p>A project&#8217;s <em>scope name</em> is the name of the npm scope a project resides in, including a leading <code>@</code>.
E.g. <code>@myScope</code>.</p>
</li>
<li>
<p>A project&#8217;s <em>N4JS project name</em> is its plain project name, prefixed by its scope name (if any),
separated by a <code>/</code>. For unscoped projects, this is identical to the plain project name.
E.g., <code>myProject</code> (if unscoped), <code>@myScope/myProject</code> (if scoped).</p>
</li>
<li>
<p>A project&#8217;s <em>Eclipse project name</em> 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 <code>:</code> instead of <code>/</code> is
used as separator between the scope and plain project name.
E.g., <code>myProject</code> (if unscoped), <code>@myScope:myProject</code> (if scoped).</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>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).</p>
</div>
<div class="paragraph">
<p>In N4JS, when importing from a module <code>M</code> contained in a scoped project <code>@myScope/myProject</code>, the import statement&#8217;s
module specifier should have one of the following forms:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>import * as N from "a/b/c/M";</code></p>
</li>
<li>
<p><code>import * as N from "@myScope/myProject/a/b/c/M";</code></p>
</li>
<li>
<p><code>import * as N from "@myScope/myProject";</code> (if <code>M</code> is specified as main module in `myProject&#8217;s package.json)</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-GH-1026"></a><strong>Req. GH-1026:</strong> <a href="#Req-GH-1026">General Constraints</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>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.</p>
</li>
<li>
<p>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.</p>
</li>
<li>
<p>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 <code>@</code>.</p>
</li>
<li>
<p>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.</p>
</li>
</ol>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="sec:N4JS-Type-Definitions"><a class="anchor" href="#sec:N4JS-Type-Definitions"></a><a class="link" href="#sec:N4JS-Type-Definitions">12.5. N4JS Type Definitions</a></h3>
<div class="paragraph">
<p>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 <em>plain-JS project</em> will be used.</p>
</div>
<div class="sect3">
<h4 id="_specify-type-definition"><a class="anchor" href="#_specify-type-definition"></a><a class="link" href="#_specify-type-definition">12.5.1. Specify Type Definition</a></h4>
<div class="paragraph">
<p>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.</p>
</div>
<div class="listingblock">
<div class="title">Package.json: Important properties for type definition projects</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">{
"n4js": {
"projectType": "definition"
"definesPackage": "..."
"mainModule": "..."
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>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 <code>definesPackage</code>).
Lastly, the main module has to be specified since this information will not be taken from the package.json of the plain-JS project.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
</div>
<div class="sect3">
<h4 id="_name-conventions"><a class="anchor" href="#_name-conventions"></a><a class="link" href="#_name-conventions">12.5.2. Name Conventions</a></h4>
<div class="paragraph">
<p>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 <code>@n4jsd</code> 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 <code>express</code>,
our related type definitions are called <code>@n4jsd/express</code>.</p>
</div>
</div>
<div class="sect3">
<h4 id="_version-conventions"><a class="anchor" href="#_version-conventions"></a><a class="link" href="#_version-conventions">12.5.3. Version Conventions</a></h4>
<div class="paragraph">
<p>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.</p>
</div>
<div class="sect4">
<h5 id="_define-a-new-type-definition-package"><a class="anchor" href="#_define-a-new-type-definition-package"></a><a class="link" href="#_define-a-new-type-definition-package">12.5.3.1. Define a New Type Definition Package</a></h5>
<div class="paragraph">
<p>We use the following convention to compute the version of type definition packages.</p>
</div>
<div class="openblock">
<div class="title">Convention for initial type definition versions</div>
<div class="content">
<div class="paragraph">
<p>&#160;&#160;&#160;&#160; Major<sub>types</sub>.Minor<sub>types</sub>.Patch<sub>types</sub> = Major<sub>impl</sub>.Minor<sub>impl</sub>.0</p>
</div>
</div>
</div>
<div class="openblock">
<div class="title">Example for initial type definition of express@3.3.3</div>
<div class="content">
<div class="paragraph">
<p>&#160;&#160;&#160;&#160; Major<sub>types</sub>.Minor<sub>types</sub>.Patch<sub>types</sub> = 3.3.0</p>
</div>
</div>
</div>
<div class="paragraph">
<p>Let&#8217;s say that a new version of a type definition package called <em>types</em> should be created
that defines types for an npm called <em>impl</em> of version Major<sub>impl</sub>.Minor<sub>impl</sub>.Patch<sub>impl</sub>.
According to our convention, the major and minor version numbers of the type definition package
should just be copied from the version of the <em>impl</em> package.
However, the version patch number of <em>types</em> should not be taken from <em>impl</em>.
Instead, the patch number of <em>types</em> starts with <em>0</em> and increases with every update of this type definition version.
For instance when a bug was found in version Major<sub>types</sub>.Minor<sub>types</sub>.0, the definitions have been extended, or
adjusted to new language features, only the patch number increases to e.g. Major<sub>types</sub>.Minor<sub>types</sub>.1.</p>
</div>
</div>
<div class="sect4">
<h5 id="_using-a-type-definition-package"><a class="anchor" href="#_using-a-type-definition-package"></a><a class="link" href="#_using-a-type-definition-package">12.5.3.2. Using a Type Definition Package</a></h5>
<div class="paragraph">
<p>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.</p>
</div>
<div class="openblock">
<div class="title">Convention for dependencies</div>
<div class="content">
<div class="paragraph">
<p>"dependencies": {<br>
&#160;&#160;&#160;&#160; "@n4jsd/Types": "&lt;=Major<sub>impl</sub>.Minor<sub>impl</sub>.*"<br>
}</p>
</div>
</div>
</div>
<div class="openblock">
<div class="title">Example of dependencies to express and its type definition project</div>
<div class="content">
<div class="paragraph">
<p>"dependencies": {<br>
&#160;&#160;&#160;&#160; "express": "^3.3.3",<br>
&#160;&#160;&#160;&#160; "@n4jsd/express": "&lt;=3.3.*"<br>
}</p>
</div>
</div>
</div>
<div class="paragraph">
<p>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. <code>latest</code>, empty string, url, etc.)
it is recommended to plain copy the non SemVer version when possible.</p>
</div>
</div>
<div class="sect4">
<h5 id="_rational"><a class="anchor" href="#_rational"></a><a class="link" href="#_rational">12.5.3.3. Rational</a></h5>
<div class="paragraph">
<p>The rational behind this convention reflects the idea of semantic versioning:</p>
</div>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>Given a version number MAJOR.MINOR.PATCH, increment the:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>MAJOR version when you make incompatible API changes,</p>
</li>
<li>
<p>MINOR version when you add functionality in a backwards-compatible manner, and</p>
</li>
<li>
<p>PATCH version when you make backwards-compatible bug fixes.</p>
</li>
</ul>
</div>
</blockquote>
<div class="attribution">
&#8212; <a href="https://www.semver.org">semver.org</a>
</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_modules"><a class="anchor" href="#_modules"></a><a class="link" href="#_modules">12.6. Modules</a></h3>
<div class="paragraph">
<p>Each N4JS source file defines a module in the sense of ECMAScript2015, [<a href="#ECMA15a">ECMA15a(p.S14)</a>].
This is the overall structure of a module, based on [<a href="#ECMA15a">ECMA15a(p.S14)</a>].</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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;
;</code></pre>
</div>
</div>
<div class="paragraph">
<p>Grammar and semantics of import statement is described in <a href="#_import-statement">Import Statement</a>; of export statement described in <a href="#_export-statement">Export Statement</a>.</p>
</div>
<div class="paragraph">
<p>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 <a href="#_qualified-names">Qualified Names</a> for details.</p>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_api-and-implementation-component"><a class="anchor" href="#_api-and-implementation-component"></a><a class="link" href="#_api-and-implementation-component">12.7. API and Implementation Component</a></h3>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
the API/Implementation concept is not fully implemented at this time (see GH-1291).
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>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 <code>package.json</code> file under <code>dependencies</code>.
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.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>Here is a summary of the most important details of this concept (they
are all subject to discussion and change):</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Support for this concept, esp. validations, should not be built into the core language but rather implemented as a separate validation/analysis tool.</p>
</li>
<li>
<p>Validation is currently provided in the form of a separate view: the API / Implementation compare view.</p>
</li>
<li>
<p>A project that defines one or more other projects in its <code>package.json</code> file under <code>implementedProjects</code> (cf. <a href="#package-json-implementedProjects">implementedProjects</a>) is called <em>implementation project</em>.
A project that has another project pointing to itself via <code>ImplementedProjects</code> is called <em>API project</em>.
Note that, at the moment, there is no explicit definition making a project an API project.</p>
</li>
<li>
<p>An implementation project must define an implementation ID in its <code>package.json</code> file using the <code>implementationId</code> property in the <code>n4js</code> section (cf. <a href="#package-json-implementationId">implementationId</a>).</p>
</li>
<li>
<p>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 <em>or</em> inherited type-compatible member in the implementation type.</p>
</li>
<li>
<p>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.</p>
</li>
<li>
<p>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.</p>
</li>
<li>
<p>If an API class <code>C</code> implements an interface <code>I</code>, it has to explicitly (re-) declare all members of <code>I</code> 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 <code>C</code> in order to be complete and avoid problems when the interface is changed or <code>C</code> is made abstract.</p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="_execution-of-api-and-implementation-components"><a class="anchor" href="#_execution-of-api-and-implementation-components"></a><a class="link" href="#_execution-of-api-and-implementation-components">12.7.1. Execution of API and Implementation Components</a></h4>
<div class="paragraph">
<p>When launching an N4JS component <em>C</em> under runtime environment <a href="#AC">RE</a>, 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 <em>A</em> in the direct or indirect dependencies of <em>C</em> an implementation project is chosen as follows:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Collect all implementation projects for <em>A</em> (i.e. projects that specify <em>A</em> in their <code>package.json</code> file under <code>implementedProjects</code>).</p>
</li>
<li>
<p>Remove implementation projects that cannot be run under runtime environment <a href="#AC">RE</a>, using the same logic as for running ordinary N4JS components (this step is not implemented yet!).</p>
</li>
<li>
<p>If there are no implementation projects left, show an error.</p>
</li>
<li>
<p>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:</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>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.</p>
</li>
<li>
<p>Otherwise, show an error.</p>
</li>
</ol>
</div>
</li>
<li>
<p>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</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>If there is exactly 1 implementation project left, use it.</p>
</li>
<li>
<p>Otherwise, in UI mode prompt the user for a choice, in headless mode how an error.</p>
</li>
</ol>
</div>
</li>
</ol>
</div>
<div class="paragraph">
<p>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 <code>class path</code>) 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>.</p>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_api-and-implementation-with-di"><a class="anchor" href="#_api-and-implementation-with-di"></a><a class="link" href="#_api-and-implementation-with-di">12.7.2. API and Implementation With DI</a></h4>
<div class="paragraph">
<p>API projects may use N4JS DI (<a href="#_dependency-injection">Dependency Injection</a>) 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.</p>
</div>
<div class="paragraph">
<p><a href="#diag_APITestsDI_Overview">Overview of API tests with DI</a> shows some of those considerations from test client point of view.</p>
</div>
<div id="diag_APITestsDI_Overview" class="imageblock">
<div class="content">
<img src="chapters/12_components/fig/diag_ApiTestsDI_Overview.svg" alt="diag ApiTestsDI Overview">
</div>
<div class="title">Figure 13. Overview of API tests with DI</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p><a href="#diag_ApiTestsDI_StaticDI">API tests with static DI</a> shows how API project defines project wiring and enforces certain level of testability.</p>
</div>
<div id="diag_ApiTestsDI_StaticDI" class="imageblock">
<div class="content">
<img src="chapters/12_components/fig/diag_ApiTestsDI_StaticDI.svg" alt="diag ApiTestsDI StaticDI">
</div>
<div class="title">Figure 14. API tests with static DI</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p><a href="#diag_ApiTestsDI_Views">Types view and Instances view</a> shows Types View perspective of the client, and Instances View perspective of the client.</p>
</div>
<div id="diag_ApiTestsDI_Views" class="imageblock" style="text-align: center">
<div class="content">
<img src="chapters/12_components/fig/diag_ApiTestsDI_Views.svg" alt="diag ApiTestsDI Views">
</div>
<div class="title">Figure 15. Types view and Instances view</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_plainjs"><a class="anchor" href="#_plainjs"></a><a class="link" href="#_plainjs">13. PlainJS</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>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:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>N4JS’ static type system may complain about some older JavaScript hacks. Declared types, in particular, are assumed to be implicitly frozen.</p>
</li>
<li>
<p>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).</p>
</li>
<li>
<p>N4JS defines a (ECMAScript 6 compatible) concept of object-oriented programming which may conflict with other plain JavaScript solutions.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>To overcome these problems, N4JS provides a couple of techniques summarized in this chapter.</p>
</div>
<div class="sect2 language-n4js">
<h3 id="_type-inference-and-validation-for-plain-js"><a class="anchor" href="#_type-inference-and-validation-for-plain-js"></a><a class="link" href="#_type-inference-and-validation-for-plain-js">13.1. Type Inference and Validation for Plain JS</a></h3>
<div class="paragraph">
<p>In plain JavaScript mode:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>All declared variables are inferred to <code>any+</code>.</p>
</li>
<li>
<p>All declared functions return and accept a variadic number of arguments of type <code>any+</code>.</p>
</li>
<li>
<p>It is allowed to use the <code>return</code> statement with or without an expression.</p>
</li>
<li>
<p>New expressions with a receiver of <code>any+</code> is allowed.</p>
</li>
<li>
<p>No type arguments are required for generic built-in types.</p>
</li>
<li>
<p>Assigning a value to a read-only variable is not checked.</p>
</li>
<li>
<p>Undeclared variables are treated as <code>any+</code> as well.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>Note that this essentially disables all validation particularly since methods such as the ’import’-like function <code>require</code> are unknown.</p>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_external-declarations"><a class="anchor" href="#_external-declarations"></a><a class="link" href="#_external-declarations">13.2. External Declarations</a></h3>
<div class="paragraph">
<p>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.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-163"></a><strong>Req. IDE-163:</strong> <a href="#Req-IDE-163">External allowed occurrences</a> (ver. 1)</p>
</div>
<div class="ulist">
<ul>
<li>
<p>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).</p>
</li>
<li>
<p>Only external classes, external interfaces marked with <code>@N4JS</code>, 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.</p>
</li>
<li>
<p>Declarations with external flags are allowed to subclass built-in type <code>Error</code> type and all of its descendants such as
<code>EvalError</code>, <code>RangeError</code>, <code>ReferenceError</code>, <code>SyntaxError</code>, <code>TypeError</code> and <code>URIError</code>, although any of the error types are annotated with <code>@N4JS</code>.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The following explanations apply to all external declarations except
where stated otherwise.</p>
</div>
<div class="paragraph">
<p>In general, an external declaration uses the same syntax as the declaration of a normal N4JS declaration with the addition of the modifier <code>external</code>.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-164"></a><strong>Req. IDE-164:</strong> <a href="#Req-IDE-164">External classes inheritance</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>An external class <em>without</em> the <code>@N4JS</code> 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 <code>Object</code>.</p>
</li>
<li>
<p>An external class <em>with</em> the annotation <code>@N4JS</code> can only inherit from another external class annotated with <code>@N4JS</code> or from non-external N4JS classes.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-165"></a><strong>Req. IDE-165:</strong> <a href="#Req-IDE-165">Structurally typed interface implementation</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>An external class <em>without</em> the annotation <code>@N4JS</code> can only be implemented by structurally typed interfaces.</p>
</li>
<li>
<p>An external class <em>with</em> the annotation <code>@N4JS</code> can only be implemented by structurally typed interfaces annotated with <code>@N4JS</code>.</p>
</li>
<li>
<p>An external interface <em>without</em> the annotation <code>@N4JS</code> must be defined structurally.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-166"></a><strong>Req. IDE-166:</strong> <a href="#Req-IDE-166">External interface inheritance</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>An interface in a n4jsd file <em>without</em> the annotation <code>@N4JS</code> can only inherit from another interface within a n4jsd file.</p>
</li>
<li>
<p>An interface <em>with</em> the <code>@N4JS</code> annotation can only inherit from another interface annotated with <code>@N4JS</code>.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-167"></a><strong>Req. IDE-167:</strong> <a href="#Req-IDE-167">External class/interface members</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The static and instance methods, getters and setters of an external class must not have a method body.</p>
</li>
<li>
<p>The static and instance fields of an external class must not have an initializer.</p>
</li>
<li>
<p>The constructor of an external class without the annotation <code>@N4JS</code> must not be declared private.</p>
</li>
<li>
<p>Methods in interfaces with default implementation which cannot be expressed in the definition file must be annotated with <code>@ProvidesDefaultImplementation</code>.
This is only allowed in interfaces annotated with <code>@N4JS</code>.</p>
</li>
<li>
<p>Fields in interfaces or classes with initializers which cannot be expressed in the definition file, must be annotated with <code>@ProvidesInitializer</code>.
This is only allowed in classes or interfaces annotated with <code>@N4JS</code>.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>This means that in external classes, all members except constructors may be declared private even if the class is not annotated with <code>@N4JS</code>. In interfaces, however, private members are disallowed anyway,
cf. <a href="#Req-IDE-48">[Req-IDE-48]</a>.</p>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-168"></a><strong>Req. IDE-168:</strong> <a href="#Req-IDE-168">Other external declarations</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The literals of an external enum must not have a value.</p>
</li>
<li>
<p>An external function declaration must not have a body.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_declaring-externals"><a class="anchor" href="#_declaring-externals"></a><a class="link" href="#_declaring-externals">13.2.1. Declaring externals</a></h4>
<div class="paragraph">
<p>By default, the implicit supertype of an external class is Object.
If the <code>@N4JS</code> annotation is provided it is N4Object.
If a superclass is explicitly given, the constraints from the previous section apply.</p>
</div>
</div>
<div class="sect3">
<h4 id="_instantiating-external-classes"><a class="anchor" href="#_instantiating-external-classes"></a><a class="link" href="#_instantiating-external-classes">13.2.2. Instantiating external classes</a></h4>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>In case of API-definitions (see <a href="#_api-and-implementation-components">[_api-and-implementation-components]</a>), it might be useful to limit the visibility of classes to narrower scopes such as package or private.</p>
</div>
<div class="paragraph">
<p>External declarations can be instantiated if the following three requirements are fulfilled (not a constraint!):</p>
</div>
<div class="ulist">
<ul>
<li>
<p>External declarations have to be exported and be marked as public so they are accessible from outside.</p>
</li>
<li>
<p>The contained or inherited constructor of an external class must be public.</p>
</li>
<li>
<p>The external class must be linked to an implementation module (see below <a href="#_implementation-of-external-declarations">Implementation of External Declarations</a>).</p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="_implementation-of-external-declarations"><a class="anchor" href="#_implementation-of-external-declarations"></a><a class="link" href="#_implementation-of-external-declarations">13.2.3. Implementation of External Declarations</a></h4>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>There are two possible ways of linking an external declaration to its corresponding implementation:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>By naming convention defined in the <code>package.json</code> file.</p>
</li>
<li>
<p>By declaring that the implementation is provided by the JavaScript runtime, see <a href="#_runtime-definitions">Runtime Definitions</a> for details.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>The naming convention is based on the <code>external</code> source fragments defined in the <code>package.json</code> file (<a href="#package-json">Package.json File</a>).
If the implementation is provided by the runtime directly, then this can be also specified in the <code>package.json</code> file by a module filter.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>In most use cases of external declarations you also want to disable validation and module wrapping by specifying appropriate filters in the <code>package.json</code> file.</p>
</div>
<div class="paragraph">
<p>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 <code>@@IgnoreImplementation</code>.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-169"></a><strong>Req. IDE-169:</strong> <a href="#Req-IDE-169">Implementation of External Declarations</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a given external declaration <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>D</mi></math> but not for
API-definitions <sup class="footnote">[<a id="_footnoteref_60" class="footnote" href="#_footnote_60" title="View footnote.">60</a>]</sup>, the
following constraints must hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>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.</p>
</li>
</ol>
</div>
</div>
</div>
<div id="external-definitions-and-implementations" class="exampleblock">
<div class="title">Example 105. External Definitions and Their Implementations</div>
<div class="content">
<div class="paragraph">
<p>If, in addition to standard <code>source</code>, the <code>source-external</code> fragment is provided in <code>Sources</code>, <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 <a href="#fig-external-class-naming">External Class Implementation, Naming Convention</a>.</p>
</div>
<div id="fig-external-class-naming" class="imageblock" style="text-align: center">
<div class="content">
<img src="chapters/13_plainJS/fig/externalClassImplementation_naming.svg" alt="externalClassImplementation naming">
</div>
<div class="title">Figure 16. External Class Implementation, Naming Convention</div>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_example"><a class="anchor" href="#_example"></a><a class="link" href="#_example">13.2.4. Example</a></h4>
<div class="paragraph">
<p>Assume the following non-N4JS module:</p>
</div>
<div id="ex:External_Classes_Example" class="exampleblock">
<div class="title">Example 106. External Classes</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-javascript" data-lang="javascript">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; }
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Assuming</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>shapes.js</code> is placed in project folder <strong>/external/a/b/shapes.js</strong></p>
</li>
<li>
<p><code>shapes.n4jsd</code> is placed in project folder <strong>/src/a/b/shapes.n4jsd</strong></p>
</li>
<li>
<p><code>package.json</code> defines <strong>src</strong> as source folder and <strong>external</strong> as external source folder</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>the following N4JS external class declarations in <strong>shapes.n4jsd</strong> are sufficient:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-javascript" data-lang="javascript">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);
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Note that the class and interface names in n4jsd files must match those in the js files, respectively.</p>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 107. Structurally-typed external interfaces</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-javascript" data-lang="javascript">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);
}</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_global-definitions"><a class="anchor" href="#_global-definitions"></a><a class="link" href="#_global-definitions">13.3. Global Definitions</a></h3>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>N4JS supports global definitions via the annotation <code>Global</code>.
This annotation can only be defined on modules (via <code>@@Global</code>) – this means that all declarations in the module are
globally defined.<sup class="footnote">[<a id="_footnoteref_61" class="footnote" href="#_footnote_61" title="View footnote.">61</a>]</sup></p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>@Global</code> </dt>
<dd>
<p>Boolean flag set to true if annotation <code>@Global</code> is set in containing module. The flag indicates that the exported element is globally available and must not be imported.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-170"></a><strong>Req. IDE-170:</strong> <a href="#Req-IDE-170">Global Definitions</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>Global Definitions</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The name of the definition must not be equal to any primitive type (<code>string</code>, <code>number</code> etc.), <code>any</code>, or an built-in N4 type (<code>N4Object</code> etc.).</p>
</li>
<li>
<p>If the name of the definition equals a basic runtime time Object Type then the project must be a runtime environment:</p>
</li>
</ol>
</div>
<div class="paragraph">
<p><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></p>
</div>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_runtime-definitions"><a class="anchor" href="#_runtime-definitions"></a><a class="link" href="#_runtime-definitions">13.4. Runtime Definitions</a></h3>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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 <a href="#_runtime-environment-and-runtime-libraries">Runtime Environment and Runtime Libraries</a>).</p>
</div>
<div class="paragraph">
<p>N4JS supports runtime definitions via the annotation <code>@ProvidedByRuntime</code>.
This annotation can be defined</p>
</div>
<div class="ulist">
<ul>
<li>
<p>on modules (via <code>@@ProvidedByRuntime</code>)– this means that all declarations in the module are provided by the runtime</p>
</li>
<li>
<p>on export statements or declarations.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>@ProvidedByRuntime</code> </dt>
<dd>
<p>Boolean flag set to true if the annotation <code>@ProvidedByRuntime</code> is set.
Flag indicates that the element is only declared in the module but its implementation is provided by the runtime.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>Since built-in types are usually defined globally, the annotation <code>@ProvidedByRuntime</code> is usually used in combination with <code>@Global</code>.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-171"></a><strong>Req. IDE-171:</strong> <a href="#Req-IDE-171">Provided By Runtime</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The declaration must either be an export declaration itself or an exportable declaration.</p>
</li>
<li>
<p>The declaration must be contained in a definition module.</p>
</li>
<li>
<p>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>.</p>
</li>
<li>
<p>There must be no implementation file with the same name as the definition module if annotation is defined for a whole module.</p>
</li>
</ol>
</div>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_applying-polyfills"><a class="anchor" href="#_applying-polyfills"></a><a class="link" href="#_applying-polyfills">13.5. Applying Polyfills</a></h3>
<div class="paragraph">
<p>(Runtime) Libraries often do not provide completely new types but modify existing types.
The ECMA-402 Internationalization Standard [<a href="#ECMA12a">ECMA12a</a>], for example, changes methods of the built-in class <code>Date</code> to be timezone-aware.
Other scenarios include new functionality provided by browsers which are not part of an official standard yet.
Even ECMAScript 6 [<a href="#ECMA15a">ECMA15a</a>] extends the predecessor [<a href="#ECMA11a">ECMA11a</a>] in terms of new methods or new method parameters added to existing types.
It also adds completely new classes and features, of course.</p>
</div>
<div class="paragraph">
<p>The syntax of runtime polyfills is described in section <a href="#_polyfill-definitions">Polyfill Definitions</a>.
Here, an example of applying a runtime polyfill is detailed.</p>
</div>
<div class="exampleblock">
<div class="title">Example 108. Object.observe with Polyfill</div>
<div class="content">
<div class="paragraph">
<p>The following snippet demonstrates how to define a polyfill of the built-in class <code>Object</code> to add the new ECMAScript 7 observer functionality.
This snippet has to be defined in a runtime library or environment.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">@@ProvidedByRuntime
@@Global
@Polyfill
export external public class Object extends Object {
public static Object observe(Object object, Function callback, Array&lt;string&gt;? accept);
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>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 <code>Object</code>.</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_jsdoc-2"><a class="anchor" href="#_jsdoc-2"></a><a class="link" href="#_jsdoc-2">14. JSDoc</a></h2>
<div class="sectionbody">
<div class="paragraph language-n4js">
<p>In N4JS, comments starting with two asterisks (in <code>/** .. */</code>) are interpreted as
documentation comments. The format is similar to JavaDoc or Google
Closure annotations.</p>
</div>
<div class="sect2 language-n4js">
<h3 id="_general-n4jsdoc-features"><a class="anchor" href="#_general-n4jsdoc-features"></a><a class="link" href="#_general-n4jsdoc-features">14.1. General N4JSDoc Features</a></h3>
<div class="paragraph">
<p>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.</p>
</div>
<div class="sect3">
<h4 id="_provided-inline-tags"><a class="anchor" href="#_provided-inline-tags"></a><a class="link" href="#_provided-inline-tags">14.1.1. Provided Inline Tags</a></h4>
<div class="sect4">
<h5 id="jsdoc_tag__code"><a class="anchor" href="#jsdoc_tag__code"></a><a class="link" href="#jsdoc_tag__code">14.1.1.1. @code</a></h5>
<div class="paragraph">
<p>Small code snippet, not validated yet.</p>
</div>
</div>
<div class="sect4">
<h5 id="jsdoc_tag__link"><a class="anchor" href="#jsdoc_tag__link"></a><a class="link" href="#jsdoc_tag__link">14.1.1.2. @link</a></h5>
<div class="paragraph">
<p>Link to a type of element, not validated or supported in navigation yet.</p>
</div>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_n4jsdoc-for-user-projects"><a class="anchor" href="#_n4jsdoc-for-user-projects"></a><a class="link" href="#_n4jsdoc-for-user-projects">14.2. N4JSdoc for User Projects</a></h3>
<div class="sect3">
<h4 id="_standard-tags"><a class="anchor" href="#_standard-tags"></a><a class="link" href="#_standard-tags">14.2.1. Standard Tags</a></h4>
<div class="sect4">
<h5 id="_-author"><a class="anchor" href="#_-author"></a><a class="link" href="#_-author">14.2.1.1. @author</a></h5>
<div class="paragraph">
<p>Name of author, repeat for multiple authors. Name of author is not
validated.</p>
</div>
</div>
<div class="sect4">
<h5 id="jsdoc_tag_param"><a class="anchor" href="#jsdoc_tag_param"></a><a class="link" href="#jsdoc_tag_param">14.2.1.2. @param</a></h5>
<div class="paragraph">
<p>Parameter description, not validated at the moment.</p>
</div>
</div>
<div class="sect4">
<h5 id="jsdoc_tag_return"><a class="anchor" href="#jsdoc_tag_return"></a><a class="link" href="#jsdoc_tag_return">14.2.1.3. @return</a></h5>
<div class="paragraph">
<p>Return description, not validated at the moment.</p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_test-related-tags"><a class="anchor" href="#_test-related-tags"></a><a class="link" href="#_test-related-tags">14.2.2. Test Related Tags</a></h4>
<div class="paragraph">
<p>The following tags are supposed to be used only in tests.</p>
</div>
<div class="sect4">
<h5 id="jsdoc_tag__testee"><a class="anchor" href="#jsdoc_tag__testee"></a><a class="link" href="#jsdoc_tag__testee">14.2.2.1. @testee</a></h5>
<div class="paragraph">
<p>Link to type (maybe a function) or member tested by the test.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-172"></a><strong>Req. IDE-172:</strong> <a href="#Req-IDE-172">@testee Semantics</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Tag can be only used on either</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>methods annotated with <code>@Test</code></p>
</li>
<li>
<p>classes in test projects or folders, cf. <a href="#jsdoc_tag__testeeFromType">@testeeFromType</a>.</p>
</li>
</ol>
</div>
</li>
<li>
<p>Tag requires a single argument, which is a fully qualified name to a type, including the module specifier.
The format is as follows: <code>moduleSpecifier '.' typeName ( ('.'|'#') memberName)?</code></p>
</li>
<li>
<p>Tag is <em>not</em> repeatable, that is a single test method (or class) can refer to only one testee.</p>
</li>
<li>
<p>Tag precedes the <code>reqid</code> tag, i.e., if a <code>@testee</code> is specified, the <code>reqid</code> will be ignored.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 109. @testee</div>
<div class="content">
<div class="paragraph">
<p>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:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">/**
* @testee some/package/Module.Type.member
*/</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="jsdoc_tag__testeeFromType"><a class="anchor" href="#jsdoc_tag__testeeFromType"></a><a class="link" href="#jsdoc_tag__testeeFromType">14.2.2.2. @testeeFromType</a></h5>
<div class="paragraph">
<p>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.</p>
</div>
<div class="exampleblock">
<div class="title">Example 110. Usage of testeeFromType</div>
<div class="content">
<div class="paragraph">
<p>In the following example, the is used. This tag will lead to a test documentation for <code>B.foo</code> and <code>C.foo</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">abstract class Base {
/**
* @testeeFromType
*/
@Test testFoo() {..}
}
/**
* @testee B.foo
*/
class TestB extends Base {}
/**
* @testee C.foo
*/
class TestC extends Base {}</code></pre>
</div>
</div>
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
The resulting spec has to be double-checked for consistency
since it is easily possible that too many constraints are generated.
</td>
</tr>
</table>
</div>
</div>
<div class="sect4">
<h5 id="_testeeType_and__testeeMember"><a class="anchor" href="#_testeeType_and__testeeMember"></a><a class="link" href="#_testeeType_and__testeeMember">14.2.2.3. @testeeType and @testeeMember</a></h5>
<div class="paragraph">
<p>Specifying the testee at the test method directly should be sufficient
for most cases. The <code>@testeeFromType</code> 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 <code>@testeeFromType</code> and <code>@@testeeMember</code> 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.</p>
</div>
<div class="paragraph">
<p>The <code>@testeeType</code> is to defined in the test class JSDoc (actually, it is not
recognized when defined in a member JSDoc). The <code>@testeeMember</code> is specified in the test method JSDoc. The "real" testee is then computed from the testee type and the testee method.</p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
1. This only works for instance members, so far!<br>
2. There is no validation for invalid combinations!
</td>
</tr>
</table>
</div>
<div id="ex:testeetype-and-testeemethod" class="exampleblock">
<div class="title">Example 111. testeeType and testeeMethod</div>
<div class="content">
<div class="paragraph">
<p>Assume the following testees:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class A {
foo(): void {..}
bar(): void { .. this.foo(); ..}
}
class B extends A {
@Override foo() { .. }
}</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>Assume that the tests have to ensure the same semantics for <code>bar</code>, which is
maybe changed by a wrong implementation of <code>foo</code>. That is, <code>bar</code> is to be tested in
case of the receiver type <code>A</code> and <code>B</code>. This can be achieved by the following
tests:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">/**
* @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(); }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>This actually defines two tests, which is also recognized by the spec
exporter:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><code>testBar</code> for a receiver of type <code>A</code>:</p>
<div class="paragraph">
<p><code>ATest</code>'s JSDoc <code>@testeeType</code> + <code>ATest.testBar</code>'s JSDoc <code>@testeeMember</code> = testee <code>A.A.bar</code></p>
</div>
</li>
<li>
<p><code>testBar</code> for a receiver of type <code>B</code>:</p>
<div class="paragraph">
<p><code>BTest</code>'s JSDoc <code>@testeeType</code> + <code>ATest.testBar</code>'s JSDoc <code>@testeeMember</code> = testee <code>B.B.bar</code></p>
</div>
</li>
</ol>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
In all cases when <code>@testeeFromType</code> or <code>@testeeType</code>/<code>@testeeMember</code> 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.
</td>
</tr>
</table>
</div>
<div class="exampleblock">
<div class="title">Example 112. testeeType and testeeMethod with omitted constraints</div>
<div class="content">
<div class="paragraph">
<p>Assume testees similar as in <a href="#ex:testeetype-and-testeemethod">testeeType and testeeMethod</a>. Since the semantics of <code>bar</code> is not changed in <code>B</code>, it is probably not necessary to generate the same constraint in the documentation for <code>bar</code> twice (one in the section for class <code>A</code> and another one in the section of class <code>B</code>).
Still, we want the test to be executed for both receivers. This is how it is achieved:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">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(); }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>This actually defines two tests as in the previous example. Only one
constraint is created in the spec by the spec exporter:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><code>testBar</code> for a receiver of type <code>A</code>:<br>
<code>ATest</code>'s JSDoc <code>@testeeType</code> + <code>BaseTest.testBar</code>'s JSDoc <code>@testeeMember</code> = testee <code>A.A.bar</code></p>
</li>
</ol>
</div>
<div class="paragraph">
<p>Although a test for receiver of type <code>B</code> is run, no additional constraint is
created since there is no <code>@testeeType</code> available neither in <code>BTest</code> nor in <code>BaseTest</code>.</p>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="jsdoc_tag_reqid_in_Tests"><a class="anchor" href="#jsdoc_tag_reqid_in_Tests"></a><a class="link" href="#jsdoc_tag_reqid_in_Tests">14.2.2.4. @reqid in Tests</a></h5>
<div class="paragraph">
<p>ID of feature used in <code>JSDoc</code> 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.</p>
</div>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_n4jsdoc-for-api-and-implementation-projects"><a class="anchor" href="#_n4jsdoc-for-api-and-implementation-projects"></a><a class="link" href="#_n4jsdoc-for-api-and-implementation-projects">14.3. N4JSDoc for API and Implementation Projects</a></h3>
<div class="paragraph">
<p>The following tags are supposed to be used in API and implementation
projects.</p>
</div>
<div class="sect3">
<h4 id="jsdoc_tag__apiNote"><a class="anchor" href="#jsdoc_tag__apiNote"></a><a class="link" href="#jsdoc_tag__apiNote">14.3.1. @apiNote</a></h4>
<div class="paragraph">
<p>Simple note that is shown in the API compare view.</p>
</div>
</div>
<div class="sect3">
<h4 id="API_Project_Tags"><a class="anchor" href="#API_Project_Tags"></a><a class="link" href="#API_Project_Tags">14.3.2. API Project Tags</a></h4>
<div class="paragraph">
<p>The following tags are supposed to be used in API projects only.</p>
</div>
<div class="sect4">
<h5 id="jsdoc_tag_apiState"><a class="anchor" href="#jsdoc_tag_apiState"></a><a class="link" href="#jsdoc_tag_apiState">14.3.2.1. @apiState</a></h5>
<div class="paragraph">
<p>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:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">/**
* @apiState stable (WK)
* @apiState reviewed (JvP)
*/</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_jsx"><a class="anchor" href="#_jsx"></a><a class="link" href="#_jsx">15. JSX</a></h2>
<div class="sectionbody">
<div class="sect2 language-jsx">
<h3 id="_jsx-support"><a class="anchor" href="#_jsx-support"></a><a class="link" href="#_jsx-support">15.1. JSX Support</a></h3>
<div class="paragraph">
<p>N4JS scripts with file extension "n4jsx" support the special <a href="https://facebook.github.io/jsx/">JSX syntax</a>. Files ending with "jsx" are considered as plain Javascript.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-241101"></a><strong>Req. IDE-241101:</strong> <a href="#Req-IDE-241101">React Component</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>There are two ways of defining a React component:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>functional component: A function is a React component, iff</p>
<div class="ulist">
<ul>
<li>
<p>its return type is React.Element</p>
</li>
<li>
<p>the function has at least one parameter. This first parameter is considered as the <code>props</code> property.
The type of the first parameter only has public fields (or methods defined by Object).
The fields may be defined as optional.</p>
</li>
</ul>
</div>
</li>
<li>
<p>class component: A class is a React component, iff</p>
<div class="ulist">
<ul>
<li>
<p>it extends <code>React.Component</code>.</p>
</li>
<li>
<p>the type parameters must be structural types.</p>
<div class="ulist">
<ul>
<li>
<p>The first parameter defines the <code>props</code> type, it must only define public fields (or methods defined by Object)</p>
</li>
<li>
<p>The second parameter defines the <code>state</code> type, it must only define public fields (or methods defined by Object)</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>In both cases, the name must be capitalized (i.e., start with an upper case letter).</p>
</div>
</div>
</div>
<div class="paragraph">
<p>Component must be written capitalized (i.e. first letter upper case), html (or other xml elements) must be written lower case.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-241102"></a><strong>Req. IDE-241102:</strong> <a href="#Req-IDE-241102">JSX Basic Syntax</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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; '}'));</code></pre>
</div>
</div>
<div class="paragraph">
<p>This syntax is similar to the syntax defined by <a href="https://facebook.github.io/jsx/">JSX</a>, except that</p>
</div>
<div class="ulist">
<ul>
<li>
<p>JSXNamedspaceName is not supported</p>
</li>
<li>
<p>JSXText is not supported, instead, string template literals are to be used</p>
</li>
</ul>
</div>
</div>
</div>
<div class="paragraph">
<p>Remarks on differences between the syntax defined in <a href="#Req-IDE-241102">[Req-IDE-241102]</a> and <a href="https://facebook.github.io/jsx/">JSX</a>:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>JSXSelfClosingElement, JSXOpeningElement and JSXClosingElement are merged into one single rule JSXElement</p>
</li>
<li>
<p>JSXAttributes is defined as fragment, that is, the attributes become fields of the JSXElement</p>
</li>
<li>
<p>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</p>
</li>
<li>
<p>JSXPropertyAttribute models the JSX non-spread JSXAttribute definition; again existing rules (IdentifierName, StringLiteral) are used to model JSX specific ones (JSXAttributeName, JSXDoubleStringCharacters, JSXSingleStringCharacters)</p>
</li>
</ul>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-241401"></a><strong>Req. IDE-241401:</strong> <a href="#Req-IDE-241401">JSX Syntax With Free Text</a> (ver. 1)</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">JSXChild:
JSXElement | JSXExpression | JSXText
;</code></pre>
</div>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-241103"></a><strong>Req. IDE-241103:</strong> <a href="#Req-IDE-241103">JSX Extended Syntax Check</a> (ver. 1)</p>
</div>
<div class="ulist">
<ul>
<li>
<p>It is an error, if corresponding opening and closing JSXElements have different names.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-241113"></a><strong>Req. IDE-241113:</strong> <a href="#Req-IDE-241113">JSX Expression Type</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>The type of a JSX expression is React.Element.</p>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-241114"></a><strong>Req. IDE-241114:</strong> <a href="#Req-IDE-241114">React Symbol</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>If a JSX literal is used, it is an error if the React symbol is not imported via</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-jsx" data-lang="jsx">import React from "react"</code></pre>
</div>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-241115"></a><strong>Req. IDE-241115:</strong> <a href="#Req-IDE-241115">JSXElement names (tags)</a> (ver. 1)</p>
</div>
<div class="ulist">
<ul>
<li>
<p>It is an error if a capitalized tag cannot be bound to a function or class declaration.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-241116"></a><strong>Req. IDE-241116:</strong> <a href="#Req-IDE-241116">JSXElements referring to React components</a> (ver. 1)</p>
</div>
<div class="ulist">
<ul>
<li>
<p>It is an error, if a tag binds to a function declaration, which is not conform to the functional component definition.</p>
</li>
<li>
<p>It is an error, if a tag binds to a class declaration, which is not conform to the class component definition.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-241117"></a><strong>Req. IDE-241117:</strong> <a href="#Req-IDE-241117">JSXAttributes and React component properties</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>If the tag binds to a component, the following constraints must hold:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The attribute must be a non-private field of the properties type.</p>
</li>
<li>
<p>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.</p>
</li>
<li>
<p>The type of the attribute expression must be conform to the type of the corresponding <code>props</code>'s property</p>
</li>
</ul>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-241118"></a><strong>Req. IDE-241118:</strong> <a href="#Req-IDE-241118">JSXElements referring to XML elements</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>If the lower-case tag does not bind to a function or class declaration, the following constraints must be hold:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>If the tag is neither a pre-defined HTML tag nor an SVG tag, a warning is issued.</p>
</li>
<li>
<p>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.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-241119"></a><strong>Req. IDE-241119:</strong> <a href="#Req-IDE-241119">JSXSpreadAttribute behavior</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>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 <a href="#Req-IDE-241117">[Req-IDE-241117]</a> apply to the attributes specified in the spread operator. In particular,</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The type of each attribute specified in spread operator must be conform to the type of the corresponding property of <code>props</code>.</p>
</li>
<li>
<p>If a non-optional property of <code>props</code> is specified neither as attribute nor in a spread operator, a warning is issued.</p>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_jsx-backend"><a class="anchor" href="#_jsx-backend"></a><a class="link" href="#_jsx-backend">15.2. JSX Backend</a></h3>
<div class="paragraph">
<p>The support for JSX in N4JS aims for an implementation that adheres to the idea of <a href="https://reactjs.org/docs/jsx-in-depth.html">React JSX</a>. This means that JSX elements are transpiled to React Element factory calls (e.g. <code>&lt;div prop="c"&gt;content&lt;/div&gt;</code> transpiles to <code>React.createElement('div', {prop: "c"}, null)</code>). For that, the transpiler must be aware of a specific implementation of React and the corresponding <code>createElement</code> function.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-GH-687"></a><strong>Req. GH-687:</strong> <a href="#Req-GH-687">React Implementation</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>A react implementation is given in terms of a module that fulfils the following properties:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The FQN of the module is <code>react</code>.</p>
</li>
<li>
<p>Type definitions are available for the module.</p>
</li>
<li>
<p>The module exports a function of name <code>createElement</code>.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_grammars"><a class="anchor" href="#_grammars"></a><a class="link" href="#_grammars">16. Grammars</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>N4JS extends the ECMAScript 2015 language grammar and combines it with type expression.</p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
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.
</td>
</tr>
</table>
</div>
<div class="sect2">
<h3 id="_type-expressions-grammar"><a class="anchor" href="#_type-expressions-grammar"></a><a class="link" href="#_type-expressions-grammar">16.1. Type Expressions Grammar</a></h3>
<div id="lst:EBNFTypeExpression" class="listingblock">
<div class="title">EBNF Type Expression Grammar</div>
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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:
'..'
;</code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_n4js-language-grammar"><a class="anchor" href="#_n4js-language-grammar"></a><a class="link" href="#_n4js-language-grammar">16.2. N4JS Language Grammar</a></h3>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">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:
','
;</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_jsobjects"><a class="anchor" href="#_jsobjects"></a><a class="link" href="#_jsobjects">17. JSObjects</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>The built-in ECMAScript Objects
[<a href="#ECMA11a">ECMA11a(p.S15, p.p.102)</a>] 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 [<a href="#ECMA11a">ECMA11a</a>] repeated here for convenience.</p>
</div>
<div class="sect2 language-n4js">
<h3 id="_object"><a class="anchor" href="#_object"></a><a class="link" href="#_object">17.1. Object</a></h3>
<div class="paragraph">
<p><code>Object</code> is the super type of all declared types and <code>N4Object</code>. It is almost similar to the JavaScript type <code>Object</code> except that no properties may be dynamically added to it. In order to declare a variable to which properties can be dynamically added, the <code>Object+</code> type has to be declared (cf.
<a href="#_dynamic">[_dynamic]</a>).</p>
</div>
<h4 id="_attributes" class="discrete">Attributes</h4>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>constructor:Object</code></dt>
<dd>
<p>Returns a reference to the Object function that created the instance’s
prototype.</p>
</dd>
</dl>
</div>
<h4 id="_methods-2" class="discrete">Methods</h4>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>toString():Object</code></dt>
<dd>
<p>Returns a string representing the specified object.</p>
</dd>
<dt class="hdlist1"><code>toLocaleString():Object</code></dt>
<dd>
<p>Returns a string representing the object. This method is meant to be
overridden by derived objects for locale-specific purposes.</p>
</dd>
<dt class="hdlist1"><code>valueOf():Object</code></dt>
<dd>
<p>Returns the primitive value of the specified object.</p>
</dd>
<dt class="hdlist1"><code>hasOwnProperty(prop:String):Boolean</code></dt>
<dd>
<p>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.</p>
</dd>
<dt class="hdlist1"><code>isPrototypeOf(object:Object):Boolean</code></dt>
<dd>
<p>Returns a boolean indication whether the specified object is in the
prototype chain of the object this method is called upon.</p>
</dd>
<dt class="hdlist1"><code>propertyIsEnumerable(prop:String):Boolean</code></dt>
<dd>
<p>Returns a boolean indicating if the internal ECMAScript DontEnum
attribute is set.</p>
</dd>
</dl>
</div>
<h4 id="_static-methods" class="discrete">Static Methods</h4>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>getPrototypeOf(object:Object):Object</code></dt>
<dd>
<p>Returns the prototype of the specified object.</p>
</dd>
<dt class="hdlist1"><code>create(object:Object,properties:Object=):Object</code></dt>
<dd>
<p>Creates a new object with the specified prototype object and properties.</p>
</dd>
<dt class="hdlist1"><code>defineProperty(object:Object,prop:Object,descriptor:Object):Object</code></dt>
<dd>
<p>Defines a new property directly on an object or modifies an existing
property on an object and returns the object.</p>
</dd>
<dt class="hdlist1"><code>defineProperties(object:Object,properties:Object):Object</code></dt>
<dd>
<p>Defines new or modifies existing properties directly on an object,
returning the object.</p>
</dd>
<dt class="hdlist1"><code>seal(object:Object,properties:Object)</code></dt>
<dd>
<p>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.</p>
</dd>
<dt class="hdlist1"><code>freeze(object:Object):Object</code></dt>
<dd>
<p>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.</p>
</dd>
<dt class="hdlist1"><code>preventExtensions(object:Object):Object</code></dt>
<dd>
<p>Prevents new properties from ever being added to an object (i.e.
prevents future extensions to the object).</p>
</dd>
<dt class="hdlist1"><code>isSealed(object:Object):Boolean static</code></dt>
<dd>
<p>Determine if an object is sealed.</p>
</dd>
<dt class="hdlist1"><code>isFrozen(object:Object):Boolean</code></dt>
<dd>
<p>Determine if an object is frozen.</p>
</dd>
<dt class="hdlist1"><code>isExtensible(object:Object):Boolean</code></dt>
<dd>
<p>Determines if an object is extensible (whether it can have new
properties added to it).</p>
</dd>
<dt class="hdlist1"><code>keys(object:Object):Array&lt;String&gt;</code></dt>
<dd>
<p>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).</p>
</dd>
</dl>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_string"><a class="anchor" href="#_string"></a><a class="link" href="#_string">17.2. String</a></h3>
<div class="paragraph">
<p>String is a global object that may be used to construct String
instances and is a sub class of Object.</p>
</div>
<h4 id="string-attributes" class="discrete">Attributes</h4>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>number: length</code> </dt>
<dd>
<p>The length of a string.</p>
</dd>
</dl>
</div>
<h4 id="methods-1" class="discrete">Methods</h4>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>String(thing:Object=)</code></dt>
<dd>
<p>-</p>
</dd>
<dt class="hdlist1"><code>anchor(anchorname:String):String</code></dt>
<dd>
<p>Creates an HTML anchor.</p>
</dd>
<dt class="hdlist1"><code>big():String</code></dt>
<dd>
<p>Returns a string in a big font.</p>
</dd>
<dt class="hdlist1"><code>blink():String</code></dt>
<dd>
<p>Returns a string in a blinking string.</p>
</dd>
<dt class="hdlist1"><code>bold():String</code></dt>
<dd>
<p>Returns a string in a bold font.</p>
</dd>
<dt class="hdlist1"><code>charAt(index:Number):String</code></dt>
<dd>
<p>Returns the character at a specified position.</p>
</dd>
<dt class="hdlist1"><code>charCodeAt(index:Number):Number</code></dt>
<dd>
<p>Returns the Unicode of the character at a specified position.</p>
</dd>
<dt class="hdlist1"><code>concat(strings:String&#8230;&#8203;):String</code></dt>
<dd>
<p>Joins two or more strings.</p>
</dd>
<dt class="hdlist1"><code>equals(object:Object):Boolean</code> </dt>
<dd>
<p>-</p>
</dd>
<dt class="hdlist1"><code>equalsIgnoreCase(object:Object):Boolean</code> </dt>
<dd>
<p>-</p>
</dd>
<dt class="hdlist1"><code>fromCharCode(num:Any&#8230;&#8203;):String</code></dt>
<dd>
<p>Returns a string created by using the specified sequence of Unicode values.</p>
</dd>
<dt class="hdlist1"><code>fixed():String</code></dt>
<dd>
<p>Returns a string as teletype text.</p>
</dd>
<dt class="hdlist1"><code>fontcolor(color):String</code></dt>
<dd>
<p>Returns a string in a specified color.</p>
</dd>
<dt class="hdlist1"><code>fontsize(size):String</code></dt>
<dd>
<p>Returns a string in a specified size.</p>
</dd>
<dt class="hdlist1"><code>indexOf(searchValue, fromIndex:Number=):Number</code></dt>
<dd>
<p>Returns the position of the first occurrence of a specified string value in a string.</p>
</dd>
<dt class="hdlist1"><code>italics():String</code></dt>
<dd>
<p>Returns a string in italic.</p>
</dd>
<dt class="hdlist1"><code>lastIndexOf(searchValue, fromIndex:Number=):Number</code></dt>
<dd>
<p>Returns the position of the last occurrence of a specified string value, searching backwards from the specified position in a string.</p>
</dd>
<dt class="hdlist1"><code>link(url):String</code></dt>
<dd>
<p>Returns a string as a hyperlink.</p>
</dd>
<dt class="hdlist1"><code>localeCompare(otherString):Number</code></dt>
<dd>
<p>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.</p>
</dd>
<dt class="hdlist1"><code>match(search value):String</code></dt>
<dd>
<p>Searches for a specified value in a string.</p>
</dd>
<dt class="hdlist1"><code>replace(findString,newString):String</code></dt>
<dd>
<p>Replaces some characters with some other characters in a string.</p>
</dd>
<dt class="hdlist1"><code>search(search string):Number</code></dt>
<dd>
<p>Searches a string for a specified value.</p>
</dd>
<dt class="hdlist1"><code>slice(beginSlice:Number, endSclice:Number=):String</code></dt>
<dd>
<p>Extracts a part of a string and returns the extracted part in a new string.</p>
</dd>
<dt class="hdlist1"><code>small():String</code></dt>
<dd>
<p>Returns a string in a small font.</p>
</dd>
<dt class="hdlist1"><code>split(separator, howmany:Number=):Array&lt;String&gt;</code></dt>
<dd>
<p>Splits a string into an array of strings.</p>
</dd>
<dt class="hdlist1"><code>strike():String</code></dt>
<dd>
<p>Returns a string with a strikethrough.</p>
</dd>
<dt class="hdlist1"><code>sub():String</code></dt>
<dd>
<p>Returns a string as subscript.</p>
</dd>
<dt class="hdlist1"><code>substr(start:Number,length:Number=):String</code></dt>
<dd>
<p>Extracts a specified number of characters in a string, from a start index.</p>
</dd>
<dt class="hdlist1"><code>substring(beginIndex:number,endIndex:Number=):String</code></dt>
<dd>
<p>Extracts the characters in a string between two specified indices.</p>
</dd>
<dt class="hdlist1"><code>sup():String</code></dt>
<dd>
<p>Returns a string as superscript.</p>
</dd>
<dt class="hdlist1"><code>toLocaleUpperCase():String</code></dt>
<dd>
<p>Returns a string in lowercase letters.</p>
</dd>
<dt class="hdlist1"><code>toString():String</code></dt>
<dd>
<p>Returns a String value for this object.</p>
</dd>
<dt class="hdlist1"><code>toUpperCase():String</code></dt>
<dd>
<p>Returns a string in uppercase letters.</p>
</dd>
<dt class="hdlist1"><code>valueOf():String</code></dt>
<dd>
<p>Returns the primitive value of a String object.</p>
</dd>
</dl>
</div>
<h4 id="static-methods-1" class="discrete">Static Methods</h4>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>String(value:Object=)</code></dt>
<dd>
<p>Static constructor.</p>
</dd>
</dl>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_boolean"><a class="anchor" href="#_boolean"></a><a class="link" href="#_boolean">17.3. Boolean</a></h3>
<div class="paragraph">
<p><code>Boolean</code> does not have a super class.</p>
</div>
<h4 id="static-methods-2" class="discrete">Static Methods</h4>
<div class="paragraph">
<p><code>Boolean(value:Object=):Boolean</code></p>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_number"><a class="anchor" href="#_number"></a><a class="link" href="#_number">17.4. Number</a></h3>
<div class="paragraph">
<p><code>Number</code> does not have a super class.</p>
</div>
<div class="sect3">
<h4 id="_static-attributes"><a class="anchor" href="#_static-attributes"></a><a class="link" href="#_static-attributes">17.4.1. Static Attributes</a></h4>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>MAX_VALUE:Number</code></dt>
<dd>
<p>The largest representable number.</p>
</dd>
<dt class="hdlist1"><code>MIN_VALUE:Number</code></dt>
<dd>
<p>The smallest representable number.</p>
</dd>
<dt class="hdlist1"><code>NaN:Number</code></dt>
<dd>
<p>Special 'not a number' value.</p>
</dd>
<dt class="hdlist1"><code>NEGATIVE_INFINITY:Number</code></dt>
<dd>
<p>Special value representing negative infinity, returned on overflow.</p>
</dd>
<dt class="hdlist1"><code>POSITIVE_INFINITY:Number</code></dt>
<dd>
<p>Special value representing infinity, returned on overflow.</p>
</dd>
</dl>
</div>
<h4 id="methods-2" class="discrete">Methods</h4>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>toExponential(numberOfDecimals:Number=):String</code></dt>
<dd>
<p>Converts the value of the object into an exponential notation.</p>
</dd>
<dt class="hdlist1"><code>toFixed(numberOfDecimals:Number=):String</code></dt>
<dd>
<p>Formats a number to the specified number of decimals.</p>
</dd>
<dt class="hdlist1"><code>toPrecision(numberOfDecimals:Number=):String</code></dt>
<dd>
<p>Converts a number into an exponential notation if it has more digits than specified.</p>
</dd>
<dt class="hdlist1"><code>valueOf():Number</code></dt>
<dd>
<p>Returns the primitive value of a Number object.</p>
</dd>
<dt class="hdlist1"><code>toString(radix:Number=):String</code></dt>
<dd>
<p>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).</p>
</dd>
<dt class="hdlist1"><code>toLocaleString(locales: String|[String]=undefined, options: <sub>r</sub>NumberFormatOptions=undefined): String</code></dt>
<dd>
<p>Returns a locale-specific String value for this object. The toLocalString accepts two optional arguments. The semantics of these arguments
is defined in <a href="https://www.ecma-international.org/ecma-402/4.0/index.html#sup-number.prototype.tolocalestring">ECMA-402 (Internationalization API Specification)</a>.
In N4JS, the base definition does not define that method, instead Number inherits <code>toLocaleString</code> from Object. The specialized
definition is found in the runtime library <code>n4js-runtime-ecma402</code>.</p>
</dd>
</dl>
</div>
<h4 id="static-methods-3" class="discrete">Static Methods</h4>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>Number(value:Object=):Number</code></dt>
<dd>
<p>Static constructor.</p>
</dd>
</dl>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="function"><a class="anchor" href="#function"></a><a class="link" href="#function">17.5. Function</a></h3>
<div class="paragraph">
<p><code>Function</code> does not have a super class.</p>
</div>
<h4 id="attributes-2" class="discrete">Attributes</h4>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>prototype:Object</code></dt>
<dd>
<p>Allows the addition of properties to the instance of the object created by the constructor function.</p>
</dd>
<dt class="hdlist1"><code>length:Number</code></dt>
<dd>
<p>Specifies the number of arguments expected by the functio</p>
</dd>
</dl>
</div>
<h4 id="methods-3" class="discrete">Methods</h4>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>apply(thisArg,argsArray:Array=):Object</code></dt>
<dd>
<p>Applies the method of another object in the context of a different object (the calling object); arguments can be passed as an Array object.</p>
</dd>
<dt class="hdlist1"><code>call(thisArg,arg&#8230;&#8203;):Object</code></dt>
<dd>
<p>Calls (executes) a method of another object in the context of a different object (the calling object); arguments can be passed as they are.</p>
</dd>
<dt class="hdlist1"><code>bind(thisArg:Object,arg&#8230;&#8203;):Function</code></dt>
<dd>
<p>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.</p>
</dd>
</dl>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_error"><a class="anchor" href="#_error"></a><a class="link" href="#_error">17.6. Error</a></h3>
<div class="paragraph">
<p><code>Error</code> does not have a super class.</p>
</div>
<h4 id="attributes-3" class="discrete">Attributes</h4>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>name:String</code></dt>
<dd>
<p>Error name.</p>
</dd>
<dt class="hdlist1"><code>message:String</code></dt>
<dd>
<p>Error message.</p>
</dd>
</dl>
</div>
<h4 id="static-methods-4" class="discrete">Static Methods</h4>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>Error(message:Object=):Error</code></dt>
<dd>
<p>Static Constructor.</p>
</dd>
</dl>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_array"><a class="anchor" href="#_array"></a><a class="link" href="#_array">17.7. Array</a></h3>
<div class="paragraph">
<p><code>Array</code> is a generic type with the type parameter <code>E</code> and does not have a super class.</p>
</div>
<h4 id="methods-4" class="discrete">Methods</h4>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>concat(array&#8230;&#8203;):Array&lt;E&gt;)</code></dt>
<dd>
<p>Joins two or more arrays and returns the result.</p>
</dd>
<dt class="hdlist1"><code>every(callback:Function):Boolean</code></dt>
<dd>
<p>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).</p>
</dd>
<dt class="hdlist1"><code>filter(callback:Function):Array&lt;E&gt;</code></dt>
<dd>
<p>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).</p>
</dd>
<dt class="hdlist1"><code>forEach(callback:Function,thisArg=)</code></dt>
<dd>
<p>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.</p>
</dd>
<dt class="hdlist1"><code>indexOf(searchElement,fromIndex=):Number</code></dt>
<dd>
<p>Returns the first index at which a given element can be found in the array, or -1 if it is not present.</p>
</dd>
<dt class="hdlist1"><code>join(separator=):String</code></dt>
<dd>
<p>Puts all the elements of an array into a string. The elements are separated by a specified delimiter.</p>
</dd>
<dt class="hdlist1"><code>lastIndexOf(searchElement,fromIndex=):Number</code></dt>
<dd>
<p>Returns the last (greatest) index of an element within the array equal to the specified value. Will return -1 if none are found.</p>
</dd>
<dt class="hdlist1"><code>length():Number</code></dt>
<dd>
<p>The length returns an integer representing the length of an array.</p>
</dd>
<dt class="hdlist1"><code>map(callback:Function,thisArg=):Array</code></dt>
<dd>
<p>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.</p>
</dd>
<dt class="hdlist1"><code>pop():E</code></dt>
<dd>
<p>Removes and returns the last element of an array.</p>
</dd>
<dt class="hdlist1"><code>push(element&#8230;&#8203;):E</code></dt>
<dd>
<p>Adds one or more elements to the end of an array and returns the new length.</p>
</dd>
<dt class="hdlist1"><code>reverse():Array&lt;E&gt;</code></dt>
<dd>
<p>Reverses the order of the elements in an array.</p>
</dd>
<dt class="hdlist1"><code>shift()</code></dt>
<dd>
<p>Removes and returns the first element of an array.</p>
</dd>
<dt class="hdlist1"><code>slice(start:Number,end:Number=):Array&lt;E&gt;</code></dt>
<dd>
<p>Returns selected elements from an existing array.</p>
</dd>
<dt class="hdlist1"><code>some(callback:Function,thisArg=):Boolean</code></dt>
<dd>
<p>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.</p>
</dd>
<dt class="hdlist1"><code>sort(sortByFunction:Function=):Array&lt;E&gt;</code></dt>
<dd>
<p>Sorts the elements of an array. The function will be called with 2 arguments (a,b).</p>
</dd>
<dt class="hdlist1"><code>splice(index:Number,how many:Number,element&#8230;&#8203;):Array&lt;E&gt;</code></dt>
<dd>
<p>Removes and adds new elements to an array. Returns the removed elements as an Array.</p>
</dd>
<dt class="hdlist1"><code>toLocaleString():String</code></dt>
<dt class="hdlist1"><code>toString():String</code></dt>
<dd>
<p>Returns a String value for Array.</p>
</dd>
<dt class="hdlist1"><code>unshift(element&#8230;&#8203;):E</code></dt>
<dd>
<p>Adds one or more elements to the beginning of an array and returns the new length.</p>
</dd>
</dl>
</div>
<h4 id="static-methods-5" class="discrete">Static Methods</h4>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>Array(item:Object&#8230;&#8203;)</code></dt>
<dd>
<p>Static constructor.</p>
</dd>
</dl>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_date"><a class="anchor" href="#_date"></a><a class="link" href="#_date">17.8. Date</a></h3>
<div class="paragraph">
<p><code>Date</code> does not have a super class.</p>
</div>
<h4 id="static-methods-6" class="discrete">Static Methods</h4>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>Date():Date</code></dt>
<dd>
<p>Static constructor.</p>
</dd>
<dt class="hdlist1"><code>Date(milliseconds:Number):Date</code></dt>
<dd>
<p>Constructor.</p>
</dd>
<dt class="hdlist1"><code>Date(date:Date):Date</code></dt>
<dd>
<p>Constructor.</p>
</dd>
<dt class="hdlist1"><code>Date(dateString:String):Date</code></dt>
<dd>
<p>Constructor.</p>
</dd>
<dt class="hdlist1"><code>Date(year:Number,month:Number,day=Number=,hour:Number=,minute:Number=,second:Number=,millisecond:Number=):Date</code></dt>
<dd>
<p>Constructor.</p>
</dd>
<dt class="hdlist1"><code>parse(dateString:String):Date</code></dt>
<dd>
<p>Parses a string representation of a date, and returns the number of milliseconds since midnight Jan 1, 1970.</p>
</dd>
<dt class="hdlist1"><code>now():Number</code></dt>
<dd>
<p>Returns the numeric value corresponding to the current time.</p>
</dd>
<dt class="hdlist1"><code>UTC(year:Number,month:Number,date:Number=,hrs:Number=,min:Number=,sec:Number=,ms:Number=):Number</code></dt>
<dd>
<p>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.</p>
</dd>
</dl>
</div>
<h4 id="methods-5" class="discrete">Methods</h4>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>getDate():Number</code></dt>
<dd>
<p>Returns the day of the month from a Date object (from 1-31).</p>
</dd>
<dt class="hdlist1"><code>getDay():Number</code></dt>
<dd>
<p>Returns the day of the week from a Date object (from 0-6).</p>
</dd>
<dt class="hdlist1"><code>getFullYear():Number</code></dt>
<dd>
<p>Returns the year, as a four-digit number.</p>
</dd>
<dt class="hdlist1"><code>getHours():Number</code></dt>
<dd>
<p>Returns the hour of a day (from 0-23).</p>
</dd>
<dt class="hdlist1"><code>getMilliseconds():Number</code></dt>
<dd>
<p>Returns the milliseconds of a Date object (from 0-999).</p>
</dd>
<dt class="hdlist1"><code>getMinutes():Number</code></dt>
<dd>
<p>Returns the minutes of a date (from 0-59).</p>
</dd>
<dt class="hdlist1"><code>getMonth():Number</code></dt>
<dd>
<p>Returns the month from a date (from 0-11).</p>
</dd>
<dt class="hdlist1"><code>getSeconds():Number</code></dt>
<dd>
<p>Returns the seconds of a date (from 0-59).</p>
</dd>
<dt class="hdlist1"><code>getTime():Number</code></dt>
<dd>
<p>Returns the number of milliseconds since midnight Jan 1, 1970.</p>
</dd>
<dt class="hdlist1"><code>valueOf():Number</code></dt>
<dd>
<p>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.</p>
</dd>
<dt class="hdlist1"><code>getTimezoneOffset():Number</code></dt>
<dd>
<p>Returns the difference in minutes between local time and Greenwich Mean Time (GMT).</p>
</dd>
<dt class="hdlist1"><code>getUTCDate():Number</code></dt>
<dd>
<p>Returns the day of the month from a date according to Universal Time (from 1-31).</p>
</dd>
<dt class="hdlist1"><code>getUTCDay():Number</code></dt>
<dd>
<p>Returns the day of the week from a date according to Universal Time (from 0-6).</p>
</dd>
<dt class="hdlist1"><code>getUTCFullYear():Number</code></dt>
<dd>
<p>Returns the four-digit year from a date according to Universal Time.</p>
</dd>
<dt class="hdlist1"><code>getUTCHours():Number</code></dt>
<dd>
<p>Returns the hour of a date according to Universal Time (from 0-23).</p>
</dd>
<dt class="hdlist1"><code>getUTCMilliseconds():Number</code></dt>
<dd>
<p>Returns the milliseconds of a date according to Universal Time (from 0-999).</p>
</dd>
<dt class="hdlist1"><code>getUTCMinutes():Number</code></dt>
<dd>
<p>Returns the minutes of a date according to Universal Time (from 0-59).</p>
</dd>
<dt class="hdlist1"><code>getUTCMonth():Number</code></dt>
<dd>
<p>Returns the month from a Date object according to Universal Time (from 0-11).</p>
</dd>
<dt class="hdlist1"><code>getUTCSeconds():Number</code></dt>
<dd>
<p>Returns the seconds of a date according to Universal Time (from 0-59).</p>
</dd>
<dt class="hdlist1"><code>getYear():Number deprecated</code></dt>
<dd>
<p>Returns the year as a two-digit or a three/four-digit number, depending on the browser. Use getFullYear() instead!</p>
</dd>
<dt class="hdlist1"><code>setDate(day):Number</code></dt>
<dd>
<p>Sets the day of the month from a Date object (from 1-31).</p>
</dd>
<dt class="hdlist1"><code>setFullYear(full year, month=, day=):Number</code></dt>
<dd>
<p>Sets the year as a four-digit number.</p>
</dd>
<dt class="hdlist1"><code>setHours(hours,minutes=,seconds=,milis=):Number</code></dt>
<dd>
<p>Sets the hour of a day (from 0-23).</p>
</dd>
<dt class="hdlist1"><code>setMilliseconds(mills):Number</code></dt>
<dd>
<p>Sets the milliseconds of a Date object (from 0-999).</p>
</dd>
<dt class="hdlist1"><code>setMinutes(minutes,=seconds,=millis):Number</code></dt>
<dd>
<p>Sets the minutes of a date (from 0-59).</p>
</dd>
<dt class="hdlist1"><code>setMonth" directType="Number(month,day=):Number</code></dt>
<dd>
<p>Sets the month from a date (from 0-11).</p>
</dd>
<dt class="hdlist1"><code>setSeconds(seconds,millis=):number</code></dt>
<dd>
<p>Sets the seconds of a date (from 0-59).</p>
</dd>
<dt class="hdlist1"><code>setTime(mills):Number</code></dt>
<dd>
<p>Sets the number of milliseconds since midnight Jan 1, 1970.</p>
</dd>
<dt class="hdlist1"><code>setUTCDate(day):Number</code></dt>
<dd>
<p>Sets the day of the month from a date according to Universal Time (from 0-6).</p>
</dd>
<dt class="hdlist1"><code>setUTCFullYear(fullyear,month=,day=):Number</code></dt>
<dd>
<p>Sets the four-digit year from a date according to Universal Time.</p>
</dd>
<dt class="hdlist1"><code>setUTCHours(hours,minutes=,seconds=,millis=):Number</code></dt>
<dd>
<p>Sets the hour of a date according to Universal Time (from 0-23).</p>
</dd>
<dt class="hdlist1"><code>setUTCMilliseconds(mills):Number</code></dt>
<dd>
<p>Sets the milliseconds of a date according to Universal Time (from 0-999).</p>
</dd>
<dt class="hdlist1"><code>setUTCMinutes(minutes,seconds=,millis=):Number</code></dt>
<dd>
<p>Sets the minutes of a date according to Universal Time time (from 0-59).</p>
</dd>
<dt class="hdlist1"><code>setUTCMonth(month,day=):Number</code></dt>
<dd>
<p>Sets the month from a Date object according to Universal Time (from 0-11).</p>
</dd>
<dt class="hdlist1"><code>setUTCSeconds(seconds,millis=):Number</code></dt>
<dd>
<p>Sets the seconds of a date according to Universal Time (from 0-59).</p>
</dd>
<dt class="hdlist1"><code>setYear(year):Number deprecated</code></dt>
<dd>
<p>Sets the year, as a two-digit or a three/four-digit number, depending on the browser. Use setFullYear() instead!!</p>
</dd>
<dt class="hdlist1"><code>toDateString():String</code></dt>
<dd>
<p>Returns the date portion of a Date object in readable form.</p>
</dd>
<dt class="hdlist1"><code>toLocaleDateString(locales: String|[String]=undefined, options: <sub>r</sub>DateTimeFormatOptions=undefined): String</code></dt>
<dd>
<p>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
<a href="https://www.ecma-international.org/ecma-402/4.0/index.html#sup-date.prototype.tolocaledatestring">ECMA-402 (Internationalization API Specification)</a>.
The specialized definition is found in the runtime library <code>n4js-runtime-ecma402</code>.</p>
</dd>
<dt class="hdlist1"><code>toLocaleString(locales: String|[String]=undefined, options: <sub>r</sub>DateTimeFormatOptions=undefined): String</code></dt>
<dd>
<p>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
<a href="https://www.ecma-international.org/ecma-402/4.0/index.html#sup-date.prototype.tolocalestring">ECMA-402 (Internationalization API Specification)</a>.
In N4JS, the base definition does not define that method, instead Date inherits <code>toLocaleString</code> from Object. The specialized
definition is found in the runtime library <code>n4js-runtime-ecma402</code>.</p>
</dd>
<dt class="hdlist1"><code>toLocaleTimeString(locales: String|[String]=undefined, options: <sub>r</sub>DateTimeFormatOptions=undefined): String</code></dt>
<dd>
<p>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
<a href="https://www.ecma-international.org/ecma-402/4.0/index.html#sup-date.prototype.tolocaletimestring">ECMA-402 (Internationalization API Specification)</a>.
The specialized definition is found in the runtime library <code>n4js-runtime-ecma402</code>.</p>
</dd>
<dt class="hdlist1"><code>toString():String</code></dt>
<dd>
<p>Returns a String value for this object.</p>
</dd>
<dt class="hdlist1"><code>toTimeString():String</code></dt>
<dd>
<p>Returns the time portion of a Date object in readable form.</p>
</dd>
<dt class="hdlist1"><code>toUTCString():String</code></dt>
<dd>
<p>Converts a Date object, according to Universal Time, to a string.</p>
</dd>
</dl>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_math"><a class="anchor" href="#_math"></a><a class="link" href="#_math">17.9. Math</a></h3>
<div class="paragraph">
<p><code>Math</code> is not instantiable and only provides static properties and methods.</p>
</div>
<div class="sect3">
<h4 id="static-attributes-1"><a class="anchor" href="#static-attributes-1"></a><a class="link" href="#static-attributes-1">17.9.1. Static Attributes</a></h4>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>E:Number</code></dt>
<dd>
<p>Euler&#8217;s constant and the base of natural logarithms, approximately 2.718.</p>
</dd>
<dt class="hdlist1"><code>LN2:Number</code></dt>
<dd>
<p>Natural logarithm of 2, approximately 0.693.</p>
</dd>
<dt class="hdlist1"><code>LN10:Number</code></dt>
<dd>
<p>Natural logarithm of 10, approximately 2.302.</p>
</dd>
<dt class="hdlist1"><code>LOG2E:Number</code></dt>
<dd>
<p>Base 2 logarithm of E, approximately 1.442.</p>
</dd>
<dt class="hdlist1"><code>LOG10E:Number</code></dt>
<dd>
<p>Base 10 logarithm of E, approximately 0.434.</p>
</dd>
<dt class="hdlist1"><code>PI:Number</code></dt>
<dd>
<p>Ratio of the circumference of a circle to its diameter, approximately 3.14159.</p>
</dd>
<dt class="hdlist1"><code>SQRT1_2:Number</code></dt>
<dd>
<p>Square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707.</p>
</dd>
<dt class="hdlist1"><code>SQRT2:Number</code></dt>
<dd>
<p>Square root of 2, approximately 1.414.</p>
</dd>
</dl>
</div>
<h4 id="static-methods-7" class="discrete">Static Methods</h4>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>abs(x):Number</code></dt>
<dd>
<p>Returns the absolute value of a number.</p>
</dd>
<dt class="hdlist1"><code>acos(x:Number):Number</code></dt>
<dd>
<p>Returns the arccosine of a number.</p>
</dd>
<dt class="hdlist1"><code>asinx:Number):Number</code></dt>
<dd>
<p>Returns the arcsine of a number.</p>
</dd>
<dt class="hdlist1"><code>atan(x:Number):Number</code></dt>
<dd>
<p>Returns the arctangent of a number.</p>
</dd>
<dt class="hdlist1"><code>atan2(y:Number,x:Number):Number</code></dt>
<dd>
<p>Returns the arctangent of the quotient of its arguments.</p>
</dd>
<dt class="hdlist1"><code>ceil(x):Number</code></dt>
<dd>
<p>Returns the smallest integer greater than or equal to a number.</p>
</dd>
<dt class="hdlist1"><code>cos(x):Number</code></dt>
<dd>
<p>Returns the arctangent of the quotient of its arguments.</p>
</dd>
<dt class="hdlist1"><code>exp(x):Number</code></dt>
<dd>
<p>Returns Enumber, where number is the argument, and E is Euler&#8217;s constant (2.718&#8230;&#8203;), the base of the natural logarithm.</p>
</dd>
<dt class="hdlist1"><code>floor(x):Number</code></dt>
<dd>
<p>Returns the largest integer less than or equal to a number.</p>
</dd>
<dt class="hdlist1"><code>log(x):Number</code></dt>
<dd>
<p>Returns the natural logarithm (loge, also ln) of a number.</p>
</dd>
<dt class="hdlist1"><code>max(value&#8230;&#8203;):Number</code></dt>
<dd>
<p>Returns the largest of zero or more numbers.</p>
</dd>
<dt class="hdlist1"><code>min(value&#8230;&#8203;):Number</code></dt>
<dd>
<p>Returns the smallest of zero or more numbers.</p>
</dd>
<dt class="hdlist1"><code>pow(base:Number,exponent:Number):Number</code></dt>
<dd>
<p>Returns base to the exponent power, that is, baseexponent.</p>
</dd>
<dt class="hdlist1"><code>random():Number</code></dt>
<dd>
<p>Returns a pseudorandom number between 0 and 1.</p>
</dd>
<dt class="hdlist1"><code>round(x:Number):Number</code></dt>
<dd>
<p>Returns the value of a number rounded to the nearest integer.</p>
</dd>
<dt class="hdlist1"><code>sin(x:Number):Number</code></dt>
<dd>
<p>Returns the sine of a number.</p>
</dd>
<dt class="hdlist1"><code>sqrt(x:Number):Number</code></dt>
<dd>
<p>Returns the positive square root of a number.</p>
</dd>
<dt class="hdlist1"><code>tan(x:Number):Number</code></dt>
<dd>
<p>Returns the tangent of a number.</p>
</dd>
</dl>
</div>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_regexp"><a class="anchor" href="#_regexp"></a><a class="link" href="#_regexp">17.10. RegExp</a></h3>
<div class="paragraph">
<p><code>RegExp</code> does not have a super class.</p>
</div>
<h4 id="attributes-4" class="discrete">Attributes</h4>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>global:Boolean</code></dt>
<dd>
<p>Whether to test the regular expression against all possible matches in a string, or only against the first.</p>
</dd>
<dt class="hdlist1"><code>ignoreCase:Boolean</code></dt>
<dd>
<p>Whether to ignore case while attempting a match in a string.</p>
</dd>
<dt class="hdlist1"><code>lastIndex:Number</code></dt>
<dd>
<p>The index at which to start the next match.</p>
</dd>
<dt class="hdlist1"><code>multiline:Boolean</code></dt>
<dd>
<p>Whether or not to search in strings across multiple lines.</p>
</dd>
<dt class="hdlist1"><code>source:String</code></dt>
<dd>
<p>The text of the pattern.</p>
</dd>
</dl>
</div>
<h4 id="methods-6" class="discrete">Methods</h4>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>exec(str:String):Array</code></dt>
<dd>
<p>Executes a search for a match in its string parameter.</p>
</dd>
<dt class="hdlist1"><code>test(str:String):Boolean</code></dt>
<dd>
<p>Tests for a match in its string parameter.</p>
</dd>
</dl>
</div>
</div>
<div class="sect2 language-n4js">
<h3 id="_json"><a class="anchor" href="#_json"></a><a class="link" href="#_json">17.11. JSON</a></h3>
<div class="paragraph">
<p><code>JSON</code> is a global object and a subclass of <code>Object</code>. Its functionality is provided by two static methods.
It is not possible to create new instances of type JSON.</p>
</div>
<h4 id="attributes-5" class="discrete">Attributes</h4>
<div class="paragraph">
<p>The JSON object does not define own properties.</p>
</div>
<h4 id="methods-7" class="discrete">Methods</h4>
<div class="paragraph">
<p>The JSON object does not define own methods.</p>
</div>
<h4 id="static-methods-8" class="discrete">Static Methods</h4>
<div class="paragraph">
<p>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 [<a href="#ECMA11a">ECMA11a(p.S15.12.2)</a>]</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>The optional space parameter is a String or Number that allows the
result to have whitespace injected into it to improve human readability.</p>
</div>
<div class="paragraph">
<p>For detailed information see [<a href="#ECMA11a">ECMA11a(p.S15.12.3)</a>].</p>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_n4js-objects"><a class="anchor" href="#_n4js-objects"></a><a class="link" href="#_n4js-objects">18. N4JS Objects</a></h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_reflection-model"><a class="anchor" href="#_reflection-model"></a><a class="link" href="#_reflection-model">18.1. Reflection Model</a></h3>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>The following class diagrams shows the defined classes. Note that for
performance reasons, the actual structure may vary from the model.</p>
</div>
<div id="fig-n4js-reflection-classes" class="imageblock">
<div class="content">
<img src="chapters/a03_n4jsobjects/fig/cd_reflectionModel.svg" alt="cd reflectionModel">
</div>
<div class="title">Figure 17. N4JS Reflection Classes</div>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">/**
* 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; }
}</code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_error-types"><a class="anchor" href="#_error-types"></a><a class="link" href="#_error-types">18.2. Error Types</a></h3>
<div class="paragraph">
<p>N4JS provides additional Error types as subtypes of <code>Error</code>.</p>
</div>
<div class="sect3 language-n4js">
<h4 id="_n4apinotimplemented"><a class="anchor" href="#_n4apinotimplemented"></a><a class="link" href="#_n4apinotimplemented">18.2.1. N4ApiNotImplemented</a></h4>
<div class="paragraph">
<p>Considering API definitions and concrete implementations of those APIs
the error <code>N4-Api-Not-Implemented-Error</code> 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 <code>N4-Api-Not-Implemented-Error</code>.</p>
</div>
<div class="paragraph">
<p>API-testing projects can catch those errors and act accordingly. This
enables tracking of completeness of implementations by counting the
occasions an <code>N4-Api-NotImplemented-Error</code> was encountered.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">/**
* Error type reporting a not implemented situation.
*/
public class N4ApiNotImplementedError extends Error { }</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_acronyms"><a class="anchor" href="#_acronyms"></a><a class="link" href="#_acronyms">Appendix A: Acronyms</a></h2>
<div class="sectionbody">
<table id="AC" class="tableblock frame-all grid-all spread language-bash">
<colgroup>
<col style="width: 8.3333%;">
<col style="width: 41.6666%;">
<col style="width: 8.3333%;">
<col style="width: 41.6668%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>CDep</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Compile-Time Dependency</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>RDep</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Run-Time Dependency</p></th>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>LDep</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Load-Time Dependency</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>IDep</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Initialization-Time Dependency</p></th>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>EDep</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Execution-Time Dependency</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>ANTLR</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">ANother Tool for Language Recognition</p></th>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>API</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Application Programming Interface</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>AST</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Abstract Syntax Tree</p></th>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>ASI</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Automatic Semicolon Insertion</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>BNF</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Backus-Naur Form</p></th>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>CLI</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Command Line Interface (including a headless compiler and runner.)</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>DI</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Dependency Injection</p></th>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>DIC</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">DI Component</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>DOM</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Document Object Model</p></th>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>DSL</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Domain Specific Language</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>EBNF</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Extended Backus-Naur Form</p></th>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>EMF</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Eclipse Modeling Framework <a id="EMF"></a></p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>FQN</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Fully Qualified Name</p></th>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>GLB</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Greatest Lower Bound, also known as <em>infimum</em></p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>GCST</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Greatest Common Sub Type, also known as <em>meet</em></p></th>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>IDE</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Integrated Development Environment</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>IDL</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Interface Definition Language</p></th>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>LSP</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Liskov Substitution Principle [<a href="#Martin96b">Martin96b</a>]</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>LUB</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Least Upper Bound, also known as <em>supremum</em></p></th>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>LCST</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Least Common Super Type, also known as <em>join</em></p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>N4JS</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">N4JS for JavaScript</p></th>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>N4JSED</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">N4JS Environment Definition</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>N4JSIDE</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">N4JS Integrated Development Environment (Eclipse-based IDE for all N4JS related languages and projects)</p></th>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>VM</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Virtual Machine</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>XML</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Extensible Markup Language</p></th>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>XSLT / XSL</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">XSL Transformations</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>XSL</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Extensible Stylesheet Language</p></th>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>WYSIWYG</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">What You See Is What You Get</p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>WLOG</code></p></td>
<th class="tableblock halign-left valign-top"><p class="tableblock">Without loss of generality</p></th>
</tr>
</tbody>
</table>
</div>
</div>
<div class="sect1">
<h2 id="sec:License"><a class="anchor" href="#sec:License"></a><a class="link" href="#sec:License">Appendix B: License</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>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 <a href="http://www.eclipse.org/legal/epl-v10.html" class="bare">http://www.eclipse.org/legal/epl-v10.html</a></p>
</div>
<h3 id="_eclipse-public-license-v-1-0" class="discrete">Eclipse Public License - v 1.0</h3>
<div class="paragraph">
<p>THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE
PUBLIC LICENSE (<code>AGREEMENT</code>). ANY USE, REPRODUCTION OR DISTRIBUTION OF
THE PROGRAM CONSTITUTES RECIPIENT’S ACCEPTANCE OF THIS AGREEMENT.</p>
</div>
<h4 id="_1-definitions" class="discrete">1. DEFINITIONS</h4>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>Contribution</code> means: </dt>
<dd>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>in the case of the initial Contributor, the initial code and
documentation distributed under this Agreement, and</p>
</li>
<li>
<p>in the case of each subsequent Contributor:</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>changes to the Program, and</p>
</li>
<li>
<p>additions to the Program;</p>
<div class="paragraph">
<p>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:</p>
</div>
<div class="olist lowerroman">
<ol class="lowerroman" type="i">
<li>
<p>are separate modules of software distributed in conjunction with the Program
under their own license agreement, and</p>
</li>
<li>
<p>are not derivative works of the Program.</p>
</li>
</ol>
</div>
</li>
</ol>
</div>
</li>
</ol>
</div>
</dd>
<dt class="hdlist1"><code>Contributor</code></dt>
<dd>
<p>means any person or entity that distributes the Program.</p>
</dd>
<dt class="hdlist1"><code>Licensed Patents</code> </dt>
<dd>
<p>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.</p>
</dd>
<dt class="hdlist1"><code>Program</code> </dt>
<dd>
<p>means the Contributions distributed in accordance with this
Agreement.</p>
</dd>
<dt class="hdlist1"><code>Recipient</code> </dt>
<dd>
<p>means anyone who receives the Program under this
Agreement, including all Contributors.</p>
</dd>
</dl>
</div>
<h4 id="_2-grant-of-rights" class="discrete">2. GRANT OF RIGHTS</h4>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>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.</p>
</li>
<li>
<p>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.</p>
</li>
<li>
<p>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.</p>
</li>
<li>
<p>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.</p>
</li>
</ol>
</div>
<h4 id="_3-requirements" class="discrete">3. REQUIREMENTS</h4>
<div class="paragraph">
<p>A Contributor may choose to distribute the Program in object code form
under its own license agreement, provided that:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>it complies with the terms and conditions of this Agreement; and</p>
</li>
<li>
<p>its license agreement:</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>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;</p>
</li>
<li>
<p>effectively excludes on behalf of all Contributors all liability for
damages, including direct, indirect, special, incidental and
consequential damages, such as lost profits;</p>
</li>
<li>
<p>states that any provisions which differ from this Agreement are
offered by that Contributor alone and not by any other party; and</p>
</li>
<li>
<p>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.</p>
</li>
</ol>
</div>
</li>
</ol>
</div>
<div class="paragraph">
<p>When the Program is made available in source code form:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>it must be made available under this Agreement; and</p>
</li>
<li>
<p>a copy of this Agreement must be included with each copy of the
Program.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>Contributors may not remove or alter any copyright notices contained
within the Program.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<h4 id="_4-commercial-distribution" class="discrete">4. COMMERCIAL DISTRIBUTION</h4>
<div class="paragraph">
<p>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 (<code>Commercial
Contributor</code>) hereby agrees to defend and indemnify every other
Contributor (<code>Indemnified Contributor</code>) against any losses, damages
and costs (collectively <code>Losses</code>) 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.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<h4 id="_5-no-warranty" class="discrete">5. NO WARRANTY</h4>
<div class="paragraph">
<p>EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED
ON AN <code>AS IS</code> 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.</p>
</div>
<h4 id="_6-disclaimer-of-liability" class="discrete">6. DISCLAIMER OF LIABILITY</h4>
<div class="paragraph">
<p>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.</p>
</div>
<h4 id="_7-general" class="discrete">7. GENERAL</h4>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
<div class="paragraph">
<p>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.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_bibliography"><a class="anchor" href="#_bibliography"></a><a class="link" href="#_bibliography">Appendix C: Bibliography</a></h2>
<div class="sectionbody">
<div class="openblock bibliography">
<div class="content">
<div class="paragraph">
<p><a id="ECMA15a"></a>ECMA. (2015). <em>ECMAScript 2015 Language Specification</em>. Retrieved from <a href="http://www.ecma-international.org/ecma-262/6.0/index.html" class="bare">http://www.ecma-international.org/ecma-262/6.0/index.html</a></p>
</div>
<div class="paragraph">
<p><a id="ECMA11a"></a>(2011). <em>ECMAScript Language Specification</em>. Retrieved from <a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm" class="bare">http://www.ecma-international.org/publications/standards/Ecma-262.htm</a></p>
</div>
<div class="paragraph">
<p><a id="Pierce02a"></a>Pierce, Benjamin C.. (2002). <em>Types and Programming Languages</em>. </p>
</div>
<div class="paragraph">
<p><a id="Gosling15a"></a>Gosling, James and Joy, Bill and Steele, Guy and Bracha, Gilad and Buckley, Alex. (2015). <em>The Java Language Specification. Java SE 8 Edition</em>. Retrieved from <a href="http://docs.oracle.com/javase/specs/jls/se8/jls8.pdf" class="bare">http://docs.oracle.com/javase/specs/jls/se8/jls8.pdf</a></p>
</div>
<div class="paragraph">
<p><a id="Gosling12a"></a>Gosling, James and Joy, Bill and Steele, Guy and Bracha, Gilad and Buckley, Alex. (2012). <em>The Java Language Specification. Java SE 7 Edition</em>. Retrieved from <a href="http://docs.oracle.com/javase/specs/jls/se7/jls7.pdf" class="bare">http://docs.oracle.com/javase/specs/jls/se7/jls7.pdf</a></p>
</div>
<div class="paragraph">
<p><a id="Nielson99a"></a>Nielson, Flemming and Nielson, HanneRiis. (1999). <em>Type and Effect Systems</em>. Retrieved from <a href="http://dx.doi.org/10.1007/3-540-48092-7_6" class="bare">http://dx.doi.org/10.1007/3-540-48092-7_6</a></p>
</div>
<div class="paragraph">
<p><a id="Crary02a"></a>Crary, Karl and Weirich, Stephanie and Morrisett, Greg. (2002). <em>Intensional polymorphism in type-erasure semantics</em>. Retrieved from <a href="http://journals.cambridge.org/article_S0956796801004282" class="bare">http://journals.cambridge.org/article_S0956796801004282</a></p>
</div>
<div class="paragraph">
<p><a id="Igarashi01a"></a>Igarashi, Atsushi and Pierce, Benjamin C. and Wadler, Philip. (2001). <em>Featherweight Java: a minimal core calculus for Java and GJ</em>. </p>
</div>
<div class="paragraph">
<p><a id="Torgersen05a"></a>Torgersen, Mads and Ernst, Erik and Hansen, Christian Plesner. (2005). <em>Wild FJ</em>. Retrieved from <a href="http://homepages.inf.ed.ac.uk/wadler/fool/program/14.html" class="bare">http://homepages.inf.ed.ac.uk/wadler/fool/program/14.html</a></p>
</div>
<div class="paragraph">
<p><a id="Cameron08b"></a>Cameron, Nicholas and Drossopoulou, Sophia and Ernst, Erik. (2008). <em>A Model for Java with Wildcards</em>. Retrieved from <a href="http://dx.doi.org/10.1007/978-3-540-70592-5_2" class="bare">http://dx.doi.org/10.1007/978-3-540-70592-5_2</a></p>
</div>
<div class="paragraph">
<p><a id="Cameron09a"></a>Cameron, Nicholas. (2009). <em>Existential Types for Variance&#8201;&#8212;&#8201;Java Wildcards and Ownership Types</em>. Retrieved from <a href="http://www.doc.ic.ac.uk/~ncameron/papers/cameron_thesis.pdf" class="bare">http://www.doc.ic.ac.uk/~ncameron/papers/cameron_thesis.pdf</a></p>
</div>
<div class="paragraph">
<p><a id="Summers10a"></a>Summers, Alexander J. and Cameron, Nicholas and Dezani-Ciancaglini, Mariangiola and Drossopoulou, Sophia. (2010). <em>Towards a semantic model for Java wildcards</em>. </p>
</div>
<div class="paragraph">
<p><a id="Wehr08a"></a>Wehr, Stefan and Thiemann, Peter. (2008). <em>Subtyping Existential Types</em>. Retrieved from <a href="http://www.informatik.uni-freiburg.de/~wehr/publications/Wehr_Subtyping_existential_types.pdf" class="bare">http://www.informatik.uni-freiburg.de/~wehr/publications/Wehr_Subtyping_existential_types.pdf</a></p>
</div>
<div class="paragraph">
<p><a id="Igarashi07a"></a>Igarashi, Atsushi and Nagira, Hideshi. (2007). <em>Union Types for Object-Oriented Programming</em>. Retrieved from <a href="http://www.jot.fm/issues/issue_2007_02/article3/" class="bare">http://www.jot.fm/issues/issue_2007_02/article3/</a></p>
</div>
<div class="paragraph">
<p><a id="King13a"></a>King, Gavin. (2013). <em>The Ceylon Language</em>. Red Hat, Inc.. Retrieved from <a href="http://ceylon-lang.org/documentation/1.0/spec/pdf/ceylon-language-specification.pdf" class="bare">http://ceylon-lang.org/documentation/1.0/spec/pdf/ceylon-language-specification.pdf</a></p>
</div>
<div class="paragraph">
<p><a id="Laurent12a"></a>Laurent, Olivier. (2012). <em>Intersection types with subtyping by means of cut elimination</em>. </p>
</div>
<div class="paragraph">
<p><a id="W3C:Steen:14:XL"></a>Steen, Hallvord and Aubourg, Julian and van Kesteren, Anne and Song, Jungkee. (2014). <em>XMLHttpRequest Level 1</em>. </p>
</div>
<div class="paragraph">
<p><a id="Dart13a"></a>Dart Team. (2013). <em>Dart Programming Language Specification</em>. Retrieved from <a href="http://www.dartlang.org/docs/spec/latest/dart-language-specification.pdf" class="bare">http://www.dartlang.org/docs/spec/latest/dart-language-specification.pdf</a></p>
</div>
<div class="paragraph">
<p><a id="OMG14a"></a>OMG. (2014). <em>Interface Definition Language</em>. Object Management Group. Retrieved from <a href="http://www.omg.org/cgi-bin/doc?formal/2014-03-01.pdf" class="bare">http://www.omg.org/cgi-bin/doc?formal/2014-03-01.pdf</a></p>
</div>
<div class="paragraph">
<p><a id="W3C12a"></a>W3C. (2012). <em>Web IDL Specification</em>. Retrieved from <a href="http://www.w3.org/TR/2012/CR-WebIDL-20120419/" class="bare">http://www.w3.org/TR/2012/CR-WebIDL-20120419/</a></p>
</div>
<div class="paragraph">
<p><a id="Kuizinas14a"></a>Kuizinas, Gajus. (2014). <em>The Definitive Guide to the JavaScript Generators</em>. Retrieved from <a href="http://gajus.com/blog/2/the-definetive-guide-to-the-javascript-generators" class="bare">http://gajus.com/blog/2/the-definetive-guide-to-the-javascript-generators</a></p>
</div>
<div class="paragraph">
<p><a id="West06a"></a>West, Mike. (2006). <em>Scope in JavaScript</em>. Retrieved from <a href="http://www.digital-web.com/articles/scope_in_javascript/" class="bare">http://www.digital-web.com/articles/scope_in_javascript/</a></p>
</div>
<div class="paragraph">
<p><a id="ECMA18a"></a>ECMA. (2018). <em>ECMAScript 2018 Language Specification</em>. Retrieved from <a href="http://www.ecma-international.org/ecma-262/9.0/index.html" class="bare">http://www.ecma-international.org/ecma-262/9.0/index.html</a></p>
</div>
<div class="paragraph">
<p><a id="MozillaJSRef"></a><em>JavaScript Reference</em>. Retrieved from <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference" class="bare">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference</a></p>
</div>
<div class="paragraph">
<p><a id="Fowler04b"></a>Fowler, Martin. (2004). <em>Inversion of Control Containers and the Dependency Injection pattern</em>. Retrieved from <a href="http://martinfowler.com/articles/injection.html" class="bare">http://martinfowler.com/articles/injection.html</a></p>
</div>
<div class="paragraph">
<p><a id="Prasanna09a"></a>Prasanna, Dhanji R. (2009). <em>Dependency Injection: Design Patterns Using Spring and Guice</em>. </p>
</div>
<div class="paragraph">
<p><a id="Zhu13a"></a>Zhu, He and Jagannathan, Suresh. (2013). <em>Compositional and Lightweight Dependent Type Inference for ML</em>. Retrieved from <a href="http://dx.doi.org/10.1007/978-3-642-35873-9_19" class="bare">http://dx.doi.org/10.1007/978-3-642-35873-9_19</a></p>
</div>
<div class="paragraph">
<p><a id="Hudli13a"></a>Hudli, Shrinidhi R. and Hudli, Raghu V.. (2013). <em>A Verification Strategy for Dependency Injection</em>. </p>
</div>
<div class="paragraph">
<p><a id="Lesiecki08a"></a>Lesiecki, Nicholas. (2008). <em>Dependency injection with Guice</em>. Retrieved from <a href="http://www.ibm.com/developerworks/library/j-guice/" class="bare">http://www.ibm.com/developerworks/library/j-guice/</a></p>
</div>
<div class="paragraph">
<p><a id="Betts13a"></a>Betts, Dominic and Melnik, Grigori and Simonazzi, Fernando and Subramanian, Mani. (2013). <em>Dependency Injection with Unity</em>. </p>
</div>
<div class="paragraph">
<p><a id="Knol13a"></a>Knol, Alex. (2013). <em>Dependency injection with AngularJS</em>. </p>
</div>
<div class="paragraph">
<p><a id="Dagger"></a><em>Dagger, Project Website</em>. Retrieved from <a href="http://square.github.io/dagger/" class="bare">http://square.github.io/dagger/</a></p>
</div>
<div class="paragraph">
<p><a id="ECMA12a"></a>(2012). <em>ECMAScript Internationalization API Specification</em>. Retrieved from <a href="http://www.ecma-international.org/publications/standards/Ecma-402.htm" class="bare">http://www.ecma-international.org/publications/standards/Ecma-402.htm</a></p>
</div>
<div class="paragraph">
<p><a id="Martin96b"></a>Martin, Robert C. (1996). <em>The Liskov Substitution Principle</em>. Retrieved from <a href="http://www.objectmentor.com/publications/lsp.pdf" class="bare">http://www.objectmentor.com/publications/lsp.pdf</a></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div id="footnotes">
<hr>
<div class="footnote" id="_footnote_1">
<a href="#_footnoteref_1">1</a>. A brief introduction can be found at <a href="http://www.cs.cornell.edu/~ross/publications/mixedsite/tutorial.html" class="bare">http://www.cs.cornell.edu/~ross/publications/mixedsite/tutorial.html</a>. In general, we refer the reader to [<a href="#Pierce02a">Pierce02a</a>]
</div>
<div class="footnote" id="_footnote_2">
<a href="#_footnoteref_2">2</a>. One can interpret this similar to delegate methods, that is, instead of writing <code class="language-n4js">r.binding().getType()</code>, a method <code class="language-n4js">r.getType()\{return binding().getType();</code> is defined.
</div>
<div class="footnote" id="_footnote_3">
<a href="#_footnoteref_3">3</a>. <a href="http://javascript.crockford.com/code.html" class="bare">http://javascript.crockford.com/code.html</a>
</div>
<div class="footnote" id="_footnote_4">
<a href="#_footnoteref_4">4</a>. See for definitions of metatype properties.
</div>
<div class="footnote" id="_footnote_5">
<a href="#_footnoteref_5">5</a>. 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.
</div>
<div class="footnote" id="_footnote_6">
<a href="#_footnoteref_6">6</a>. See <a href="#_n4js-specific-classifiers">N4JS Specific Classifiers</a> 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.
</div>
<div class="footnote" id="_footnote_7">
<a href="#_footnoteref_7">7</a>. Note the Java-like access restriction for members of visibility <code class="language-n4js">protected</code> or <code class="language-n4js">protected@Internal</code> to code that is responsible for the implementation of that object. [<a href="#Gosling15a">Gosling15a(p.S6.6.2, p.p.166)</a>]
</div>
<div class="footnote" id="_footnote_8">
<a href="#_footnoteref_8">8</a>. <code class="language-n4js">string, boolean, number, any, null</code>
</div>
<div class="footnote" id="_footnote_9">
<a href="#_footnoteref_9">9</a>. This conflicts with the implicit parameter arguments introduced by the transpiler when wrapping the script/module into a definition function.
</div>
<div class="footnote" id="_footnote_10">
<a href="#_footnoteref_10">10</a>. In the N4JS IDE, type annotations are highlighted differently than ordinary code.
</div>
<div class="footnote" id="_footnote_11">
<a href="#_footnoteref_11">11</a>. The notation <math xmlns="http://www.w3.org/1998/Math/MathML"><mo>|</mo><mi>T</mi><mo>|</mo></math> used in [<a href="#Gosling12a">Gosling12a</a>] 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 [<a href="#Crary02a">Crary02a</a>], 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><sup>o</sup>.
</div>
<div class="footnote" id="_footnote_12">
<a href="#_footnoteref_12">12</a>. Although raw type usage is prohibited, the N4JS validator interprets raw types according to the first case, which may lead to consequential errors.
</div>
<div class="footnote" id="_footnote_13">
<a href="#_footnoteref_13">13</a>. in the Java 8 spec and compiler, they are called type variables, which are types as well
</div>
<div class="footnote" id="_footnote_14">
<a href="#_footnoteref_14">14</a>. 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, <strong>this is checked neither statically nor at runtime</strong>!
</div>
<div class="footnote" id="_footnote_15">
<a href="#_footnoteref_15">15</a>. This is a consequence of the syntax definition.
</div>
<div class="footnote" id="_footnote_16">
<a href="#_footnoteref_16">16</a>. For type theory about union types, [<a href="#Pierce02a">Pierce02a(p.15.7)</a>] and [<a href="#Igarashi07a">Igarashi07a</a>], other languages that explicitly support the notion of union type include Ceylon [<a href="#King13a">King13a(p.3.2.4/5)</a>]
</div>
<div class="footnote" id="_footnote_17">
<a href="#_footnoteref_17">17</a>. This is different from Ceylon ( [<a href="#King13a">King13a(p.3.2.3)</a>]), in which the union is defined to be <code>the same type as</code> <em>A</em>. Although the meaning of <code>same</code> is not clear, it is possibly used as a synonym for <code>equivalent</code>.
</div>
<div class="footnote" id="_footnote_18">
<a href="#_footnoteref_18">18</a>. See Example <a href="#ex:class-hierarchy">Type Examples, Class Hierarchy</a> for class definitions.
</div>
<div class="footnote" id="_footnote_19">
<a href="#_footnoteref_19">19</a>. For type theory about intersection types, see [<a href="#Pierce02a">Pierce02a(p.15.7)</a>] and [<a href="#Laurent12a">Laurent12a</a>], other languages supporting explicit notion of intersection type include Ceylon [<a href="#King13a">King13a(p.3.2.4/5)</a>].
</div>
<div class="footnote" id="_footnote_20">
<a href="#_footnoteref_20">20</a>. This rule is a generalization of the subtyping rules given in [<a href="#Laurent12a">Laurent12a</a>] 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>
</div>
<div class="footnote" id="_footnote_21">
<a href="#_footnoteref_21">21</a>. This rule is a generalization of the subtyping rules given in [<a href="#Laurent12a">Laurent12a</a>] Table 2, <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mo></mo><mi>r</mi></msub></math>
</div>
<div class="footnote" id="_footnote_22">
<a href="#_footnoteref_22">22</a>. 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 [<a href="#King13a">King13a(p.3.2.4/5)</a>]
</div>
<div class="footnote" id="_footnote_23">
<a href="#_footnoteref_23">23</a>. See Example <a href="#ex:class-hierarchy">Type Examples, Class Hierarchy</a> for class definitions.
</div>
<div class="footnote" id="_footnote_24">
<a href="#_footnoteref_24">24</a>. see "Covariance and contravariance with unions and intersections" at <a href="http://ceylon-lang.org/documentation/1.1/tour/generics/" class="bare">http://ceylon-lang.org/documentation/1.1/tour/generics/</a>
</div>
<div class="footnote" id="_footnote_25">
<a href="#_footnoteref_25">25</a>. The classifier type is, in fact, the <code>type type</code> or <code>metatype</code> of a type. We use the term classifier type in the specification to avoid the bogus <code>type type</code> terminology.
</div>
<div class="footnote" id="_footnote_26">
<a href="#_footnoteref_26">26</a>. The phenomenon is described in IDEBUG-263
</div>
<div class="footnote" id="_footnote_27">
<a href="#_footnoteref_27">27</a>. E.g., for given <code class="language-n4js">class A{ foo(A):A{}} class B{ foo(B):B{}}</code>, a class C could be defined as <code class="language-n4js">class C{ foo(union{A,B}):intersection{A,B}{}}</code>. 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.
</div>
<div class="footnote" id="_footnote_28">
<a href="#_footnoteref_28">28</a>. <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>
</div>
<div class="footnote" id="_footnote_29">
<a href="#_footnoteref_29">29</a>. not yet implemented
</div>
<div class="footnote" id="_footnote_30">
<a href="#_footnoteref_30">30</a>. In order to emulate method overloading, union types are to be used.
</div>
<div class="footnote" id="_footnote_31">
<a href="#_footnoteref_31">31</a>. [<a href="#ECMA15a">ECMA15a(p.p214)</a>], <code>ClassBody : ClassElementList</code> indicates that it is possible to have the same name for instance and static members.
</div>
<div class="footnote" id="_footnote_32">
<a href="#_footnoteref_32">32</a>. version 4.0
</div>
<div class="footnote" id="_footnote_33">
<a href="#_footnoteref_33">33</a>. This cannot be done w/o <code>null</code>/<code>undefined</code> analysis
</div>
<div class="footnote" id="_footnote_34">
<a href="#_footnoteref_34">34</a>. 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.
</div>
<div class="footnote" id="_footnote_35">
<a href="#_footnoteref_35">35</a>. Thus, the type of one accessor is not used to infer the type of the other one. E.g., from <code>set x(string s)</code>, we cannot infer <code>get x()</code> to return <code>string</code> — instead, the getter is inferred to return <code>any</code>.
</div>
<div class="footnote" id="_footnote_36">
<a href="#_footnoteref_36">36</a>. The technical reason for this rule is the way properties are stored in JavaScript. Take for an example subclass-write access : <code class="language-n4js">class C { static f="a";}</code> with <code class="language-n4js">class D extends C { }</code>. Now the data field <code>f</code> on <code>C</code> can also be queried using <code>D</code> (<code class="language-n4js">var q=D.f;</code>) but writing (<code class="language-n4js">D.f="b";</code>) would introduce a newly created property <code>f</code> on class <code>D</code>, which differs from the one defined on <code>C</code>. It would do this without explicitly overriding the inherited property. Subsequent reads to <code class="language-n4js">D.f</code> 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.
</div>
<div class="footnote" id="_footnote_37">
<a href="#_footnoteref_37">37</a>. There had been the idea of preventing static members of being consumed. However, this would break the type subtype relation.
</div>
<div class="footnote" id="_footnote_38">
<a href="#_footnoteref_38">38</a>. <code>C</code> could either be a class or an interface.
</div>
<div class="footnote" id="_footnote_39">
<a href="#_footnoteref_39">39</a>. As defined in <a href="#_function-type">Function Type</a> for function types.
</div>
<div class="footnote" id="_footnote_40">
<a href="#_footnoteref_40">40</a>. 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.
</div>
<div class="footnote" id="_footnote_41">
<a href="#_footnoteref_41">41</a>. 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 <code>N4Object</code>).
</div>
<div class="footnote" id="_footnote_42">
<a href="#_footnoteref_42">42</a>. 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.
</div>
<div class="footnote" id="_footnote_43">
<a href="#_footnoteref_43">43</a>. Note that due to this relaxed definition (compared with definition-site structural types) it is possible to pass an <code>N4Object</code> instance to a function of method with a declared structural type parameter.
</div>
<div class="footnote" id="_footnote_44">
<a href="#_footnoteref_44">44</a>. This is already constrained by the grammar.
</div>
<div class="footnote" id="_footnote_45">
<a href="#_footnoteref_45">45</a>. see <a href="http://tc39.github.io/ecmascript-asyncawait/" class="bare">http://tc39.github.io/ecmascript-asyncawait/</a>
</div>
<div class="footnote" id="_footnote_46">
<a href="#_footnoteref_46">46</a>. 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).
</div>
<div class="footnote" id="_footnote_47">
<a href="#_footnoteref_47">47</a>. <code>i</code> iterates over all boundaries
</div>
<div class="footnote" id="_footnote_48">
<a href="#_footnoteref_48">48</a>. <code>i</code> iterates over all type args
</div>
<div class="footnote" id="_footnote_49">
<a href="#_footnoteref_49">49</a>. Only <code class="language-n4js">Function</code> implements the ECMAScript specification property <code class="language-n4js">hasInstance</code>. 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.
</div>
<div class="footnote" id="_footnote_50">
<a href="#_footnoteref_50">50</a>. 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>
</div>
<div class="footnote" id="_footnote_51">
<a href="#_footnoteref_51">51</a>. See [<a href="#ECMA15a">ECMA15a</a>], Chapter 12.3.5 "The Super Keyword"; note the use of <code>HomeObject</code> instead of <code>thisValue</code>; also see this blog - <a href="http://www.2ality.com/2011/11/super-references.html" class="bare">http://www.2ality.com/2011/11/super-references.html</a>.
</div>
<div class="footnote" id="_footnote_52">
<a href="#_footnoteref_52">52</a>. <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>.
</div>
<div class="footnote" id="_footnote_53">
<a href="#_footnoteref_53">53</a>. available at: <a href="http://www.2ality.com/2013/06/iterators-generators.html" class="bare">http://www.2ality.com/2013/06/iterators-generators.html</a>
</div>
<div class="footnote" id="_footnote_54">
<a href="#_footnoteref_54">54</a>. For more details on module specifiers, see <a href="#_qualified-names">Qualified Names</a>.
</div>
<div class="footnote" id="_footnote_55">
<a href="#_footnoteref_55">55</a>. Further reading on <a href="#_acronyms">DI</a> Basics: [<a href="#Fowler04b">Fowler04b</a>; <a href="#Prasanna09a">Prasanna09a</a>], Verification [<a href="#Zhu13a">Zhu13a</a>; <a href="#Hudli13a">Hudli13a</a>], Frameworks [<a href="#Lesiecki08a">Lesiecki08a</a>; <a href="#Betts13a">Betts13a</a>; <a href="#Knol13a">Knol13a</a>; <a href="#Dagger">Dagger</a>]
</div>
<div class="footnote" id="_footnote_56">
<a href="#_footnoteref_56">56</a>. Usually, only the <code>DIC</code> itself is created like that, e.g., <code class="language-n4js">var dic = N4Injector.of(DIC).create(DIC);</code>
</div>
<div class="footnote" id="_footnote_57">
<a href="#_footnoteref_57">57</a>. Note that other frameworks may define other constraints, e.g., arbitrary keys.
</div>
<div class="footnote" id="_footnote_58">
<a href="#_footnoteref_58">58</a>. Cf. a blog post about micro types - <a href="http://www.markhneedham.com/blog/2009/03/10/oo-micro-types/" class="bare">http://www.markhneedham.com/blog/2009/03/10/oo-micro-types/</a>, and tiny types - <a href="http://darrenhobbs.com/2007/04/11/tiny-types/" class="bare">http://darrenhobbs.com/2007/04/11/tiny-types/</a>
</div>
<div class="footnote" id="_footnote_59">
<a href="#_footnoteref_59">59</a>. 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.
</div>
<div class="footnote" id="_footnote_60">
<a href="#_footnoteref_60">60</a>. <a href="#_api-and-implementation-components">[_api-and-implementation-components]</a>
</div>
<div class="footnote" id="_footnote_61">
<a href="#_footnoteref_61">61</a>. Global basically means that the module defines no namespace on its own. Thus the annotation is a script/module related annotation.
</div>
</div>
<div id="footer">
<div id="footer-text">
Version 0.9<br>
Last updated 2019-08-08 13:15:33 CEST
</div>
</div>
<!-- ************* docinfo-footer *************************************************************** -->
<div class="Grid social" style="color:#d5dfea">
<div class="Cell Cell--2-12 m-Cell--withMargin">
<h2>Quick Links</h2>
<ul>
<li><a href="../downloads.html">Download</a></li>
<li><a href="../userguides/index.html">Documentation</a></li>
<li><a href="https://github.com/eclipse/n4js/">Source</a></li>
<li><a href="https://github.com/eclipse/n4js/issues">Issues</a></li>
</ul>
</div>
<div class="Cell Cell--2-12 m-Cell--withMargin">
<br/><br/>
<ul>
<li><a href="https://www.eclipse.org/forums/index.php/f/365/">Forum</a></li>
<li><a href="http://n4js.blogspot.de/">Blog</a></li>
<li><a href="https://dev.eclipse.org/mailman/listinfo/n4js-dev">Mailing List</a></li>
<li><a href="https://projects.eclipse.org/projects/technology.n4js">Eclipse Project Page</a></li>
<li><a href="https://twitter.com/n4jsdev">Tweets by n4jsdev</a></li>
</ul>
</div>
<div class="Cell Cell--2-12 m-Cell--withMargin">
<br/><br/>
<ul>
<li><a href="http://www.eclipse.org/">Eclipse Home</a></li>
<li><a href="http://www.eclipse.org/legal/privacy.php">Privacy Policy</a></li>
<li><a href="http://www.eclipse.org/legal/termsofuse.php">Terms of Use</a></li>
<li><a href="http://www.eclipse.org/legal/copyright.php">Copyright Agent</a></li>
<li><a href="http://www.eclipse.org/legal/">Legal</a></li>
</ul>
</div>
<div style="clear: both; height: 0; overflow: hidden;"></div>
</div>
<!-- ************* UI Scripts ************* -->
<script type="text/javascript" src="scripts/back-to-top.js"></script>
<script type="text/javascript" src="scripts/treeview.js"></script>
<script type="text/javascript" src="scripts/toc.js"></script>
<!-- ************* Prism.js Syntax Highlighting ************* -->
<script src="scripts/prism.js"></script>
<script type="text/javascript">
// Add the 'toclist' id for search function
$(".toc2 > ul").attr('id', 'toclist');
// Generate a Search input form
$("#toclist > li:first-of-type").before('<input type="text" id="pagesearch" onkeyup="search()" placeholder="Search for section...">');
$("#toclist > li:first-of-type").before('<i id="clear" class="fa fa-times-circle-o"></i>');
$("#clear").click(function(){
$("#pagesearch").val('');
search();
$('.toc2 > ul').treeView('collapseAll');
});
// intialize Treeview.js
$(".toc2 > ul").treeView();
// Initialize Scrollspy
</script>
<!-- ************* docinfo-footer *************************************************************** -->
</body>
</html>