blob: ffba1edfe23de320b388e4691a5482fa2a7be0c1 [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.html#_introduction">1. Introduction</a>
<ul class="sectlevel2">
<li><a href="introduction.html#_notation">1.1. Notation</a>
<ul class="sectlevel3">
<li><a href="introduction.html#_grammar-notation">1.1.1. Grammar Notation</a></li>
<li><a href="introduction.html#_type-judgments-and-rules-and-constraints-notation">1.1.2. Type Judgments and Rules and Constraints Notation</a>
<ul class="sectlevel4">
<li><a href="introduction.html#_typing-rules-and-judgments">1.1.2.1. Typing Rules and Judgments</a></li>
<li><a href="introduction.html#_types-of-an-element">1.1.2.2. Types of an Element</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="introduction.html#_auxiliary-functions">1.2. Auxiliary Functions</a>
<ul class="sectlevel3">
<li><a href="introduction.html#_binding">1.2.1. Binding</a></li>
<li><a href="introduction.html#_merging-types">1.2.2. Merging Types</a>
<ul class="sectlevel4">
<li><a href="introduction.html#_logic-formulas">1.2.2.1. Logic Formulas</a></li>
</ul>
</li>
<li><a href="introduction.html#_symbols-and-font-convention">1.2.3. Symbols and Font Convention</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="grammar.html#_grammar">2. Grammar</a>
<ul class="sectlevel2">
<li><a href="grammar.html#_lexical-conventions">2.1. Lexical Conventions</a>
<ul class="sectlevel3">
<li><a href="grammar.html#_identifier-names-and-identifiers">2.1.1. Identifier Names and Identifiers</a></li>
<li><a href="grammar.html#_this-keyword">2.1.2. This Keyword</a></li>
<li><a href="grammar.html#_regular-expression-literals">2.1.3. Regular Expression Literals</a></li>
<li><a href="grammar.html#_automatic-semicolon-insertion">2.1.4. Automatic Semicolon Insertion</a></li>
<li><a href="grammar.html#_jsdoc">2.1.5. JSDoc</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="names.html#_names">3. Names</a>
<ul class="sectlevel2">
<li><a href="names.html#_access-control">3.1. Access Control</a></li>
<li><a href="names.html#_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="names.html#_accessibility-of-members">3.2.1. Accessibility of Members</a></li>
<li><a href="names.html#_valid-names">3.2.2. Valid Names</a></li>
<li><a href="names.html#_qualified-names">3.2.3. Qualified Names</a></li>
<li><a href="names.html#_name-duplicates">3.2.4. Name Duplicates</a>
<ul class="sectlevel4">
<li><a href="names.html#_lexical-environment">3.2.4.1. Lexical Environment</a></li>
<li><a href="names.html#_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.html#_classifiers">5. Classifiers</a>
<ul class="sectlevel2">
<li><a href="classifiers.html#_n4js-specific-classifiers">5.1. N4JS Specific Classifiers</a>
<ul class="sectlevel3">
<li><a href="classifiers.html#_properties-2">5.1.1. Properties</a></li>
<li><a href="classifiers.html#_common-semantics-of-classifiers">5.1.2. Common Semantics of Classifiers</a></li>
<li><a href="classifiers.html#_classes">5.1.3. Classes</a>
<ul class="sectlevel4">
<li><a href="classifiers.html#_definition-of-classes">5.1.3.1. Definition of Classes</a>
<ul class="sectlevel5">
<li><a href="classifiers.html#class-syntax">5.1.3.1.1. Syntax</a></li>
<li><a href="classifiers.html#class-properties">5.1.3.1.2. Properties</a></li>
<li><a href="classifiers.html#class-type-inference">5.1.3.1.3. Type Inference</a></li>
</ul>
</li>
<li><a href="classifiers.html#class-semantics">5.1.3.2. Semantics</a></li>
<li><a href="classifiers.html#_final-modifier">5.1.3.3. Final Modifier</a></li>
<li><a href="classifiers.html#_abstract-classes">5.1.3.4. Abstract Classes</a></li>
<li><a href="classifiers.html#_non-instantiable-classes">5.1.3.5. Non-Instantiable Classes</a></li>
<li><a href="classifiers.html#_superclass">5.1.3.6. Superclass</a></li>
</ul>
</li>
<li><a href="classifiers.html#_interfaces">5.1.4. Interfaces</a>
<ul class="sectlevel4">
<li><a href="classifiers.html#_definition-of-interfaces">5.1.4.1. Definition of Interfaces</a>
<ul class="sectlevel5">
<li><a href="classifiers.html#interfaces-syntax">5.1.4.1.1. Syntax</a></li>
<li><a href="classifiers.html#interfaces-properties">5.1.4.1.2. Properties</a></li>
<li><a href="classifiers.html#interfaces-type-inference">5.1.4.1.3. Type Inference</a></li>
<li><a href="classifiers.html#interfaces-semantics">5.1.4.1.4. Semantics</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="classifiers.html#_generic-classifiers">5.1.5. Generic Classifiers</a></li>
<li><a href="classifiers.html#sec:definition-site-variance">5.1.6. Definition-Site Variance</a></li>
</ul>
</li>
<li><a href="classifiers.html#_members">5.2. Members</a>
<ul class="sectlevel3">
<li><a href="classifiers.html#_syntax-4">5.2.1. Syntax</a>
<ul class="sectlevel4">
<li><a href="classifiers.html#_properties-3">5.2.1.1. Properties</a></li>
</ul>
</li>
<li><a href="classifiers.html#_semantics-3">5.2.2. Semantics</a></li>
<li><a href="classifiers.html#_methods">5.2.3. Methods</a>
<ul class="sectlevel4">
<li><a href="classifiers.html#_syntax-5">5.2.3.1. Syntax</a></li>
<li><a href="classifiers.html#_properties-4">5.2.3.2. Properties</a></li>
<li><a href="classifiers.html#_semantics-4">5.2.3.3. Semantics</a></li>
<li><a href="classifiers.html#_final-methods">5.2.3.4. Final Methods</a></li>
<li><a href="classifiers.html#_abstract-methods">5.2.3.5. Abstract Methods</a></li>
<li><a href="classifiers.html#_generic-methods">5.2.3.6. Generic Methods</a></li>
</ul>
</li>
<li><a href="classifiers.html#_default-methods-in-interfaces">5.2.4. Default Methods in Interfaces</a>
<ul class="sectlevel4">
<li><a href="classifiers.html#_asynchronous-methods">5.2.4.1. Asynchronous Methods</a></li>
</ul>
</li>
<li><a href="classifiers.html#_constructors">5.2.5. Constructors</a>
<ul class="sectlevel4">
<li><a href="classifiers.html#_structural-this-type-in-constructor">5.2.5.1. Structural This Type in Constructor</a></li>
<li><a href="classifiers.html#spec-constructor">5.2.5.2. @Spec Constructor</a></li>
<li><a href="classifiers.html#_callable-constructors">5.2.5.3. Callable Constructors</a></li>
<li><a href="classifiers.html#_covariant-constructors">5.2.5.4. Covariant Constructors</a></li>
</ul>
</li>
<li><a href="classifiers.html#_data-fields">5.2.6. Data Fields</a>
<ul class="sectlevel4">
<li><a href="classifiers.html#data-fields-syntax">5.2.6.1. Syntax</a></li>
<li><a href="classifiers.html#data-fields-properties">5.2.6.2. Properties</a>
<ul class="sectlevel5">
<li><a href="classifiers.html#data-fields-semantics">5.2.6.2.1. Semantics</a></li>
<li><a href="classifiers.html#data-fields-type-inference">5.2.6.2.2. Type Inference</a></li>
</ul>
</li>
<li><a href="classifiers.html#_assignment-modifiers">5.2.6.3. Assignment Modifiers</a></li>
<li><a href="classifiers.html#_field-accessors-getter-setter">5.2.6.4. Field Accessors (Getter/Setter)</a>
<ul class="sectlevel5">
<li><a href="classifiers.html#field-acessors-syntax">5.2.6.4.1. Syntax</a></li>
<li><a href="classifiers.html#field-acessors-properties">5.2.6.4.2. Properties</a></li>
<li><a href="classifiers.html#field-accessors-semantics">5.2.6.4.3. Semantics</a></li>
</ul>
</li>
<li><a href="classifiers.html#optional-fields">5.2.6.5. Optional Fields</a>
<ul class="sectlevel5">
<li><a href="classifiers.html#_syntax-6">5.2.6.5.1. Syntax</a></li>
<li><a href="classifiers.html#_semantics-5">5.2.6.5.2. Semantics</a></li>
<li><a href="classifiers.html#_background">5.2.6.5.3. Background</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="classifiers.html#_static-members">5.2.7. Static Members</a>
<ul class="sectlevel4">
<li><a href="classifiers.html#_access-from-and-to-static-members">5.2.7.1. Access From and To Static Members</a></li>
<li><a href="classifiers.html#_generic-static-methods">5.2.7.2. Generic static methods</a></li>
<li><a href="classifiers.html#_static-members-of-interfaces">5.2.7.3. Static Members of Interfaces</a></li>
</ul>
</li>
<li><a href="classifiers.html#_redefinition-of-members">5.2.8. Redefinition of Members</a>
<ul class="sectlevel4">
<li><a href="classifiers.html#_overriding-of-members">5.2.8.1. Overriding of Members</a></li>
<li><a href="classifiers.html#_implementation-of-members">5.2.8.2. Implementation of Members</a>
<ul class="sectlevel5">
<li><a href="classifiers.html#_member-consumption">5.2.8.2.1. Member Consumption</a></li>
<li><a href="classifiers.html#_member-implementation">5.2.8.2.2. Member Implementation</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="classifiers.html#_structural-typing">5.3. Structural Typing</a>
<ul class="sectlevel3">
<li><a href="classifiers.html#_syntax-7">5.3.1. Syntax</a></li>
<li><a href="classifiers.html#_definition-site-structural-typing">5.3.2. Definition Site Structural Typing</a></li>
<li><a href="classifiers.html#_use-site-structural-typing">5.3.3. Use-Site Structural Typing</a></li>
<li><a href="classifiers.html#structural-readWriteInit-field-typing">5.3.4. Structural Read-only, Write-only and Initializer Field Typing</a></li>
<li><a href="classifiers.html#_public-setter-annotated-with-code-providesinitializer-code">5.3.5. Public Setter Annotated With <code>ProvidesInitializer</code></a></li>
<li><a href="classifiers.html#_structural-types-with-optional-fields">5.3.6. Structural Types With Optional Fields</a></li>
<li><a href="classifiers.html#_structural-types-with-access-modifier">5.3.7. Structural Types With Access Modifier</a></li>
<li><a href="classifiers.html#_structural-types-with-additional-members">5.3.8. Structural Types With Additional Members</a>
<ul class="sectlevel4">
<li><a href="classifiers.html#_syntax-8">5.3.8.1. Syntax</a>
<ul class="sectlevel5">
<li><a href="classifiers.html#_semantics-6">5.3.8.1.1. Semantics</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="functions.html#_functions">6. Functions</a>
<ul class="sectlevel2">
<li><a href="functions.html#_function-type">6.1. Function Type</a>
<ul class="sectlevel3">
<li><a href="functions.html#_properties-5">6.1.1. Properties</a></li>
<li><a href="functions.html#function-type-inference">6.1.2. Type Inference</a></li>
<li><a href="functions.html#_autoboxing-of-function-type">6.1.3. Autoboxing of Function Type</a></li>
<li><a href="functions.html#_arguments-object">6.1.4. Arguments Object</a></li>
</ul>
</li>
<li><a href="functions.html#_ecmascript-5-function-definition">6.2. ECMAScript 5 Function Definition</a>
<ul class="sectlevel3">
<li><a href="functions.html#_function-declaration">6.2.1. Function Declaration</a>
<ul class="sectlevel4">
<li><a href="functions.html#_syntax-9">6.2.1.1. Syntax</a></li>
<li><a href="functions.html#_semantics-8">6.2.1.2. Semantics</a></li>
</ul>
</li>
<li><a href="functions.html#_function-expression">6.2.2. Function Expression</a>
<ul class="sectlevel4">
<li><a href="functions.html#function-expression-syntax">6.2.2.1. Syntax</a></li>
<li><a href="functions.html#_semantics-and-type-inference">6.2.2.2. Semantics and Type Inference</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="functions.html#_ecmascript-2015-function-definition">6.3. ECMAScript 2015 Function Definition</a>
<ul class="sectlevel3">
<li><a href="functions.html#_formal-parameters">6.3.1. Formal Parameters</a>
<ul class="sectlevel4">
<li><a href="functions.html#Type_Modifiers_Optional">6.3.1.1. Optional Parameters</a></li>
<li><a href="functions.html#Type_Modifiers_Default">6.3.1.2. Default Parameters</a></li>
<li><a href="functions.html#Type_Modifiers_Variadic">6.3.1.3. Variadic</a></li>
</ul>
</li>
<li><a href="functions.html#_generator-functions">6.3.2. Generator Functions</a>
<ul class="sectlevel4">
<li><a href="functions.html#generator-functions-syntax">6.3.2.1. Syntax</a></li>
<li><a href="functions.html#generator-functions-semantics">6.3.2.2. Semantics</a></li>
<li><a href="functions.html#_generator-arrow-functions">6.3.2.3. Generator Arrow Functions</a></li>
</ul>
</li>
<li><a href="functions.html#_arrow-function-expression">6.3.3. Arrow Function Expression</a>
<ul class="sectlevel4">
<li><a href="functions.html#arrow-function-expression-syntax">6.3.3.1. Syntax</a></li>
<li><a href="functions.html#arrow-function-expression-semantics-and-type-inference">6.3.3.2. Semantics and Type Inference</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="functions.html#_ecmascript-proposals-function-definition">6.4. ECMAScript Proposals Function Definition</a>
<ul class="sectlevel3">
<li><a href="functions.html#_asynchronous-functions">6.4.1. Asynchronous Functions</a>
<ul class="sectlevel4">
<li><a href="functions.html#asynchronous-functions-syntax">6.4.1.1. Syntax</a></li>
<li><a href="functions.html#asynchronous-functions-semantics">6.4.1.2. Semantics</a></li>
<li><a href="functions.html#_asynchronous-arrow-functions">6.4.1.3. Asynchronous Arrow Functions</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="functions.html#_n4js-extended-function-definition">6.5. N4JS Extended Function Definition</a>
<ul class="sectlevel3">
<li><a href="functions.html#_generic-functions">6.5.1. Generic Functions</a></li>
<li><a href="functions.html#_promisifiable-functions">6.5.2. Promisifiable Functions</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="conversions_and_reflection.html#_conversions-and-reflection">7. Conversions and Reflection</a>
<ul class="sectlevel2">
<li><a href="conversions_and_reflection.html#_autoboxing-and-coercing">7.1. Autoboxing and Coercing</a>
<ul class="sectlevel3">
<li><a href="conversions_and_reflection.html#_coercing">7.1.1. Coercing</a></li>
<li><a href="conversions_and_reflection.html#_autoboxing-of-primitives">7.1.2. Autoboxing of Primitives</a></li>
<li><a href="conversions_and_reflection.html#_autoboxing-of-function-expressions-and-declarations">7.1.3. Autoboxing of Function Expressions and Declarations</a></li>
</ul>
</li>
<li><a href="conversions_and_reflection.html#_auto-conversion-of-objects">7.2. Auto-Conversion of Objects</a>
<ul class="sectlevel3">
<li><a href="conversions_and_reflection.html#_auto-conversion-of-class-instances">7.2.1. Auto-Conversion of Class Instances</a>
<ul class="sectlevel4">
<li><a href="conversions_and_reflection.html#_auto-conversion-of-interface-instances">7.2.1.1. Auto-Conversion of Interface Instances</a></li>
</ul>
</li>
<li><a href="conversions_and_reflection.html#_auto-conversion-of-enum-literals">7.2.2. Auto-Conversion of Enum Literals</a></li>
</ul>
</li>
<li><a href="conversions_and_reflection.html#_type-cast-and-type-check">7.3. Type Cast and Type Check</a>
<ul class="sectlevel3">
<li><a href="conversions_and_reflection.html#_type-cast">7.3.1. Type Cast</a></li>
<li><a href="conversions_and_reflection.html#_type-check">7.3.2. Type Check</a></li>
</ul>
</li>
<li><a href="conversions_and_reflection.html#_reflection-meta-information">7.4. Reflection meta-information</a>
<ul class="sectlevel3">
<li><a href="conversions_and_reflection.html#_reflection-for-classes">7.4.1. Reflection for Classes</a></li>
<li><a href="conversions_and_reflection.html#_reflection-for-interfaces">7.4.2. Reflection for Interfaces</a></li>
<li><a href="conversions_and_reflection.html#_reflection-for-enumerations">7.4.3. Reflection for Enumerations</a></li>
</ul>
</li>
<li><a href="conversions_and_reflection.html#_conversion-of-primitive-types">7.5. Conversion of primitive types</a></li>
</ul>
</li>
<li><a href="expressions.html#_expressions">8. Expressions</a>
<ul class="sectlevel2">
<li><a href="expressions.html#_ecmascript-5-expressions">8.1. ECMAScript 5 Expressions</a>
<ul class="sectlevel3">
<li><a href="expressions.html#_the-this-literal">8.1.1. The this Literal</a></li>
<li><a href="expressions.html#_identifier">8.1.2. Identifier</a></li>
<li><a href="expressions.html#_literals">8.1.3. Literals</a>
<ul class="sectlevel4">
<li><a href="expressions.html#_integer-literals">8.1.3.1. Integer Literals</a></li>
</ul>
</li>
<li><a href="expressions.html#_array-literal">8.1.4. Array Literal</a></li>
<li><a href="expressions.html#_object-literal">8.1.5. Object Literal</a>
<ul class="sectlevel4">
<li><a href="expressions.html#_properties-6">8.1.5.1. Properties</a></li>
<li><a href="expressions.html#_scoping-and-linking">8.1.5.2. Scoping and linking</a></li>
</ul>
</li>
<li><a href="expressions.html#_parenthesized-expression-and-grouping-operator">8.1.6. Parenthesized Expression and Grouping Operator</a></li>
<li><a href="expressions.html#_property-accessors">8.1.7. Property Accessors</a>
<ul class="sectlevel4">
<li><a href="expressions.html#properties-1">8.1.7.1. Properties</a></li>
</ul>
</li>
<li><a href="expressions.html#_new-expression">8.1.8. New Expression</a></li>
<li><a href="expressions.html#_function-expression-2">8.1.9. Function Expression</a></li>
<li><a href="expressions.html#_function-calls">8.1.10. Function Calls</a></li>
<li><a href="expressions.html#_postfix-expression">8.1.11. Postfix Expression</a></li>
<li><a href="expressions.html#_unary-expression">8.1.12. Unary Expression</a></li>
<li><a href="expressions.html#_multiplicative-expression">8.1.13. Multiplicative Expression</a></li>
<li><a href="expressions.html#_additive-expression">8.1.14. Additive Expression</a>
<ul class="sectlevel4">
<li><a href="expressions.html#type-inference-10">8.1.14.1. Type Inference</a></li>
</ul>
</li>
<li><a href="expressions.html#_bitwise-shift-expression">8.1.15. Bitwise Shift Expression</a></li>
<li><a href="expressions.html#_relational-expression">8.1.16. Relational Expression</a></li>
<li><a href="expressions.html#_equality-expression">8.1.17. Equality Expression</a></li>
<li><a href="expressions.html#_binary-bitwise-expression">8.1.18. Binary Bitwise Expression</a></li>
<li><a href="expressions.html#_binary-logical-expression">8.1.19. Binary Logical Expression</a></li>
<li><a href="expressions.html#_conditional-expression">8.1.20. Conditional Expression</a></li>
<li><a href="expressions.html#_assignment-expression">8.1.21. Assignment Expression</a></li>
<li><a href="expressions.html#_comma-expression">8.1.22. Comma Expression</a></li>
</ul>
</li>
<li><a href="expressions.html#_ecmascript-6-expressions">8.2. ECMAScript 6 Expressions</a>
<ul class="sectlevel3">
<li><a href="expressions.html#_the-super-keyword">8.2.1. The super Keyword</a></li>
</ul>
</li>
<li><a href="expressions.html#_ecmascript-7-expressions">8.3. ECMAScript 7 Expressions</a>
<ul class="sectlevel3">
<li><a href="expressions.html#_await-expression">8.3.1. Await Expression</a></li>
</ul>
</li>
<li><a href="expressions.html#_n4js-specific-expressions">8.4. N4JS Specific Expressions</a>
<ul class="sectlevel3">
<li><a href="expressions.html#_class-expression">8.4.1. Class Expression</a></li>
<li><a href="expressions.html#_cast-as-expression">8.4.2. Cast (As) Expression</a>
<ul class="sectlevel4">
<li><a href="expressions.html#cast-as-expression-semantics-type-inference">8.4.2.1. Semantics and Type Inference</a></li>
</ul>
</li>
<li><a href="expressions.html#Import_Calls">8.4.3. Import Calls</a></li>
</ul>
</li>
<li><a href="expressions.html#compile-time-expressions">8.5. Compile-Time Expressions</a></li>
</ul>
</li>
<li><a href="statements.html#_statements">9. Statements</a>
<ul class="sectlevel2">
<li><a href="statements.html#_ecmascript-5-statements">9.1. ECMAScript 5 Statements</a>
<ul class="sectlevel3">
<li><a href="statements.html#_function-or-field-accessor-bodies">9.1.1. Function or Field Accessor Bodies</a></li>
<li><a href="statements.html#_variable-statement">9.1.2. Variable Statement</a></li>
<li><a href="statements.html#_if-statement">9.1.3. If Statement</a></li>
<li><a href="statements.html#_iteration-statements">9.1.4. Iteration Statements</a></li>
<li><a href="statements.html#_return-statement">9.1.5. Return Statement</a></li>
<li><a href="statements.html#_with-statement">9.1.6. With Statement</a></li>
<li><a href="statements.html#_switch-statement">9.1.7. Switch Statement</a></li>
<li><a href="statements.html#_throw-try-and-catch-statements">9.1.8. Throw, Try, and Catch Statements</a></li>
<li><a href="statements.html#_debugger-statement">9.1.9. Debugger Statement</a></li>
</ul>
</li>
<li><a href="statements.html#_ecmascript-6-statements">9.2. ECMAScript 6 Statements</a>
<ul class="sectlevel3">
<li><a href="statements.html#_let">9.2.1. Let</a></li>
<li><a href="statements.html#_const">9.2.2. Const</a></li>
<li><a href="statements.html#_for-of-statement">9.2.3. <code>for &#8230;&#8203; of</code> statement</a></li>
<li><a href="statements.html#_import-statement">9.2.4. Import Statement</a>
<ul class="sectlevel4">
<li><a href="statements.html#Dynamic_Imports">9.2.4.1. Dynamic Imports</a></li>
<li><a href="statements.html#_immutabilaty-of-imports">9.2.4.2. Immutabilaty of Imports</a></li>
</ul>
</li>
<li><a href="statements.html#_export-statement">9.2.5. Export Statement</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="annotations.html#_annotations">10. Annotations</a>
<ul class="sectlevel2">
<li><a href="annotations.html#_introduction-2">10.1. Introduction</a>
<ul class="sectlevel3">
<li><a href="annotations.html#_syntax-13">10.1.1. Syntax</a></li>
<li><a href="annotations.html#_properties-7">10.1.2. Properties</a></li>
<li><a href="annotations.html#_element-specific-annotations">10.1.3. Element-Specific Annotations</a></li>
<li><a href="annotations.html#_general-annotations">10.1.4. General Annotations</a>
<ul class="sectlevel4">
<li><a href="annotations.html#_idebug">10.1.4.1. IDEBUG</a></li>
</ul>
</li>
<li><a href="annotations.html#idebug-syntax">10.1.5. Syntax</a>
<ul class="sectlevel4">
<li><a href="annotations.html#_semantics-11">10.1.5.1. Semantics</a></li>
<li><a href="annotations.html#_suppress-warnings">10.1.5.2. Suppress Warnings</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="annotations.html#_declaration-of-annotations">10.2. Declaration of Annotations</a></li>
</ul>
</li>
<li><a href="extended_fetaures.html#_extended-fetaures">11. Extended Fetaures</a>
<ul class="sectlevel2">
<li><a href="extended_fetaures.html#_array-and-object-destructuring">11.1. Array and Object Destructuring</a>
<ul class="sectlevel3">
<li><a href="extended_fetaures.html#_syntax-14">11.1.1. Syntax</a></li>
<li><a href="extended_fetaures.html#_semantics-12">11.1.2. Semantics</a></li>
</ul>
</li>
<li><a href="extended_fetaures.html#_dependency-injection">11.2. Dependency Injection</a>
<ul class="sectlevel3">
<li><a href="extended_fetaures.html#_di-components-and-injectors">11.2.1. DI Components and Injectors</a>
<ul class="sectlevel4">
<li><a href="extended_fetaures.html#_dicomponent-relations">11.2.1.1. DIComponent Relations</a></li>
</ul>
</li>
<li><a href="extended_fetaures.html#_binders-and-bindings">11.2.2. Binders and Bindings</a></li>
<li><a href="extended_fetaures.html#_injection-points">11.2.3. Injection Points</a>
<ul class="sectlevel4">
<li><a href="extended_fetaures.html#_field-injection">11.2.3.1. Field Injection</a></li>
<li><a href="extended_fetaures.html#_constructor-injection">11.2.3.2. Constructor Injection</a></li>
<li><a href="extended_fetaures.html#_method-injection">11.2.3.3. Method Injection</a>
<ul class="sectlevel5">
<li><a href="extended_fetaures.html#_provider">11.2.3.3.1. Provider</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="extended_fetaures.html#_n4js-di-life-cycle-and-scopes">11.2.4. N4JS DI Life Cycle and Scopes</a>
<ul class="sectlevel4">
<li><a href="extended_fetaures.html#_injection-cylces">11.2.4.1. Injection Cylces</a></li>
<li><a href="extended_fetaures.html#_default-scope">11.2.4.2. Default Scope</a></li>
<li><a href="extended_fetaures.html#_singleton-scope">11.2.4.3. Singleton Scope</a></li>
<li><a href="extended_fetaures.html#_per-injection-chain-singleton">11.2.4.4. Per Injection Chain Singleton</a></li>
</ul>
</li>
<li><a href="extended_fetaures.html#_validation-of-callsites-targeting-n4injector-methods">11.2.5. Validation of callsites targeting N4Injector methods</a></li>
<li><a href="extended_fetaures.html#_n4js-di-annotations">11.2.6. N4JS DI Annotations</a>
<ul class="sectlevel4">
<li><a href="extended_fetaures.html#_n4js-di-generateinjector">11.2.6.1. N4JS DI @GenerateInjector</a></li>
<li><a href="extended_fetaures.html#_n4js-di-withparentinjector">11.2.6.2. N4JS DI @WithParentInjector</a></li>
<li><a href="extended_fetaures.html#_n4js-di-usebinder">11.2.6.3. N4JS DI @UseBinder</a></li>
<li><a href="extended_fetaures.html#_n4js-di-binder">11.2.6.4. N4JS DI @Binder</a></li>
<li><a href="extended_fetaures.html#_n4js-di-bind">11.2.6.5. N4JS DI @Bind</a></li>
<li><a href="extended_fetaures.html#_n4js-di-provides">11.2.6.6. N4JS DI @Provides</a></li>
<li><a href="extended_fetaures.html#_n4js-di-inject">11.2.6.7. N4JS DI @Inject</a></li>
<li><a href="extended_fetaures.html#_n4js-di-singleton">11.2.6.8. N4JS DI @Singleton</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="extended_fetaures.html#_test-support">11.3. Test Support</a></li>
<li><a href="extended_fetaures.html#_polyfill-definitions">11.4. Polyfill Definitions</a>
<ul class="sectlevel3">
<li><a href="extended_fetaures.html#_runtime-polyfill-definitions">11.4.1. Runtime Polyfill Definitions</a></li>
<li><a href="extended_fetaures.html#_static-polyfill-definitions">11.4.2. Static Polyfill Definitions</a></li>
<li><a href="extended_fetaures.html#_transpiling-static-polyfilled-classes">11.4.3. Transpiling static polyfilled classes</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="components.html#_components">12. Components</a>
<ul class="sectlevel2">
<li><a href="components.html#_overview-2">12.1. Overview</a></li>
<li><a href="components.html#Component_Types">12.2. Component Types</a>
<ul class="sectlevel3">
<li><a href="components.html#_libraries">12.2.1. Libraries</a></li>
<li><a href="components.html#_runtime-environment-and-runtime-libraries">12.2.2. Runtime Environment and Runtime Libraries</a></li>
<li><a href="components.html#_tests">12.2.3. Tests</a></li>
<li><a href="components.html#_type-definitions">12.2.4. Type Definitions</a></li>
</ul>
</li>
<li><a href="components.html#package-json">12.3. Package.json File</a>
<ul class="sectlevel3">
<li><a href="components.html#_basic-properties">12.3.1. Basic Properties</a></li>
<li><a href="components.html#_n4js-properties">12.3.2. N4JS Properties</a></li>
<li><a href="components.html#_constraints">12.3.3. Constraints</a></li>
</ul>
</li>
<li><a href="components.html#_support-for-npm-scopes">12.4. Support for NPM Scopes</a></li>
<li><a href="components.html#sec:N4JS-Type-Definitions">12.5. N4JS Type Definitions</a>
<ul class="sectlevel3">
<li><a href="components.html#_specify-type-definition">12.5.1. Specify Type Definition</a></li>
<li><a href="components.html#_name-conventions">12.5.2. Name Conventions</a></li>
<li><a href="components.html#_version-conventions">12.5.3. Version Conventions</a>
<ul class="sectlevel4">
<li><a href="components.html#_define-a-new-type-definition-package">12.5.3.1. Define a New Type Definition Package</a></li>
<li><a href="components.html#_using-a-type-definition-package">12.5.3.2. Using a Type Definition Package</a></li>
<li><a href="components.html#_rational">12.5.3.3. Rational</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="components.html#_modules">12.6. Modules</a></li>
<li><a href="components.html#_api-and-implementation-component">12.7. API and Implementation Component</a>
<ul class="sectlevel3">
<li><a href="components.html#_execution-of-api-and-implementation-components">12.7.1. Execution of API and Implementation Components</a></li>
<li><a href="components.html#_api-and-implementation-with-di">12.7.2. API and Implementation With DI</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="plainjs.html#_plainjs">13. PlainJS</a>
<ul class="sectlevel2">
<li><a href="plainjs.html#_type-inference-and-validation-for-plain-js">13.1. Type Inference and Validation for Plain JS</a></li>
<li><a href="plainjs.html#_external-declarations">13.2. External Declarations</a>
<ul class="sectlevel3">
<li><a href="plainjs.html#_declaring-externals">13.2.1. Declaring externals</a></li>
<li><a href="plainjs.html#_instantiating-external-classes">13.2.2. Instantiating external classes</a></li>
<li><a href="plainjs.html#_implementation-of-external-declarations">13.2.3. Implementation of External Declarations</a></li>
<li><a href="plainjs.html#_example">13.2.4. Example</a></li>
</ul>
</li>
<li><a href="plainjs.html#_global-definitions">13.3. Global Definitions</a></li>
<li><a href="plainjs.html#_runtime-definitions">13.4. Runtime Definitions</a></li>
<li><a href="plainjs.html#_applying-polyfills">13.5. Applying Polyfills</a></li>
</ul>
</li>
<li><a href="jsdoc.html#_jsdoc-2">14. JSDoc</a>
<ul class="sectlevel2">
<li><a href="jsdoc.html#_general-n4jsdoc-features">14.1. General N4JSDoc Features</a>
<ul class="sectlevel3">
<li><a href="jsdoc.html#_provided-inline-tags">14.1.1. Provided Inline Tags</a>
<ul class="sectlevel4">
<li><a href="jsdoc.html#jsdoc_tag__code">14.1.1.1. @code</a></li>
<li><a href="jsdoc.html#jsdoc_tag__link">14.1.1.2. @link</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="jsdoc.html#_n4jsdoc-for-user-projects">14.2. N4JSdoc for User Projects</a>
<ul class="sectlevel3">
<li><a href="jsdoc.html#_standard-tags">14.2.1. Standard Tags</a>
<ul class="sectlevel4">
<li><a href="jsdoc.html#_-author">14.2.1.1. @author</a></li>
<li><a href="jsdoc.html#jsdoc_tag_param">14.2.1.2. @param</a></li>
<li><a href="jsdoc.html#jsdoc_tag_return">14.2.1.3. @return</a></li>
</ul>
</li>
<li><a href="jsdoc.html#_test-related-tags">14.2.2. Test Related Tags</a>
<ul class="sectlevel4">
<li><a href="jsdoc.html#jsdoc_tag__testee">14.2.2.1. @testee</a></li>
<li><a href="jsdoc.html#jsdoc_tag__testeeFromType">14.2.2.2. @testeeFromType</a></li>
<li><a href="jsdoc.html#_testeeType_and__testeeMember">14.2.2.3. @testeeType and @testeeMember</a></li>
<li><a href="jsdoc.html#jsdoc_tag_reqid_in_Tests">14.2.2.4. @reqid in Tests</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="jsdoc.html#_n4jsdoc-for-api-and-implementation-projects">14.3. N4JSDoc for API and Implementation Projects</a>
<ul class="sectlevel3">
<li><a href="jsdoc.html#jsdoc_tag__apiNote">14.3.1. @apiNote</a></li>
<li><a href="jsdoc.html#API_Project_Tags">14.3.2. API Project Tags</a>
<ul class="sectlevel4">
<li><a href="jsdoc.html#jsdoc_tag_apiState">14.3.2.1. @apiState</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="jsx.html#_jsx">15. JSX</a>
<ul class="sectlevel2">
<li><a href="jsx.html#_jsx-support">15.1. JSX Support</a></li>
<li><a href="jsx.html#_jsx-backend">15.2. JSX Backend</a></li>
</ul>
</li>
<li><a href="grammars.html#_grammars">16. Grammars</a>
<ul class="sectlevel2">
<li><a href="grammars.html#_type-expressions-grammar">16.1. Type Expressions Grammar</a></li>
<li><a href="grammars.html#_n4js-language-grammar">16.2. N4JS Language Grammar</a></li>
</ul>
</li>
<li><a href="jsobjects.html#_jsobjects">17. JSObjects</a>
<ul class="sectlevel2">
<li><a href="jsobjects.html#_object">17.1. Object</a></li>
<li><a href="jsobjects.html#_string">17.2. String</a></li>
<li><a href="jsobjects.html#_boolean">17.3. Boolean</a></li>
<li><a href="jsobjects.html#_number">17.4. Number</a>
<ul class="sectlevel3">
<li><a href="jsobjects.html#_static-attributes">17.4.1. Static Attributes</a></li>
</ul>
</li>
<li><a href="jsobjects.html#function">17.5. Function</a></li>
<li><a href="jsobjects.html#_error">17.6. Error</a></li>
<li><a href="jsobjects.html#_array">17.7. Array</a></li>
<li><a href="jsobjects.html#_date">17.8. Date</a></li>
<li><a href="jsobjects.html#_math">17.9. Math</a>
<ul class="sectlevel3">
<li><a href="jsobjects.html#static-attributes-1">17.9.1. Static Attributes</a></li>
</ul>
</li>
<li><a href="jsobjects.html#_regexp">17.10. RegExp</a></li>
<li><a href="jsobjects.html#_json">17.11. JSON</a></li>
</ul>
</li>
<li><a href="n4js_objects.html#_n4js-objects">18. N4JS Objects</a>
<ul class="sectlevel2">
<li><a href="n4js_objects.html#_reflection-model">18.1. Reflection Model</a></li>
<li><a href="n4js_objects.html#_error-types">18.2. Error Types</a>
<ul class="sectlevel3">
<li><a href="n4js_objects.html#_n4apinotimplemented">18.2.1. N4ApiNotImplemented</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="appendix_a_acronyms.html#_acronyms">Appendix A: Acronyms</a></li>
<li><a href="appendix_b_license.html#sec:License">Appendix B: License</a></li>
<li><a href="appendix_c_bibliography.html#_bibliography">Appendix C: Bibliography</a></li>
</ul>
</div>
</div>
<div id="content"><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.html#_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="classifiers.html#_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="appendix_c_bibliography.html#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="appendix_c_bibliography.html#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="appendix_c_bibliography.html#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="grammars.html#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="names.html#_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="appendix_c_bibliography.html#_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.html#_functions">Functions</a>, <a href="functions.html#_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="classifiers.html#_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="appendix_c_bibliography.html#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="classifiers.html#_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="classifiers.html#_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="classifiers.html#_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="statements.html#_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="appendix_c_bibliography.html#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="appendix_c_bibliography.html#_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="appendix_c_bibliography.html#_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="expressions.html#_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="appendix_c_bibliography.html#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="appendix_c_bibliography.html#Torgersen05a">Torgersen05a</a>], further developed in [<a href="appendix_c_bibliography.html#Cameron08b">Cameron08b</a>] (further developed in [<a href="appendix_c_bibliography.html#Cameron09a">Cameron09a</a>] [<a href="appendix_c_bibliography.html#Summers10a">Summers10a</a>], also see [<a href="appendix_c_bibliography.html#Wehr08a">Wehr08a</a>] for a similar approach).
The key feature of the Java generic wildcard handling is called capture conversion, described in [<a href="appendix_c_bibliography.html#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="appendix_c_bibliography.html#_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="classifiers.html#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="appendix_c_bibliography.html#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="appendix_c_bibliography.html#ECMA11a">ECMA11a(p.S8Types, p.p.28)</a>] and as Objects [<a href="appendix_c_bibliography.html#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="appendix_c_bibliography.html#ECMA11a">ECMA11a(p.S8.3)</a>]; cannot be used in type expression, see void below.</p>
</li>
<li>
<p><code>null</code> [<a href="appendix_c_bibliography.html#ECMA11a">ECMA11a(p.S8.3)</a>]; cannot be used in type expression</p>
</li>
<li>
<p><code>boolean</code> [<a href="appendix_c_bibliography.html#ECMA11a">ECMA11a(p.S8.3)</a>]</p>
</li>
<li>
<p><code>string</code> [<a href="appendix_c_bibliography.html#ECMA11a">ECMA11a(p.S8.4)</a>]</p>
</li>
<li>
<p><code>number</code> [<a href="appendix_c_bibliography.html#ECMA11a">ECMA11a(p.S8.5)</a>]</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Although Object is a primitive type in [<a href="appendix_c_bibliography.html#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="statements.html#_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="conversions_and_reflection.html#_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="conversions_and_reflection.html#_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="appendix_c_bibliography.html#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="expressions.html#_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="expressions.html#_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="appendix_c_bibliography.html#_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>