blob: 3d223a8d3fe5542109480a6908f3669f1939f8b3 [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.html#_types">4. Types</a>
<ul class="sectlevel2">
<li><a href="types.html#_overview">4.1. Overview</a></li>
<li><a href="types.html#_type-expressions">4.2. Type Expressions</a>
<ul class="sectlevel3">
<li><a href="types.html#_syntax">4.2.1. Syntax</a></li>
<li><a href="types.html#_properties">4.2.2. Properties</a></li>
<li><a href="types.html#_semantics">4.2.3. Semantics</a></li>
</ul>
</li>
<li><a href="types.html#_type-inference">4.3. Type Inference</a></li>
<li><a href="types.html#_generic-and-parameterized-types">4.4. Generic and Parameterized Types</a>
<ul class="sectlevel3">
<li><a href="types.html#_generic-types">4.4.1. Generic Types</a></li>
<li><a href="types.html#_type-variables">4.4.2. Type Variables</a></li>
<li><a href="types.html#_parameterized-types">4.4.3. Parameterized Types</a></li>
</ul>
</li>
<li><a href="types.html#_primitive-ecmascript-types">4.5. Primitive ECMAScript Types</a>
<ul class="sectlevel3">
<li><a href="types.html#_undefined-type">4.5.1. Undefined Type</a></li>
<li><a href="types.html#_null-type">4.5.2. Null Type</a></li>
<li><a href="types.html#_primitive-boolean-type">4.5.3. Primitive Boolean Type</a></li>
<li><a href="types.html#_primitive-string-type">4.5.4. Primitive String Type</a></li>
<li><a href="types.html#_primitive-number-type">4.5.5. Primitive Number Type</a></li>
<li><a href="types.html#_primitive-type-int">4.5.6. Primitive Type int</a></li>
<li><a href="types.html#_primitive-symbol-type">4.5.7. Primitive Symbol Type</a></li>
</ul>
</li>
<li><a href="types.html#_primitive-n4js-types">4.6. Primitive N4JS Types</a>
<ul class="sectlevel3">
<li><a href="types.html#_any-type">4.6.1. Any Type</a>
<ul class="sectlevel4">
<li><a href="types.html#any-type-semantics">4.6.1.1. Semantics</a></li>
<li><a href="types.html#any-type-type-inference">4.6.1.2. Type Inference</a>
<ul class="sectlevel5">
<li><a href="types.html#_default-type-of-variables">4.6.1.2.1. Default Type of Variables</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="types.html#_void-type">4.6.2. Void Type</a>
<ul class="sectlevel4">
<li><a href="types.html#void-type-semantics">4.6.2.1. Semantics</a></li>
</ul>
</li>
<li><a href="types.html#_unknown-type">4.6.3. Unknown Type</a></li>
<li><a href="types.html#_primitive-pathselector-and-i18nkey">4.6.4. Primitive Pathselector and I18nKey</a>
<ul class="sectlevel4">
<li><a href="types.html#pathselector-semantics">4.6.4.1. Semantics</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="types.html#_built-in-ecmascript-object-types">4.7. Built-in ECMAScript Object Types</a>
<ul class="sectlevel3">
<li><a href="types.html#ECMAScript-objects-semantics">4.7.1. Semantics</a></li>
<li><a href="types.html#_object-type">4.7.2. Object Type</a></li>
<li><a href="types.html#_function-object-type">4.7.3. Function-Object-Type</a></li>
<li><a href="types.html#_array-object-type">4.7.4. Array Object Type</a></li>
<li><a href="types.html#_string-object-type">4.7.5. String Object Type</a></li>
<li><a href="types.html#_boolean-object-type">4.7.6. Boolean Object Type</a></li>
<li><a href="types.html#_number-object-type">4.7.7. Number Object Type</a></li>
<li><a href="types.html#_global-object-type">4.7.8. Global Object Type</a></li>
<li><a href="types.html#_symbol">4.7.9. Symbol</a></li>
<li><a href="types.html#_promise">4.7.10. Promise</a></li>
<li><a href="types.html#_iterator-interface">4.7.11. Iterator Interface</a></li>
<li><a href="types.html#_iterable-interface">4.7.12. Iterable Interface</a></li>
</ul>
</li>
<li><a href="types.html#_built-in-n4js-types">4.8. Built-In N4JS Types</a>
<ul class="sectlevel3">
<li><a href="types.html#_n4object">4.8.1. N4Object</a>
<ul class="sectlevel4">
<li><a href="types.html#N4Object-semantics">4.8.1.1. Semantics</a></li>
</ul>
</li>
<li><a href="types.html#_n4class">4.8.2. N4Class</a></li>
<li><a href="types.html#IterableN">4.8.3. IterableN</a></li>
</ul>
</li>
<li><a href="types.html#_type-modifiers">4.9. Type Modifiers</a>
<ul class="sectlevel3">
<li><a href="types.html#Type_Modifiers_Dynamic">4.9.1. Dynamic</a></li>
<li><a href="types.html#_optional-return-types">4.9.2. Optional Return Types</a></li>
</ul>
</li>
<li><a href="types.html#_union-and-intersection-type-composed-types">4.10. Union and Intersection Type (Composed Types)</a>
<ul class="sectlevel3">
<li><a href="types.html#_union-type">4.10.1. Union Type</a>
<ul class="sectlevel4">
<li><a href="types.html#union-type-syntax">4.10.1.1. Syntax</a></li>
<li><a href="types.html#union-type-semantics">4.10.1.2. Semantics</a></li>
<li><a href="types.html#_warnings">4.10.1.3. Warnings</a></li>
</ul>
</li>
<li><a href="types.html#_intersection-type">4.10.2. Intersection Type</a>
<ul class="sectlevel4">
<li><a href="types.html#intersection-type-syntax">4.10.2.1. Syntax</a></li>
<li><a href="types.html#intersection-type-semantics">4.10.2.2. Semantics</a></li>
<li><a href="types.html#_warnings-2">4.10.2.3. Warnings</a></li>
</ul>
</li>
<li><a href="types.html#_composed-types-in-wildcards">4.10.3. Composed Types in Wildcards</a></li>
<li><a href="types.html#_property-access-for-composed-types">4.10.4. Property Access for Composed Types</a>
<ul class="sectlevel4">
<li><a href="types.html#_properties-of-union-type">4.10.4.1. Properties of Union Type</a>
<ul class="sectlevel5">
<li><a href="types.html#_remarks-on-union-type-s-members">4.10.4.1.1. Remarks on union type’s members:</a></li>
</ul>
</li>
<li><a href="types.html#_properties-of-intersection-type">4.10.4.2. Properties of Intersection Type</a>
<ul class="sectlevel5">
<li><a href="types.html#_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="types.html#_constructor-and-classifier-type">4.11. Constructor and Classifier Type</a>
<ul class="sectlevel3">
<li><a href="types.html#_syntax-3">4.11.1. Syntax</a></li>
<li><a href="types.html#_semantics-2">4.11.2. Semantics</a></li>
<li><a href="types.html#_constructors-and-prototypes-in-ecmascript-2015">4.11.3. Constructors and Prototypes in ECMAScript 2015</a></li>
</ul>
</li>
<li><a href="types.html#_this-type">4.12. This Type</a>
<ul class="sectlevel3">
<li><a href="types.html#this-type-syntax">4.12.1. Syntax</a></li>
<li><a href="types.html#this-keyword-semantics">4.12.2. Semantics</a></li>
</ul>
</li>
<li><a href="types.html#_enums">4.13. Enums</a>
<ul class="sectlevel3">
<li><a href="types.html#_enums-n4js">4.13.1. Enums (N4JS)</a>
<ul class="sectlevel4">
<li><a href="types.html#enums-syntax">4.13.1.1. Syntax</a></li>
<li><a href="types.html#enums-semantics">4.13.1.2. Semantics</a></li>
</ul>
</li>
<li><a href="types.html#_string-based-enums">4.13.2. String-Based Enums</a></li>
</ul>
</li>
<li><a href="types.html#_short-hand-syntax">4.14. Short-Hand Syntax</a>
<ul class="sectlevel3">
<li><a href="types.html#_array-short-hand-syntax">4.14.1. Array Short-Hand Syntax</a></li>
<li><a href="types.html#_iterablen-short-hand-syntax">4.14.2. IterableN Short-Hand Syntax</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#_classifiers">5. Classifiers</a>
<ul class="sectlevel2">
<li><a href="#_n4js-specific-classifiers">5.1. N4JS Specific Classifiers</a>
<ul class="sectlevel3">
<li><a href="#_properties-2">5.1.1. Properties</a></li>
<li><a href="#_common-semantics-of-classifiers">5.1.2. Common Semantics of Classifiers</a></li>
<li><a href="#_classes">5.1.3. Classes</a>
<ul class="sectlevel4">
<li><a href="#_definition-of-classes">5.1.3.1. Definition of Classes</a>
<ul class="sectlevel5">
<li><a href="#class-syntax">5.1.3.1.1. Syntax</a></li>
<li><a href="#class-properties">5.1.3.1.2. Properties</a></li>
<li><a href="#class-type-inference">5.1.3.1.3. Type Inference</a></li>
</ul>
</li>
<li><a href="#class-semantics">5.1.3.2. Semantics</a></li>
<li><a href="#_final-modifier">5.1.3.3. Final Modifier</a></li>
<li><a href="#_abstract-classes">5.1.3.4. Abstract Classes</a></li>
<li><a href="#_non-instantiable-classes">5.1.3.5. Non-Instantiable Classes</a></li>
<li><a href="#_superclass">5.1.3.6. Superclass</a></li>
</ul>
</li>
<li><a href="#_interfaces">5.1.4. Interfaces</a>
<ul class="sectlevel4">
<li><a href="#_definition-of-interfaces">5.1.4.1. Definition of Interfaces</a>
<ul class="sectlevel5">
<li><a href="#interfaces-syntax">5.1.4.1.1. Syntax</a></li>
<li><a href="#interfaces-properties">5.1.4.1.2. Properties</a></li>
<li><a href="#interfaces-type-inference">5.1.4.1.3. Type Inference</a></li>
<li><a href="#interfaces-semantics">5.1.4.1.4. Semantics</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#_generic-classifiers">5.1.5. Generic Classifiers</a></li>
<li><a href="#sec:definition-site-variance">5.1.6. Definition-Site Variance</a></li>
</ul>
</li>
<li><a href="#_members">5.2. Members</a>
<ul class="sectlevel3">
<li><a href="#_syntax-4">5.2.1. Syntax</a>
<ul class="sectlevel4">
<li><a href="#_properties-3">5.2.1.1. Properties</a></li>
</ul>
</li>
<li><a href="#_semantics-3">5.2.2. Semantics</a></li>
<li><a href="#_methods">5.2.3. Methods</a>
<ul class="sectlevel4">
<li><a href="#_syntax-5">5.2.3.1. Syntax</a></li>
<li><a href="#_properties-4">5.2.3.2. Properties</a></li>
<li><a href="#_semantics-4">5.2.3.3. Semantics</a></li>
<li><a href="#_final-methods">5.2.3.4. Final Methods</a></li>
<li><a href="#_abstract-methods">5.2.3.5. Abstract Methods</a></li>
<li><a href="#_generic-methods">5.2.3.6. Generic Methods</a></li>
</ul>
</li>
<li><a href="#_default-methods-in-interfaces">5.2.4. Default Methods in Interfaces</a>
<ul class="sectlevel4">
<li><a href="#_asynchronous-methods">5.2.4.1. Asynchronous Methods</a></li>
</ul>
</li>
<li><a href="#_constructors">5.2.5. Constructors</a>
<ul class="sectlevel4">
<li><a href="#_structural-this-type-in-constructor">5.2.5.1. Structural This Type in Constructor</a></li>
<li><a href="#spec-constructor">5.2.5.2. @Spec Constructor</a></li>
<li><a href="#_callable-constructors">5.2.5.3. Callable Constructors</a></li>
<li><a href="#_covariant-constructors">5.2.5.4. Covariant Constructors</a></li>
</ul>
</li>
<li><a href="#_data-fields">5.2.6. Data Fields</a>
<ul class="sectlevel4">
<li><a href="#data-fields-syntax">5.2.6.1. Syntax</a></li>
<li><a href="#data-fields-properties">5.2.6.2. Properties</a>
<ul class="sectlevel5">
<li><a href="#data-fields-semantics">5.2.6.2.1. Semantics</a></li>
<li><a href="#data-fields-type-inference">5.2.6.2.2. Type Inference</a></li>
</ul>
</li>
<li><a href="#_assignment-modifiers">5.2.6.3. Assignment Modifiers</a></li>
<li><a href="#_field-accessors-getter-setter">5.2.6.4. Field Accessors (Getter/Setter)</a>
<ul class="sectlevel5">
<li><a href="#field-acessors-syntax">5.2.6.4.1. Syntax</a></li>
<li><a href="#field-acessors-properties">5.2.6.4.2. Properties</a></li>
<li><a href="#field-accessors-semantics">5.2.6.4.3. Semantics</a></li>
</ul>
</li>
<li><a href="#optional-fields">5.2.6.5. Optional Fields</a>
<ul class="sectlevel5">
<li><a href="#_syntax-6">5.2.6.5.1. Syntax</a></li>
<li><a href="#_semantics-5">5.2.6.5.2. Semantics</a></li>
<li><a href="#_background">5.2.6.5.3. Background</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#_static-members">5.2.7. Static Members</a>
<ul class="sectlevel4">
<li><a href="#_access-from-and-to-static-members">5.2.7.1. Access From and To Static Members</a></li>
<li><a href="#_generic-static-methods">5.2.7.2. Generic static methods</a></li>
<li><a href="#_static-members-of-interfaces">5.2.7.3. Static Members of Interfaces</a></li>
</ul>
</li>
<li><a href="#_redefinition-of-members">5.2.8. Redefinition of Members</a>
<ul class="sectlevel4">
<li><a href="#_overriding-of-members">5.2.8.1. Overriding of Members</a></li>
<li><a href="#_implementation-of-members">5.2.8.2. Implementation of Members</a>
<ul class="sectlevel5">
<li><a href="#_member-consumption">5.2.8.2.1. Member Consumption</a></li>
<li><a href="#_member-implementation">5.2.8.2.2. Member Implementation</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="#_structural-typing">5.3. Structural Typing</a>
<ul class="sectlevel3">
<li><a href="#_syntax-7">5.3.1. Syntax</a></li>
<li><a href="#_definition-site-structural-typing">5.3.2. Definition Site Structural Typing</a></li>
<li><a href="#_use-site-structural-typing">5.3.3. Use-Site Structural Typing</a></li>
<li><a href="#structural-readWriteInit-field-typing">5.3.4. Structural Read-only, Write-only and Initializer Field Typing</a></li>
<li><a href="#_public-setter-annotated-with-code-providesinitializer-code">5.3.5. Public Setter Annotated With <code>ProvidesInitializer</code></a></li>
<li><a href="#_structural-types-with-optional-fields">5.3.6. Structural Types With Optional Fields</a></li>
<li><a href="#_structural-types-with-access-modifier">5.3.7. Structural Types With Access Modifier</a></li>
<li><a href="#_structural-types-with-additional-members">5.3.8. Structural Types With Additional Members</a>
<ul class="sectlevel4">
<li><a href="#_syntax-8">5.3.8.1. Syntax</a>
<ul class="sectlevel5">
<li><a href="#_semantics-6">5.3.8.1.1. Semantics</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="functions.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="_classifiers"><a class="anchor" href="#_classifiers"></a><a class="link" href="#_classifiers">5. Classifiers</a></h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_n4js-specific-classifiers"><a class="anchor" href="#_n4js-specific-classifiers"></a><a class="link" href="#_n4js-specific-classifiers">5.1. N4JS Specific Classifiers</a></h3>
<div class="paragraph">
<p>N4JS provides three new metatypes: class, interface, and enums.
In this section we describe classes and interfaces.
These metatypes, called <em>classifiers</em>, share some common properties which are described before type specific properties are outlined in the following sections.</p>
</div>
<div class="paragraph">
<p>All of these metatypes can be marked with type access modifiers:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">enum N4JSTypeAccessModifier: project | public;</code></pre>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_properties-2"><a class="anchor" href="#_properties-2"></a><a class="link" href="#_properties-2">5.1.1. Properties</a></h4>
<div class="paragraph">
<p><strong>Properties defined by syntactic elements:</strong></p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>annotations</code> </dt>
<dd>
<p>Arbitrary annotations, see <a href="annotations.html#_annotations">Annotations</a> for details.</p>
</dd>
<dt class="hdlist1"><code>accessModifier</code> </dt>
<dd>
<p>N4JS type access modifier: <code>public</code>, or <code>project</code>; <code>public</code> can be combined with <code>@Internal</code>; if
<strong>export</strong> is <code>true</code> the default is else the default is <code>private</code>.</p>
</dd>
<dt class="hdlist1"><code>name</code> </dt>
<dd>
<p>The simple name of a classifier. If the classifier is defined by an anonymous class expression, an artificial but unique name is created.
The name needs to be a valid identifier, see <a href="names.html#_valid-names">Valid Names</a>.</p>
</dd>
<dt class="hdlist1"><code>typePars</code> </dt>
<dd>
<p>Collection of type parameters of a generic classifier; empty by default.</p>
</dd>
<dt class="hdlist1"><code>ownedMembers</code> </dt>
<dd>
<p>Collection of owned members, i.e. methods and fields defined directly in the classifier and, if present, the explicitly defined constructor.
Depending on the concrete classifier, additional constraints are defined.</p>
</dd>
<dt class="hdlist1"><code>typingStrategy</code> </dt>
<dd>
<p>The definition-site typing strategy. By default nominal typing is used.
See <a href="#_structural-typing">Structural Typing</a> for details.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p><strong>The following pseudo properties are defined via annotations:</strong></p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>export</code> </dt>
<dd>
<p>Boolean property set to true if the <code>export</code> modifier is set.
If value is true, the classifier may be accessible outside the project.</p>
</dd>
<dt class="hdlist1"><code>final</code> </dt>
<dd>
<p>Boolean property which is set to final if annotation <code>@Final</code> is set.
Also see <a href="#_final-modifier">Final Modifier</a></p>
</dd>
<dt class="hdlist1"><code>deprecated</code> </dt>
<dd>
<p>Boolean property set to true if annotation <code>@Deprecated</code> is set.</p>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
Version 0.4, not implemented in Version 0.3
</td>
</tr>
</table>
</div>
</dd>
</dl>
</div>
<div class="paragraph">
<p><strong>We additionally define the following pseudo properties:</strong></p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>acc</code> </dt>
<dd>
<p>Type access modifier as described in <a href="names.html#_accessibility-of-types-top-level-variables-and-function-declarations">Accessibility of Types, Top-Level Variables and Function Declarations</a>, it is the aggregated value of the
<code>accessModifier</code> and the <strong>export</strong> property.</p>
</dd>
<dt class="hdlist1"><code>owned{Fields|Methods|Getters|Setters|Accessors}</code> </dt>
<dd>
<p>Filters ownedMembers by metatype, short for<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>x</mi><mo>∈</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>,</mo><mi>μ</mi><mfenced close=")" open="("><mi>x</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Field</mtext></mstyle></math> etc.</p>
</dd>
<dt class="hdlist1"><code>members</code> </dt>
<dd>
<p>Reflexive transitive closure of all members of a classifier and its super classifiers, see <a href="#_common-semantics-of-classifiers">Common Semantics of Classifiers</a> on how this is calculated.</p>
</dd>
<dt class="hdlist1"><code>fields|methods|getters|setters|accessors</code> </dt>
<dd>
<p>Filters members by metatype, short for<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>x</mi><mo>∈</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>,</mo><mi>μ</mi><mfenced close=")" open="("><mi>x</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Field</mtext></mstyle></math> etc.</p>
</dd>
<dt class="hdlist1"><code>superClassifiers</code> </dt>
<dd>
<p>Classes and interface may extend or implement classes or interfaces.
Any class or interface extended or interface implemented is called <em>super classifier</em>.
We distinguish the directly subtyped classifiers and from the transitive closure of supertypes <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>u</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>C</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>f</mi><mi>i</mi><mi>e</mi><mi>r</mi><msup><mi>s</mi><mo>*</mo></msup></math></p>
</dd>
</dl>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_common-semantics-of-classifiers"><a class="anchor" href="#_common-semantics-of-classifiers"></a><a class="link" href="#_common-semantics-of-classifiers">5.1.2. Common Semantics of Classifiers</a></h4>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-42"></a><strong>Req. IDE-42:</strong> <a href="#Req-IDE-42">Subtyping of Classifiers</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a given type <code>C</code>, and supertypes <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>u</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>C</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>f</mi><mi>i</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>=</mo><mfenced close="}" open="{"><msub><mi>S</mi><mn>1</mn></msub><mrow><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></mrow><msub><mi>S</mi><mi>n</mi></msub></mfenced></math> directly subtyped
<code>C</code>, the following constraints must be true:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The supertypes must be accessible to the subtype:<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>S</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>S</mi><mi>n</mi></msub></math> must be accessible to <code>C</code>.</p>
</li>
<li>
<p>All type parameters of the direct supertypes have to be bound by type arguments in the subtype and the type arguments have to be substitutable types of the type parameters.</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mo>∀</mo><mn>0</mn><mo>&lt;</mo><mi>i</mi><mo>≤</mo><mi>k</mi><mi>:</mi><mo>∀</mo><mi>P</mi><mo>∈</mo><msub><mi>S</mi><mi>i</mi></msub><mi>:</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>∃</mo><mi>A</mi><mo>∈</mo><msub><mi>C</mi><mo>.</mo></msub><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>A</mi><mi>r</mi><mi>g</mi><mi>s</mi><mi>:</mi><mi>b</mi><mi>i</mi><mi>n</mi><mi>d</mi><mfenced close=")" open="("><mi>A</mi><mi>P</mi></mfenced><mo>∧</mo><mi>A</mi><mo>.</mo><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>B</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>d</mi><mo>&lt;</mo><mi>:</mi><mi>P</mi><mo>.</mo><mi>u</mi><mi>p</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>B</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>d</mi></math></p>
</div>
</li>
<li>
<p>Wildcards may not be used as type argument when binding a supertype’s type parameters.</p>
</li>
<li>
<p>A classifier cannot be directly subtyped directly multiple times:<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>∀</mo><msub><mi>S</mi><mi>i</mi></msub><mo>,</mo><msub><mi>S</mi><mi>j</mi></msub><mfenced close=")" open="("><mi>i</mi><mrow><mi>j</mi><mo>∈</mo><mfenced close="}" open="{"><mrow><mn>1.</mn><mo>.</mo><mi>n</mi></mrow></mfenced></mrow></mfenced><mi>:</mi><msub><mi>S</mi><mi>i</mi></msub><mo>=</mo><msub><mi>S</mi><mi>j</mi></msub><mo>→</mo><mi>i</mi><mo>=</mo><mi>j</mi></math></p>
</li>
</ol>
</div>
<div class="paragraph">
<p>In order to simplify the following constraints, we use the pseudo property <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi></math> to refer to all members of a classifier.
This includes all members directly declared by the classifier itself, i.e. the <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi></math>, and all members inherited from its super classifiers.
The concrete mechanisms for inheriting a member are different and further constraint (cf. <a href="#_redefinition-of-members">Redefinition of Members</a>).
A classifier only inherits its members from its direct supertypes, although the supertypes may contains members inherited from their supertypes.</p>
</div>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_classes"><a class="anchor" href="#_classes"></a><a class="link" href="#_classes">5.1.3. Classes</a></h4>
<div class="sect4">
<h5 id="_definition-of-classes"><a class="anchor" href="#_definition-of-classes"></a><a class="link" href="#_definition-of-classes">5.1.3.1. Definition of Classes</a></h5>
<div class="paragraph">
<p>Classes are either declared with a class declaration on top level, or they can be used as anonymous classes in expressions.
The latter may have a name, which may be used for error messages and reflection.</p>
</div>
<div class="paragraph">
<p>At the current stage, class expressions are effectively disabled at least until the semantics of them are finalized in ECMAScript 6.</p>
</div>
<div class="paragraph">
<p>In N4JS (as in many other languages) multi-inheritance of classes is not supported.
Although the <code>diamond problem</code> (of functions being defined in both superclasses) could be solved via union and intersection types, this would lead to problems when calling these super implementations.
This is particularly an issue due to JavaScript not supporting
multiple prototypes.<sup class="footnote">[<a id="_footnoteref_27" class="footnote" href="appendix_c_bibliography.html#_footnote_27" title="View footnote.">27</a>]</sup>
Interfaces, however, allow for multi-inheritance. Since the former can also define functions with bodies, this is not a hard restriction.</p>
</div>
<div class="sect5">
<h6 id="class-syntax"><a class="anchor" href="#class-syntax"></a><a class="link" href="#class-syntax">5.1.3.1.1. Syntax</a></h6>
<div class="listingblock">
<div class="title">Syntax N4 Class Declaration and Expression</div>
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">N4ClassDeclaration &lt;Yield&gt;:
=&gt;(
{N4ClassDeclaration}
annotations+=Annotation*
(declaredModifiers+=N4Modifier)*
'class' typingStrategy=TypingStrategyDefSiteOperator? name=BindingIdentifier&lt;Yield&gt;?
)
TypeVariables?
ClassExtendsClause&lt;Yield&gt;?
Members&lt;Yield&gt;
;
N4ClassExpression &lt;Yield&gt;:
{N4ClassExpression}
'class' name=BindingIdentifier&lt;Yield&gt;?
ClassExtendsClause&lt;Yield&gt;?
Members&lt;Yield&gt;;
fragment ClassExtendsClause &lt;Yield&gt;*:
'extends' (
=&gt;superClassRef=ParameterizedTypeRefNominal ('implements' ClassImplementsList)?
| superClassExpression=LeftHandSideExpression&lt;Yield&gt;
)
| 'implements' ClassImplementsList
;
fragment ClassImplementsList*:
implementedInterfaceRefs+=ParameterizedTypeRefNominal
(',' implementedInterfaceRefs+=ParameterizedTypeRefNominal)*
;
fragment Members &lt;Yield&gt;*:
'{'
ownedMembers+=N4MemberDeclaration&lt;Yield&gt;*
'}'
;</code></pre>
</div>
</div>
</div>
<div class="sect5">
<h6 id="class-properties"><a class="anchor" href="#class-properties"></a><a class="link" href="#class-properties">5.1.3.1.2. Properties</a></h6>
<div class="paragraph">
<p>These are the properties of class, which can be specified by the user:
Syntax N4 Class Declaration and Expression</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>abstract</code> </dt>
<dd>
<p>Boolean flag indicating whether class may be instantiable; default is <code>false</code>, see <a href="#_abstract-classes">Abstract Classes</a>.</p>
</dd>
<dt class="hdlist1"><code>external</code> </dt>
<dd>
<p>Boolean flag indicating whether class is a declaration without implementation or with an external (non-N4JS) implementation; default is <code>false</code>, see <a href="#_definition-site-structural-typing">Definition Site Structural Typing</a>.</p>
</dd>
<dt class="hdlist1"><code>defStructural</code> </dt>
<dd>
<p>Boolean flag indicating whether subtype relation uses nominal or structural typing, see <a href="#_definition-site-structural-typing">Definition Site Structural Typing</a> for details.</p>
</dd>
<dt class="hdlist1"><code>superType/sup</code> </dt>
<dd>
<p>The type referenced by <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>u</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi></math> is called direct superclass of a class, and vice versa the class is a direct subclass of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>u</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi></math>.
Instead of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>u</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi></math>, we sometimes simply write <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>u</mi><mi>p</mi></math>.
The derived set <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>u</mi><msup><mi>p</mi><mo>+</mo></msup></math> is defined as the transitive closures of all direct and indirect superclasses of a class.
If no supertype is explicitly stated, classes are derived from <code>N4Object</code>.</p>
</dd>
<dt class="hdlist1"><code>implementedInterfaces</code>/<code>interfaces</code> </dt>
<dd>
<p>Collection of interfaces directly <em>implemented</em> by the class; empty by default.
Instead of <code>implementedInterfaces</code>, we simply write <code>interfaces</code>.</p>
</dd>
<dt class="hdlist1"><code>ownedCtor</code> </dt>
<dd>
<p>Explicit constructor of a class (if any), see <a href="types.html#_constructor-and-classifier-type">Constructor and Classifier Type</a>.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>And we additionally define the following pseudo properties:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>ctor</code> </dt>
<dd>
<p>Explicit or implicit constructor of a class, see <a href="types.html#_constructor-and-classifier-type">Constructor and Classifier Type</a>.</p>
</dd>
<dt class="hdlist1"><code>fields</code> </dt>
<dd>
<p>Further derived properties for retrieving all methods (property <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>m</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><mi>s</mi></math>), fields (property <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mi>s</mi></math>), static
members (property <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mi>O</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi></math>), etc. can easily be added by filtering properties <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi></math> or <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi></math>.</p>
</dd>
</dl>
</div>
</div>
<div class="sect5">
<h6 id="class-type-inference"><a class="anchor" href="#class-type-inference"></a><a class="link" href="#class-type-inference">5.1.3.1.3. Type Inference</a></h6>
<div class="paragraph">
<p>The type of a class declaration or class expression <code>C</code> (i.e., a class definition in general) is of type <code>constructor{C}</code> if it is not abstract,
that is if it can be instantiated.
If it is abstract, the type of the definition simply is <code>type{C}</code>:</p>
</div>
<div class="openblock">
<div class="content">
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mfrac><mrow><mo>¬</mo><mi>C</mi><mo>.</mo><mi>a</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi></mrow><mrow><mi>Γ</mi><mo>⊢</mo><mi>C</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>constructor</mtext></mstyle><mfenced close="}" open="{"><mi>C</mi></mfenced></mrow></mfrac></mtd></mtr><mtr><mtd><mfrac><mrow><mi>C</mi><mo>.</mo><mi>a</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi></mrow><mrow><mi>Γ</mi><mo>⊢</mo><mi>C</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>type</mtext></mstyle><mfenced close="}" open="{"><mi>C</mi></mfenced></mrow></mfrac></mtd></mtr></mtable></math>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-43"></a><strong>Req. IDE-43:</strong> <a href="#Req-IDE-43">Structural and Nominal Supertypes</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>The type of supertypes and implemented interfaces is always the nominal type, even if the supertype is declared structurally.</p>
</div>
<div class="openblock">
<div class="content">
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="class-semantics"><a class="anchor" href="#class-semantics"></a><a class="link" href="#class-semantics">5.1.3.2. Semantics</a></h5>
<div class="paragraph">
<p>This section deals with the (more or less) type-independent constraints on classes.</p>
</div>
<div class="paragraph">
<p>Class expressions are not fully supported at the moment.</p>
</div>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="transitive_closure_of_members"></a><strong>Definition:</strong> <a href="#transitive_closure_of_members">Transitive closure of members</a></p>
</div>
<div class="paragraph">
<p>The reflexive transitive closure of members of a class is indirectly defined by the override and implementation constraints defined in <a href="#_redefinition-of-members">Redefinition of Members</a>.</p>
</div>
<div class="paragraph">
<p>Note that since overloading is forbidden, the following constraint is true <sup class="footnote">[<a id="_footnoteref_28" class="footnote" href="appendix_c_bibliography.html#_footnote_28" title="View footnote.">28</a>]</sup>:</p>
</div>
<div class="openblock">
<div class="content">
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mo>∀</mo><msub><mi>m</mi><mn>1</mn></msub><mo>,</mo><msub><mi>m</mi><mn>2</mn></msub><mo>∈</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi><msub><mi>m</mi><mn>1</mn></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><msub><mi>m</mi><mn>2</mn></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>⇔</mo><msub><mi>m</mi><mn>1</mn></msub><mo>=</mo><msub><mi>m</mi><mn>2</mn></msub><mo>∨</mo><mi>a</mi><mi>c</mi><mi>c</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>o</mi><mi>r</mi><mi>P</mi><mi>a</mi><mi>i</mi><mi>r</mi><mfenced close=")" open="("><msub><mi>m</mi><mn>1</mn></msub><msub><mi>m</mi><mn>2</mn></msub></mfenced></math>
</div>
</div>
<div class="paragraph">
<p>Remarks: Class and method definition is quite similar to the proposed ECMAScript version 6 draft [<a href="appendix_c_bibliography.html#ECMA15a">ECMA15a(p.S13.5)</a>], except that an N4 class and members may contain</p>
</div>
<div class="ulist">
<ul>
<li>
<p>annotations, abstract and access modifiers</p>
</li>
<li>
<p>fields</p>
</li>
<li>
<p>types</p>
</li>
<li>
<p>implemented interfaces</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Note that even <code>static</code> is used in ECMAScript 6.</p>
</div>
</div>
</div>
<div class="paragraph">
<p>Mixing in members (i.e. interface’s methods with default implementation or fields) is similar to mixing in members from roles as defined in [<a href="appendix_c_bibliography.html#Dart13a">Dart13a(p.S9.1)</a>].
It is also similar to default implementations in Java 8 [<a href="appendix_c_bibliography.html#Gosling15a">Gosling15a</a>].
In Java, however, more constraints exist, (for example, methods of interfaces must be public).</p>
</div>
<div class="exampleblock">
<div class="title">Example 30. Simple Class</div>
<div class="content">
<div class="paragraph">
<p>This first example shows a very simple class with a field, a constructor and a method.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {
data: any;
constructor(data: any) {
this.data = data;
}
foo(): void { }
}</code></pre>
</div>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 31. Extend and implement</div>
<div class="content">
<div class="paragraph">
<p>The following example demonstrate how a class can extend a superclass and implement an interface.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">interface I {
foo(): void
}
class C{}
class X extends C implements I {
@Override
foo(): void {}
}</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>A class <code>C</code> is a subtype of another classifier <code>S</code> (which can be a class or interface) if the other classifier <code>S</code> is (transitively) contained in the supertypes (superclasses or implemented interfaces) of the class:</p>
</div>
<div class="openblock">
<div class="content">
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mo>=</mo><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow><mrow><mi>Γ</mi><mo>⊢</mo><mstyle mathvariant="monospace"><mtext>TClass</mtext></mstyle><mi> </mi><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mfenced close="]" open="["><mrow><mi>T</mi><mi>C</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi></mrow></mfenced><mi> </mi><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></mfrac><mfenced close="]" open="["><mrow><mi>s</mi><mi>h</mi><mi>o</mi><mi>r</mi><mi>t</mi><mi>c</mi><mi>u</mi><mi>t</mi></mrow></mfenced><mtext>
</mtext></math>
</div>
</div>
<div class="openblock">
<div class="content">
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>Γ</mi><mo>⊢</mo><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mo>.</mo><mi>s</mi><mi>u</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow><mrow><mi>Γ</mi><mo>⊢</mo><mstyle mathvariant="monospace"><mtext>TClass</mtext></mstyle><mi> </mi><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mfenced close="]" open="["><mrow><mi>T</mi><mi>C</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi></mrow></mfenced><mi> </mi><mi>r</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi></mrow></mfrac></math>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-44"></a><strong>Req. IDE-44:</strong> <a href="#Req-IDE-44">Implicit Supertype of Classes</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The implicit supertype of all classes is <code>N4Object</code>.
All classes with no explicit supertype are inherited from <code>N4Object</code>.</p>
</li>
<li>
<p>If the supertype is explicitly set to <code>Object</code>, then the class is not derived from <code>N4Object</code>.
Meta-information is created similar to an <code>N4Object</code>-derived class.
Usually, there is no reason to explicitly derive a class from <code>Object</code>.</p>
</li>
<li>
<p>External classes are implicitly derived from , unless they are annotated with <code>@N4JS</code>(cf.<a href="plainjs.html#_external-declarations">External Declarations</a>).</p>
</li>
</ol>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_final-modifier"><a class="anchor" href="#_final-modifier"></a><a class="link" href="#_final-modifier">5.1.3.3. Final Modifier</a></h5>
<div class="paragraph">
<p>Extensibility refers to whether a given classifier can be subtyped.
Accessibility is a prerequisite for extensibility.
If a type cannot be seen, it cannot be subclassed.
The only modifier influencing the extensibility directly is the annotation <code>@Final</code>, which prevents all subtyping.
The following table shows how to prevent other projects or vendors from subtyping by also restricting the accessibility of the constructor:</p>
</div>
<table class="tableblock frame-all grid-all spread">
<caption class="title">Table 6. Extensibility of Types</caption>
<colgroup>
<col style="width: 62.5%;">
<col style="width: 12.5%;">
<col style="width: 12.5%;">
<col style="width: 12.5%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-center valign-top">Type <code>C</code> Settings</th>
<th class="tableblock halign-center valign-top" colspan="3">Subclassed in</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><strong>Project</strong></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><strong>Vendor</strong></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><strong>World</strong></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>C.final</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>C.ctor.accessModifier=\lenum{project}</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>C.ctor.accessModifier=\lenum{public@Internal}</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">no</p></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>Since interfaces are always to be implemented, they must not be declared final.</p>
</div>
</div>
<div class="sect4">
<h5 id="_abstract-classes"><a class="anchor" href="#_abstract-classes"></a><a class="link" href="#_abstract-classes">5.1.3.4. Abstract Classes</a></h5>
<div class="paragraph">
<p>A class with modifier <code>abstract</code> is called an <em>abstract class</em> and has its <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi></math> property set to true.
Other classes are called <em>concrete</em> classes.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-45"></a><strong>Req. IDE-45:</strong> <a href="#Req-IDE-45">Abstract Class</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>A class <code>C</code> must be declared abstract if it owns or inherits one or more abstract members and neither C nor any interfaces implemented by C implements these members.
A concrete class has to, therefore, implement all abstract members of its superclasses’ implemented interfaces.
Note that a class may implement fields with field accessors and vice versa.</p>
</li>
<li>
<p>An abstract class may not be instantiated.</p>
</li>
<li>
<p>An abstract class cannot be set to final (with annotation <code>@Final</code>).</p>
</li>
</ol>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-46"></a><strong>Req. IDE-46:</strong> <a href="#Req-IDE-46">Abstract Member</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p></p>
</div>
<div class="sidebarblock">
<div class="content">
<a href="https://github.com/eclipse/n4js/issues/1047" class="gray" title="GitHub Issues"><span class="image"><img src="images/issue.svg" alt=""></span> #1047</a>
</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>A member declared as abstract must not have a method body (in contrary a method not declared as abstract have to have a method body).</p>
</li>
<li>
<p>Only methods, getters and setters can be declared as abstract (fields cannot be abstract).</p>
</li>
<li>
<p>It is not possible to inherit from an abstract class which contains abstract members which are not visible in the subclass.</p>
</li>
<li>
<p>An abstract member must not be set to final (with annotation <code>@Final</code>).</p>
</li>
<li>
<p>Static members must not be declared abstract.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>Remarks:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>We decided to disallow abstract static members, since we cannot guarantee that a static members is not accessed in all cases</p>
</li>
<li>
<p>Only static members can override static members and only instance members can override other instance members of course.</p>
</li>
<li>
<p>An abstract member must not be declared in a final class (i.e. a class annotated with <code>@Final</code>). This is not explicitly defined as constraint in <a href="#Req-IDE-46">[Req-IDE-46]</a> since abstract classes must not defined final anyway. We also do not produce error message for abstract members in final classes since these errors would be consequential errors.</p>
</li>
</ul>
</div>
<div class="admonitionblock warning">
<table>
<tr>
<td class="icon">
<i class="fa icon-warning" title=""></i>
</td>
<td class="content">
Abstract members might be declared private, as they can be accessed from within the module.
This is to be changed in order to be aligned with TypeScript, cf. <a href="https://github.com/eclipse/n4js/issues/1221" class="gray" title="GitHub Issues"><span class="image"><img src="images/issue.svg" alt=""></span> #1221</a>. However we also want to add class expressions&#8201;&#8212;&#8201;and then the abstract members may be accessed (and overridden) in nested classes created by means of class expressions.
</td>
</tr>
</table>
</div>
</div>
<div class="sect4">
<h5 id="_non-instantiable-classes"><a class="anchor" href="#_non-instantiable-classes"></a><a class="link" href="#_non-instantiable-classes">5.1.3.5. Non-Instantiable Classes</a></h5>
<div class="paragraph">
<p>To make a class non-instantiable outside a defining compilation unit, i.e. disallow creation of instances for this class, simply declare the constructor as private.
This can be used for singletons.</p>
</div>
</div>
<div class="sect4">
<h5 id="_superclass"><a class="anchor" href="#_superclass"></a><a class="link" href="#_superclass">5.1.3.6. Superclass</a></h5>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-47"></a><strong>Req. IDE-47:</strong> <a href="#Req-IDE-47">Superclass</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a class <code>C</code> with a supertype <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mo>=</mo><mi>C</mi><mo>.</mo><mi>s</mi><mi>u</mi><mi>p</mi></math>, the following constraints must hold;</p>
</div>
<div class="ulist">
<ul>
<li>
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi><mo>.</mo><mi>s</mi><mi>u</mi><mi>p</mi></math> must reference a class declaration <code>S</code></p>
</li>
<li>
<p><code>S</code> must be be extendable in the project of <code>C</code></p>
</li>
<li>
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi><mo>∉</mo><mi>C</mi><mo>.</mo><mi>s</mi><mi>u</mi><msup><mi>p</mi><mo>+</mo></msup></math></p>
</li>
<li>
<p>All abstract members in <code>S</code> must be accessible from <code>C</code>:</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mo>∀</mo><mi>M</mi><mo>∈</mo><mi>S</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi><mi>M</mi><mo>.</mo><mi>a</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi><mo>⇒</mo></math><br>
<code>M</code> is accessible from <code>C</code>.<br>
Note that <code>M</code> need not be an owned member of <code>S</code> and that this constraint applies even if <code>C</code> is abstract).</p>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>All members of superclasses become members of a class.
This is true even if the owning classes are not directly accessible to a class.
The member-specific access control is not changed.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_interfaces"><a class="anchor" href="#_interfaces"></a><a class="link" href="#_interfaces">5.1.4. Interfaces</a></h4>
<div class="sect4">
<h5 id="_definition-of-interfaces"><a class="anchor" href="#_definition-of-interfaces"></a><a class="link" href="#_definition-of-interfaces">5.1.4.1. Definition of Interfaces</a></h5>
<div class="sect5">
<h6 id="interfaces-syntax"><a class="anchor" href="#interfaces-syntax"></a><a class="link" href="#interfaces-syntax">5.1.4.1.1. Syntax</a></h6>
<div class="listingblock">
<div class="title">Syntax N4 Interface Declaration</div>
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">N4InterfaceDeclaration &lt;Yield&gt;:
=&gt; (
{N4InterfaceDeclaration}
annotations+=Annotation*
(declaredModifiers+=N4Modifier)*
'interface' typingStrategy=TypingStrategyDefSiteOperator? name=BindingIdentifier&lt;Yield&gt;?
)
TypeVariables?
InterfaceImplementsList?
Members&lt;Yield&gt;
;
fragment InterfaceImplementsList*:
'implements' superInterfaceRefs+=ParameterizedTypeRefNominal
(',' superInterfaceRefs+=ParameterizedTypeRefNominal)*
;</code></pre>
</div>
</div>
</div>
<div class="sect5">
<h6 id="interfaces-properties"><a class="anchor" href="#interfaces-properties"></a><a class="link" href="#interfaces-properties">5.1.4.1.2. Properties</a></h6>
<div class="paragraph">
<p>These are the additional properties of interfaces, which can be specified by the user:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>superInterfaces</code></dt>
<dd>
<p>Collection of interfaces extended by this interface; empty by default.
Instead of <code>superInterfaces</code>, we simply write <code>interfaces</code>.</p>
</dd>
</dl>
</div>
</div>
<div class="sect5">
<h6 id="interfaces-type-inference"><a class="anchor" href="#interfaces-type-inference"></a><a class="link" href="#interfaces-type-inference">5.1.4.1.3. Type Inference</a></h6>
<div class="paragraph">
<p>The type of an interface declaration <code>I</code> is of type <code>type{I}</code>:</p>
</div>
<div class="openblock">
<div class="content">
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow/><mrow><mi>Γ</mi><mo>⊢</mo><mi>I</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>type</mtext></mstyle><mfenced close="}" open="{"><mi>I</mi></mfenced></mrow></mfrac></math>
</div>
</div>
</div>
<div class="sect5">
<h6 id="interfaces-semantics"><a class="anchor" href="#interfaces-semantics"></a><a class="link" href="#interfaces-semantics">5.1.4.1.4. Semantics</a></h6>
<div class="paragraph">
<p>Interfaces are used to describe the public <a href="appendix_a_acronyms.html#_acronyms">API</a> of a classifier.
The main requirement is that the instance of an interface, which must be an instance of a class since interfaces cannot have instances, provides all members declared in the interface.
Thus, a (concrete) class implementing an interface must provide implementations for all the fields, methods, getters and setters of the interface (otherwise it the class must be declared abstract).
The implementations have to be provided either directly in the class itself, through a superclass, or by the interface if the member has a default implementation.</p>
</div>
<div class="paragraph">
<p>A field declaration in an interface denotes that all implementing classes can either provide a field of the same name and the same(!) type or corresponding field accessors.
If no such members are defined in the class or a (transitive) superclass, the field is mixed in from the interface automatically.
This is also true for the initializer of the field.</p>
</div>
<div class="paragraph">
<p>All instance methods, getters and setters declared in an interface are implicitly abstract if they do not provide a default implementation.
The modifier <code>abstract</code> is not required, therefore, in the source code.
The following constraints apply:</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-48"></a><strong>Req. IDE-48:</strong> <a href="#Req-IDE-48">Interfaces</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For any interface <code>I</code>, the following must hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Interfaces may not be instantiated.</p>
</li>
<li>
<p>Interfaces cannot be set to final (with annotation @Final): <math xmlns="http://www.w3.org/1998/Math/MathML"><mo>¬</mo><mi>I</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi></math>.</p>
</li>
<li>
<p>Members of an interface must not be declared private.
The default access modifier in interfaces is the the type’s visibility or <code>project</code>, if the type’s visibility is <code>private</code>.</p>
</li>
<li>
<p>Members of an interface, except methods, must not be declared <code>@Final</code>:</p>
<div class="openblock">
<div class="content">
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mo>∀</mo><mi>m</mi><mo>∈</mo><mi>I</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>:</mi><mi>m</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi><mo>→</mo><mi>m</mi><mo>∈</mo><mi>I</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><mi>s</mi></math>
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
not allowing field accessors to be declared final was a deliberate decision, because it would complicate the internal handling of member redefinition; might be reconsidered at a later time
</td>
</tr>
</table>
</div>
</li>
<li>
<p>The literal may not be used in the initializer expression of a field of an interface.<br>
This restriction is required, because the order of implementation of these fields in an implementing class cannot be guaranteed.
This applies to both instance and static fields in interfaces, but in case of static fields, <code>this</code> is also disallowed due to <a href="#_static-members-of-interfaces">Static Members of Interfaces</a>.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>It is possible to declare members in interfaces with a smaller visibility as the interface itself.
In that case, clients of the interface may be able to use the interface but not to implement it.</p>
</div>
<div class="paragraph">
<p>In order to simplify modeling of runtime types, such as elements, interfaces do not only support the notation of static methods but constant data fields as well.
Since <a href="appendix_a_acronyms.html#_acronyms">IDL</a> [<a href="appendix_c_bibliography.html#OMG14a">OMG14a</a>] is used to describe these elements in specifications (and mapped to JavaScript via rules described in [<a href="appendix_c_bibliography.html#W3C12a">W3C12a</a>])
constant data fields are an often-used technique there and they can be modeled in N4JS 1:1.</p>
</div>
<div class="paragraph">
<p>As specified in <a href="#Req-IDE-56">[Req-IDE-56]</a>, interfaces cannot contain a constructor i.e.<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>∀</mo><mi>m</mi><mo>∈</mo><mi>I</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><mi>s</mi><mi>:</mi><mi>m</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><msup><mo>≠</mo><mi>'</mi></msup><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>o</mi><msup><mi>r</mi><mi>'</mi></msup></math>.</p>
</div>
<div class="exampleblock">
<div class="title">Example 32. Simple Interfaces</div>
<div class="content">
<div class="paragraph">
<p>The following example shows the syntax for defining interfaces.
The second interface extends the first one.
Note that methods are implicitly defined abstract in interfaces.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">interface I {
foo(): void
}
interface I2 extends I {
someText: string;
bar(): void
}</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>If a classifier <code>C</code> <em>implements</em> an interface <code>I</code>, we say <code>I</code> is <em>implemented</em> by <code>C</code>.
If <code>C</code> redefines members declared in <code>I</code>, we say that these members are <em>implemented</em> by <code>C</code>.
Members not redefined by <code>C</code> but with a default implementations are <em>mixed in</em> or <em>consumed by</em> <code>C</code>.
We all cases we call <code>C</code> the <em>implementor</em>.</p>
</div>
<div class="paragraph">
<p>Besides the general constraints described in <a href="#_common-semantics-of-classifiers">Common Semantics of Classifiers</a>, the following constraints must hold for extending or implementing interfaces:</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-49"></a><strong>Req. IDE-49:</strong> <a href="#Req-IDE-49">Extending Interfaces</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a given type
<code>I</code>, and <math xmlns="http://www.w3.org/1998/Math/MathML"><mfenced close="}" open="{"><msub><mi>I</mi><mn>1</mn></msub><mrow><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></mrow><msub><mi>I</mi><mi>n</mi></msub></mfenced></math> directly extended by <code>I</code>, the following constraints must be true:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Only interfaces can extend interfaces: <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>I</mi><mo>,</mo><msub><mi>I</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>I</mi><mi>n</mi></msub></math> must be interfaces.</p>
</li>
<li>
<p>An interface may not directly extend the same interface more than once:<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>I</mi><mi>i</mi></msub><mo>=</mo><msub><mi>I</mi><mi>j</mi></msub><mo>→</mo><mi>i</mi><mo>=</mo><mi>j</mi></math> for any
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi><mo>,</mo><mi>j</mi><mo>∈</mo><mfenced close="}" open="{"><mrow><mn>1</mn><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mi>n</mi></mrow></mfenced></math>.</p>
</li>
<li>
<p>An interface may (indirectly) extend the same interface <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>J</mi></math> more than once only if</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>J</mi></math> is not parameterized, or</p>
</li>
<li>
<p>in all cases <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>J</mi></math> is extended with the same type arguments for all invariant type parameters.<br>
Note that for type parameters of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>J</mi></math> that are declared covariant or contravariant on definition site, different type arguments may be used.</p>
</li>
</ol>
</div>
</li>
<li>
<p>All abstract members in <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>I</mi><mi>i</mi></msub></math>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi><mo>∈</mo><mfenced close="}" open="{"><mn>1</mn><mrow><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></mrow><mi>n</mi></mfenced></math>, must be accessible from <code>I</code>:<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>∀</mo><mi>i</mi><mo>∈</mo><mfenced close="}" open="{"><mn>1</mn><mrow><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></mrow><mi>n</mi></mfenced><mi>:</mi><mi>M</mi><mo>∈</mo><msub><mi>I</mi><mi>i</mi></msub><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>∧</mo><mi>M</mi><mo>.</mo><mi>a</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi><mo>→</mo></math> <code>M</code> is accessible from <code>I</code>.<br>
Note that <code>M</code> need not be an owned member of <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>I</mi><mi>i</mi></msub></math>.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-50"></a><strong>Req. IDE-50:</strong> <a href="#Req-IDE-50">Implementing Interfaces</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a given type
<code>C</code>, and <math xmlns="http://www.w3.org/1998/Math/MathML"><mfenced close="}" open="{"><msub><mi>I</mi><mn>1</mn></msub><mrow><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></mrow><msub><mi>I</mi><mi>n</mi></msub></mfenced></math> directly implemented
by <code>C</code>, the following constraints must be true:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Only classes can implement interfaces: <code>C</code> must be a Class.</p>
</li>
<li>
<p>A class can only implement interfaces: <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>I</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>I</mi><mi>n</mi></msub></math> must be interfaces.</p>
</li>
<li>
<p>A class may not directly implement the same interface more than once:<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>I</mi><mi>i</mi></msub><mo>=</mo><msub><mi>I</mi><mi>j</mi></msub><mo>⇒</mo><mi>i</mi><mo>=</mo><mi>j</mi></math> for any <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi><mo>,</mo><mi>j</mi><mo>∈</mo><mfenced close="}" open="{"><mn>1</mn><mrow><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></mrow><mi>n</mi></mfenced></math>.</p>
</li>
<li>
<p>A class may (indirectly) implement the same interface <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>J</mi></math> more than once only if</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>J</mi></math> is not parameterized, or</p>
</li>
<li>
<p>in all cases <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>J</mi></math> is implemented with the same type arguments for all invariant type parameters.<br>
Note that for type parameters of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>J</mi></math> that are declared covariant or contravariant on definition site, different type arguments may be used.</p>
</li>
</ol>
</div>
</li>
<li>
<p>All abstract members in <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>I</mi><mi>i</mi></msub></math>, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi><mo>∈</mo><mfenced close="}" open="{"><mn>1</mn><mrow><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></mrow><mi>n</mi></mfenced></math>, must be accessible from <code>C</code>:<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>∀</mo><mi>i</mi><mo>∈</mo><mfenced close="}" open="{"><mn>1</mn><mrow><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/></mrow><mi>n</mi></mfenced><mi>:</mi><mi>M</mi><mo>∈</mo><msub><mi>I</mi><mi>i</mi></msub><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>∧</mo><mi>M</mi><mo>.</mo><mi>a</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi><mo>→</mo></math> <code>M</code> is accessible from <code>C</code>.<br>
Note that <code>M</code> need not be an owned member of <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>I</mi><mi>i</mi></msub></math>.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>For default methods in interfaces, see <a href="#_default-methods-in-interfaces">Default Methods in Interfaces</a>.</p>
</div>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_generic-classifiers"><a class="anchor" href="#_generic-classifiers"></a><a class="link" href="#_generic-classifiers">5.1.5. Generic Classifiers</a></h4>
<div class="paragraph">
<p>Classifiers can be declared generic by defining a type parameter via <code>type-param</code>.</p>
</div>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="generic_classifiers"></a><strong>Definition:</strong> <a href="#generic_classifiers">Generic Classifiers</a></p>
</div>
<div class="paragraph">
<p>A generic classifier is a classifier with at least one type parameter.
That is, a given classifier <code>C</code> is generic if and only if <math xmlns="http://www.w3.org/1998/Math/MathML"><mo>|</mo><mi>C</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>P</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>|</mo><mo>≥</mo><mn>1</mn></math>.</p>
</div>
<div class="paragraph">
<p>If a classifier does not define any type parameters, it is not generic, even if its superclass or any implemented interface is generic.</p>
</div>
<div class="paragraph">
<p>The format of the type parameter expression is described in <a href="types.html#_parameterized-types">Parameterized Types</a>.
The type variable defined by the type parameter’s type expression can be used just like a normal type inside the class definition.</p>
</div>
<div class="paragraph">
<p>If using a generic classifier as type of a variable, it may be parameterized.
This is usually done via a type expression (cf. <a href="types.html#_parameterized-types">Parameterized Types</a>) or via <code>typearg</code> in case of supertypes.
If a generic classifier defines multiple type variables, these variables are bound in the order of their definition.
In any case, all type variables have to be bound.
That means in particular that raw types are not allowed. (cf <a href="types.html#_parameterized-types">Parameterized Types</a> for details).</p>
</div>
<div class="paragraph">
<p>If a generic classifier is used as super classifier, the type arguments can be type variables.
Note that the type variable of the super classifier is not lifted, that is to say that all type variables are to be explicitly bound in the type references used in the <code>extend</code>, <code>with</code>, or <code>implements</code> section using <code>typearg</code>.
If a type variable is used in <code>typearg</code> to bound a type variable of a type parameter, it has to fulfil possible type constraints (upper/lower bound) specified in the type parameter.</p>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 33. Generic Type Definition and Usage as Type of Variable</div>
<div class="content">
<div class="paragraph">
<p>This example demonstrates how to define a generic type and how to refer to it in a variable definition.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">export class Container&lt;T&gt; {
private item: T;
getItem(): T {
return this.item;
}
setItem(item: T): void {
this.item = item;
}
}</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>This type can now be used as a type of a variable as follows</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">import Container from "p/Container"
var stringContainer: Container&lt;string&gt; = new Container&lt;string&gt;();
stringContainer.setItem("Hello");
var s: string = stringContainer.getItem();</code></pre>
</div>
</div>
<div class="paragraph">
<p>In line 3, the type variable <code>T</code> of the generic class <code>Container</code> is bound to <code>string</code>.</p>
</div>
<div class="exampleblock">
<div class="title">Example 34. Binding of type variables with multiple types</div>
<div class="content">
<div class="paragraph">
<p>For a given generic class <code>G</code></p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class A{}
class B{}
class C extends A{}
class G&lt;S, T extends A, U extends B&gt; {
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>the variable definition</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var x: G&lt;Number,C,B&gt;;</code></pre>
</div>
</div>
<div class="paragraph">
<p>would bind the type variables as follows:</p>
</div>
<table class="tableblock frame-all grid-all spread">
<colgroup>
<col style="width: 16.6666%;">
<col style="width: 16.6666%;">
<col style="width: 66.6668%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>S</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>Number</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Bound by first type argument, no bound constraints defined for <code>S</code>.</p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>T</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>C</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Bound by second type argument, <code>C</code> must be a subtype of in order to fulfill the type constraint.</p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>U</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>B</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Bound by third type argument, <code>extends</code> is reflexive, that is <code>B</code> fulfills the
type constraint.</p></td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-51"></a><strong>Req. IDE-51:</strong> <a href="#Req-IDE-51">Generic Superclass, Type Argument with Type Variable</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a given generic superclass <code>SuperClass</code></p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class SuperClass&lt;S, T extends A, U extends B&gt; {};</code></pre>
</div>
</div>
<div class="paragraph">
<p>and a generic subclass <code>SubClass</code></p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class SubClass&lt;X extends A&gt; extends SuperClass&lt;Number, X, B&gt; {..};</code></pre>
</div>
</div>
<div class="paragraph">
<p>the variable definition</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var s: SubClass&lt;C&gt;;</code></pre>
</div>
</div>
<div class="paragraph">
<p>would bind the type variables as follows:<br></p>
</div>
<table class="tableblock frame-all grid-all spread">
<colgroup>
<col style="width: 16.6666%;">
<col style="width: 16.6666%;">
<col style="width: 66.6668%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-center valign-top">TypeVariable</th>
<th class="tableblock halign-center valign-top">Bound to</th>
<th class="tableblock halign-center valign-top">Explanation</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>SuperClass.S</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>Number</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Type variable <code>s</code> of supertype <code>SuperClass</code> is bound to <code>Number</code>.</p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>SuperClass.T</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>SubClass.X=C</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Type variable <code>T</code> of supertype <code>SuperClass</code> is bound to type variable <code>X</code> of <code>SubClass</code>. It gets
then indirectly bound to <code>C</code> as specified by the type argument of the
variable definition.</p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>SuperClass.U</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>B</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Type variable <code>U</code> of supertype <code>SuperClass</code> is auto-bound to <code>C</code> as no explicit binding for the third type variable is specified.</p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>SubClass.X</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>C</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Bound by first type argument specified in variable definition.</p></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="sec:definition-site-variance"><a class="anchor" href="#sec:definition-site-variance"></a><a class="link" href="#sec:definition-site-variance">5.1.6. Definition-Site Variance</a></h4>
<div class="paragraph">
<p>In addition to use-site declaration of variance in the form of Java-like wildcards, N4JS provides support for definition-site declaration of variance as known from languages such as C# and Scala.</p>
</div>
<div class="paragraph">
<p>The <em>variance</em> of a parameterized type states how its subtyping relates to its type arguments’ subtyping.
For example, given a parameterized type <code>G&lt;T&gt;</code> and plain types <code>A</code> and <code>B</code>, we know</p>
</div>
<div class="ulist">
<ul>
<li>
<p>if <code>G</code> is <strong>covariant</strong> w.r.t. its parameter <code>T</code>, then</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>B</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>A</mtext></mstyle><mo>→</mo><mstyle mathvariant="monospace"><mtext>G</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>B</mtext></mstyle><mo>&gt;</mo><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>G</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>A</mtext></mstyle><mo>&gt;</mo></math></p>
</div>
</li>
<li>
<p>if <code>G</code> is <strong>contravariant</strong> w.r.t. its parameter <code>T</code>, then</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>B</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>A</mtext></mstyle><mo>→</mo><mstyle mathvariant="monospace"><mtext>G</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>A</mtext></mstyle><mo>&gt;</mo><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>G</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>B</mtext></mstyle><mo>&gt;</mo></math></p>
</div>
</li>
<li>
<p>if <code>G</code> is <strong>invariant</strong> w.r.t. its parameter <code>T</code>, then</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>B</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>A</mtext></mstyle><mo>→</mo><mstyle mathvariant="monospace"><mtext>G</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>A</mtext></mstyle><mo>&gt;</mo><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>G</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>B</mtext></mstyle><mo>&gt;</mo></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>B</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>A</mtext></mstyle><mo>→</mo><mstyle mathvariant="monospace"><mtext>G</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>A</mtext></mstyle><mo>&gt;</mo><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>G</mtext></mstyle><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>B</mtext></mstyle><mo>&gt;</mo></math></p>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>Note that variance is declared per type parameter, so a single parameterized type with more than one type parameter may be, for example, covariant w.r.t. one type parameter and contravariant w.r.t. another.</p>
</div>
<div class="paragraph">
<p>Strictly speaking, a type parameter/variable itself is not co- or contravariant;<br>
however, for the sake of simplicity we say <em><code>T</code> is covariant</em> as a short form for <em><code>G</code> is covariant with respect to its type parameter <code>T</code></em> (for contravariant and invariant accordingly).</p>
</div>
<div class="paragraph">
<p>To declare the variance of a parameterized classifier on definition site, simply add keyword <code>in</code> or <code>out</code> before the corresponding type parameter:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class ReadOnlyList&lt;out T&gt; { // covariance
// ...
}
interface Consumer&lt;in T&gt; { // contravariance
// ...
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>In such cases, the following constraints apply.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-174"></a><strong>Req. IDE-174:</strong> <a href="#Req-IDE-174">Definition-Site Variance</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>Given a parameterized type with a type parameter , the following must hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><code>T</code> may only appear in variance-compatible positions:</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>if <code>T</code> is declared on definition site to be <strong>covariant</strong>, then it may only appear in covariant positions within the type’s non-private member declarations.</p>
</li>
<li>
<p>if <code>T</code> is declared on definition site to be <strong>contravariant</strong>, then it may only appear in contravariant positions within the type’s non-private member declarations.</p>
</li>
<li>
<p>if <code>T</code> is <strong>invariant</strong>, i.e. neither declared covariant nor declared contravariant on definition site, then it may appear in any position (where type variables are allowed).</p>
<div class="paragraph">
<p>Thus, no restrictions apply within the declaration of private members and within the body of field accessors and methods.</p>
</div>
</li>
</ol>
</div>
</li>
<li>
<p>definition-site variance may not be combined with incompatible use-site variance:</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>if <code>T</code> is declared on definition site to be <strong>covariant</strong>, then no wildcard with a <strong>lower</strong> bound may be provided as type argument for <code>T</code>.</p>
</li>
<li>
<p>if <code>T</code> is declared on definition site to be <strong>contravariant</strong>, then no wildcard with an <strong>upper</strong> bound may be provided as type argument for <code>T</code>.</p>
</li>
<li>
<p>if <code>T</code> is <strong>invariant</strong>, i.e. neither declared covariant nor declared contravariant on definition site, then any kind of wildcard may be provided as type argument.</p>
<div class="paragraph">
<p>Unbounded wildcards are allowed in all cases.</p>
</div>
</li>
</ol>
</div>
</li>
</ol>
</div>
</div>
</div>
<div id="ex:use-site-declaration-variance" class="exampleblock">
<div class="title">Example 35. Use-site declaration of variance</div>
<div class="content">
<div class="paragraph">
<p>For illustration purposes, let’s compare use-site and definition-site declaration of variance.
Since use-site variance is more familiar to the Java developer, we start with this flavor.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class Person {
name: string;
}
class Employee extends Person {}
interface List&lt;T&gt; {
add(elem: T)
read(idx: int): T
}
function getNameOfFirstPerson(list: List&lt;? extends Person&gt;): string {
return list.read(0).name;
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Function <code>getNameOfFirstPerson</code> below takes a list and returns the name of the first person in the list.
Since it never adds new elements to the given list, it could accept <code>List</code>s of any subtype of <code>Person</code>, for example a <code>List&lt;Employee&gt;</code>.
To allow this, its formal parameter has a type of <code>List&lt;? extends Person&gt;</code> instead of <code>List&lt;Person&gt;</code>.
Such use-site variance is useful whenever an invariant type, like <code>List</code> above, is being used in a way such that it can be treated as if it were co- or contravariant.</p>
</div>
<div class="paragraph">
<p>Sometimes, however, we are dealing with types that are inherently covariant or contravariant, for example an <code>ImmutableList</code> from which we can only read elements would be covariant.
In such a case, use-site declaration of variance is tedious and error-prone: we would have to declare the variance wherever the type is being used and would have to
make sure not to forget the declaration or otherwise limit the flexibility and reusability of the code (for example, in the above code we could not call <code>getNameOfFirstPerson</code> with a <code>List&lt;Employee&gt;</code>).</p>
</div>
<div class="paragraph">
<p>The solution is to declare the variance on declaration site, as in the following code sample:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">interface ImmutableList&lt;out T&gt; {
// add(elem: T) // error: such a method would now be disallowed
read(idx: int): T
}
function getNameOfFirstPerson2(list: ImmutableList&lt;Person&gt;): string {
return list.read(0).name;
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Now we can invoke <code>getNameOfFirstPerson2</code> with a <code>List&lt;Employee&gt;</code> even though the implementor of <code>getNameOfFirstPerson2</code> did not add a
use-site declaration of covariance, because the type <code>ImmutableList</code> is declared to be covariant with respect to its parameter <code>T</code>, and this applies globally
throughout the program.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_members"><a class="anchor" href="#_members"></a><a class="link" href="#_members">5.2. Members</a></h3>
<div class="paragraph">
<p>A member is either a method (which may be a special constructor function), a data field, or a getter or a setter.
The latter two implicitly define an accessor field.
Similar to object literals, there must be no data field with the same name as a getter or setter.</p>
</div>
<div class="paragraph">
<p>(overriding, implementation and consumption) is
described in <a href="#_redefinition-of-members">Redefinition of Members</a>.</p>
</div>
<div class="sect3 language-n4js">
<h4 id="_syntax-4"><a class="anchor" href="#_syntax-4"></a><a class="link" href="#_syntax-4">5.2.1. Syntax</a></h4>
<div class="listingblock">
<div class="title">Syntax N4JS member access modifier</div>
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">enum N4JSMemberAccessModifier: private | project | protected | public;
N4MemberDeclaration: N4MethodDeclaration | N4FieldDeclaration | N4GetterDeclaration | N4SetterDeclaration;</code></pre>
</div>
</div>
<div class="sect4">
<h5 id="_properties-3"><a class="anchor" href="#_properties-3"></a><a class="link" href="#_properties-3">5.2.1.1. Properties</a></h5>
<div class="paragraph">
<p>Members share the following properties:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>annotations</code> </dt>
<dd>
<p>Arbitrary annotations, see <a href="annotations.html#_annotations">Annotations</a> for details.</p>
</dd>
<dt class="hdlist1"><code>accessModifier</code> </dt>
<dd>
<p>N4JS member access modifier: <code>private</code>, <code>project</code>, <code>potected</code>, or <code>public</code>; the latter two can be combined with <code>@Internal</code>; default is <code>project</code> for classes and private interfaces.
For a non-private interface defaults to the interface’s visibility.</p>
</dd>
<dt class="hdlist1"><code>name</code> </dt>
<dd>
<p>The simple name of the member, that is an identifier name (cf. <a href="names.html#_valid-names">Valid Names</a>).</p>
</dd>
<dt class="hdlist1"><code>static</code> </dt>
<dd>
<p>Boolean property to distinguish instance from classifier members, see <a href="#_static-members">Static Members</a>.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>The following pseudo properties are defined via annotations:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>deprecated</code> </dt>
<dd>
<p>Boolean property set to true if annotation <code>@Deprecated</code> is set. <sup class="footnote">[<a id="_footnoteref_29" class="footnote" href="appendix_c_bibliography.html#_footnote_29" title="View footnote.">29</a>]</sup></p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>And we additionally define the following pseudo properties:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>acc</code> </dt>
<dd>
<p>Member access modifier as described in <a href="names.html#_accessibility-of-members">Accessibility of Members</a>, it is the aggregated value of
the <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>c</mi><mi>c</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>M</mi><mi>o</mi><mi>d</mi><mi>i</mi><mi>f</mi><mi>i</mi><mi>e</mi><mi>r</mi></math> and the <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><mi>x</mi><mi>p</mi><mi>o</mi><mi>r</mi><mi>t</mi></math> property.</p>
</dd>
<dt class="hdlist1"><code>owner</code> </dt>
<dd>
<p>Owner classifier of the member.</p>
</dd>
<dt class="hdlist1"><code>typeRef</code> </dt>
<dd>
<p>Type of the member—this is the type of a field or the type of the method which is a function type (and not the return type).</p>
</dd>
<dt class="hdlist1"><code>assignability</code> </dt>
<dd>
<p>Enumeration, may be one of the following values:</p>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>set</code></dt>
<dd>
<p>Member may only be set, i.e. it could only be used on the left hand side of an assignment.</p>
</dd>
<dt class="hdlist1"><code>get</code></dt>
<dd>
<p>Member may only be retrieved, i.e. it could only be used on the right hand side of an assignment. This is the default setting for methods.</p>
</dd>
<dt class="hdlist1"><code>any</code></dt>
<dd>
<p> Member may be set or retrieved, i.e. it could only be used on the left or right hand side of an assignment.
This is the default setting for fields.</p>
</dd>
</dl>
</div>
</dd>
</dl>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>a</mi><mi>b</mi><mi>i</mi><mi>l</mi><mi>i</mi><mi>t</mi><mi>y</mi></math> is related but not equal to writable modifiers used for fields.
We define a partial order on this enumeration as follows:<br>
</td>
</tr>
</table>
</div>
<div class="openblock">
<div class="content">
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mo>&lt;</mo><mfenced close=")" open="("><mi>l</mi><mi>r</mi></mfenced><mspace width="3.0mm"/><mi>:</mi><mi>:</mi><mo>=</mo><mspace width="3.0mm"/><mfenced close="}" open="{"><mfenced close=")" open="("><mstyle mathvariant="monospace"><mtext>set</mtext></mstyle><mstyle mathvariant="monospace"><mtext>any</mtext></mstyle></mfenced><mfenced close=")" open="("><mstyle mathvariant="monospace"><mtext>get</mtext></mstyle><mstyle mathvariant="monospace"><mtext>any</mtext></mstyle></mfenced></mfenced></math>
</div>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>abstract</code> </dt>
<dd>
<p>All members have a flag <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi></math>, which is user-defined for methods, getters and setter, but which is always false for fields.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>The following pseudo property is set to make fields compatible with properties of an object literal, however it cannot be changed:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>configurable</code> </dt>
<dd>
<p>Boolean flag reflecting the property descriptor <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>o</mi><mi>n</mi><mi>f</mi><mi>i</mi><mi>g</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>b</mi><mi>l</mi><mi>e</mi></math>, this is always set to false for members.</p>
</dd>
</dl>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_semantics-3"><a class="anchor" href="#_semantics-3"></a><a class="link" href="#_semantics-3">5.2.2. Semantics</a></h4>
<div class="paragraph">
<p>The members of a given classifier <code>C</code> must be named such that the following constraints are met:</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-52"></a><strong>Req. IDE-52:</strong> <a href="#Req-IDE-52">Member Names</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The name of a member is given as an identifier, a string literal, a numeric literal, or as a computed property name with a compile-time expression (see <a href="expressions.html#compile-time-expressions">Compile-Time Expressions</a>). In particular, string literals, e.g. <code>['myProp']</code>, built-in symbols, e.g. <code>[Symbol.iterator]</code>, and literals of <code>@StringBased</code> enums are all valid computed property names.</p>
</li>
<li>
<p>No two members may have the same name, except one is static and the
other is non-static:</p>
<div class="openblock">
<div class="content">
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mo>∀</mo><msub><mi>m</mi><mn>1</mn></msub><mo>,</mo><msub><mi>m</mi><mn>2</mn></msub><mo>∈</mo><mi>C</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>,</mo><msub><mi>m</mi><mn>1</mn></msub><mo>≠</mo><msub><mi>m</mi><mn>2</mn></msub><mi>:</mi><msub><mi>m</mi><mn>1</mn></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>≠</mo><msub><mi>m</mi><mn>2</mn></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>∨</mo><msub><mi>m</mi><mn>1</mn></msub><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo>≠</mo><msub><mi>m</mi><mn>2</mn></msub><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi></mtd></mtr></mtable></math>
</div>
</div>
</li>
<li>
<p>The member name must be a valid identifier name, see <a href="grammar.html#_identifier-names-and-identifiers">Identifier Grammar</a>.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>Thus, over<em>loading</em> of methods is not supported <sup class="footnote">[<a id="_footnoteref_30" class="footnote" href="appendix_c_bibliography.html#_footnote_30" title="View footnote.">30</a>]</sup> and no field may have the same name as a method.
However, over<em>riding</em> of methods, getters, and setters are possible, see <a href="#_redefinition-of-members">Redefinition of Members</a>.
Static members may also have the same name as non-static members.<sup class="footnote">[<a id="_footnoteref_31" class="footnote" href="appendix_c_bibliography.html#_footnote_31" title="View footnote.">31</a>]</sup></p>
</div>
<div class="paragraph">
<p>The dollar character <code>$</code> is not allowed for user-defined member identifiers as the dollar sign is used for rewriting private members.</p>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_methods"><a class="anchor" href="#_methods"></a><a class="link" href="#_methods">5.2.3. Methods</a></h4>
<div class="paragraph">
<p>Methods are simply JavaScript functions.
They are defined similarly to methods as proposed in [<a href="appendix_c_bibliography.html#ECMA15a">ECMA15a(p.S13.5)</a>] except for the type information and some modifiers.</p>
</div>
<div class="sect4">
<h5 id="_syntax-5"><a class="anchor" href="#_syntax-5"></a><a class="link" href="#_syntax-5">5.2.3.1. Syntax</a></h5>
<div class="listingblock">
<div class="title">Syntax Method Declaration</div>
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">N4MethodDeclaration &lt;Yield&gt;:
=&gt; ({N4MethodDeclaration}
annotations+=Annotation*
accessModifier=N4JSMemberAccessModifier?
(abstract?=’abstract’ | static?=’static’)?
TypeVariables?
(
generator?='*' LiteralOrComputedPropertyName&lt;Yield&gt; -&gt; MethodParamsReturnAndBody &lt;Generator=true&gt;
| AsyncNoTrailingLineBreak LiteralOrComputedPropertyName&lt;Yield&gt; -&gt; MethodParamsReturnAndBody &lt;Generator=false&gt;
)
) ';'?
;
fragment MethodParamsAndBody &lt;Generator&gt;*:
StrictFormalParameters&lt;Yield=Generator&gt;
(body=Block&lt;Yield=Generator&gt;)?
;
fragment MethodParamsReturnAndBody &lt;Generator&gt;*:
StrictFormalParameters&lt;Yield=Generator&gt;
(':' returnTypeRef=TypeRef)?
(body=Block&lt;Yield=Generator&gt;)?
;
fragment LiteralOrComputedPropertyName &lt;Yield&gt;*:
name=IdentifierName | name=STRING | name=NumericLiteralAsString
| '[' (=&gt;((name=SymbolLiteralComputedName&lt;Yield&gt; | name=StringLiteralAsName) ']') | computeNameFrom=AssignmentExpression&lt;In=true,Yield&gt; ']')
;
SymbolLiteralComputedName &lt;Yield&gt;:
BindingIdentifier&lt;Yield&gt; ('.' IdentifierName)?
;
BindingIdentifier &lt;Yield&gt;:
IDENTIFIER
| &lt;!Yield&gt; 'yield'
| N4Keyword
;
IdentifierName: IDENTIFIER | ReservedWord | N4Keyword;
NumericLiteralAsString: DOUBLE | INT | OCTAL_INT | HEX_INT | SCIENTIFIC_INT;
StringLiteralAsName: STRING;
fragment AsyncNoTrailingLineBreak *: (declaredAsync?='async' NoLineTerminator)?; // See Asynchronous Functions
fragment StrictFormalParameters &lt;Yield&gt;*:
'(' (fpars+=FormalParameter&lt;Yield&gt; (',' fpars+=FormalParameter&lt;Yield&gt;)*)? ')'
;
FormalParameter &lt;Yield&gt;:
{FormalParameter} BindingElementFragment&lt;Yield&gt;
;
fragment BindingElementFragment &lt;Yield&gt;*:
(=&gt; bindingPattern=BindingPattern&lt;Yield&gt;
| annotations+=Annotation*
(
variadic?='...'? name=BindingIdentifier&lt;Yield&gt; ColonSepTypeRef?
)
)
('=' initializer=AssignmentExpression&lt;In=true, Yield&gt;)?
;
fragment ColonSepTypeRef*:
':' declaredTypeRef=TypeRef
;</code></pre>
</div>
</div>
</div>
<div class="sect4 language-n4js">
<h5 id="_properties-4"><a class="anchor" href="#_properties-4"></a><a class="link" href="#_properties-4">5.2.3.2. Properties</a></h5>
<div class="paragraph">
<p>Methods have all the properties of members and the following additional properties can be explicitly defined:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>abstract</code> </dt>
<dd>
<p>Method is declared but not defined.</p>
</dd>
<dt class="hdlist1"><code>typePars</code> </dt>
<dd>
<p>Collection of type parameters of a generic method; empty by default.</p>
</dd>
<dt class="hdlist1"><code>returnTypeRef</code> </dt>
<dd>
<p>Return type of the method, default return type is <math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="italic"><mi>V</mi><mi>o</mi><mi>i</mi><mi>d</mi></mstyle></math>.
The type of the method as a member of the owning classifier is not the method’s return type but is instead a function type.</p>
</dd>
<dt class="hdlist1"><code>fpars</code> </dt>
<dd>
<p>List of formal parameters, may be left empty.</p>
</dd>
<dt class="hdlist1"><code>body</code> </dt>
<dd>
<p>The body of the method (this is not available in the pure types model)</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>The following pseudo properties are defined via annotations:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>final</code> </dt>
<dd>
<p>Boolean flag set to true if annotation <code>@Final</code> is set.
The flag indicates that method must not be overridden in subclasses; see <a href="#_final-methods">Final Methods</a>.</p>
</dd>
<dt class="hdlist1"><code>declaresOverride</code> </dt>
<dd>
<p>Flag set to true if annotation <code>@Overrides</code> is set. The flag indicates that method must override a method of a superclass; see <a href="#_overriding-of-members">Overriding of Members</a>.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>Additionally, we define the following pseudo properties:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>overrides</code> </dt>
<dd>
<p>True if method overrides a super method or implements an interface method, false otherwise.</p>
</dd>
<dt class="hdlist1"><code>typeRef</code> </dt>
<dd>
<p>Type of the method. This is, in fact, a function type (and not the return type).</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>The following pseudo property is set to make methods compatible with properties of an object literal, however it cannot be changed:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>enumerable</code> </dt>
<dd>
<p>Boolean flag reflecting the property descriptor <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><mi>n</mi><mi>u</mi><mi>m</mi><mi>e</mi><mi>r</mi><mi>a</mi><mi>b</mi><mi>l</mi><mi>e</mi></math>, this is always set to false for methods.</p>
</dd>
</dl>
</div>
</div>
<div class="sect4">
<h5 id="_semantics-4"><a class="anchor" href="#_semantics-4"></a><a class="link" href="#_semantics-4">5.2.3.3. Semantics</a></h5>
<div class="paragraph">
<p>Since methods are ECMAScript functions, all constraints specified in <a href="functions.html#_function-type">Function Type</a> apply to methods as well.
This section describes default values and function type conformance which is required for overriding and implementing methods.</p>
</div>
<div class="paragraph">
<p>In addition, method declarations and definitions have to comply with the constraints for naming members of classifiers (cf. <a href="#Req-IDE-52">[Req-IDE-52]</a>)
and with the constraints detailed in the following sections on final methods (<a href="#_final-methods">Final Methods</a>), abstract methods (<a href="#_abstract-methods">Abstract Methods</a> and
method overriding and implementation (<a href="#_overriding-of-members">Overriding of Members</a>, <a href="#_implementation-of-members">Implementation of Members</a>).</p>
</div>
<div class="paragraph">
<p>The following constraints are defined for methods in ECMAScript 6 [<a href="appendix_c_bibliography.html#ECMA15a">ECMA15a(p.207)</a>]</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-53"></a><strong>Req. IDE-53:</strong> <a href="#Req-IDE-53">Method Definition ECMAScript 6</a> (ver. 1)</p>
</div>
<div class="ulist">
<ul>
<li>
<p>It is a Syntax Error if any element of the BoundNames of StrictFormalParameters also occurs in the VarDeclaredNames of FunctionBody.</p>
</li>
<li>
<p>It is a Syntax Error if any element of the BoundNames of StrictFormalParameters also occurs in the LexicallyDeclaredNames of FunctionBody.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="paragraph">
<p>Methods – like functions – define a variable execution environment and therefore provide access to the actual passed-in parameters through the implicit <code>arguments</code> variable inside of their bodies (c.f. <a href="functions.html#_arguments-object">Arguments Object</a>).</p>
</div>
<div class="paragraph">
<p>Methods are similar to function definitions but they must not be assigned to or from variables.
The following code issues an error although the type of the method would be compatible to the type of the variable <code>v</code>:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {
m(): void {}
}
var v: {function():void} = new C().m;</code></pre>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-54"></a><strong>Req. IDE-54:</strong> <a href="#Req-IDE-54">Method Assignment</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>In contrast to ECMAScript 2015, methods are defined as readonly, that is, it is not possible to dynamically re-assign a property defined as method with a new value.
This is because assigning or re-assigning a method breaks encapsulation. Methods are the <a href="appendix_a_acronyms.html#_acronyms">Acronyms</a> of a class, their implementation is internal to the class.</p>
</li>
<li>
<p>When assigning a method to a variable, a warning is issued since this would lead to an detached this reference inside the method when it is called without explicitly providing the receiver. No warning is issued only if it is guaranteed that no problems will occur:</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>The method’s body can be determined at compile time (i.e., it has been declared <code>@Final</code>) and it lacks usages of <code>this</code> or <code>super</code>. This is true for instance and static methods.</p>
</li>
<li>
<p>The method is the constructor.</p>
</li>
</ol>
</div>
</li>
</ol>
</div>
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
The following code demonstrates problems arising when methods are assigned to variables in terms of function expressions.
Given are two classes and instances of each class as follows:
</td>
</tr>
</table>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {
m(): void { }
static k(): void {}
}
class D extends C {
@Override m(): void { this.f()}
f(): void {}
@Override static k(): void { this.f()}
static f(): void {}
}
var c: C = new C();
var d: C = new D(); // d looks like a C</code></pre>
</div>
</div>
<div class="paragraph">
<p>Assigning an instance method to a variable could cause problems, as the method assumes this to be bound to the class in which it is defined.
This may work in some cases, but will cause problems in particular in combination with method overriding:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var v1: {@This(C)function():void} = c.m;
var v2: {@This(C)function():void} = d.m;
v1.call(c);
v2.call(c);</code></pre>
</div>
</div>
<div class="paragraph">
<p>Calling <code>c.m</code> indirectly via <code>v1</code> with <code>c</code> as this object will work.
However, it won’t work for <code>v2</code>: the method is overridden in <code>D</code>, and the method in expects other methods available in <code>D</code> but not in <code>C</code>.
That is, the last call would lead to a runtime error as method <code>f</code> which is called in <code>D.m</code> won’t be available.</p>
</div>
<div class="paragraph">
<p>The same scenario occurs in case of static methods if they are retrieved polymorphically via the variables of type <code>constructor{C}</code>:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var ctor: constructor{C} = C;
var dtor: constructor{C} = D;
var v3: {@This(constructor{C})function():void} = ctor.k;
var v4: {@This(constructor{C})function():void} = dtor.k;</code></pre>
</div>
</div>
<div class="paragraph">
<p>In both cases, the problem could be solved by restricting these kinds of assignments to final methods only.
In the static case, the problem would also be solved by accessing the static method directly via the class type (and not polymorphically via the constructor).
Both restrictions are severe but would be necessary to avoid unexpected runtime problems.</p>
</div>
<div class="paragraph">
<p>The following example shows a problem with breaking the encapsulation of a class.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {
x: any = "";
f(): void { this.g(this); }
g(c: C): void { c.h(); }
h(): void {}
}
class D extends C {
@Override f(): void {
this.g(this.x);
}
@Override g(c: any) {
// do nothing, do not call h())
}
}
var c = new C();
var d = new D();
var v5: {@This(C)function():void} = c.f;
var v6: {@This(C)function():void} = d.f;
v5.call(c)
v6.call(c)</code></pre>
</div>
</div>
<div class="paragraph">
<p>In <code>D</code>, method <code>g</code> is overridden to accept more types as the original method defined in <code>C</code>.
Calling this new method with receiver type <code>C</code> (as done in the last line) will cause problems, as in <code>D</code> not only <code>f</code> has been adapted but also <code>g</code>.
Eventually, this would lead to a runtime error as well.</p>
</div>
</div>
<div class="sect4">
<h5 id="_final-methods"><a class="anchor" href="#_final-methods"></a><a class="link" href="#_final-methods">5.2.3.4. Final Methods</a></h5>
<div class="paragraph">
<p>By default, methods can be overridden.
To prevent a method from being overridden, it must be annotated with <code>@Final</code>.</p>
</div>
<div class="paragraph">
<p>Of course, a method cannot be declared both abstract and final (cf. <a href="#Req-IDE-46">[Req-IDE-46]</a>).
Private methods are implicitly declared final.
Because static methods can be overridden in subclasses (which is different to Java), they also can be marked as final.</p>
</div>
<div class="paragraph">
<p>Default methods in interfaces, cf. <a href="#_default-methods-in-interfaces">Default Methods in Interfaces</a>, may also be declared <code>@Final</code>.</p>
</div>
<div class="exampleblock">
<div class="title">Example 36. Final Methods in Interfaces</div>
<div class="content">
<div class="paragraph">
<p>If a method in an interface is provided with a body, it may be declared final.
This will ensure that the given method’s body will be in effect for all instances of the interface.
Note that this means that;</p>
</div>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>a class implementing that interface must not define a method with the same name and</p>
</li>
<li>
<p>a class inheriting a method of that name cannot implement this interface.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>The latter case is illustrated here:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">interface I {
@Final m(): void {}
}
class C1 {
m(): void {}
}
// error at "I": "The method C1.m cannot override final method I.m."
class C2 extends C1 implements I {
}</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_abstract-methods"><a class="anchor" href="#_abstract-methods"></a><a class="link" href="#_abstract-methods">5.2.3.5. Abstract Methods</a></h5>
<div class="paragraph">
<p>A method can be declared without defining it, i.e. without providing a method body, and is then called an <em>abstract method</em>.
Such methods must be declared with modifier <code>abstract</code> and have their property <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi></math> set to true.
Constraints for abstract methods are covered in <a href="#Req-IDE-46">[Req-IDE-46]</a> (see <a href="#_abstract-classes">Abstract Classes</a>).</p>
</div>
<div class="paragraph">
<p>In interfaces, methods are always abstract by default and they do not have to be marked as abstract.
If a method in an interface provides a body, then this is the default implementation.
See <a href="#_implementation-of-members">Implementation of Members</a> about how the default implementation may be mixed in the consumer.</p>
</div>
</div>
<div class="sect4 language-n4js">
<h5 id="_generic-methods"><a class="anchor" href="#_generic-methods"></a><a class="link" href="#_generic-methods">5.2.3.6. Generic Methods</a></h5>
<div class="paragraph">
<p>Methods of generic classes can, of course, refer to the type variables defined by type parameters of the generic class.
These type variables are used similarly to predefined or declared types.
Additionally, methods may be declared generic independently from their containing class.
That is to say that type parameters (with type variables) can be defined for methods as well, just like for generic functions (see <a href="functions.html#_generic-functions">Generic Functions</a>).</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-55"></a><strong>Req. IDE-55:</strong> <a href="#Req-IDE-55">Type variable names for generic methods</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a given generic method <code>M</code> of a class <code>C</code>, the following
constraint must hold:<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>∀</mo><mi> </mi><mi>t</mi><msub><mi>p</mi><mi>m</mi></msub><mo>∈</mo><mi>m</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>P</mi><mi>a</mi><mi>r</mi><mi>s</mi><mo>,</mo><mi>t</mi><msub><mi>p</mi><mi>C</mi></msub><mo>∈</mo><mi>C</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>P</mi><mi>a</mi><mi>r</mi><mi>s</mi><mi>:</mi><mi>t</mi><msub><mi>p</mi><mi>m</mi></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>≠</mo><mi>t</mi><msub><mi>p</mi><mi>C</mi></msub><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math></p>
</div>
</div>
</div>
<div class="paragraph">
<p>Since type variables can be used similarly to types in the scope of a generic class, a generic method may refer to a type variable of its containing class.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {
&lt;T&gt; foo(p: T p): T { return p;}
};</code></pre>
</div>
</div>
<div class="paragraph">
<p>If a generic type parameter is not used as a formal parameter type or the return type, a warning is generated unless the method overrides a member inherited from a super class or interface.</p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_default-methods-in-interfaces"><a class="anchor" href="#_default-methods-in-interfaces"></a><a class="link" href="#_default-methods-in-interfaces">5.2.4. Default Methods in Interfaces</a></h4>
<div class="paragraph">
<p>If a method declared in an interface defines a body, then this is the so-called <em>default implementation</em> and the method is called a <em>default method</em>.
This will be mixed into an implementor of the interface if, and only if, neither the implementing class nor any of its direct or indirect superclasses already provides an implementation for this method;
for details see <a href="#_member-consumption">Member Consumption</a>.
Since the implementor is not known, some constraints exist for the body. I.e., no access to super is possible, cf. <a href="expressions.html#Req-IDE-124">[Req-IDE-124]</a>.</p>
</div>
<div class="paragraph">
<p>In order to declare an interface to provide a default implementation in a definition file, annotation <code>@ProvidesDefaultImplementation</code> can be used, cf. <a href="plainjs.html#Req-IDE-167">[Req-IDE-167]</a>.</p>
</div>
<div class="paragraph">
<p>When a method in an interface is provided with a default implementation, it may even be declared <code>@Final</code>, see <a href="#_final-methods">Final Methods</a>.</p>
</div>
<div class="sect4">
<h5 id="_asynchronous-methods"><a class="anchor" href="#_asynchronous-methods"></a><a class="link" href="#_asynchronous-methods">5.2.4.1. Asynchronous Methods</a></h5>
<div class="paragraph">
<p>N4JS implements the async/await concept proposed for ECMAScript 7, which provides a more convenient and readable syntax for writing asynchronous code compared to using built-in type Promise directly.
This concept can be applied to methods in exactly the same way as to declared functions.
See <a href="functions.html#_asynchronous-functions">Asynchronous Functions</a> and <a href="functions.html#_asynchronous-arrow-functions">Asynchronous Arrow Functions</a> for details.</p>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_constructors"><a class="anchor" href="#_constructors"></a><a class="link" href="#_constructors">5.2.5. Constructors</a></h4>
<div class="paragraph">
<p>A constructor is a special function defined on a class which returns an instance of that class.
The constructor looks like a normal method with name "constructor".
The constructor can be defined explicitly or implicitly and every class has an (implicit) constructor.</p>
</div>
<div class="paragraph">
<p>For a given a class <code>C</code>, the constructor is available via two properties:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>C</mi><mi>t</mi><mi>o</mi><mi>r</mi></math></dt>
<dd>
<p>the explicitly defined constructor (if any).</p>
</dd>
<dt class="hdlist1"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math></dt>
<dd>
<p>the explicit or implicit constructor.</p>
</dd>
</dl>
</div>
<div class="paragraph">
<p>If <code>C</code> is provided with an explicit constructor, we have <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi><mo>.</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mo>=</mo><mi>C</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>C</mi><mi>t</mi><mi>o</mi><mi>r</mi></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>C</mi><mi>t</mi><mi>o</mi><mi>r</mi><mo>∈</mo><mi>C</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi></math>.
Note that <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi><mo>.</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mo>∉</mo><mi>C</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><mi>s</mi></math> in all cases.</p>
</div>
<div class="paragraph">
<p>The return type of the constructor of a class <code>C</code> is <code>C</code>.
If <code>C</code> has type parameters <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>T</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub></math>, then the return type is <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi><mo>&lt;</mo><msub><mi>T</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>T</mi><mi>n</mi></msub><mo>&gt;</mo></math>. The constructor is called with the operator.
Since the return type of a constructor is implicitly defined by the class, it is to be omitted.
By this definition, a constructor looks like the following:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {
public constructor(s: string) {
// init something
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Constructors define a variable execution environment and therefore provide access to the actual passed-in parameters through the implicit variable inside of their bodies (c.f. <a href="functions.html#_arguments-object">Arguments Object</a>).</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-56"></a><strong>Req. IDE-56:</strong> <a href="#Req-IDE-56">Defining and Calling Constructors</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a constructor <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math> of a class <code>C</code>, the following conditions
must hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math> must neither be abstract nor static nor final and it must not be annotated with <code>@Override</code>.</p>
</li>
<li>
<p>If a class does not explicitly define a constructor then the constructor’s signature of the superclass constructor is assumed.</p>
</li>
<li>
<p>If a class defines a constructor with formal parameters then this constructor has to be called explicitly in constructors defined in subclasses.</p>
</li>
<li>
<p>If a super constructor is called explicitly, this call must be the only expression of an expression statement which has to be the first statement of the body.</p>
</li>
<li>
<p>Constructors may appear in interfaces, but some restrictions apply:</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>constructors in interfaces must not have a body.</p>
</li>
<li>
<p>constructors in interfaces or their containing interface or one of its direct or indirect super interfaces must be annotated with <code>@CovariantConstructor</code>.</p>
</li>
</ol>
</div>
</li>
<li>
<p>A constructor must not have an explicit return type declaration.</p>
</li>
<li>
<p>The implicit return type of a constructor is <code>this?</code>.</p>
</li>
<li>
<p>A constructor must not have any type parameters.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>Properties of object literals may be called <code>constructor</code>.
However they are not recognized as constructors in these cases.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-57"></a><strong>Req. IDE-57:</strong> <a href="#Req-IDE-57">Initialization of Final Fields in the Constructor</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Required attributes must be initialized:<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>∀</mo><mi>a</mi><mo>∈</mo><mi>C</mi><mo>.</mo><mi>a</mi><mi>t</mi><mi>t</mi><mi>r</mi><mi>:</mi><mi>a</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>q</mi><mi>u</mi><mi>i</mi><mi>r</mi><mi>e</mi><mi>d</mi><mo>⇒</mo><mo>∃</mo><mi>e</mi><mo>∈</mo><mi>r</mi><mo>.</mo><mi>e</mi><mi>l</mi><mi>e</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi>s</mi><mi>:</mi><mi>a</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>e</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math></p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>Note on syntax: ECMAScript 6 defines constructors similarly, [<a href="appendix_c_bibliography.html#ECMA15a">ECMA15a(p.S13.5)</a>]. In
ECMAScript 6 the super constructor is not called automatically as well.</p>
</div>
<div class="paragraph">
<p>The super literal used in order to call super methods is further
described in <a href="expressions.html#_the-super-keyword">The super Keyword</a>.</p>
</div>
<div class="sect4">
<h5 id="_structural-this-type-in-constructor"><a class="anchor" href="#_structural-this-type-in-constructor"></a><a class="link" href="#_structural-this-type-in-constructor">5.2.5.1. Structural This Type in Constructor</a></h5>
<div class="paragraph">
<p>The use of a structural this reference as a formal parameter type is possible only in constructors.
This parameter can be annotated with <code>@Spec</code> which causes the compiler to generate initialization code.</p>
</div>
<div class="paragraph">
<p>Simply using <code>this</code> as a type in the constructor causes the constructor to require an object providing all public fields of the class for initialization purposes.
The fields have to be set manually as shown in the following code snippet.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class A{
public s: string;
public constructor(src: ~~this) {
this.s = src.s;
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Remarks:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The type of the formal parameter <code>~~this</code> refers to the structural field type, see <a href="#_structural-typing">Structural Typing</a> for details on structural typing.
It contains all public fields of the type.</p>
</li>
<li>
<p>Subclasses may override the constructor and introduce additional parameters.
They have to call the super constructor explicitly, however, providing a parameter with at least all required attributes of the superclass.
Usually the type <code>this</code> is replaced with the actual subclass, but in the case of a <code>super()</code> call the <code>this</code> type of structural formal parameters is replaced with the <code>this</code> type of the superclass,
hence only required fields of the superclass must be present.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>As with other structural references, it is possible to add the structural reference with additional structural members, which can be used to initialize private fields which
become not automatically part of the structural field type. For example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class A{
public s: string;
private myPrivateNumber: number;
public constructor(src: ~~this with { x: number; }) {
this.s = src.s;
this.myPrivateNumber = src.x;
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Defining additional members may become a problem if a subclass defines public fields with the same name, as the <code>~~this</code> type will contain these fields in the subclass.
This is marked as an error in the subclass.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-58"></a><strong>Req. IDE-58:</strong> <a href="#Req-IDE-58">Names of additional members of structural this type in constructor</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>If the structural this type is used in a constructor of a class <code>C</code>, and if this structural reference contains an additional structural member <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mi>M</mi></math>, the following constraints must hold true:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>For any subclass <code>S</code> of <code>C</code>, with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mo>.</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mo>=</mo><mi>C</mi><mo>.</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math> (the subclass does not define its own constructor), <code>S</code> must not contain a public member with same name as <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mi>M</mi></math>:</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mo>&lt;</mo><mi>:</mi><mi>C</mi><mo>,</mo><mi>S</mi><mo>.</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mo>=</mo><mi>C</mi><mo>.</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0em"/><mo>⇒</mo><mo>∄</mo><mi>M</mi><mo>∈</mo><mi>S</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi></math>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="5.0em"/><mi>M</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo>∧</mo><mi>M</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>S</mi><mi>M</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math></p>
</div>
</li>
<li>
<p><code>C</code> itself must not contain a public member with same name as <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mi>M</mi></math>:</p>
<div class="openblock">
<div class="content">
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mo>∄</mo><mi>M</mi><mo>∈</mo><mi>C</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi><mi>M</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo>∧</mo><mi>M</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>S</mi><mi>M</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math>
</div>
</div>
</li>
</ol>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 37. Field name conflicts with structural member name</div>
<div class="content">
<div class="paragraph">
<p>The situation described in <a href="#Req-IDE-58">[Req-IDE-58]</a> is demonstrated in the following code fragment:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class A {
private myPrivateNumber: number;
public constructor(src: ~~this with { x: number; }) {
this.myPrivateNumber = src.x;
}
}
class B extends A {
public x: number; // will cause an error message
}</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="spec-constructor"><a class="anchor" href="#spec-constructor"></a><a class="link" href="#spec-constructor">5.2.5.2. @Spec Constructor</a></h5>
<div class="paragraph">
<p>The tedious process of copying the members of the parameter to the fields of the class can be automated via the <code>@Spec</code>
annotation if the argument has <code>~i~this</code> structural initializer field typing. More details about this typing can be
found in <a href="#structural-readWriteInit-field-typing">Structural Read-only, Write-only and Initializer Field Typing</a>. This can be used as shown in the following listing:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class A {
public field: string;
public constructor(@Spec spec: ~i~this) {}
}
let a = new A({field: 'hello'});
console.log(a.field); // prints: hello</code></pre>
</div>
</div>
<div class="paragraph">
<p>The code for initializing the public field of <code>A</code> is automatically generated, thanks to the <code>@Spec</code> annotation being
given in the constructor.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-59"></a><strong>Req. IDE-59:</strong> <a href="#Req-IDE-59">@Spec Constructor</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Annotation <code>@Spec</code> may only appear on a formal parameter of a constructor. Such a formal parameter is then called
<em>@Spec parameter</em> or simply <em>spec parameter</em> and its owning constructor is referred to as a <em>@Spec constructor</em> or
<em>spec constructor</em>. An argument to the spec parameter is called <em>spec object</em>.</p>
</li>
<li>
<p>Only a single formal parameter of a constructor may be annotated with <code>@Spec</code>.</p>
</li>
<li>
<p>If a formal parameter is annotated with <code>@Spec</code>, the parameter’s type must be <code>~i~this</code> (i.e. a use-site
structural initializer field type of <code>this</code>, see <a href="#structural-readWriteInit-field-typing">Structural Read-only, Write-only and Initializer Field Typing</a>).</p>
</li>
<li>
<p>Using the data provided in the spec object, i.e. in the argument to the spec parameter, a spec constructor will
automatically initialize</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>all <strong>owned</strong> data fields and <strong>owned</strong> setters of the containing class, and</p>
</li>
<li>
<p>all data fields and setters from interfaces implemented by the containing class</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>if and only if those members are also part of the spec parameter&#8217;s structural initializer field type.</p>
</div>
</li>
<li>
<p>Fields explicitly added to the spec parameter, e.g. <code>@Spec spec: ~i~this with {name:string}</code>, are used for initialization
if a non-public field of the same name exists in the class, either as an owned member or from an implemented interface.
The type of such an additional field must be a subtype of the declared type of the field being initialized:</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mo>∀</mo><mi>s</mi><mo>∈</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mo>.</mo><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>,</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mo>.</mo><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><mo>.</mo><mi>s</mi><mi>p</mi><mi>e</mi><mi>c</mi><mi>:</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="2.0em"/><mo>∃</mo><mi>f</mi><mo>∈</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>F</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mi>s</mi><mo>⇒</mo><mi>Γ</mi><mo>⊢</mo><mi>s</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>f</mi></math></p>
</div>
</li>
<li>
<p>Even if the <code>@Spec</code> annotation is used, the super constructor must be invoked explicitly (as usual).</p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>It follows from no. 4 above that</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>non-public data fields and setters are never initialized (because they will never be part of the spec parameter&#8217;s
structural initializer field type),</p>
</li>
<li>
<p>properties provided in the spec object but not defined in the parameter&#8217;s structural initializer field type, are
<em>not</em> used for initialization, even if a (protected or private) field of the same name exists in the class,</p>
</li>
<li>
<p>data fields and setters inherited from a super class are never initialized by a spec constructor (instead, this will
happen in the spec constructor of the super class).</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>The last of these implications will be detailed further at the end of the coming section.</p>
</div>
<div class="paragraph">
<p><strong>@Spec Constructors and Inheritance</strong></p>
</div>
<div class="paragraph">
<p>Spec constructors are inherited by subclasses that do not have a constructor and, when creating instances of the
subclass, will then require properties for writable public fields of the subclass in the spec object <strong>and</strong> include
initialization code for them.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class A {
public fa;
public constructor(@Spec spec: ~i~this) {}
}
class B extends A {
public fb;
}
const b = new B({fa: 'hello', fb: 'world'}); // requires &amp; initializes fb too!
console.log(b.fa); // prints: hello
console.log(b.fb); // prints: world</code></pre>
</div>
</div>
<div class="paragraph">
<p>Public writable fields from implemented interfaces are included as well, i.e. required as property in spec object <strong>and</strong>
initialized by auto-generated code in the <code>@Spec</code> constructor:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">interface I {
public fi;
}
class B implements I {
public fb;
public constructor(@Spec spec: ~i~this) {}
}
const a = new B({fb: 'hello', fi: 'world'}); // requires &amp; initializes fi too!
console.log(a.fb); // prints: hello
console.log(a.fi); // prints: world</code></pre>
</div>
</div>
<div class="paragraph">
<p>When having a spec constructor in a class <code>B</code> that extends a super class <code>A</code> without an owned or inherited spec
constructor, it should be noted that the <code>~i~this</code> type will require properties for public writable fields of <code>A</code>,
but the initialization code automatically generated due to the <code>@Spec</code> annotation will <strong>not</strong> initialize those members.
For public writable fields from an interface <code>I</code> implemented by <code>B</code>, however, both a property will be required by
<code>~i~this</code> <strong>and</strong> initialization code will be generated in the <code>@Spec</code> constructor. This is illustrated in the
following code example.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class A {
public fa;
}
interface I {
public fi;
}
class B extends A implements I {
public fb;
public constructor(@Spec spec: ~i~this) { // &lt;- fa, fi, fb required in spec object
// Constructor is responsible for initializing fa, fi, fb.
// The @Spec annotation will generate initialization code
// for fb and fi, but not for fa!
}
}
let b = new B({
fa: 'hello', // &lt;- fa is required (removing it would be a compile error)
fi: 'world',
fb: '!!'
});
console.log(b.fa); // undefined
console.log(b.fi); // world
console.log(b.fb); // !!</code></pre>
</div>
</div>
<div class="paragraph">
<p>The rationale for this different handling of fields from super classes and implemented interfaces is
1. fields from an implemented interface are not seen as inherited but rather implemented by implementing class, so from
the <code>@Spec</code> annotation&#8217;s perspective the field is a field of the implementing class, and
2. in case of a field inherited from a super class the correct way of initialization may depend on details of the super
class and has to be taken care of by custom code in the constructor of the subclass (usually by invoking the non-<code>@Spec</code>
constructor of the superclass with <code>super</code>).</p>
</div>
<div class="paragraph">
<p><strong>Special Use Cases</strong></p>
</div>
<div class="paragraph">
<p>The following examples illustrate further details of other use cases of spec constructors.</p>
</div>
<div class="exampleblock">
<div class="title">Example 38. Anonymous Interface in Constructor</div>
<div class="content">
<div class="paragraph">
<p>The base class <code>A</code> in the examples redefines the constructor already defined in <code>N4Object</code>. This is not
generally necessary and is only used here to make the example legible.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class A {
public s: string;
public constructor(@Spec spec: ~i~this) {
// initialization of s is automatically generated
}
}
class B extends A {
public t: string;
private n: number;
public constructor(spec: ~~this with {n: number;}) {
super(spec); // only inherited field s is set in super constructor
}
}</code></pre>
</div>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 39. Spec Object and Subclasses</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class A1 {
public s: string;
public n: number;
public constructor(@Spec spec: ~i~this) {}
}
class B extends A1 {
public constructor() {
super({s:"Hello"}); // &lt;-- error, n must be set in object literal
}
}
class C extends A1 {
public constructor() {
super({s:"Hello"}); // &lt;-- error, n must be set in object literal
this.n = 10; // &lt;-- this has no effect on the super constructor!
}
}
class A2 {
public s: string;
public n: number?; // now n is optional!
public constructor(@Spec spec: ~i~this) {}
}
class D extends A2 {
public constructor() {
super({s:"Hello"}); // and this is ok now!
this.n = 10; // this explains why it is optional
}
}
class A3 {
public s: string;
public n: number = 10; // now n is not required in ~~this
public constructor(@Spec spec: ~i~this) {}
}
class E extends A3 {
public constructor() {
super({s:"Hello"}); // and this is ok now!
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The last case (class E) demonstrates a special feature of the typing strategy modifier in combination with the <code>this</code> type, see <a href="#_structural-typing">Structural Typing</a> for details.</p>
</div>
<div class="paragraph">
<p>The constructor in class <code>B</code> contains an error because the super constructor expects all required attributes in <code>A1</code> to be set.
The additional initialization of the required field <code>A1.n</code> as seen in <code>C</code> does not change that expectation.
In this example, the field <code>n</code> should not have been defined as required in the first place.</p>
</div>
<div class="paragraph">
<p>Optional fields like <code>n?</code> in class <code>A2</code> or fields with default values like <code>n=10</code> in class <code>A3</code> are not required to be part of the <code>spec</code> object.</p>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 40. Superfluous Properties in @Spec Constructors</div>
<div class="content">
<div class="paragraph">
<p>Each non-<math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle></math> field has to be set in the constructor via the <math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>with</mtext></mstyle></math> to the parameter otherwise properties are <em>not</em> used to set non-<math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle></math> fields.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {
public s: string;
n: number;
constructor(@Spec spec: ~i~this) {}
}
// n is ignored here
new C( { s: "Hello", n: 42 });
// but:
var ol = { s: "Hello", n: 42 };
// "ol may be used elsewhere, we cannot issue warning here" at "ol"
new C(ol) ;
// of course this is true for all superfluous properties
// weird is not used in constructor
new C( { s: "Hello", weird: true } );</code></pre>
</div>
</div>
</div>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1">Restriction when initializing interface fields via @Spec constructor <a id="restriction-interface-field-spec-constructor"></a> </dt>
<dd>
<p>In most cases, interface definitions in <code>n4jsd</code> files simply declare functions and fields that are supposed to be provided by the runtime environment.
As a result, there are restrictions as to whether fields of interfaces defined in <code>n4jsd</code> files can initialized via <code>@Spec</code> constructors or not.
In particular, fields of an interface declared in a <code>n4jsd</code> file cannot be initialized via @Spec constructor if the interface</p>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>is a built-in or</p>
</li>
<li>
<p>does not have an <code>@N4JS</code> annotation</p>
</li>
</ol>
</div>
</dd>
</dl>
</div>
<div class="paragraph">
<p>The following example illustrates this restriction.</p>
</div>
<div class="exampleblock">
<div class="title">Example 41. Interface fields that cannot be initialized via @Spec constructors</div>
<div class="content">
<div class="listingblock">
<div class="title">Inf.n4jsd</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">export external interface I {
public m: string;
}
@N4JS
export external interface J {
public n: string;
}</code></pre>
</div>
</div>
<div class="listingblock">
<div class="title">Test.n4js</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">import { I } from "Inf";
// I is an external interface WITHOUT @N4JS annotation
class C implements I {
constructor(@Spec spec:~i~this) {}
}
// J is an external interface with @N4JS annotation
class D implements J {
constructor(@Spec spec:~i~this) {}
}
// XPECT warnings --&gt; "m is a property of built-in / provided by runtime / external without @N4JS annotation interface I and can not be initialized in Spec constructor." at "m"
let c:C = new C({m: "Hello"});
// XPECT nowarnings
let d:D = new D({n: "Bye"});
console.log(c.m)
console.log(d.n)
/* XPECT output ---
&lt;==
stdout:
undefined
Bye
stderr:
==&gt;
--- */</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>In this example, the interface <code>I</code> is defined in the <code>Inf.n4jsd</code> file without the <code>@N4JS</code> annotation. As a result, its field <code>m</code> cannot be initialized via the <code>@Spec</code> constructor and hence the output of <code>console.log(c.m)</code> is <code>undefined</code>. On the other hand, since the interface <code>J</code> is declared with the annotation <code>@N4JS</code>, it is possible to initialize its field <code>n</code> in the <code>@Spec</code> constructor. That&#8217;s why the result of <code>console.log(d.n)</code> is <code>Bye</code>.</p>
</div>
</div>
<div class="sect4">
<h5 id="_callable-constructors"><a class="anchor" href="#_callable-constructors"></a><a class="link" href="#_callable-constructors">5.2.5.3. Callable Constructors</a></h5>
</div>
<div class="sect4">
<h5 id="_covariant-constructors"><a class="anchor" href="#_covariant-constructors"></a><a class="link" href="#_covariant-constructors">5.2.5.4. Covariant Constructors</a></h5>
<div class="paragraph">
<p>Usually, the constructor of a subclass need not be override compatible with the constructor of its super class.
By way of annotation <code>@CovariantConstructor</code> it is possible to change this default behavior and enforce all subclasses to have constructors with override compatible signatures.
A subclass can achieve this by either inheriting the constructor from the super class (which is usually override compatible,
with the special case of <code>@Spec</code> constructors) or by defining a new constructor with a signature compatible to the inherited constructor.
The same rules as for method overriding apply.</p>
</div>
<div class="paragraph">
<p>The <code>@CovariantConstructor</code> annotation may be applied to the constructor, the containing classifier, or both.
It can also be used for interfaces; in fact, constructors are allowed in interfaces only if they themselves or the interface is annotated with <code>@CovariantConstructor</code> (see <a href="#Req-IDE-60">[Req-IDE-60]</a>).</p>
</div>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="covariant_constructor"></a><strong>Definition:</strong> <a href="#covariant_constructor">Covariant Constructor</a></p>
</div>
<div class="paragraph">
<p>A classifier <code>C</code> is said to <code><em>have a covariant constructor</em></code> if and
only if one of the following applies:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><code>C</code> has a direct super class <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>C</mi><mi>'</mi></msup></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>C</mi><mi>'</mi></msup></math> is annotated with <code>@CovariantConstructor</code> or <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>C</mi><mi>'</mi></msup></math> has a constructor annotated with <code>@CovariantConstructor</code>.</p>
</li>
<li>
<p><code>C</code> has a directly implemented interface <code>I and `I</code> is annotated with <code>@CovariantConstructor</code> or <code>I</code> has a constructor annotated with <code>@CovariantConstructor</code>.</p>
</li>
<li>
<p><code>C</code> has a direct super class or directly implemented interface that <code><em>has a covariant constructor</em></code> (as defined here).</p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>Note that <code>C</code> does not need to have an owned(!) constructor; also a constructor inherited from a super class can be declared covariant.</p>
</div>
<div class="paragraph">
<p>The following rules apply to covariant constructors.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-60"></a><strong>Req. IDE-60:</strong> <a href="#Req-IDE-60">Covariant Constructors</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Annotation <code>@CovariantConstructor</code> may only be applied to classes, interfaces, and constructors.
Annotating a constructor with this annotation, or its containing classifier, or both have all the same effect.</p>
</li>
<li>
<p>Given a class <code>C</code> with an owned constructor <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math> and a super class <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mi>u</mi><mi>p</mi></math> that has a covariant constructor (owned or inherited, see <a href="#_covariant-constructors">Covariant Constructors</a>),
then <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mi>u</mi><mi>p</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math> must be accessible from <code>C</code>,</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math> must be override compatible with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math>:</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi><mi>C</mi><mi>o</mi><mi>m</mi><mi>p</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>b</mi><mi>l</mi><mi>e</mi><mfenced close=")" open="("><mrow><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></mrow><mrow><mi>S</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></mrow></mfenced></math></p>
</div>
<div class="paragraph">
<p>This constraint corresponds to <a href="#Req-IDE-72">[Req-IDE-72]</a> except for the <code>Override</code> annotation which is not required here.</p>
</div>
</li>
</ol>
</div>
</li>
<li>
<p>Given a classifier <code>C</code> implementing interface <code>I</code> and <code>I</code> has a covariant constructor (owned or inherited, see <a href="#_covariant-constructors">Covariant Constructors</a>), we require</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>I</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math> must be accessible from <code>C</code>,</p>
</li>
<li>
<p>an implementation-compatible constructor <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math> must be defined in C with</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi><mi>C</mi><mi>o</mi><mi>m</mi><mi>p</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>b</mi><mi>l</mi><mi>e</mi><mfenced close=")" open="("><mrow><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></mrow><mrow><mi>I</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></mrow></mfenced></math></p>
</div>
<div class="paragraph">
<p>This constraint corresponds to <a href="#Req-IDE-74">[Req-IDE-74]</a> except for the <code>@Override</code> annotation, which is not required, here.</p>
</div>
</li>
<li>
<p>Given a classifier <code>C</code> without an owned constructor and an extended class or interface <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mi>u</mi><mi>p</mi></math> that has a covariant constructor (owned or inherited, see <a href="#_covariant-constructors">Covariant Constructors</a>),
we require the inherited constructor <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></math> of <code>C</code> within the context of <code>C</code> to be override compatible to itself in the context of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mi>u</mi><mi>p</mi></math>.
Using notation <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>m</mi><mfenced close="]" open="["><mi>T</mi></mfenced></math> to denote that a member <code>M</code> is to be treated as defined in container type <code>T</code>, which means the this-binding is set to <code>T</code>, we can write:</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi><mi>C</mi><mi>o</mi><mi>m</mi><mi>p</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>b</mi><mi>l</mi><mi>e</mi><mfenced close=")" open="("><mrow><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mfenced close="]" open="["><mi>C</mi></mfenced></mrow><mrow><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mfenced close="]" open="["><mrow><mi>S</mi><mi>u</mi><mi>p</mi></mrow></mfenced></mrow></mfenced></math></p>
</div>
<div class="paragraph">
<p>This constraint does not correspond to any of the constraints for the redefinition of ordinary members.</p>
</div>
</li>
</ol>
</div>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>The following example demonstrates a use case for covariant constructors.
It shows a small class hierarchy using covariant constructors, <code>Cls</code> and <code>Cls2</code>, together with a helper function <code>createAnother</code> that creates and returns a new instance of the same type as its argument <code>value</code>.</p>
</div>
<div id="ex:covariant_constructors" class="exampleblock">
<div class="title">Example 42. Covariant Constructors</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class A {}
class B extends A {}
@CovariantConstructor
class Cls {
constructor(p: B) {}
}
class Cls2 extends Cls {
constructor(p: A) { // it's legal to generalize the type of parameter 'p'
super(null);
}
}
function &lt;T extends Cls&gt; createAnother(value: T, p: B): T {
let ctor = value.constructor;
return new ctor(p);
}
let x = new Cls2(new A());
let y: Cls2;
y = createAnother(x, new B());</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>In the code of <a href="#ex:covariant_constructors">Covariant Constructors</a>, we would get an error if we changed the type of parameter <code>p</code> in the constructor of <code>Cls2</code> to some other type that
is not a super type of <code>B</code>, i.e. the type of the corresponding parameter of <code>Cls</code>’s constructor.
If we removed the <code>@CovariantConstructor</code> annotation on <code>Cls</code>, we would get an error in the new expression inside function <code>createAnother</code>.</p>
</div>
<div class="paragraph">
<p>The next example illustrates how to use <code>@CovariantConstructor</code> with interfaces and shows a behavior that might be surprising at first sight.</p>
</div>
<div id="ex:covariant-constructors-in-interfaces" class="exampleblock">
<div class="title">Example 43. Covariant Constructors in Interfaces</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">@CovariantConstructor
interface I {
constructor(p: number)
}
class C implements I {
// no constructor required!
}
class D extends C {
// XPECT errors --&gt; "Signature of constructor of class D does not conform to overridden constructor of class N4Object: {function(number)} is not a subtype of {function()}." at "constructor"
constructor(p: number) {}
}</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>Interface <code>I</code> declares a covariant constructor expecting a single parameter of type <code>number</code>.
Even though class <code>C</code> implements <code>I</code>, it does not need to define an owned constructor with such a parameter.
According to <a href="#Req-IDE-60">[Req-IDE-60]</a>, it is enough for <code>C</code> to have a constructor, either owned or inherited, that is override compatible with the one declared by <code>I</code>.
Class <code>C</code> inherits the default constructor from <code>N4Object</code>, which does not have any arguments and is thus override compatible to <code>I</code>’s constructor.</p>
</div>
<div class="paragraph">
<p>In addition, subclasses are now required to have constructors which are override compatible with the constructor of class <code>C</code>, i.e. the one inherited from <code>N4Object</code>.
<a href="#ex:covariant-constructors-in-interfaces">Covariant Constructors in Interfaces</a> shows that this is violated even when repeating the exact same constructor signature from interface <code>I</code>,
because that constructor now appears on the other side of the subtype test during checking override compatibility.</p>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_data-fields"><a class="anchor" href="#_data-fields"></a><a class="link" href="#_data-fields">5.2.6. Data Fields</a></h4>
<div class="paragraph">
<p>A data field is a simple property of a class.
There must be no getter or setter defined with the same name as the data field.
In ECMAScript 6, a class has no explicit data fields.
It is possible, however, to implicitly define a data field by simply assigning a value to a variable of the this element (e.g. <code>this.x = 10</code> implicitly defines a field <code>x</code>).
Data fields in N4JS are similar to these implicit fields in ECMAScript 6 except that they are defined explicitly in order to simplify validation and user assistance.</p>
</div>
<div class="sect4">
<h5 id="data-fields-syntax"><a class="anchor" href="#data-fields-syntax"></a><a class="link" href="#data-fields-syntax">5.2.6.1. Syntax</a></h5>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">N4FieldDeclaration &lt;Yield&gt;:
{N4FieldDeclaration}
FieldDeclarationImpl&lt;Yield&gt;
;
fragment FieldDeclarationImpl &lt;Yield&gt;*:
(declaredModifiers+=N4Modifier)* BogusTypeRefFragment?
declaredName=LiteralOrComputedPropertyName&lt;Yield&gt;
(declaredOptional?='?')?
ColonSepTypeRef?
('=' expression=Expression&lt;In=true,Yield&gt;)?
Semi
;</code></pre>
</div>
</div>
</div>
<div class="sect4">
<h5 id="data-fields-properties"><a class="anchor" href="#data-fields-properties"></a><a class="link" href="#data-fields-properties">5.2.6.2. Properties</a></h5>
<div class="paragraph">
<p>Fields have the following properties which can be explicitly defined:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>declaredOptional</code> </dt>
<dd>
<p>Tells whether the accessor was declared optional.</p>
</dd>
<dt class="hdlist1"><code>typeRef</code> </dt>
<dd>
<p>Type of the field; default value is <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>A</mi><mi>n</mi><mi>y</mi></math>.</p>
</dd>
<dt class="hdlist1"><code>expr</code> </dt>
<dd>
<p>Initializer expression, i.e. sets default value.</p>
</dd>
<dt class="hdlist1"><code>static</code> </dt>
<dd>
<p>Boolean flag set to true if field is a static field.</p>
</dd>
<dt class="hdlist1"><code>const</code> </dt>
<dd>
<p>Boolean flag set to true if field cannot be changed. Note that const fields are automatically static.
Const fields need an initializer. Also see <a href="#_assignment-modifiers">Assignment Modifiers</a>.</p>
</dd>
</dl>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi></math> is <em>not</em> the (reversed) value of the property descriptor <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>w</mi><mi>r</mi><mi>i</mi><mi>t</mi><mi>a</mi><mi>b</mi><mi>l</mi><mi>e</mi></math> as the latter is checked at runtime while const may or may not be checked at runtime.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>The following pseudo properties are defined via annotations for setting the values of the property descriptor:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>enumerable</code> </dt>
<dd>
<p>Boolean flag reflecting the property descriptor <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>e</mi><mi>n</mi><mi>u</mi><mi>m</mi><mi>e</mi><mi>r</mi><mi>a</mi><mi>b</mi><mi>l</mi><mi>e</mi></math>, set via annotation <code>@Enumerable(true|false)</code>.
The default value is <math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>true</mtext></mstyle></math>.<sup class="footnote">[<a id="_footnoteref_32" class="footnote" href="appendix_c_bibliography.html#_footnote_32" title="View footnote.">32</a>]</sup></p>
</dd>
<dt class="hdlist1"><code>declaredWriteable</code> </dt>
<dd>
<p>Boolean flag reflecting the property descriptor <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>w</mi><mi>r</mi><mi>i</mi><mi>t</mi><mi>e</mi><mi>a</mi><mi>b</mi><mi>l</mi><mi>e</mi></math>, set via annotation <code>@Writeable(true|false)</code>.
The default value is <math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathvariant="monospace"><mtext>true</mtext></mstyle></math>.<sup class="footnote">[<a id="_footnoteref_33" class="footnote" href="appendix_c_bibliography.html#_footnote_33" title="View footnote.">33</a>]</sup></p>
</dd>
<dt class="hdlist1"><code>final</code> </dt>
<dd>
<p>Boolean flag making the field read-only, and it must be set in the constructor. Also see <a href="#_assignment-modifiers">Assignment Modifiers</a>.</p>
</dd>
</dl>
</div>
<h6 id="data-fields-derived-values" class="discrete">Derived values for fields</h6>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>readable</code> </dt>
<dd>
<p>Always true for fields.</p>
</dd>
<dt class="hdlist1"><code>abstract</code> </dt>
<dd>
<p>Always false for fields.</p>
</dd>
<dt class="hdlist1"><code>writeable</code> </dt>
<dd>
<p>Set to false if field is declared const or final. In the latter case, it may be set in the constructor (cf. <a href="#_assignment-modifiers">Assignment Modifiers</a>).</p>
</dd>
</dl>
</div>
<div class="sect5">
<h6 id="data-fields-semantics"><a class="anchor" href="#data-fields-semantics"></a><a class="link" href="#data-fields-semantics">5.2.6.2.1. Semantics</a></h6>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-61"></a><strong>Req. IDE-61:</strong> <a href="#Req-IDE-61">Attributes</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For any attribute <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi></math> if a
class <code>C</code>, the following constraints must hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>A required data field must not define an initializer:<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>q</mi><mi>u</mi><mi>i</mi><mi>r</mi><mi>e</mi><mi>d</mi><mo>⇒</mo><mi>a</mi><mo>.</mo><mi>i</mi><mi>n</mi><mi>i</mi><mi>t</mi><mo>=</mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi></math></p>
</li>
<li>
<p>There must be no other member with the same name of a data field <code>f</code>.
In particular, there must be no getter or setter defined with the same name:<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo>∀</mo><mi> </mi><mi>m</mi><mo>∈</mo><mi>f</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi><mi>m</mi><mo>≠</mo><mi>f</mi><mo>⇒</mo><mi>m</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>≠</mo><mi>f</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math></p>
</li>
</ol>
</div>
<div class="paragraph">
<p>If a subclass should set a different default value, this has to be done in the constructor of the subclass.</p>
</div>
<div class="paragraph">
<p>For the relation of data fields and field accessors in the context of extending classes or implementing interfaces see <a href="#_redefinition-of-members">Redefinition of Members</a>.</p>
</div>
</div>
</div>
</div>
<div class="sect5">
<h6 id="data-fields-type-inference"><a class="anchor" href="#data-fields-type-inference"></a><a class="link" href="#data-fields-type-inference">5.2.6.2.2. Type Inference</a></h6>
<div class="paragraph">
<p>The type of a field is the type of its declaration:</p>
</div>
<div class="openblock">
<div class="content">
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow/><mrow><mi>Γ</mi><mo>⊢</mo><mi>f</mi><mi>:</mi><mi>Γ</mi><mo>⊢</mo><mi>d</mi></mrow></mfrac></math>
</div>
</div>
<div class="paragraph">
<p>The type of a field declaration is either the declared type or the inferred type of the initializer expression:</p>
</div>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mfrac><mrow><mi>d</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mo>≠</mo><mstyle mathvariant="monospace"><mtext>null</mtext></mstyle><mspace width="3.0mm"/><mi>T</mi><mo>=</mo><mi>d</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi></mrow><mrow><mi>Γ</mi><mo>⊢</mo><mi>d</mi><mi>:</mi><mi>T</mi></mrow></mfrac></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mfrac><mrow><mi>d</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>null</mtext></mstyle><mspace width="3.0mm"/><mi>d</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo>≠</mo><mstyle mathvariant="monospace"><mtext>null</mtext></mstyle></mrow><mrow><mi>Γ</mi><mo>⊢</mo><mi>d</mi><mi>:</mi><mi>T</mi></mrow></mfrac></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mi>E</mi><mo>=</mo><mi>Γ</mi><mo>⊢</mo><mi>d</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mspace width="3.0mm"/><mi>E</mi><mo>∉</mo><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>null, undefined</mtext></mstyle></mfenced><mspace width="3.0mm"/><mi>T</mi><mo>=</mo><mi>E</mi><mi>}</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mfrac><mrow><mi>e</mi><mi>l</mi><mi>s</mi><mi>e</mi></mrow><mrow><mi>Γ</mi><mo>⊢</mo><mi>d</mi><mi>:</mi><mstyle mathvariant="monospace"><mtext>any</mtext></mstyle></mrow></mfrac></math></p>
</div>
<div class="paragraph">
<p>If the type contains type variables they are substituted according to
type parameters which are provided by the reference:</p>
</div>
<div class="openblock">
<div class="content">
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>Γ</mi><mo>⊢</mo><mi>t</mi><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mo>.</mo><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>f</mi><mi>:</mi><mi>T</mi></mrow><mrow><mi>Γ</mi><mo>⊢</mo><mstyle mathvariant="monospace"><mtext>TField</mtext></mstyle><mi> </mi><mi>t</mi><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mi>:</mi><mi>T</mi></mrow></mfrac></math>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_assignment-modifiers"><a class="anchor" href="#_assignment-modifiers"></a><a class="link" href="#_assignment-modifiers">5.2.6.3. Assignment Modifiers</a></h5>
<div class="paragraph">
<p>Assignment of data fields can be modified by the assignment modifiers <code>const</code> (similar to constant variable declarations, see <a href="statements.html#_const">Const</a>) and <code>@Final</code>.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-62"></a><strong>Req. IDE-62:</strong> <a href="#Req-IDE-62">Const Data Fields</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a data field <code>f</code> marked as <code>const</code>, the following constraints must hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>An initializer expression must be provided in the declaration (except in n4jsd files):<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo>≠</mo><mstyle mathvariant="monospace"><mtext>null</mtext></mstyle></math></p>
</li>
<li>
<p>A constant data field is implicitly static and must be accessed only via the classifier type.
It is not possible, therefore, to use the <code>this</code> keyword in the initializer expression of a constant field:<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>∄</mo><mi>s</mi><mi>u</mi><mi>b</mi><mo>∈</mo><mi>f</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><msup><mi>r</mi><mo>*</mo></msup><mi>:</mi><mi>s</mi><mi>u</mi><mi>b</mi><mo>=</mo><mi>"</mi><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi><mi>"</mi></math></p>
</li>
<li>
<p>A constant data field must not be annotated with <code>@Final</code>:<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mo>→</mo><mo>¬</mo><mi>f</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi></math></p>
</li>
<li>
<p>Constant data fields are not writeable (cf. <a href="#Req-IDE-68">[Req-IDE-68]</a>):<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mo>→</mo><mo>¬</mo><mi>f</mi><mo>.</mo><mi>w</mi><mi>r</mi><mi>i</mi><mi>t</mi><mi>e</mi><mi>a</mi><mi>b</mi><mi>l</mi><mi>e</mi></math></p>
</li>
</ol>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-63"></a><strong>Req. IDE-63:</strong> <a href="#Req-IDE-63">Final Data Fields</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a data field <code>f</code> marked as <code>@Final</code>, the following constraints must hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>A final data field must not be modified with <code>const</code> or <code>static</code>:<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi><mo>→</mo><mo>¬</mo><mi>f</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mo>∧</mo><mo>¬</mo><mi>f</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>c</mi><mi>l</mi><mi>a</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>S</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi></math></p>
</li>
<li>
<p>A final data field is not writeable:<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi><mo>→</mo><mo>¬</mo><mi>f</mi><mo>.</mo><mi>w</mi><mi>r</mi><mi>i</mi><mi>t</mi><mi>e</mi><mi>a</mi><mi>b</mi><mi>l</mi><mi>e</mi></math><br>
A final field may, however, be set in the constructor.
See <a href="#Req-IDE-68">[Req-IDE-68]</a> for details.</p>
</li>
<li>
<p>A final data field must be either initialized by an initializer expression or in the constructor.
If the field is initialized in the constructor, this may be done either explicitly or via a spec style constructor.</p>
<div class="openblock">
<div class="content">
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable><mtr><mtd><mspace width="3.0mm"/><mi>f</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo>≠</mo><mstyle mathvariant="monospace"><mtext>null</mtext></mstyle></mtd></mtr><mtr><mtd><mspace width="3.0mm"/><mo>∨</mo><mrow><mo>(</mo><mo>∃</mo><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>E</mi><mi>x</mi><mi>p</mi><mi>:</mi><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>E</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>t</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>i</mi><mi>n</mi><mi>g</mi><mi>F</mi><mi>u</mi><mi>n</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo>=</mo><mi>f</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi></mrow></mtd></mtr><mtr><mtd><mspace width="3.0mm"/><mspace width="3.0em"/><mo>∧</mo><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>E</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo>.</mo><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mo>.</mo><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>e</mi><mi>t</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>"this"</mtext></mstyle></mtd></mtr><mtr><mtd><mrow><mspace width="3.0mm"/><mspace width="3.0em"/><mo>∧</mo><mi>b</mi><mi>i</mi><mi>n</mi><mi>d</mi><mfenced close=")" open="("><mrow><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>E</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo>.</mo><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mo>.</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>y</mi></mrow><mi>f</mi></mfenced><mo>)</mo></mrow></mtd></mtr><mtr><mtd><mspace width="3.0mm"/><mo>∨</mo><mrow><mo>(</mo><mi>f</mi><mo>.</mo><mi>p</mi><mi>u</mi><mi>b</mi><mi>l</mi><mi>i</mi><mi>c</mi><mo>∧</mo><mo>∃</mo><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><mo>∈</mo><mi>f</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mo>.</mo><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><mi>s</mi><mi>:</mi></mrow></mtd></mtr><mtr><mtd><mrow><mspace width="3.0mm"/><mspace width="3.0em"/><mi>f</mi><mi>p</mi><mi>a</mi><mi>r</mi><mo>.</mo><mi>s</mi><mi>p</mi><mi>e</mi><mi>c</mi><mo>∧</mo><mo>∃</mo><mi>s</mi><mi>m</mi><mo>∈</mo><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi><mi>s</mi><mi>m</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>f</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>)</mo></mrow></mtd></mtr></mtable></math>
</div>
</div>
</li>
</ol>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_field-accessors-getter-setter"><a class="anchor" href="#_field-accessors-getter-setter"></a><a class="link" href="#_field-accessors-getter-setter">5.2.6.4. Field Accessors (Getter/Setter)</a></h5>
<div class="paragraph">
<p>Instead of a simple data field, a field can be defined by means of the getter and setter accessor methods.
These accessor methods are similar to the accuser methods in object literals:</p>
</div>
<div class="sect5">
<h6 id="field-acessors-syntax"><a class="anchor" href="#field-acessors-syntax"></a><a class="link" href="#field-acessors-syntax">5.2.6.4.1. Syntax</a></h6>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">N4GetterDeclaration &lt;Yield&gt;:
=&gt; ({N4GetterDeclaration}
(declaredModifiers+=N4Modifier)*
GetterHeader&lt;Yield&gt;)
(body=Block&lt;Yield&gt;)? ';'?
;
fragment GetterHeader &lt;Yield&gt;*:
BogusTypeRefFragment? 'get' -&gt; declaredName=LiteralOrComputedPropertyName&lt;Yield&gt;
(declaredOptional?='?')?
'(' ')'
ColonSepTypeRef?
;
N4SetterDeclaration &lt;Yield&gt;:
=&gt;({N4SetterDeclaration}
(declaredModifiers+=N4Modifier)*
'set'
-&gt;declaredName=LiteralOrComputedPropertyName &lt;Yield&gt;
)
(declaredOptional?='?')?
'(' fpar=FormalParameter&lt;Yield&gt; ')' (body=Block&lt;Yield&gt;)? ';'?
;</code></pre>
</div>
</div>
<div class="paragraph">
<p>Notes with regard to syntax: Although ECMAScript 6 does not define fields in classes, it defines getter and setter methods similarly (cf. [<a href="appendix_c_bibliography.html#ECMA15a">ECMA15a(p.S13.3, p.p.209)</a>]).</p>
</div>
<div class="exampleblock">
<div class="title">Example 44. Getter and Setter</div>
<div class="content">
<div class="paragraph">
<p>The getter and setter implementations usually reference data fields internally.
These are to be declared explicitly (although ECMAScript allows creating fields on the fly on their first usage).
The following example demonstrates a typical usage of getter and setter in combination with a data field.
The getter lazily initializes the field on demand.
The setter performs some notification.</p>
</div>
<div class="listingblock">
<div class="title">Getter Setter</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class A {}
class C {
private _data: A = null;
public get data(): A {
if (this._data==null) {
this._data = new A();
}
return this._data;
}
public set data(data: A) {
this._data = data;
this.notifyListeners();
}
notifyListeners(): void {
// ...
}
}</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect5">
<h6 id="field-acessors-properties"><a class="anchor" href="#field-acessors-properties"></a><a class="link" href="#field-acessors-properties">5.2.6.4.2. Properties</a></h6>
<div class="paragraph">
<p>Properties for field accessors:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1"><code>declaredOptional</code> </dt>
<dd>
<p>Tells whether the accessor was declared optional.</p>
</dd>
<dt class="hdlist1"><code>readable</code> </dt>
<dd>
<p>Derived value: true for getters and false for setters.</p>
</dd>
<dt class="hdlist1"><code>writable</code> </dt>
<dd>
<p>Derived value: false for getters and true for setters.</p>
</dd>
</dl>
</div>
</div>
<div class="sect5">
<h6 id="field-accessors-semantics"><a class="anchor" href="#field-accessors-semantics"></a><a class="link" href="#field-accessors-semantics">5.2.6.4.3. Semantics</a></h6>
<div class="paragraph">
<p>There must be no field or method with the same name as a field accessor (follows from <a href="#Req-IDE-52">[Req-IDE-52]</a>). In addition, the following constraints must hold:</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-64"></a><strong>Req. IDE-64:</strong> <a href="#Req-IDE-64">Field Accessors</a> (ver. 1)</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The return type of a getter must not be <code>void</code>.</p>
</li>
<li>
<p>The type of the parameter of a setter must not be <code>void</code>.</p>
</li>
<li>
<p>If a getter <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>g</mi></math> is defined or consumed (from an interface) or merged-in (via static polyfill) in a class <code>C</code> and a setter <code>S</code> with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>g</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>∧</mo><mi>s</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo>=</mo><mi>g</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi></math> is inherited by
<code>C</code> from one of its super classes, then <code>C</code> must define a setter <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>s</mi><mi>'</mi></msup></math> with
<math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>s</mi><mi>'</mi></msup><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>g</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>∧</mo><msup><mi>s</mi><mi>'</mi></msup><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo>=</mo><mi>g</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi></math> <sup class="footnote">[<a id="_footnoteref_34" class="footnote" href="appendix_c_bibliography.html#_footnote_34" title="View footnote.">34</a>]</sup>.</p>
</li>
<li>
<p>A setter must have exactly one formal parameter, i.e. variadic or default modifiers are not allowed.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The same applies to setters, accordingly.</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#Req-IDE-72">[Req-IDE-72]</a>, <a href="#Req-IDE-73">[Req-IDE-73]</a>, and <a href="#Req-IDE-74">[Req-IDE-74]</a> apply to field accessors accordingly (getter / setter overriding).</p>
</li>
</ul>
</div>
<div class="admonitionblock note language-n4js">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title=""></i>
</td>
<td class="content">
A getter and setter with the same name need not have the same type, i.e. the getter’s return type need not be the same as a subtype of
the type of the setter’s parameter (the types can be completely unrelated).<sup class="footnote">[<a id="_footnoteref_35" class="footnote" href="appendix_c_bibliography.html#_footnote_35" title="View footnote.">35</a>]</sup>
</td>
</tr>
</table>
</div>
</div>
</div>
<div class="paragraph">
<p>Getters and setters – like functions – define a variable execution environment and therefore provide access to the actual passed-in parameters through the implicit <code>arguments</code>
variable inside of their bodies (c.f. <a href="functions.html#_arguments-object">Arguments Object</a>).</p>
</div>
</div>
</div>
<div class="sect4">
<h5 id="optional-fields"><a class="anchor" href="#optional-fields"></a><a class="link" href="#optional-fields">5.2.6.5. Optional Fields</a></h5>
<div class="paragraph">
<p>Data fields and field accessors of a classifier C can be declared optional, meaning that a structural subtype of C
need not provide this field, but if it does, the field must be of correct type. However, to ensure overall type safety,
the scope of application of this optionality is limited to a small number of specific use cases, as described in the
following.</p>
</div>
<div class="sect5">
<h6 id="_syntax-6"><a class="anchor" href="#_syntax-6"></a><a class="link" href="#_syntax-6">5.2.6.5.1. Syntax</a></h6>
<div class="paragraph">
<p>To denote a data field or accessor as optional, a question mark is placed right after the name:</p>
</div>
<div class="listingblock">
<div class="title">Syntax of optional fields</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {
public field?: string;
public get getter?(): number {
return 42;
}
public set setter?(value: number) {}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The detailed grammar is given in the sections for data fields, cf. <a href="#data-fields-syntax">Syntax</a>,
and field accessors, cf. <a href="#field-acessors-syntax">Syntax</a>.</p>
</div>
</div>
<div class="sect5">
<h6 id="_semantics-5"><a class="anchor" href="#_semantics-5"></a><a class="link" href="#_semantics-5">5.2.6.5.2. Semantics</a></h6>
<div class="paragraph">
<p>It is important to note that the optionality of a field is, by default and in most cases, ignored and
has an effect only in certain special cases.</p>
</div>
<div class="paragraph">
<p>The effect of a field being optional is defined by the following requirement.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-240500"></a><strong>Req. IDE-240500:</strong> <a href="#Req-IDE-240500">Optional Fields</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>By default, a data field, getter, or setter that is declared optional is handled in the
exact same way as if no optionality were involved (i.e. by default, optionality is ignored).</p>
</div>
<div class="paragraph">
<p>Optionality has an effect only in case of structural subtype checks <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>L</mi><mo>&lt;</mo><mi>:</mi><mi>R</mi></math> in which
the left-hand side is one of the following:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>an object literal.</p>
</li>
<li>
<p>a new expression.</p>
</li>
<li>
<p>an instance of a final class, i.e. the type of the value on left-hand side must be nominal and refer to a final class.</p>
</li>
<li>
<p>a reference to a const variable if its initializer expression is one of the following:</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>an object literal.</p>
</li>
<li>
<p>a new expression.</p>
</li>
<li>
<p>an instance of a final class (as explained above).</p>
</li>
<li>
<p>an ternary expression</p>
</li>
</ol>
</div>
</li>
</ol>
</div>
<div class="paragraph">
<p>and then</p>
</div>
<div class="ulist">
<ul>
<li>
<p>in cases 1 and 4a, <strong>both</strong> fields and accessors (getters and setters) are optional.
That means, an optional data field, getter, or setter of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>R</mi></math> needs not be present in <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>L</mi></math>.</p>
</li>
<li>
<p>in cases 2, 3, 4b, and 4c, only <strong>getters</strong> are optional, setters are not optional.
That means, an optional getter of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>R</mi></math> needs not be present in <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>L</mi></math> and an optional field of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>R</mi></math> requires only a setter in <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>L</mi></math>. Note that these cases are more restricted than the cases 1 and 4a.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Moreover, optionality has an effect in case of ternary expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>L</mi><mo>&lt;</mo><mi>:</mi><mi>R</mi></math> in which the left-hand side is a ternary expression, e.g. <code>l = b? trueExpr : falseExpr</code> whose <code>trueExpr</code> or <code>falseExpr</code> possibly recursively contains an expression of the kind mentioned above. In this case, the optionality effect is the more restricted optinality of <code>trueExpr</code> and <code>falseExpr</code>.</p>
</div>
<div class="paragraph">
<p>If, according to these rules, a data field / getter / setter of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>R</mi></math> need not be present in
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>L</mi></math> but a member with the same name and access is actually present in <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>L</mi></math>, that member in <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>L</mi></math>
must be a data field / getter / setter of the same type / a subtype / a super type, respectively.
In other words, if a not actually required member is present in the subtype, ordinary rules
for member compatibility apply as if no optionality were involved (cf. general subtyping rules
for structural types).</p>
</div>
</div>
</div>
<div class="paragraph">
<p>In other words, in object literals (cases 1 and 4a) neither optional getters, optional setters,
nor optional data fields are required. However, in case of new expressions and instances of
final classes (cases 2, 3, 4b, 4c) only optional getters are not required in a subtype;
optional setters are required as normal (i.e. optionality ignored) and optional data fields
require at least a setter.</p>
</div>
<div class="paragraph">
<p>The following table summarizes the most common cases and shows how this relates to the different
forms of structural typing.</p>
</div>
<table id="tab:optionalFields" class="tableblock frame-all grid-all spread">
<caption class="title">Table 7. Optional Fields</caption>
<colgroup>
<col style="width: 25%;">
<col style="width: 8.3333%;">
<col style="width: 8.3333%;">
<col style="width: 8.3333%;">
<col style="width: 8.3333%;">
<col style="width: 8.3333%;">
<col style="width: 8.3333%;">
<col style="width: 25.0002%;">
</colgroup>
<tbody>
<tr>
<th class="tableblock halign-left valign-top"></th>
<td class="tableblock halign-center valign-top" colspan="5"><p class="tableblock">&#916;</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Case</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Comment</p></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>~</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>~~</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>~w~</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>~r~</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>~i~</code></p></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"></th>
<td class="tableblock halign-center valign-top" colspan="3"><p class="tableblock">may have setter</p></td>
<td class="tableblock halign-center valign-top" colspan="2"><p class="tableblock">never has setter</p></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock"><code>let x: &#916;C = {};</code></p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">nothing mandatory</p></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock"><code>let x: &#916;C = new D0();</code></p></th>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">2</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">setters mandatory</p></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock"><code>let x: &#916;C = new DG();</code></p></th>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">2</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">setters mandatory</p></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock"><code>let x: &#916;C = new DS();</code></p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">2</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">setters mandatory</p></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock"><code>let x: &#916;C = fooD0();</code></p></th>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">none</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">D0 not final</p></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock"><code>let x: &#916;C = fooSF0();</code></p></th>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">none</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">fooSF0() not nominal</p></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock"><code>let x: &#916;C = fooF0();</code></p></th>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">3</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">setters mandatory</p></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>In the table, a "✓" means that the particular example is valid; in all other cases an error would
be shown in N4JS source code. Here are the classes and functions used in the above table:</p>
</div>
<div class="listingblock">
<div class="title">Classes and functions used in table</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {
public field?: string;
}
class D0 {}
class DG {
public get field(): string { return "hello"; }
}
class DS {
public set field(value: string) {}
}
@Final class F0 {}
function fooD0(): D0 { return new D0(); }
function fooSF0(): ~F0 { return new F0(); }
function fooF0(): F0 { return new F0(); }</code></pre>
</div>
</div>
<div class="paragraph">
<p>It follows from the above definitions in Requirements <a href="#Req-IDE-240500">[Req-IDE-240500]</a> that cases 4a and 4b are not
transitive across a chain of several <code>const</code> variables, whereas case 4c is transitive. For example:</p>
</div>
<div class="listingblock">
<div class="title">Transitivity of the use cases of optional fields</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {
public get getter?(): string {return null;}
}
class D {}
@Final class F {}
let c: ~C;
// no transitivity via several const variables in use case "object literal":
const ol1 = {};
const ol2 = ol1;
// XPECT errors --&gt; "~Object is not a structural subtype of ~C: missing getter getter." at "ol2"
c = ol2;
// no transitivity via several const variables in use case "new expression":
const new1 = new D();
const new2 = new1;
// XPECT errors --&gt; "D is not a structural subtype of ~C: missing getter getter." at "new2"
c = new2;
// BUT: we do have transitivity via several const variables in use case "final nominal type":
const finalNominal1 = new F();
const finalNominal2 = finalNominal1;
// XPECT noerrors --&gt;
c = finalNominal1;
// XPECT noerrors --&gt; "transitivity applies in this case"
c = finalNominal2;</code></pre>
</div>
</div>
<div class="paragraph">
<p>The following example demonstrates how optionality behaves in ternay expressions.</p>
</div>
<div class="listingblock">
<div class="title">Optional fields in ternay expressions</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">interface ~I {
public m?: int;
}
class ~C { }
@Final class F { }
let b: boolean;
const cc: C = {}
let f1 = new F();
let f2: ~F = {};
// True expression is a const object literal, so both fields and accessors in I are optional.
// False expression is a new expression, so only getters in I are optionals.
// As a result, only getters in I are optional.
// XPECT errors --&gt; "C is not a structural subtype of I: missing field m." at "b? cc : new C()"
var te1: I = b? cc : new C()
// No errors because both true and false expressions are object literal constants and hence
// Both fields and accessors in I are optional.
// XPECT noerrors
var te2: I = b? cc : {}</code></pre>
</div>
</div>
</div>
<div class="sect5">
<h6 id="_background"><a class="anchor" href="#_background"></a><a class="link" href="#_background">5.2.6.5.3. Background</a></h6>
<div class="paragraph">
<p>The following example illustrates why optionality of fields has to be restricted
to the few special cases defined above (i.e. object literals, new expressions, etc.).</p>
</div>
<div class="listingblock">
<div class="title">Problem 1 of optional fields</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {
public field?: string = "hello";
}
class D {}
class DD extends D {
public field: number = 42;
}
let c: ~C;
let d: D;
d = new DD();
c = d; // without the restrictive semantics of optional fields, this assignment would be allowed (but shows compile-time error in N4JS)
console.log(c.field); // prints 42 even though the type is string
c.field.charAt(0); // exception at runtime: c.field.charAt is not a function</code></pre>
</div>
</div>
<div class="paragraph">
<p>In the last line of the above example, <code>c.field</code> is actually <code>42</code> but the type systems claims it is of type <code>string</code> and
thus allows accessing member <code>charAt</code> of type <code>string</code> which is undefined at runtime the actual value <code>42</code>.</p>
</div>
<div class="paragraph">
<p>The next example shows why cases 2 and 3 (i.e. new expressions and instances of final classes) have to be handled in
a more restrictive manner than case 1 (i.e. object literals).</p>
</div>
<div class="listingblock">
<div class="title">Problem 2 of optional fields</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {
public field?: string;
}
class D {}
let c: ~C;
c = new D(); // error: new expression but D is missing setter
c.field = "hello";</code></pre>
</div>
</div>
<div class="paragraph">
<p>In the previous code, if <code>c = new D()</code> were allowed, we would add a new property <code>field</code> to the instance of class
<code>D</code> in the last line, which N4JS aims to avoid in general, unless unsafe language features such as dynamic types
are being employed.</p>
</div>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_static-members"><a class="anchor" href="#_static-members"></a><a class="link" href="#_static-members">5.2.7. Static Members</a></h4>
<div class="paragraph">
<p>Static data fields, field accessors and methods are quite similar to instance members, however they are not members of instances of the type but the type itself.
They are defined similarly to instance members except that they are specified with the modifier <code>static</code>.
Since they are members of the type, the <code>this</code> keyword is not bound to instances of the class, but again to the type itself.
This is similar as in ECMAScript 6 ([<a href="appendix_c_bibliography.html#ECMA15a">ECMA15a(p.14.5.15)</a>]).
Since static members are not instance but type members, it is even possible that a static member has the same name as an instance member.</p>
</div>
<div class="paragraph">
<p>Note that static members are not only allowed in classes but also in interfaces, but there are important differences
(for example, no inheritance of static members of interfaces, cf. Section <a href="#_static-members-of-interfaces">Static Members of Interfaces</a>).</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-65"></a><strong>Req. IDE-65:</strong> <a href="#Req-IDE-65">Static member not abstract</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For a static field accessor or method <code>S</code>, the following constraint must hold:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>s</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo>⇔</mo><mo>¬</mo><mi>s</mi><mo>.</mo><mi>a</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi></math></p>
</li>
</ul>
</div>
</div>
</div>
<div class="paragraph">
<p>Like instance methods, static methods of classes are inherited by subclasses and it is possible to override static methods in subclasses.
The very same override constraints are valid in this case as well.</p>
</div>
<div class="sect4">
<h5 id="_access-from-and-to-static-members"><a class="anchor" href="#_access-from-and-to-static-members"></a><a class="link" href="#_access-from-and-to-static-members">5.2.7.1. Access From and To Static Members</a></h5>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-66"></a><strong>Req. IDE-66:</strong> <a href="#Req-IDE-66">Accessing Static Members</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>Let <code>M</code> be a static member of class <code>C</code>. Except for write-access to
fields, which will be explained later, you can access <code>M</code>
via:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The class declaration instance, i.e. the classifier or constructor type, <code>constructor{C}</code>, i.e. <code>C.m</code></p>
</li>
<li>
<p>The class declaration instance of a subtype, i.e. the classifier or constructor type, i.e. <code>D.m</code>, if <code>D</code> is a subclass of <code>C</code>.</p>
</li>
<li>
<p><code>v.m</code>, if <code>v</code> is a variable of type <code>C</code> (i.e. classifier type as defined in <a href="types.html#_constructor-and-classifier-type">Constructor and Classifier Type</a>) or a subtype thereof.</p>
</li>
<li>
<p><code>this.m</code> inside the body of any static method declared in <code>C</code> or any sub-class of <code>C</code>.</p>
</li>
<li>
<p>Via a type variable <code>T</code> which upper bound is a subclassof <code>C</code> e.g., <code>function &lt;T extends C&gt; f(){T.m}</code></p>
</li>
</ol>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-67"></a><strong>Req. IDE-67:</strong> <a href="#Req-IDE-67">Static Member Access</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>It is not possible to access instance members from static members.
This is true in particular for type variables defined by a generic classifier.</p>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-68"></a><strong>Req. IDE-68:</strong> <a href="#Req-IDE-68">Write-access to static data fields and static setter</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For static data fields and static setter <code>f</code> the following constraint must hold:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>For every assign expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>E</mi><mi>x</mi><mi>p</mi><mi>r</mi></math> with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo>∧</mo><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>E</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo>.</mo><mi>l</mi><mi>e</mi><mi>f</mi><mi>t</mi><mo>=</mo><mi>T</mi><mo>.</mo><mi>f</mi><mo>⇒</mo><mi>T</mi><mo>=</mo><mi>f</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi></math>.</p>
</li>
<li>
<p>For every writing unary expression <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi></math> with <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>u</mi><mo>.</mo><mi>o</mi><mi>p</mi><mo>∈</mo><mfenced close="}" open="{"><mrow><mo>+</mo><mo>+</mo></mrow><mrow><mo>-</mo><mo>-</mo></mrow></mfenced><mo>∧</mo><mi>f</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo>∧</mo><mi>u</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo>=</mo><mi>T</mi><mo>.</mo><mi>f</mi><mo>⇒</mo><mi>T</mi><mo>=</mo><mi>f</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi></math>.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="paragraph">
<p>In the special case of <code>m</code> being a static data field, write-access is only possible via the defining type name <code>C.m</code>.
In the list above, only the first line can be used when assigning values to a field. Note that this only applies to fields and set-accessors.<sup class="footnote">[<a id="_footnoteref_36" class="footnote" href="appendix_c_bibliography.html#_footnote_36" title="View footnote.">36</a>]</sup></p>
</div>
<div class="paragraph">
<p>It is even possible to call a static field accessor or method of a class using dynamic polymorphism, as demonstrated in the following example:</p>
</div>
<div id="ex:Polymorphism_and_static_methods" class="exampleblock">
<div class="title">Example 45. Static members of classes, inheritance and polymorphism</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class A {
static m(): void { console.log('A#m'); }
static foo(): void { console.log('A#foo'); }
static bar(): void {
this.foo();
}
}
class B extends A {
@Override
static foo(): void { console.log('B#foo'); }
}
A.m(); // will print "A#m"
B.m(); // will print "A#m" (m is inherited by B)
var t: type{A} = A;
t.foo(); // will print "A#foo"
t = B;
t.foo(); // will print "B#foo"
// using 'this':
A.bar(); // will print "A#foo"
B.bar(); // will print "B#foo"</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>This is quite different from Java where static methods are not inherited and references to static methods are statically bound at compile time
depending on the declared type of the receiver (and not its value):</p>
</div>
<div class="exampleblock">
<div class="title">Example 46. Static members in Java</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-java" data-lang="java">// !!! JAVA CODE !!!
public class C {
static void m() { System.out.println("C#m"); }
public static void main(String[] args) {
final C c = null;
c.m(); // will print "C#m" (no NullPointerException at runtime)
}
}</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_generic-static-methods"><a class="anchor" href="#_generic-static-methods"></a><a class="link" href="#_generic-static-methods">5.2.7.2. Generic static methods</a></h5>
<div class="paragraph">
<p>It is not possible to refer to type variables of a generic class, as these type variables are never bound to any concrete types.
A static method can, however, be declared generic.
Generic static methods are defined similarly to generic instance methods.
Since they cannot refer to type variables of a generic class, the constraint to avoid type variables with equal names (see <a href="#Req-IDE-55">[Req-IDE-55]</a>) does not need to hold for generic static methods.</p>
</div>
</div>
<div class="sect4">
<h5 id="_static-members-of-interfaces"><a class="anchor" href="#_static-members-of-interfaces"></a><a class="link" href="#_static-members-of-interfaces">5.2.7.3. Static Members of Interfaces</a></h5>
<div class="paragraph">
<p>Data fields, field accessors and methods of interfaces may be declared
static. A few restrictions apply:</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-69"></a><strong>Req. IDE-69:</strong> <a href="#Req-IDE-69">Static Members of Interfaces</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Static members of interfaces may only be accessed directly via the containing interface’s type name
(this means, of the four ways of accessing static members of classes defined in <a href="#Req-IDE-66">[Req-IDE-66]</a> above, only the first one applies to static members of interfaces).</p>
</li>
<li>
<p>The <code>this</code> literal may not be used in static methods or field accessors of interfaces and it may not be used in the initializer expression of static fields of interfaces. See <a href="expressions.html#Req-IDE-173">[Req-IDE-173]</a>.</p>
</li>
<li>
<p>The <code>super</code> literal may not be used in static methods or field accessors of interfaces (in fact, it may not be used in interfaces at all, cf. <a href="expressions.html#Req-IDE-123">[Req-IDE-123]</a>).</p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>Note that the <code>this</code> type as a return type for methods is only allowed for instance methods and as an argument type only in constructors (structurally typed).
There is no need to disallow these cases for static interface methods in the constraints above.</p>
</div>
<div class="paragraph">
<p>In general, static members may not be abstract, cf. <a href="#Req-IDE-46">[Req-IDE-46]</a>, which applies here as well.
Static methods and field accessors of interfaces, therefore, always have to provide a body.</p>
</div>
<div class="paragraph">
<p>Static members of interfaces are much more restricted than those of classes.
Compare the following example to <a href="types.html#_polymorphism-and-static-methods">Static Polymorphism</a> for classes above:</p>
</div>
<div class="exampleblock">
<div class="title">Example 47. Static members of interfaces</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">interface I {
static m(): void { console.log('I#m'); }
}
interface J extends I {}
I.m(); // prints "I#m"
J.m(); // ERROR! (m is not inherited by J)
var ti: type{I} = I;
ti.m(); // ERROR! (access to m only allowed directly via type name I)
ti = J;
ti.m(); // ERROR! (access to m only allowed directly via type name I)</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>The last line in is the reason why access to static members has to be restricted to direct access via the type name of the containing interfaces.</p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_redefinition-of-members"><a class="anchor" href="#_redefinition-of-members"></a><a class="link" href="#_redefinition-of-members">5.2.8. Redefinition of Members</a></h4>
<div class="paragraph">
<p>Members defined in classes or interfaces can be redefined by means of being overridden or implemented in subclasses, sub-interfaces, or implementing classes.
Fields and methods with default implementation defined in interfaces can be consumed by the implementor, but certain restrictions apply.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-70"></a><strong>Req. IDE-70:</strong> <a href="#Req-IDE-70">Override Compatible</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>A member <code>M</code> is <em>override compatible</em> to a member <code>S</code> if and only if the
following constraints hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The name and static modifiers are equal:<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>S</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>∧</mo><mi>M</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo>=</mo><mi>S</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi></math></p>
</li>
<li>
<p>The metatypes are compatible:</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>S</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Method</mtext></mstyle><mspace width="3.0mm"/><mo>→</mo><mspace width="3.0mm"/><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Method</mtext></mstyle></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>S</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Field</mtext></mstyle><mspace width="3.0mm"/><mo>→</mo><mspace width="3.0mm"/><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>∈</mo><mstyle mathvariant="monospace"><mtext>Field, Getter, Setter</mtext></mstyle></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>S</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Getter</mtext></mstyle><mspace width="3.0mm"/><mo>→</mo><mspace width="3.0mm"/><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>∈</mo><mstyle mathvariant="monospace"><mtext>Field, Getter</mtext></mstyle></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>S</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Setter</mtext></mstyle><mspace width="3.0mm"/><mo>→</mo><mspace width="3.0mm"/><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>∈</mo><mstyle mathvariant="monospace"><mtext>Field, Setter</mtext></mstyle></math></p>
</div>
</li>
<li>
<p>The overridden member must not be declared final:<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>¬</mo><mi>S</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi></math></p>
</li>
<li>
<p>Overridden member declared const can only be overridden (redefined) by const members:<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mo>⇔</mo><mi>M</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi></math></p>
</li>
<li>
<p>It is not possible to override a non-final / non-const field or a setter with a final / const field:<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfenced close=")" open="("><mrow><mi>μ</mi><mfenced close=")" open="("><mi>S</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Field</mtext></mstyle><mo>∧</mo><mo>¬</mo><mfenced close=")" open="("><mrow><mi>S</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi><mo>∨</mo><mi>S</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi></mrow></mfenced></mrow></mfenced><mo>∨</mo><mi>μ</mi><mfenced close=")" open="("><mi>S</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Setter</mtext></mstyle><mo>⇒</mo><mo>¬</mo><mfenced close=")" open="("><mrow><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Field</mtext></mstyle><mo>∧</mo><mfenced close=")" open="("><mrow><mi>M</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi><mo>∨</mo><mi>M</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi></mrow></mfenced></mrow></mfenced></math></p>
</li>
<li>
<p>It is not possible to override a non-abstract member with an abstract one:<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>¬</mo><mi>M</mi><mo>.</mo><mi>a</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi><mo>∨</mo><mi>S</mi><mo>.</mo><mi>a</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi></math></p>
</li>
<li>
<p>The types are compatible:</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mfenced close=")" open="("><mrow><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>∈</mo><mstyle mathvariant="monospace"><mtext>Method, Getter, Field</mtext></mstyle><mo>∧</mo><mi>μ</mi><mfenced close=")" open="("><mi>S</mi></mfenced><mo>≠</mo><mstyle mathvariant="monospace"><mtext>Setter</mtext></mstyle></mrow></mfenced><mspace width="3.0mm"/><mo>→</mo><mi>Γ</mi><mo>⊢</mo><mi>M</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>S</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfenced close=")" open="("><mrow><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>∈</mo><mstyle mathvariant="monospace"><mtext>Setter, Field</mtext></mstyle><mo>∧</mo><mi>μ</mi><mfenced close=")" open="("><mi>S</mi></mfenced><mo>≠</mo><mstyle mathvariant="monospace"><mtext>Getter</mtext></mstyle><mo>∧</mo><mo>¬</mo><mi>S</mi><mo>.</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi></mrow></mfenced><mspace width="3.0mm"/><mo>→</mo><mi>Γ</mi><mo>⊢</mo><mi>S</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>M</mi></math>4</p>
</div>
</li>
<li>
<p>The access modifier is compatible:<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>≥</mo><mi>S</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi></math></p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>We define a relation <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi><mi>C</mi><mi>o</mi><mi>m</mi><mi>p</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>b</mi><mi>l</mi><mi>e</mi><mfenced close=")" open="("><mi>M</mi><mi>S</mi></mfenced></math> accordingly.</p>
</div>
<div class="paragraph">
<p>Members overriding or implementing other members must be declared as override.
If a member does not override another, however, it must not be declared as override.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-71"></a><strong>Req. IDE-71:</strong> <a href="#Req-IDE-71">Non-Override Declaration</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>If and only if a member <code>M</code> of a class <code>C</code> (extending a class <code>S</code> and interfaces <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>I</mi><mi>i</mi></msub></math>) does not override or implement another member, then it must not be declared as override.
That is the following constraint must hold:</p>
</div>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo>¬</mo><mi>M</mi><mo>.</mo><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo>∧</mo></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo>∄</mo><msup><mi>M</mi><mi>'</mi></msup><mo>∈</mo><mi>C</mi><mo>.</mo><mi>s</mi><mi>u</mi><mi>p</mi><mi>e</mi><mi>r</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>∪</mo><msubsup><mo>⋃</mo><mrow><mi>i</mi><mo>=</mo><mn>1</mn></mrow><mi>n</mi></msubsup><msub><mi>I</mi><mi>i</mi></msub><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><msup><mi>M</mi><mi>'</mi></msup><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>M</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>∧</mo><msup><mi>M</mi><mi>'</mi></msup><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo>=</mo><mi>M</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo>∧</mo><msup><mi>M</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>&gt;</mo><mstyle mathvariant="monospace"><mtext>private</mtext></mstyle></math></p>
</div>
</div>
</div>
<div class="sect4 language-n4js">
<h5 id="_overriding-of-members"><a class="anchor" href="#_overriding-of-members"></a><a class="link" href="#_overriding-of-members">5.2.8.1. Overriding of Members</a></h5>
<div class="paragraph">
<p>In general, the N4JS platform supports overriding members by redefining them in sub-classes.
This definition allows for overriding of static methods, but it does not apply to constructors because <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>C</mi><mo>.</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mo>∉</mo><mi>C</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><mi>s</mi></math>.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-72"></a><strong>Req. IDE-72:</strong> <a href="#Req-IDE-72">Overriding Members</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>Given a class <code>C</code> and a superclass <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi><mi>u</mi><mi>p</mi></math>.
If for an instance or static member <code>M</code> defined in <code>C</code> a member <code>S</code> exists with null
then we call <code>M</code> the overriding member and <code>S</code> the overridden member.
In that case the following constraints must hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><code>S</code> must be accessible from <code>C</code></p>
</li>
<li>
<p><code>M</code> must be override compatible with <code>S</code>:<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi><mi>C</mi><mi>o</mi><mi>m</mi><mi>p</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>b</mi><mi>l</mi><mi>e</mi><mfenced close=")" open="("><mi>M</mi><mi>S</mi></mfenced></math></p>
</li>
<li>
<p>If <code>S</code> is a field and <code>M</code> is an accessor, then an additional accessor <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>M</mi><mi>'</mi></msup></math> must exists so that <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi><mo>,</mo><msup><mi>M</mi><mi>'</mi></msup></math> are an accessor pair for <code>S</code>:</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mi>μ</mi><mfenced close=")" open="("><mi>S</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Field</mtext></mstyle><mo>∧</mo><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>=</mo><mrow><mi>A</mi><mi>c</mi><mi>c</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>o</mi><mi>r</mi></mrow></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo>→</mo><mo>∃</mo><msup><mi>M</mi><mi>'</mi></msup><mo>∈</mo><mi>C</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>:</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="4.0em"/><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi><mi>C</mi><mi>o</mi><mi>m</mi><mi>p</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>b</mi><mi>l</mi><mi>e</mi><mfenced close=")" open="("><msup><mi>M</mi><mi>'</mi></msup><mi>S</mi></mfenced><mo>∧</mo><mfenced close="}" open="{"><mrow><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced></mrow><mrow><mi>μ</mi><mfenced close=")" open="("><msup><mi>M</mi><mi>'</mi></msup></mfenced></mrow></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Getter,Setter</mtext></mstyle></math></p>
</div>
</li>
<li>
<p><code>M</code> must be declared as override:<br>
<code>M.override</code></p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>Remarks:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>An overridden method, getter, or setter may called via <code>super</code>.
Note that this is not possible for fields.</p>
</li>
<li>
<p>There is no ’hiding’ of fields as in Java, instead there is field overriding.</p>
</li>
<li>
<p>It is not possible to override a field with a consumed getter and an overridden setter, because the getter is not consumed if there exists a field in a superclass.
In this case, the consuming and extending class needs to define the accessor pair explicitly.
The same is true for other combination of accessors and fields.</p>
</li>
<li>
<p>Overriding a field usually makes only sense if the visibility of the field is to be increased.</p>
</li>
</ul>
</div>
</div>
<div class="sect4">
<h5 id="_implementation-of-members"><a class="anchor" href="#_implementation-of-members"></a><a class="link" href="#_implementation-of-members">5.2.8.2. Implementation of Members</a></h5>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="interface_and_class_member_sets"></a><strong>Definition:</strong> <a href="#interface_and_class_member_sets">Interface and Class Member Sets</a></p>
</div>
<div class="paragraph">
<p>For the following constraints, we define two helper sets <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>M</mi><mi>C</mi></msub></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>M</mi><mi>I</mi></msub></math> as follows:</p>
</div>
<div class="paragraph">
<p>Given a <code>C</code>, and interface <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>I</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>I</mi><mi>n</mi></msub></math>, implemented by <code>C</code>, with</p>
</div>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>M</mi><mi>C</mi></msub><mspace width="3.0mm"/><mo>=</mo><mi>C</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>∪</mo><mrow><mo>{</mo><mrow><mi>m</mi><mo>∈</mo><mi>C</mi><mo>.</mo><mi>s</mi><mi>u</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>|</mo><mi>m</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>&gt;</mo><mstyle mathvariant="monospace"><mtext>private</mtext></mstyle><mo>}</mo></mrow></mrow></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>M</mi><mi>I</mi></msub><mspace width="3.0mm"/><mo>=</mo><msubsup><mo>⋃</mo><mrow><mi>i</mi><mo>=</mo><mn>1</mn></mrow><mi>n</mi></msubsup><msub><mi>I</mi><mi>i</mi></msub><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi></math></p>
</div>
<div class="paragraph">
<p>Note that these sets already contain only non-private data fields.</p>
</div>
</div>
</div>
<div class="sect5">
<h6 id="_member-consumption"><a class="anchor" href="#_member-consumption"></a><a class="link" href="#_member-consumption">5.2.8.2.1. Member Consumption</a></h6>
<div class="openblock definition">
<div class="content">
<div class="paragraph">
<p><a id="member_consumption_and_implementation"></a><strong>Definition:</strong> <a href="#member_consumption_and_implementation">Member Consumption and Implementation</a></p>
</div>
<div class="paragraph">
<p>A member <code>M</code> defined in an interface <code>I</code> is <em>consumed</em> by an implementor <code>C</code>, if it becomes a member of the class, that is, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>M</mi><mo>∈</mo><mi>C</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi></math>.</p>
</div>
<div class="paragraph">
<p>A member <code>M</code> is consumed if there is no member defined in the implementor with the same name and if there is no non-private,
non-abstract member with that name inherited by the implementor from its superclass. <sup class="footnote">[<a id="_footnoteref_37" class="footnote" href="appendix_c_bibliography.html#_footnote_37" title="View footnote.">37</a>]</sup></p>
</div>
<div class="paragraph">
<p>If the implementor defines the member itself, then the member is implemented rather than consumed.</p>
</div>
<div class="paragraph">
<p>The concrete rules are described in the following;</p>
</div>
<div class="paragraph">
<p>It is not always possible to directly consume a member.
In general, a rather conservative strategy is used: if two implemented interfaces define the same (non-abstract) member then the implementor must redefine the member in order to solve conflicts.
Even if the two conflicting members have the same types, the implementor must redefine them as we generally assume semantic differences which the consumer has to be aware of.
Data fields defined in interfaces, in particular, are assumed to be concrete.
It is not, therefore, possible to consume a field defined in two implemented interfaces.</p>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-73"></a><strong>Req. IDE-73:</strong> <a href="#Req-IDE-73">Consumption of Interface Members</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>Given a classifier <code>C</code> <sup class="footnote">[<a id="_footnoteref_38" class="footnote" href="appendix_c_bibliography.html#_footnote_38" title="View footnote.">38</a>]</sup>, and interfaces <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>I</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>I</mi><mi>n</mi></msub></math> implemented (or extended) by <code>C</code>, and sets <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>M</mi><mi>C</mi></msub></math> and <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>M</mi><mi>I</mi></msub></math> as defined in <a href="#interface_and_class_member_sets">[interface_and_class_member_sets]</a>.
A non-static member <code>M</code> defined in any interface <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>I</mi><mi>i</mi></msub></math> is merged into the consumer (<code>C</code>), if for all other (possible) members <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>M</mi><mi>'</mi></msup></math> of <code>C</code></p>
</div>
<div class="openblock">
<div class="content">
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mo>∀</mo><msup><mi>M</mi><mi>'</mi></msup><mo>∈</mo><msub><mi>M</mi><mi>C</mi></msub><mo>∪</mo><msub><mi>M</mi><mi>I</mi></msub><mo>∖</mo><mfenced close="}" open="{"><mi>M</mi></mfenced><mi>:</mi><mi>M</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><msup><mi>M</mi><mi>'</mi></msup><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>∧</mo><mo>¬</mo><msup><mi>M</mi><mi>'</mi></msup><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi></math>
</div>
</div>
<div class="paragraph">
<p>the following constraints hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The other member’s meta type matches the meta type of the merge candiate:</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Method</mtext></mstyle><mspace width="3.0mm"/><mo>→</mo><mi>μ</mi><mfenced close=")" open="("><msup><mi>M</mi><mi>'</mi></msup></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Method</mtext></mstyle></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>≠</mo><mstyle mathvariant="monospace"><mtext>Method</mtext></mstyle><mspace width="3.0mm"/><mo>→</mo><mi>μ</mi><mfenced close=")" open="("><msup><mi>M</mi><mi>'</mi></msup></mfenced><mo>∈</mo><mstyle mathvariant="monospace"><mtext>Field, FieldAccessor</mtext></mstyle></math></p>
</div>
</li>
<li>
<p>The other member is abstract and not owned by the consumer:</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>=</mo><mi>μ</mi><mfenced close=")" open="("><msup><mi>M</mi><mi>'</mi></msup></mfenced><mo>∨</mo><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Field</mtext></mstyle></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="2.0em"/><mo>→</mo><msup><mi>M</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>b</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi><mo>∧</mo><msup><mi>M</mi><mi>'</mi></msup><mo>∉</mo><mi>C</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi></math></p>
</div>
</li>
<li>
<p>The merge candidate’s access modifier is not less than the modifier of the other member:</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>=</mo><mi>μ</mi><mfenced close=")" open="("><msup><mi>M</mi><mi>'</mi></msup></mfenced><mo>∨</mo><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Field</mtext></mstyle></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="2.0em"/><mo>→</mo><mi>M</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>≥</mo><msup><mi>M</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi></math></p>
</div>
</li>
<li>
<p>The merge candidate’s type compatible with the other member:</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>∈</mo><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>Method, Getter, Field</mtext></mstyle></mfenced><mo>∧</mo><mi>μ</mi><mfenced close=")" open="("><msup><mi>M</mi><mi>'</mi></msup></mfenced><mo>≠</mo><mstyle mathvariant="monospace"><mtext>Setter</mtext></mstyle><mspace width="3.0mm"/><mo>→</mo><mi>Γ</mi><mo>⊢</mo><mi>M</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><msup><mi>M</mi><mi>'</mi></msup></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>∈</mo><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>Setter, Field</mtext></mstyle></mfenced><mo>∧</mo><mi>μ</mi><mfenced close=")" open="("><msup><mi>M</mi><mi>'</mi></msup></mfenced><mo>≠</mo><mstyle mathvariant="monospace"><mtext>Getter</mtext></mstyle><mspace width="3.0mm"/><mo>→</mo><mi>Γ</mi><mo>⊢</mo><msup><mi>M</mi><mi>'</mi></msup><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>M</mi></math></p>
</div>
</li>
</ol>
</div>
</div>
</div>
</div>
<div class="sect5 language-n4js">
<h6 id="_member-implementation"><a class="anchor" href="#_member-implementation"></a><a class="link" href="#_member-implementation">5.2.8.2.2. Member Implementation</a></h6>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-74"></a><strong>Req. IDE-74:</strong> <a href="#Req-IDE-74">Implementation of Interface Members</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>For any non-static abstract member <code>M</code> defined in an interface <code>I implemented (or extended) by a classifier `C</code>, <code>M</code> must be accessible
from <code>C</code> and one or two member(s) in <code>C</code> must exist which are implementation-compatible with <code>M</code>.
The implementing member(s) must be declared as override if they are directly defined in the consumer.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p><code>M</code> must be accessible from <code>C</code>.</p>
</li>
<li>
<p>An implementation-compatible member <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>M</mi><mi>'</mi></msup></math> must exist in <code>C</code>:</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>if <code>M</code> is not a field:</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>≠</mo><mstyle mathvariant="monospace"><mtext>Field</mtext></mstyle><mspace width="3.0mm"/><mo>→</mo></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo>∃</mo><msup><mi>M</mi><mi>'</mi></msup><mo>∈</mo><mi>C</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="3.0em"/><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi><mi>C</mi><mi>o</mi><mi>m</mi><mi>p</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>b</mi><mi>l</mi><mi>e</mi><mfenced close=")" open="("><msup><mi>M</mi><mi>'</mi></msup><mi>M</mi></mfenced></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="3.0em"/><mo>∧</mo><mfenced close=")" open="("><mrow><msup><mi>M</mi><mi>'</mi></msup><mo>∈</mo><mi>C</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>→</mo><msup><mi>M</mi><mi>'</mi></msup><mo>.</mo><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi></mrow></mfenced></math></p>
</div>
</li>
<li>
<p>if <code>M</code> is a field, then either an
implementation-compatible field <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>F</mi><mi>'</mi></msup></math> or accessor pair <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>G</mi><mi>'</mi></msup><mo>,</mo><msup><mi>S</mi><mi>'</mi></msup></math> must exist:</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>μ</mi><mfenced close=")" open="("><mi>M</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Field</mtext></mstyle><mspace width="3.0mm"/><mo>→</mo></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo>∃</mo><msup><mi>F</mi><mi>'</mi></msup><mo>∈</mo><mi>C</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mi>s</mi><mi>:</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="3.0em"/><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi><mi>C</mi><mi>o</mi><mi>m</mi><mi>p</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>b</mi><mi>l</mi><mi>e</mi><mfenced close=")" open="("><msup><mi>F</mi><mi>'</mi></msup><mi>M</mi></mfenced></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="3.0em"/><mo>∧</mo><mfenced close=")" open="("><mrow><msup><mi>F</mi><mi>'</mi></msup><mo>∈</mo><mi>C</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>→</mo><msup><mi>F</mi><mi>'</mi></msup><mo>.</mo><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi></mrow></mfenced></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo>∨</mo></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo>∃</mo><msup><mi>G</mi><mi>'</mi></msup><mo>∈</mo><mi>C</mi><mo>.</mo><mi>g</mi><mi>e</mi><mi>t</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>,</mo><msup><mi>S</mi><mi>'</mi></msup><mo>∈</mo><mi>C</mi><mo>.</mo><mi>s</mi><mi>e</mi><mi>t</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="3.0em"/><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi><mi>C</mi><mi>o</mi><mi>m</mi><mi>p</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>b</mi><mi>l</mi><mi>e</mi><mfenced close=")" open="("><msup><mi>G</mi><mi>'</mi></msup><mi>M</mi></mfenced></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="3.0em"/><mo>∧</mo><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi><mi>C</mi><mi>o</mi><mi>m</mi><mi>p</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>b</mi><mi>l</mi><mi>e</mi><mfenced close=")" open="("><msup><mi>S</mi><mi>'</mi></msup><mi>M</mi></mfenced></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="3.0em"/><mo>∧</mo><mfenced close=")" open="("><mrow><msup><mi>G</mi><mi>'</mi></msup><mo>∈</mo><mi>C</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>→</mo><msup><mi>G</mi><mi>'</mi></msup><mo>.</mo><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi></mrow></mfenced></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="3.0em"/><mo>∧</mo><mfenced close=")" open="("><mrow><msup><mi>S</mi><mi>'</mi></msup><mo>∈</mo><mi>C</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>d</mi><mi>M</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>→</mo><msup><mi>S</mi><mi>'</mi></msup><mo>.</mo><mi>o</mi><mi>v</mi><mi>e</mi><mi>r</mi><mi>r</mi><mi>i</mi><mi>d</mi><mi>e</mi></mrow></mfenced></math></p>
</div>
</li>
</ol>
</div>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>Methods defined in interfaces are automatically declared abstract if they do not provide a default implementation.
This can also be expressed explicitly via adding the <code>abstract</code> modifier.
If a class implementing an abstract interface does not implement a method declared in the interface, the class needs to be declared abstract (cf. <a href="#_abstract-classes">Abstract Classes</a>).</p>
</div>
<div class="paragraph">
<p>Consequences for method implementation:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>It may be require the implementor to explicitly define a method in order to solve type conflicts produced by methods of different interfaces with same name but different signatures.</p>
</li>
<li>
<p>Methods in an implementor cannot decrease the accessibility of methods from implemented interfaces, that is</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mo>∀</mo><mi>M</mi><mo>∈</mo><mi>C</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><mi>s</mi><mo>,</mo><msup><mi>M</mi><mi>'</mi></msup><mo>∈</mo><msub><mi>I</mi><mi>i</mi></msub><mo>.</mo><mi>m</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><mi>s</mi><mfenced close=")" open="("><mrow><mi>i</mi><mo>=</mo><mn>1</mn><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mi>n</mi></mrow></mfenced><mi>:</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="2.0em"/><mi>M</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><msup><mi>M</mi><mi>'</mi></msup><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>→</mo><mi>M</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>≠</mo><mi>p</mi><mi>r</mi><mi>i</mi><mi>v</mi><mi>a</mi><mi>t</mi><mi>e</mi><mo>⇒</mo><mi>M</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>≥</mo><msup><mi>M</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi></math></p>
</div>
</li>
<li>
<p>Methods in the implementor must be a supertype <sup class="footnote">[<a id="_footnoteref_39" class="footnote" href="appendix_c_bibliography.html#_footnote_39" title="View footnote.">39</a>]</sup> of methods from implemented interfaces.
That is to say the implemented methods are override-compatible.</p>
</li>
<li>
<p>There may be several methods <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>M</mi><mn>1</mn></msub><mo>,</mo><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>.</mo><mspace width="1.0mm"/><mo>,</mo><msub><mi>M</mi><mi>n</mi></msub></math> defined in different implemented interfaces and a single owned method <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>M</mi><mi>'</mi></msup></math> in <math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>M</mi><mi>C</mi></msub></math>.
In this case, the above constraints must hold for <em>all</em> methods. In particular, <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>M</mi><mi>'</mi></msup></math>’s signature must conform to all conflicting methods’ signatures.
This is possible by using union types for the arguments and an intersection type as return type.
Such a method <math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>M</mi><mi>'</mi></msup></math> is said to <em>resolve</em> the conflict between the implemented (and also inherited) methods.</p>
</li>
<li>
<p>Since abstracts methods may become part of the implementor methods, the implementor must either define these methods or it must be declared abstract itself.
Since interfaces are abstract by default, responsibility for implementing abstract methods is passed on to any implementor of interfaces.</p>
</li>
<li>
<p>If two implemented interfaces provide (non-abstract) members with the same name, they are not automatically consumed by the implementor even if the types would be similar.
In these cases, the implementor has to redefine the members in order to be aware of possible semantic differences.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>There is currently no separate annotation to indicate that methods are implemented or overridden in order to solve conflicts.
We always use the <code>@Override</code> annotation.</p>
</div>
<div class="exampleblock">
<div class="title">Example 48. Method Consumption</div>
<div class="content">
<div class="paragraph">
<p><a href="#tab:methodConsumption">Consumption of methods</a> shows simple examples of above rules.
Assuming that <code>class C</code> extends super <code>class S</code> and implements interface <code>I1</code> and <code>I2</code>:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C extends S implements I1, I2 {...}</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>The columns describe different scenarios in which a method (with same name) is defined in different classifiers.
We assume that the defined methods are always non-abstract (i.e. have default implementations), non-private and have the same signature.
The last row shows which method will be actually used in class <code>C</code>.
If the method is defined in class <code>C</code>, and if this method is printed bold, then this means that the method is required to be defined in <code>C</code> in order to solve conflicts.</p>
</div>
<table id="tab:methodConsumption" class="tableblock frame-all grid-all spread">
<caption class="title">Table 8. Consumption of methods</caption>
<colgroup>
<col style="width: 25%;">
<col style="width: 12.5%;">
<col style="width: 12.5%;">
<col style="width: 12.5%;">
<col style="width: 12.5%;">
<col style="width: 12.5%;">
<col style="width: 12.5%;">
</colgroup>
<tbody>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock">Interface <code>I1</code></p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>I1</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>I1</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>I1</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>I1</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>I1</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>I1</sub></em></p></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock">Interface <code>I2</code></p></th>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>I2</sub></em></p></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>I2</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>I2</sub></em></p></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock">class <code>S</code></p></th>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>S</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>S</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>S</sub></em></p></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock">class <code>C</code></p></th>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>C</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><strong>M<sub>C</sub></strong></p></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>C</sub></em></p></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><mo>∈</mo><mi>C</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi></math></p></th>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>I1</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>C</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>C</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>S</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>S</sub></em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>M<sub>C</sub></em></p></td>
</tr>
</tbody>
</table>
<div id="consuming-field-initializers" class="dlist">
<dl>
<dt class="hdlist1">Consuming Field Initializers </dt>
<dd>
<p>Aside from the fields themselves, an implementor <em>always</em> consumes the field initialization if the field is consumed – this is how the consumption is noticed at runtime.</p>
</dd>
</dl>
</div>
<div class="exampleblock">
<div class="title">Example 49. Field and Field Initializer Consumption</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">/* XPECT output ~~~
&lt;==
stdout:
s: C , t: D ,u: I1 ,v: I2
stderr:
==&gt;
~~~ */
interface I0 {
v: string = "I0";
}
interface I1 {
s: string = "I1";
t: string = "I1";
u: string = "I1";
}
interface I2 extends I1, I0 {
@Override
t: string = "I2";
@Override
v: string = "I2";
}
class C {
s: string = "C";
}
class D extends C implements I1, I2 {
@Override
t: string = "D";
}
var d = new D();
console.log(
"s:", d.s, ", t:", d.t, ",u:", d.u, ",v:", d.v
)</code></pre>
</div>
</div>
<div class="paragraph">
<p>We expect the following output (for each field):</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>d.s = "C"</code> : <code>s</code>: is inherited from <code>C</code>, so it is not consumed from <code>I1</code> (or <code>I2</code>).
Consequently, the initializer of <code>s</code> in <code>C</code> is used.</p>
</li>
<li>
<p><code>d.t = "D"</code>: <code>t</code> is defined in <code>D</code>, solving a conflict stemming from the definition of <code>t</code> in <code>I1</code> and <code>I2</code>. Thus, the initializer of <code>t</code> in <code>D</code> is used.</p>
</li>
<li>
<p><code>d.u = "I1"</code> : <code>u</code> is only defined in <code>I1</code>, thus the initializer defined in <code>I1</code> is used.</p>
</li>
<li>
<p><code>d.v = "I2"</code> : <code>v</code> is overridden in <code>I2</code>, so is the field initializer. This is why <code>d.v</code> must be assigned to <code>I2</code> and not <code>I0</code>.</p>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_structural-typing"><a class="anchor" href="#_structural-typing"></a><a class="link" href="#_structural-typing">5.3. Structural Typing</a></h3>
<div class="paragraph">
<p>In general, N4JS uses nominal typing.
This is to say that a duck is a duck only if it is declared to be a duck.
In particular when working with external APIs, it is more convenient to use structural or duck typing.
That is, a thing that can swim and quacks like a duck, is a duck.</p>
</div>
<div class="paragraph language-n4js">
<p>Interfaces or classes can be used for this purpose with a <em>typing strategy modifier</em>.
Given a type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math>, the simple <code>~</code> (tilde) can be added to its declaration (on definition-site) or in a reference (on use-site) to indicate that the type system should use structural typing
rather than nominal typing.<sup class="footnote">[<a id="_footnoteref_40" class="footnote" href="appendix_c_bibliography.html#_footnote_40" title="View footnote.">40</a>]</sup>
This means that some other type must provide the same members as type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math> to be deemed a structural subtype.
However, the operator cannot be used anymore with the type or reference as this operator relies on the declaration information (or at least the closest thing available at runtime).
In this case, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math> is, therefore, always a structural subtype of <em>~T</em>.</p>
</div>
<div class="paragraph language-n4js">
<p>Sometimes it is convenient to refer only to the fields of a classifier, for example when the initial field values are to be provided in a variable passed to the constructor.
In that case, the type can be modified with <code>~~</code> (two tildes). This is only possible on use-site, i.e. on type references.
Furthermore, only on the use-site, it is possible to consider only either readable or writable or fields by using the read-only <code>~r~</code> or write-only <code>~w~</code> structural field typing.
For initialization blocks, it is even possible to use structural initializer field typing via the <code>~i~</code> operator.</p>
</div>
<div class="sect3 language-n4js">
<h4 id="_syntax-7"><a class="anchor" href="#_syntax-7"></a><a class="link" href="#_syntax-7">5.3.1. Syntax</a></h4>
<div class="paragraph">
<p>Structural typing is specified using the typing strategy modifier. There
are two modifiers defined; one for definition-site and one for use-site
structural typing.</p>
</div>
<div id="lst:Structural_Type_Operator_and_References" class="listingblock">
<div class="title">Structural Type Operator and References</div>
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">TypingStrategyUseSiteOperator returns TypingStrategy:
'~' ('~' | STRUCTMODSUFFIX)?;
TypingStrategyDefSiteOperator returns TypingStrategy:
'~';
terminal STRUCTMODSUFFIX:
('r' | 'i' | 'w') '~'
;
ParameterizedTypeRefStructural returns ParameterizedTypeRefStructural:
definedTypingStrategy=TypingStrategyUseSiteOperator
declaredType=[Type|TypeReferenceName]
(=&gt; '&lt;' typeArgs+=TypeArgument (',' typeArgs+=TypeArgument)* '&gt;')?
(=&gt; 'with' '{' astStructuralMembers+=TStructMember* '}')?
;
ThisTypeRefStructural returns ThisTypeRefStructural:
definedTypingStrategy=TypingStrategyUseSiteOperator
'this'
('with' '{' astStructuralMembers+=TStructMember* '}')?
;</code></pre>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_definition-site-structural-typing"><a class="anchor" href="#_definition-site-structural-typing"></a><a class="link" href="#_definition-site-structural-typing">5.3.2. Definition Site Structural Typing</a></h4>
<div class="paragraph">
<p>An interface or class can be defined to be used with structural typing by adding the structural modifier to its definition (or, in case of external classes, to the declaration).
This changes the default type system strategy from nominal to structural typing for that type.
That means that all types with the same members as the specified type are subtypes of that type, except for subtypes of <code>N4Object</code>.
In the latter case, programmers are enforced to nominal declare the type relation.</p>
</div>
<div class="paragraph">
<p>If a type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math> is declared as structural at its definition, <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi></math> is true.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-75"></a><strong>Req. IDE-75:</strong> <a href="#Req-IDE-75">Definition Site Structural Typing</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The structurally defined type cannot be used on the right hand side of the <code>instanceof</code> operator:
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>x</mi><mi> </mi><mstyle mathvariant="monospace"><mtext>instanceof</mtext></mstyle><mi> </mi><mi>T</mi><mo>⇒</mo><mo>¬</mo><mi>T</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi></math></p>
</li>
<li>
<p>A type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math> is a subtype of a structurally defined type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math> either</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>if it is not a subtype of <code>N4Object</code> <sup class="footnote">[<a id="_footnoteref_41" class="footnote" href="appendix_c_bibliography.html#_footnote_41" title="View footnote.">41</a>]</sup>
but it contains all public, non-static members of that type</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi><mspace width="1.0mm"/><mo>≮</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>N4Object</mtext></mstyle></mfenced><mspace width="3.0em"/><mi>T</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>}</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>∀</mo><mi>m</mi><mo>∈</mo><mi>T</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>,</mo><mi>m</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo>,</mo><mo>¬</mo><mi>m</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo>,</mo><mi>m</mi><mo>≠</mo><mi>T</mi><mo>.</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mi>:</mi><mi>}</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="1.0em"/><mo>∃</mo><msup><mi>m</mi><mi>'</mi></msup><mo>∈</mo><mi>X</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi><mi>}</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="2.0em"/><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo>∧</mo><mo>¬</mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo>∧</mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>m</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mi>}</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="2.0em"/><mo>∧</mo><mi>Γ</mi><mo>⊢</mo><msup><mi>m</mi><mi>'</mi></msup><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>m</mi><mi>}</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mspace width="2.0em"/><mo>∧</mo><mi>μ</mi><mfenced close=")" open="("><mi>m</mi></mfenced><mo>=</mo><mstyle mathvariant="monospace"><mtext>Field</mtext></mstyle><mo>⇒</mo><mi>Γ</mi><mo>⊢</mo><mi>m</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><msup><mi>m</mi><mi>'</mi></msup></mrow><mrow><mspace width="13.0em"/><mi>Γ</mi><mo>⊢</mo><mi>X</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>T</mi><mspace width="13.0em"/></mrow></mfrac></math><br>
or</p>
</div>
</li>
<li>
<p>if it is a subtype of <code>N4Object</code> which explicitly extends or implements the
structurally defined type.</p>
<div class="openblock">
<div class="content">
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>X</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mfenced close="}" open="{"><mstyle mathvariant="monospace"><mtext>N4Object</mtext></mstyle></mfenced><mspace width="3.0mm"/><mi>T</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mspace width="3.0mm"/><mrow><mi>T</mi><mo>∈</mo><mi>X</mi><mo>.</mo><mi>s</mi><mi>u</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><msup><mi>s</mi><mo>*</mo></msup></mrow></mrow><mrow><mi>Γ</mi><mo>⊢</mo><mi>X</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>T</mi></mrow></mfrac></math>
</div>
</div>
</li>
<li>
<p>A structurally defined type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math> is implicitly derived
from <code>Object</code> if no other type is specified. In particular, a structurally
defined type must not be inherited from</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mi>T</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi></mrow><mrow><mi>Γ</mi><mo>⊢</mo><mi>T</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>Object</mtext></mstyle></mrow></mfrac></math></p>
</div>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>.</mo><mi>d</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mo>⇒</mo><mi>Γ</mi><mo>⊢</mo><mi>T</mi><mspace width="1.0mm"/><mo>≮</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mstyle mathvariant="monospace"><mtext>N4Object</mtext></mstyle><mo>∧</mo><mstyle mathvariant="monospace"><mtext>N4Object</mtext></mstyle><mo>∉</mo><mi>T</mi><mo>.</mo><mi>s</mi><mi>u</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi><msup><mi>s</mi><mo>*</mo></msup></math></p>
</div>
</li>
</ol>
</div>
</li>
</ol>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 50. Declaration Site Structural Typing</div>
<div class="content">
<div class="paragraph">
<p>The following snippet demonstrates the effect of definition-site structural types by comparing them to
nominal declared types: <a id="ex:declaration-site-structural-typing"></a></p>
</div>
<div class="listingblock">
<div class="title">Declaration Site Structural Typing</div>
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">interface ~Tilde { x; y; }
interface Nominal { x; y; }
class C { public x; public y;}
class D extends C implements Tilde { }
function f(p: Tilde) {}
function g(p: Nominal) {}
f(new C()); // error: nominal typing, C does not implement ~Tilde
f(new D()); // ok, D is a nominal subtype (as it implements Tilde)
f({x:10,y:10}); // ok: Tilde is used with structural typing for non-N4-classifiers</code></pre>
</div>
</div>
<div class="paragraph">
<p>Definition site structural typing may lead to unexpected results. For
example;</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C{}
class ~E extends C{}</code></pre>
</div>
</div>
<div class="paragraph">
<p>It may be unexpected, but <code>E</code> is not a subtype of <code>C</code>, i.e.
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>E</mi><mspace width="1.0mm"/><mo>≮</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>C</mi></math>! E.g., <code>instanceof</code> won’t work with <code>E</code>, while it works
with <code>C</code>!</p>
</div>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_use-site-structural-typing"><a class="anchor" href="#_use-site-structural-typing"></a><a class="link" href="#_use-site-structural-typing">5.3.3. Use-Site Structural Typing</a></h4>
<div class="paragraph">
<p>Use-site structural typing offers several typing strategy modifiers to define the accessability of public properties of classes and interfaces.
They can be used e.g. on variable declarations like this: <code>var c : ~C</code>.
The table <a href="#tab:available-fields-of-structural-types">Available Fields of Structural Types</a> shows which properties of structural types can be accessed in the different type strategies.
For example, when using the write-only structural strategy (i.e. <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>w</mtext></mstyle><mi>~</mi><mstyle mathvariant="monospace"><mtext>X</mtext></mstyle></math>), only the writeable fields, can be accessed for writing.
In the table, the term field to both, public datafields and accessors of type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math>.
Non-public properties are never accessable in use-site structural types.
In any field-structural type, methods of the referenced nominal type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math> are not available.
The initializer structural typing provides readable fields for every writeable field of the references type.</p>
</div>
<table id="tab:available-fields-of-structural-types" class="tableblock frame-all grid-all spread">
<caption class="title">Table 9. Available Fields of Structural Types</caption>
<colgroup>
<col style="width: 16.6666%;">
<col style="width: 16.6666%;">
<col style="width: 16.6666%;">
<col style="width: 16.6666%;">
<col style="width: 16.6666%;">
<col style="width: 16.667%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Property of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math></th>
<th class="tableblock halign-center valign-top"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>X</mtext></mstyle></math></th>
<th class="tableblock halign-center valign-top"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>X</mtext></mstyle></math></th>
<th class="tableblock halign-center valign-top"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>r</mtext></mstyle><mi>~</mi><mstyle mathvariant="monospace"><mtext>X</mtext></mstyle></math></th>
<th class="tableblock halign-center valign-top"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>w</mtext></mstyle><mi>~</mi><mstyle mathvariant="monospace"><mtext>X</mtext></mstyle></math></th>
<th class="tableblock halign-center valign-top"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>i</mtext></mstyle><mi>~</mi><mstyle mathvariant="monospace"><mtext>X</mtext></mstyle></math></th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">public method</p></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>∅</mi></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>∅</mi></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>∅</mi></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>∅</mi></math></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">public writable field</p></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>∅</mi></math></p></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>∅</mi></math></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">public readable field</p></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>∅</mi></math></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">writable fields</p></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>Multiple structural typing strategies can be nested when there are multiple use sites, like in the example <a href="#ex:nested-structural-typing-strategies">Nested Structural Typing Strategies</a> below at the locations ST1 and ST2.
In the example, the datafield <code>a.field</code> has the nested structural type <code>~r~ ~i~ A</code> and thus the datafield <code>a.field.df</code> is readable.
Nested structural types are evaluated on the fly when doing subtype checks.</p>
</div>
<div id="ex:nested-structural-typing-strategies" class="exampleblock">
<div class="title">Example 51. Nested Structural Typing Strategies</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class A {
public df : string;
}
interface I&lt;T&gt; {
public field : ~r~T; // ST1
}
var a : ~i~A; // ST2</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>The following example demonstrates the effect of the structural type modifiers:</p>
</div>
<div class="exampleblock">
<div class="title">Example 52. Effect of structural type modifiers on use-site</div>
<div class="content">
<div class="paragraph">
<p>Let’s assume the type defined on the left.
The following <em>pseudo</em> code snippets explicitly list the type with its members virtually created by a structural modifier.
Note that this is pseudo code, as there are no real or virtual types created.
Instead, only the subtype relation is defined accordingly:</p>
</div>
<div class="paragraph">
<p>Effect of structural type modifiers on use-site</p>
</div>
<table class="tableblock frame-all grid-all spread">
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
<col style="width: 33.3334%;">
</colgroup>
<tbody>
<tr>
<th class="tableblock halign-center valign-top" colspan="3"><p class="tableblock">Effect of structural type modifiers on use-site</p></th>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var c:C
class C {
private x;
public y;
public f()
private g()
public get z():Z
public set z(z:Z)
}
interface I {
a;
func();
}</code></pre>
</div>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var cstructural:~C
class cstructural {
public y;
public f()
public get z():Z
public set z(z:Z)
}
interface ~I {
a;
func();
}</code></pre>
</div>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var cfields:~~C
class cfields {
public y;
public get z():Z
public set z(z:Z)
}
interface ~~I {
a;
}</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">Type</p></th>
<th class="tableblock halign-center valign-top"><p class="tableblock">Structural Type</p></th>
<th class="tableblock halign-center valign-top"><p class="tableblock">Structural Field Type</p></th>
</tr>
</tbody>
</table>
<table class="tableblock frame-all grid-all spread">
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
<col style="width: 33.3334%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var crofields:~r~C
class crofields {
public get y():Y
public get z():Z
}
interface ~r~I {
public get a():A
}</code></pre>
</div>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var cwofields:~w~C
class cwofields {
public set y(y:Y)
public set z(z:Z)
}
interface ~w~I {
public set a(a:A)
}</code></pre>
</div>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">var cinitfields:~i~C
class cinitfields {
public get y():Y
public get z():Z
}
interface ~i~I {
public get a():A
}</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">Structural Read-only Field Type</p></th>
<th class="tableblock halign-center valign-top"><p class="tableblock">Structural Write-only Field Type</p></th>
<th class="tableblock halign-center valign-top"><p class="tableblock">Structural Initializer Field Type</p></th>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>Note that even if a type is defined without the structural modifier, it is not possible to use <code>instanceof</code> for variables declared as structural, as shown in the next example:</p>
</div>
<table class="tableblock frame-all grid-all spread">
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
<col style="width: 33.3334%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {..}
interface I {..}
foo(c: C, i: I) {
c instanceof C; // ok
c instanceof I; // ok
}</code></pre>
</div>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {..}
interface I {..}
foo(c: ~C, i: ~I) {
c instanceof C; // error
c instanceof I; // error
}</code></pre>
</div>
</div></div></td>
<td class="tableblock halign-left valign-top"><div><div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {..}
interface I {..}
foo(c: ~~C, i: ~~I) {
c instanceof C; // error
C instanceof I; // error
}</code></pre>
</div>
</div></div></td>
</tr>
<tr>
<th class="tableblock halign-center valign-top"><p class="tableblock">Type</p></th>
<th class="tableblock halign-center valign-top"><p class="tableblock">Structural Type</p></th>
<th class="tableblock halign-center valign-top"><p class="tableblock">Structural Field Type</p></th>
</tr>
</tbody>
</table>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-78701"></a><strong>Req. IDE-78701:</strong> <a href="#Req-IDE-78701">Nominal and structural typing spec attributes</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>Within this spec, we define the following attributes of a type reference <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math>:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>If a type is referenced with the structural type modifier <code>~</code> , the property <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi></math> is true.</p>
</li>
<li>
<p>If a type is referenced with the structural field type modifier <code>~~</code>, the property <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>F</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi></math> is true.</p>
</li>
<li>
<p>If a type is referenced with the structural read-only field type modifier <code>~r~</code>, the property <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>R</mi><mi>e</mi><mi>a</mi><mi>d</mi><mi>O</mi><mi>n</mi><mi>l</mi><mi>y</mi><mi>F</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi></math> is true.</p>
</li>
<li>
<p>If a type is referenced with the structural write-only field type modifier <code>~w~</code>, then the property <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>W</mi><mi>r</mi><mi>i</mi><mi>t</mi><mi>e</mi><mi>O</mi><mi>n</mi><mi>l</mi><mi>y</mi><mi>F</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi></math> is true.</p>
</li>
<li>
<p>If a type is referenced with the structural initializer field type modifier <code>~i~</code>, then the property <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>I</mi><mi>n</mi><mi>i</mi><mi>t</mi><mi>F</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi></math> is true.</p>
</li>
<li>
<p>We use <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>.</mo><mi>i</mi><mi>s</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi></math> to simply refer any structural typing, i.e.+
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>.</mo><mi>i</mi><mi>s</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mo>=</mo><mi>T</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mo>∨</mo><mi>T</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>F</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mo>∨</mo></math>T.refStructuralReadOnlyField \lor T.refStructuralWriteOnlyField || T.refStructuralInitField || T.defStructural$</p>
</li>
<li>
<p>We use <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>.</mo><mi>i</mi><mi>s</mi><mi>N</mi><mi>o</mi><mi>m</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi></math> as the opposite of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>.</mo><mi>i</mi><mi>s</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi></math>, i.e.<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mo>.</mo><mi>i</mi><mi>s</mi><mi>N</mi><mi>o</mi><mi>m</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi><mo>=</mo><mo>¬</mo><mi>T</mi><mo>.</mo><mi>i</mi><mi>s</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi></math></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>We call the following:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math> the (nominal) type T,</p>
</li>
<li>
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math> the structural version of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math>,</p>
</li>
<li>
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math> the structural field version of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math>,</p>
</li>
<li>
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>r</mtext></mstyle><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math> the structural read-only field,</p>
</li>
<li>
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>w</mtext></mstyle><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math> the structural write-only field and</p>
</li>
<li>
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>i</mtext></mstyle><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math> the structural initializer field version of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math>.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-76"></a><strong>Req. IDE-76:</strong> <a href="#Req-IDE-76">Use-Site Structural Typing</a> (ver. 1)</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The structural version of a type is a supertype of the nominal type:<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>:</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math></p>
</li>
<li>
<p>The structural field version of a type is a supertype of the structural type:<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>:</mi><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math></p>
</li>
<li>
<p>The structural read-only field version of a type is a supertype of the structural field type:<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>:</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>r</mtext></mstyle><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math></p>
</li>
<li>
<p>The structural write-only field version of a type is a supertype of the structural field type:<br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>:</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>w</mtext></mstyle><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math></p>
</li>
<li>
<p>The structural (field) version of a type cannot be used on the right hand side of the <code>instanceof</code> operator:</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mi>x</mi><mi> </mi><mstyle mathvariant="monospace"><mtext>instanceof</mtext></mstyle><mi> </mi><mi>E</mi><mo>⇒</mo><mi>Γ</mi><mo>⊢</mo><mi>E</mi><mi>:</mi><mi>T</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="3.0em"/><mo>⇒</mo><mo>¬</mo><mrow><mo>(</mo><mi>T</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi></mrow></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="6.0em"/><mo>∨</mo><mi>T</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>F</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="6.0em"/><mo>∨</mo><mi>T</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>R</mi><mi>e</mi><mi>a</mi><mi>d</mi><mi>O</mi><mi>n</mi><mi>l</mi><mi>y</mi><mi>F</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0mm"/><mspace width="6.0em"/><mo>∨</mo><mi>T</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>W</mi><mi>r</mi><mi>i</mi><mi>t</mi><mi>e</mi><mi>O</mi><mi>n</mi><mi>l</mi><mi>y</mi><mi>F</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mspace width="3.0mm"/><mspace width="6.0em"/><mo>∨</mo><mi>T</mi><mo>.</mo><mi>r</mi><mi>e</mi><mi>f</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>I</mi><mi>n</mi><mi>i</mi><mi>t</mi><mi>F</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mo>)</mo></mrow></math></p>
</div>
<div class="paragraph">
<p>That is, the following code will always issue an error: <code>x instanceof ~T</code> <sup class="footnote">[<a id="_footnoteref_42" class="footnote" href="appendix_c_bibliography.html#_footnote_42" title="View footnote.">42</a>]</sup>.</p>
</div>
</li>
<li>
<p>A type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math> is a subtype of a structural version of a type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math>, if it contains all public, non-static members of the type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math>: <sup class="footnote">[<a id="_footnoteref_43" class="footnote" href="appendix_c_bibliography.html#_footnote_43" title="View footnote.">43</a>]</sup></p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mo>∀</mo><mi>m</mi><mo>∈</mo><mi>T</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>,</mo><mi>m</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo>∉</mo><mstyle mathvariant="monospace"><mtext>N4Object</mtext></mstyle><mo>,</mo><mi>m</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo>,</mo><mo>¬</mo><mi>m</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo>,</mo><mi>m</mi><mo>≠</mo><mi>T</mi><mo>.</mo><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mi>:</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="1.0em"/><mo>∃</mo><msup><mi>m</mi><mi>'</mi></msup><mo>∈</mo><mi>X</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="2.0em"/><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo>∧</mo><mo>¬</mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo>∧</mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>m</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mspace width="2.0em"/><mo>∧</mo><mi>Γ</mi><mo>⊢</mo><msup><mi>m</mi><mi>'</mi></msup><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>Γ</mi><mo>⊢</mo><mi>m</mi></mrow><mrow><mspace width="13.0em"/><mi>Γ</mi><mo>⊢</mo><mi>X</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>Γ</mi><mo>⊢</mo><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mspace width="13.0em"/></mrow></mfrac></math></p>
</div>
</li>
<li>
<p>A type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math> is a subtype of a structural field version of a type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math>, if it contains all public, non-static fields of the type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math>.
Special cases regarding optional fields are described in <a href="#optional-fields">Optional Fields</a>.</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mo>∀</mo><mi>m</mi><mo>∈</mo><mi>T</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mi>s</mi><mo>,</mo><mi>m</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo>∉</mo><mstyle mathvariant="monospace"><mtext>N4Object</mtext></mstyle><mo>,</mo><mi>m</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo>,</mo><mo>¬</mo><mi>m</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="1.0em"/><mo>∨</mo><mi> </mi><mo>∃</mo><msup><mi>m</mi><mi>'</mi></msup><mo>∈</mo><mi>X</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mi>s</mi><mi>:</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0em"/><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo>∧</mo><mo>¬</mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo>∧</mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>m</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0em"/><mo>∧</mo><mi>Γ</mi><mo>⊢</mo><msup><mi>m</mi><mi>'</mi></msup><mi>:</mi><msub><mi>T</mi><mi>m</mi></msub><mo>∧</mo><mi>Γ</mi><mo>⊢</mo><mi>m</mi><mi>:</mi><msub><mi>T</mi><msup><mi>m</mi><mi>'</mi></msup></msub><mo>∧</mo><msub><mi>T</mi><mi>m</mi></msub><mo>=</mo><msub><mi>T</mi><msup><mi>m</mi><mi>'</mi></msup></msub><mi>}</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mspace width="3.0em"/><mo>∧</mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>a</mi><mi>b</mi><mi>i</mi><mi>l</mi><mi>i</mi><mi>t</mi><mi>y</mi><mo>≥</mo><mi>m</mi><mo>.</mo><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>a</mi><mi>b</mi><mi>i</mi><mi>l</mi><mi>i</mi><mi>t</mi><mi>y</mi></mrow><mrow><mspace width="13.0em"/><mi>Γ</mi><mo>⊢</mo><mi>X</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mspace width="13.0em"/></mrow></mfrac></math></p>
</div>
</li>
<li>
<p>A type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math> is a subtype of a structural read-only field version of a type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>r</mtext></mstyle><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math>, if it contains all public and non-static readable fields of the type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math>.
Special cases regarding optional fields are described in <a href="#optional-fields">Optional Fields</a>.</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mo>∀</mo><mi>m</mi><mo>∈</mo><mi>T</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mi>s</mi><mo>∪</mo><mi>T</mi><mo>.</mo><mi>g</mi><mi>e</mi><mi>t</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>,</mo><mi>m</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo>∉</mo><mstyle mathvariant="monospace"><mtext>N4Object</mtext></mstyle><mo>,</mo><mi>m</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo>,</mo><mo>¬</mo><mi>m</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="1.0em"/><mo>∨</mo><mi> </mi><mo>∃</mo><msup><mi>m</mi><mi>'</mi></msup><mo>∈</mo><mi>X</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mi>s</mi><mo>∪</mo><mi>X</mi><mo>.</mo><mi>g</mi><mi>e</mi><mi>t</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0em"/><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo>∧</mo><mo>¬</mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo>∧</mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>m</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0em"/><mo>∧</mo><mi>Γ</mi><mo>⊢</mo><msup><mi>m</mi><mi>'</mi></msup><mi>:</mi><msub><mi>T</mi><mi>m</mi></msub><mo>∧</mo><mi>Γ</mi><mo>⊢</mo><mi>m</mi><mi>:</mi><msub><mi>T</mi><msup><mi>m</mi><mi>'</mi></msup></msub><mo>∧</mo><msub><mi>T</mi><mi>m</mi></msub><mo>=</mo><msub><mi>T</mi><msup><mi>m</mi><mi>'</mi></msup></msub></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mspace width="3.0em"/><mo>∧</mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>a</mi><mi>b</mi><mi>i</mi><mi>l</mi><mi>i</mi><mi>t</mi><mi>y</mi><mo>≥</mo><mi>m</mi><mo>.</mo><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>a</mi><mi>b</mi><mi>i</mi><mi>l</mi><mi>i</mi><mi>t</mi><mi>y</mi></mrow><mrow><mspace width="13.0em"/><mi>Γ</mi><mo>⊢</mo><mi>X</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>~</mi><mstyle mathvariant="monospace"><mtext>r</mtext></mstyle><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mspace width="13.0em"/></mrow></mfrac></math></p>
</div>
</li>
<li>
<p>A type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math> is a subtype of a structural write-only field version of a type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>w</mtext></mstyle><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math>, if it contains all public and non-static writable fields of the type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>T</mi></math>. Special cases regarding optional fields are described in <a href="#optional-fields">Optional Fields</a>.</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mo>∀</mo><mi>m</mi><mo>∈</mo><mi>T</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mi>s</mi><mo>∪</mo><mi>T</mi><mo>.</mo><mi>s</mi><mi>e</mi><mi>t</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>,</mo><mi>m</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo>∉</mo><mstyle mathvariant="monospace"><mtext>N4Object</mtext></mstyle><mo>,</mo><mi>m</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo>,</mo><mo>¬</mo><mi>m</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo>,</mo><mo>¬</mo><mi>m</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="1.0em"/><mo>∨</mo><mi> </mi><mo>∃</mo><msup><mi>m</mi><mi>'</mi></msup><mo>∈</mo><mi>X</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mi>s</mi><mo>∪</mo><mi>X</mi><mo>.</mo><mi>s</mi><mi>e</mi><mi>t</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0em"/><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo>∧</mo><mo>¬</mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo>∧</mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>m</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0em"/><mo>∧</mo><mi>Γ</mi><mo>⊢</mo><msup><mi>m</mi><mi>'</mi></msup><mi>:</mi><msub><mi>T</mi><mi>m</mi></msub><mo>∧</mo><mi>Γ</mi><mo>⊢</mo><mi>m</mi><mi>:</mi><msub><mi>T</mi><msup><mi>m</mi><mi>'</mi></msup></msub><mo>∧</mo><msub><mi>T</mi><mi>m</mi></msub><mo>=</mo><msub><mi>T</mi><msup><mi>m</mi><mi>'</mi></msup></msub></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mspace width="3.0em"/><mo>∧</mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>a</mi><mi>b</mi><mi>i</mi><mi>l</mi><mi>i</mi><mi>t</mi><mi>y</mi><mo>≥</mo><mi>m</mi><mo>.</mo><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>a</mi><mi>b</mi><mi>i</mi><mi>l</mi><mi>i</mi><mi>t</mi><mi>y</mi></mrow><mrow><mspace width="13.0em"/><mi>Γ</mi><mo>⊢</mo><mi>X</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>~</mi><mstyle mathvariant="monospace"><mtext>w</mtext></mstyle><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mspace width="13.0em"/></mrow></mfrac></math></p>
</div>
</li>
<li>
<p>A type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math> is a subtype of a structural field version of a type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>this</mtext></mstyle></math>, if it contains all public, non-static fields,
either defined via data fields or field get accessors, of the inferred type of <code>this</code>.
Special cases regarding optional fields are described in <a href="#optional-fields">Optional Fields</a>.</p>
<div class="paragraph">
<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>Γ</mi><mo>⊢</mo><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi><mi>:</mi><mi>T</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>∀</mo><mi>m</mi><mo>∈</mo><mi>T</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mi>s</mi><mo>∪</mo><mi>T</mi><mo>.</mo><mi>s</mi><mi>e</mi><mi>t</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo>,</mo><mi>m</mi><mo>.</mo><mi>o</mi><mi>w</mi><mi>n</mi><mi>e</mi><mi>r</mi><mo>∉</mo><mstyle mathvariant="monospace"><mtext>N4Object</mtext></mstyle><mo>,</mo><mi>m</mi><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo>,</mo><mo>¬</mo><mi>m</mi><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mo>∨</mo><mi>m</mi><mo>.</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo>≠</mo><mstyle mathvariant="monospace"><mtext>null</mtext></mstyle></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="1.0em"/><mo>∨</mo><mi> </mi><mo>∃</mo><msup><mi>m</mi><mi>'</mi></msup><mo>∈</mo><mi>X</mi><mo>.</mo><mi>f</mi><mi>i</mi><mi>e</mi><mi>l</mi><mi>d</mi><mi>s</mi><mo>∪</mo><mi>X</mi><mo>.</mo><mi>g</mi><mi>e</mi><mi>t</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mi>:</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mspace width="3.0em"/><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>c</mi><mi>c</mi><mo>=</mo><mstyle mathvariant="monospace"><mtext>public</mtext></mstyle><mo>∧</mo><mo>¬</mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>s</mi><mi>t</mi><mi>a</mi><mi>t</mi><mi>i</mi><mi>c</mi><mo>∧</mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>=</mo><mi>m</mi><mo>.</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></math><br>
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mrow><mspace width="3.0em"/><mo>∧</mo><mi>Γ</mi><mo>⊢</mo><msup><mi>m</mi><mi>'</mi></msup><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>m</mi><mo>∧</mo><msup><mi>m</mi><mi>'</mi></msup><mo>.</mo><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>a</mi><mi>b</mi><mi>i</mi><mi>l</mi><mi>i</mi><mi>t</mi><mi>y</mi><mo>≥</mo><mi>m</mi><mo>.</mo><mi>a</mi><mi>s</mi><mi>s</mi><mi>i</mi><mi>g</mi><mi>n</mi><mi>a</mi><mi>b</mi><mi>i</mi><mi>l</mi><mi>i</mi><mi>t</mi><mi>y</mi></mrow><mrow><mspace width="13.0em"/><mi>Γ</mi><mo>⊢</mo><mi>X</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>t</mtext></mstyle><mi>h</mi><mi>i</mi><mi>s</mi><mspace width="13.0em"/></mrow></mfrac></math></p>
</div>
</li>
<li>
<p>A structural field type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math> is a subtype of a structural type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>X</mtext></mstyle></math>, if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>X</mtext></mstyle></math> only contains fields (except methods inherited from <code>Object</code>) and if <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>X</mtext></mstyle></math>.</p>
<div class="openblock">
<div class="content">
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>X</mi><mo>.</mo><mi>m</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><mi>s</mi><mo>∖</mo><mstyle mathvariant="monospace"><mtext>Object</mtext></mstyle><mo>.</mo><mi>m</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>o</mi><mi>d</mi><mi>s</mi><mo>=</mo><mi>∅</mi><mo>∧</mo><mi>Γ</mi><mo>⊢</mo><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>X</mtext></mstyle></mrow><mrow><mi>Γ</mi><mo>⊢</mo><mi>~</mi><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>~</mi><mstyle mathvariant="monospace"><mtext>X</mtext></mstyle></mrow></mfrac></math>
</div>
</div>
</li>
<li>
<p>Use-site structural typing cannot be used for declaring supertypes of classes or interfaces.
That is to say that structural types cannot be used after <code>extends</code>, <code>implements</code> or <code>with</code> in type declarations <sup class="footnote">[<a id="_footnoteref_44" class="footnote" href="appendix_c_bibliography.html#_footnote_44" title="View footnote.">44</a>]</sup>.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="paragraph">
<p>Note that all members of <code>N4Object</code> are excluded.
This implies that extended reflective features (cf. <a href="conversions_and_reflection.html#_reflection-meta-information">Reflection meta-information</a> ) are not available in the context of structural typing.
The <code>instanceof</code> operator is still working as described in <a href="expressions.html#_relational-expression">Relational Expression</a>, in that it can be used to check the type of an instance.</p>
</div>
<div class="paragraph">
<p>If a type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math> is a (nominal) subtype of T, it is, of course, also a subtype of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></math>:</p>
</div>
<div class="openblock">
<div class="content">
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi>Γ</mi><mo>⊢</mo><mi>X</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>Γ</mi><mo>⊢</mo><mi>T</mi></mrow><mrow><mi>Γ</mi><mo>⊢</mo><mi>X</mi><mspace width="1.0mm"/><mo>&lt;</mo><mstyle mathvariant="monospace"><mtext>:</mtext></mstyle><mspace width="1.0mm"/><mi>Γ</mi><mo>⊢</mo><mi>~</mi><mstyle mathvariant="monospace"><mtext>T</mtext></mstyle></mrow></mfrac></math>
</div>
</div>
<div class="paragraph">
<p>This is only a shortcut for the type inference defined above.</p>
</div>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-77"></a><strong>Req. IDE-77:</strong> <a href="#Req-IDE-77">Definition and Use-Site Precedence</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>If a type is structurally typed on both definition and use-site, the rules for
use-site structural typing (<a href="#Req-IDE-76">[Req-IDE-76]</a>) are
applied.</p>
</div>
<div class="exampleblock">
<div class="title">Example 53. Use-Site Structural Typing</div>
<div class="content">
<div class="paragraph">
<p>The following example demonstrates the effect of the structural (field) modifier, used in this case for function parameters.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">interface I { public x: number; public foo()};
class C { public x: number; public foo() {}};
function n(p: I) {}
function f(p: ~I) {}
function g(p: ~~I) {}
n(new C()); // error: nominal typing, C does not implement I
f(new C()); // ok: C is a (structural) subtype of ~I
f({x:10}); // error, the object literal does not provide function foo()
g({x:10}); // ok: object literal provides all fields of I</code></pre>
</div>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 54. Structural types variable and instanceof operator</div>
<div class="content">
<div class="paragraph">
<p>It is possible to use a variable typed with a structural version of a type on the left hand side of the <code>instanceof</code> operator, as demonstrated in this example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {
public x;
public betterX() { return this.x||1;}
}
function f(p: ~~C) {
if (p instanceof C) {
console.log((p as C).betterX);
} else {
console.log(p.x||1);
}
}</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>The following table describes the member availability of <code>X</code> in various
typing scenarios. Such as <code>~~X</code>, <code>~r~X</code>, <code>~w~X</code>, <code>~i~X</code>.</p>
</div>
<table class="tableblock frame-all grid-all spread language-n4js">
<caption class="title">Table 10. Member Availability in various Typing Scenarios</caption>
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 16.6666%;">
<col style="width: 16.6666%;">
<col style="width: 16.6666%;">
<col style="width: 16.6669%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Member of type <em>X</em></th>
<th class="tableblock halign-center valign-top"><code>~~X</code></th>
<th class="tableblock halign-center valign-top"><code>~r~X</code></th>
<th class="tableblock halign-center valign-top"><code>~w~X</code></th>
<th class="tableblock halign-center valign-top"><code>~i~X</code></th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>private m0;</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">&#8201;&#8212;&#8201;</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">&#8201;&#8212;&#8201;</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">&#8201;&#8212;&#8201;</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">&#8201;&#8212;&#8201;</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>public set m1(m) { }</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">write</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">&#8201;&#8212;&#8201;</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">write</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>public get m2() {&#8230;&#8203;}</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">&#8201;&#8212;&#8201;</p></td>
<td class="tableblock halign-center valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>public m3;</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read/write</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">write</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>public m4 = 'init.m4';</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read/write</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">write</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read <em>?</em></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>public m5: any?;</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read<em>?</em>/write</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read<em>?</em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">write</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>?</mi></math></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@Final public m6 = 'init.m6';</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read</p></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>@Final public m7;</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read</p></td>
<td class="tableblock halign-center valign-top"></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">read</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>public get m8() {&#8230;&#8203;}</code></p></td>
<td class="tableblock halign-center valign-middle" rowspan="2"><p class="tableblock">read/write</p></td>
<td class="tableblock halign-center valign-middle" rowspan="2"><p class="tableblock">read</p></td>
<td class="tableblock halign-center valign-middle" rowspan="2"><p class="tableblock">write</p></td>
<td class="tableblock halign-center valign-middle" rowspan="2"><p class="tableblock">read</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>public set m8(m) { }</code></p></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="structural-readWriteInit-field-typing"><a class="anchor" href="#structural-readWriteInit-field-typing"></a><a class="link" href="#structural-readWriteInit-field-typing">5.3.4. Structural Read-only, Write-only and Initializer Field Typing</a></h4>
<div class="paragraph">
<p>Structural read-only, write-only and initializer field typings are extensions of structural field typing.
Everything that is defined for the field structural typing must comply with these extension field typings.
For the read-only type, readable fields (mutable and <code>@Final</code> ones) and setters are considered, for the write-only type; only the setters and mutable fields are considered.
Due to the hybrid nature of the initializer type it can act two different ways.
To be more precise, a type <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math> (structural initializer field type) is a supertype of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>Y</mi></math> (structural initializer field type) if for each public, non-static, non-optional writable field (mutable data field of setter) of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math>, there is a corresponding, public, non-static readable field of <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>Y</mi></math>.
All public member fields with <code>@Final</code> annotation are considered to be mandatory in the initializer field typing <code>@Spec</code> constructors.
The already-initialized <code>@Final</code> fields can be either omitted from, or can be re-initialized via, an initializer field typing <code>@Spec</code> style constructor.</p>
</div>
<div class="exampleblock">
<div class="title">Example 55. Subtype relationship between structural field typing</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class A1 {
public s: string;
}
class A2 {
public set s(s: string) { }
public get s(): string { return null; }
}
class A3 {
@Final public s: string = null;
}
class A4 {
public get s(): string { return null; }
}
class A5 {
public set s(s: string) { }
}</code></pre>
</div>
</div>
<div style="page-break-after: always;"></div>
<table class="tableblock frame-all grid-all spread small">
<colgroup>
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2631%;">
<col style="width: 5.2642%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">A1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~A1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~~A1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A2</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A3</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A4</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A5</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~w~A1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~w~A2</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~w~A3</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~w~A4</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~w~A5</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~i~A1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~i~A2</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~i~A3</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A4</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A5</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">A1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~A1</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~~A1</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A1</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A2</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A3</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A4</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A5</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~w~A1</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~w~A2</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~w~A3</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~w~A4</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~w~A5</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~i~A1</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~i~A2</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~i~A3</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A4</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">~r~A5</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">✓</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_public-setter-annotated-with-code-providesinitializer-code"><a class="anchor" href="#_public-setter-annotated-with-code-providesinitializer-code"></a><a class="link" href="#_public-setter-annotated-with-code-providesinitializer-code">5.3.5. Public Setter Annotated With <code>ProvidesInitializer</code></a></h4>
<div class="paragraph">
<p>Public setters with <code>ProvidesInitializer</code> annotation can declare optional fields implemented by means of field accessors instead of data fields.
Data fields with an initializer are treated as optional in the initializer field type.</p>
</div>
<div class="paragraph">
<p>It is important to note that it is valid to use the <code>ProvidesInitializer</code> annotation for setters in <code>n4js</code> files and not only definition files.</p>
</div>
<div class="exampleblock">
<div class="title">Example 56. Setters with <code>@ProvidesInitializer</code> treated as optional</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {
private _y: int = 1;
public get y() { return this._y; }
@ProvidesInitializer
public set y(v: int) { this._y = v; }
public constructor(@Spec spec: ~i~this) { }
}
console.log(new C({}).y); // 1
console.log(new C({y: 42}).y); //24</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_structural-types-with-optional-fields"><a class="anchor" href="#_structural-types-with-optional-fields"></a><a class="link" href="#_structural-types-with-optional-fields">5.3.6. Structural Types With Optional Fields</a></h4>
<div class="paragraph">
<p>Public optional fields become a member of the structural (field) type as
well. To ensure the overall type safety, the semantics of optionality (e.g. on or off) depends on the context, in which the optional fields are currently being used (See <a href="#optional-fields">Optional Fields</a>).</p>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_structural-types-with-access-modifier"><a class="anchor" href="#_structural-types-with-access-modifier"></a><a class="link" href="#_structural-types-with-access-modifier">5.3.7. Structural Types With Access Modifier</a></h4>
<div class="paragraph">
<p>The access modifier of the subtype have to provide equal or higher
visibility.</p>
</div>
<div class="exampleblock">
<div class="title">Example 57. Access modifier in structural typing</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {
public s: number;
}
class D {
project s: number;
}
function f(c: ~C) {}
f(new D()); // error: D is no (structural) subtype of ~C, as visibility of s in D is lower
function g(d: ~D) {}
g(new C()); // ok: C is a (structural) subtype of ~D, as visibility of s in C is greater-than-or-equal to s in D</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect3 language-n4js">
<h4 id="_structural-types-with-additional-members"><a class="anchor" href="#_structural-types-with-additional-members"></a><a class="link" href="#_structural-types-with-additional-members">5.3.8. Structural Types With Additional Members</a></h4>
<div class="paragraph">
<p>It is possible to add additional members when structurally referencing a
declared type.</p>
</div>
<div class="sect4">
<h5 id="_syntax-8"><a class="anchor" href="#_syntax-8"></a><a class="link" href="#_syntax-8">5.3.8.1. Syntax</a></h5>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-xtext" data-lang="xtext">TStructMember:
TStructGetter | TStructGetterES4 | TStructSetter | TStructMethod | TStructMethodES4 | TStructField;
TStructMethod:
=&gt;
({TStructMethod} ('&lt;' typeVars+=TypeVariable (',' typeVars+=TypeVariable)* '&gt;')?
returnTypeRef=TypeRef name=TypesIdentifier '(')
(fpars+=TAnonymousFormalParameter (',' fpars+=TAnonymousFormalParameter)*)? ')'
';'?;
TStructMethodES4 returns TStructMethod:
=&gt;
({TStructMethod} ('&lt;' typeVars+=TypeVariable (',' typeVars+=TypeVariable)* '&gt;')?
name=TypesIdentifier '(')
(fpars+=TAnonymousFormalParameter (',' fpars+=TAnonymousFormalParameter)*)? ')'
(':' returnTypeRef=TypeRef)?
';'?;
TStructField:
(
typeRef=TypeRef name=TypesIdentifier
| name=TypesIdentifier (':' typeRef=TypeRef)?
)
';';
TStructGetter:
=&gt; ({TStructGetter}
declaredTypeRef=TypeRef
'get'
name=TypesIdentifier)
'(' ')' ';'?;
TStructGetterES4 returns TStructGetter:
=&gt; ({TStructGetter}
'get'
name=TypesIdentifier)
'(' ')' (':' declaredTypeRef=TypeRef)? ';'?;
TStructSetter:
=&gt; ({TStructSetter}
'set'
name=TypesIdentifier)
'(' fpar=TAnonymousFormalParameter ')' ';'?;
TAnonymousFormalParameter:
typeRef=TypeRef variadic?='...'? name=TIdentifier?
| variadic?='...'? (=&gt; name=TIdentifier ':') typeRef=TypeRef;</code></pre>
</div>
</div>
<div class="sect5">
<h6 id="_semantics-6"><a class="anchor" href="#_semantics-6"></a><a class="link" href="#_semantics-6">5.3.8.1.1. Semantics</a></h6>
<div class="openblock requirement">
<div class="content">
<div class="paragraph">
<p><a id="Req-IDE-78"></a><strong>Req. IDE-78:</strong> <a href="#Req-IDE-78">Additional structural members</a> (ver. 1)</p>
</div>
<div class="paragraph">
<p>It is only possible to add additional members to a type if use-site structural typing is used.</p>
</div>
<div class="paragraph">
<p>The following constraints must hold:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>For all additional members defined in a structural type reference, the constraints for member overriding (<a href="#Req-IDE-72">[Req-IDE-72]</a>) apply as well.</p>
</li>
<li>
<p>All additional members have the access modifier set to <code>public</code>.</p>
</li>
<li>
<p>Type variables must not be augmented with additional structural members.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>Additional fields may be declared optional in the same way as fields in classes.
The rules given in <a href="#_structural-types-with-optional-fields">Structural Types With Optional Fields</a> apply accordingly.
Consider the following example:</p>
</div>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 58. Additional optional members in structural typing</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">class C {
public f1: number;
}
var c1: ~C with { f3: string; } c1;
var c2: ~C with { f3: string?; } c2;
c1 = { f1:42 }; // error: "~Object with { number f1; } is not a subtype of ~C with { string f3; }."
c2 = { f1:42 }; // ok!!</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>Augmenting a type variable T with additional structural members can cause collisions with another member of a type argument for T.
Hence, type variables must not be augmented with additional structural members like in the following example.</p>
</div>
<div class="exampleblock">
<div class="title">Example 59. Forbidden additional structural members on type variables</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">interface I&lt;T&gt; {
public field : ~T with {prop : int} // error "No additional structural members allowed on type variables."
}</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>Using an additional structural member on a type variable T could be seen as a constraint to T.
However, constraints like this should be rather stated using an explicit interface like in the example below.</p>
</div>
<div class="exampleblock">
<div class="title">Example 60. Use explicitly defined Interfaces</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-n4js" data-lang="n4js">interface ~J {
prop : int;
}
interface II&lt;T extends J&gt; {
}</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div id="footer">
<div id="footer-text">
Version 0.9<br>
Last updated 2019-08-08 13:15:33 CEST
</div>
</div>
<!-- ************* docinfo-footer *************************************************************** -->
<div class="Grid social" style="color:#d5dfea">
<div class="Cell Cell--2-12 m-Cell--withMargin">
<h2>Quick Links</h2>
<ul>
<li><a href="../downloads.html">Download</a></li>
<li><a href="../userguides/index.html">Documentation</a></li>
<li><a href="https://github.com/eclipse/n4js/">Source</a></li>
<li><a href="https://github.com/eclipse/n4js/issues">Issues</a></li>
</ul>
</div>
<div class="Cell Cell--2-12 m-Cell--withMargin">
<br/><br/>
<ul>
<li><a href="https://www.eclipse.org/forums/index.php/f/365/">Forum</a></li>
<li><a href="http://n4js.blogspot.de/">Blog</a></li>
<li><a href="https://dev.eclipse.org/mailman/listinfo/n4js-dev">Mailing List</a></li>
<li><a href="https://projects.eclipse.org/projects/technology.n4js">Eclipse Project Page</a></li>
<li><a href="https://twitter.com/n4jsdev">Tweets by n4jsdev</a></li>
</ul>
</div>
<div class="Cell Cell--2-12 m-Cell--withMargin">
<br/><br/>
<ul>
<li><a href="http://www.eclipse.org/">Eclipse Home</a></li>
<li><a href="http://www.eclipse.org/legal/privacy.php">Privacy Policy</a></li>
<li><a href="http://www.eclipse.org/legal/termsofuse.php">Terms of Use</a></li>
<li><a href="http://www.eclipse.org/legal/copyright.php">Copyright Agent</a></li>
<li><a href="http://www.eclipse.org/legal/">Legal</a></li>
</ul>
</div>
<div style="clear: both; height: 0; overflow: hidden;"></div>
</div>
<!-- ************* UI Scripts ************* -->
<script type="text/javascript" src="scripts/back-to-top.js"></script>
<script type="text/javascript" src="scripts/treeview.js"></script>
<script type="text/javascript" src="scripts/toc.js"></script>
<!-- ************* Prism.js Syntax Highlighting ************* -->
<script src="scripts/prism.js"></script>
<script type="text/javascript">
// Add the 'toclist' id for search function
$(".toc2 > ul").attr('id', 'toclist');
// Generate a Search input form
$("#toclist > li:first-of-type").before('<input type="text" id="pagesearch" onkeyup="search()" placeholder="Search for section...">');
$("#toclist > li:first-of-type").before('<i id="clear" class="fa fa-times-circle-o"></i>');
$("#clear").click(function(){
$("#pagesearch").val('');
search();
$('.toc2 > ul').treeView('collapseAll');
});
// intialize Treeview.js
$(".toc2 > ul").treeView();
// Initialize Scrollspy
</script>
<!-- ************* docinfo-footer *************************************************************** -->
</body>
</html>