<c- Copyright (c) 99-2025 by4he D Language Foundation All Rights Reserved. https:]ådlang.org/foundation_overview.html --> Expressions - D Programming Language<zËitle> <link rel="stylesheet" href="..ussuodemirror.css"> <link rel="stylesheet" href="../css/style.css"> <link rel="stylesheet" href="../css/print.css" media="print"> <link rel="shortcut icon" href="..u‹avicon.ico"> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.1, maximum-scale=10.0"> </head> <body id='Expressions' class='doc'> <script type="textv®avascript">document.body.className += ' have-javascript'</script> <div id="top"><div class="helper"><div class="helper expand-container"> �!�!div class="logo"><a href=".."><img id="logo" alt="D Logo" src="../imagesu(logo.svg"></a></div> �!�!a href="..wÛenu.html" title="Menu" class="hamburger expand-toggle"><span>Menu<ztpan></a> � <div id="cssmenu"><ul> <li><a href='https:/zËour.dlang.org'><span>Learn</span><tæ><vîi> �!�!li class='expand-container'><a class='expand-toggle' href='../documentation.html'><span>Documentation<ztpan></a> � <ul class='expand-content'> �!�!li><a href='..ztpec/spec.html'>Language Reference<tæ><vîi> �!�!li><a href='..y:hobos/index.html'>Library Reference<tæ><vîi> �!�!li><a href='..u(md.html'>Command-line Reference<tæ><vîi> �!�!li class="menu-divider"><a href='../comparison.html'>Feature Overview<tæ><vîi> �!�!li><a href='..tærticles.html'>Articles</a></li> �!�!zùl></li> <li><a href='../download.html'><span>Downloads</span><tæ><vîi> �!�!li><a href='https:]åcode.dlang.org'><span>Packages</span><tæ><vîi> �!�!li class='expand-container'><a class='expand-toggle' href='../community.html'><span>Community<ztpan></a> � <ul class='expand-content'> �!�!li><a href='https:]ådlang.org/blog'>Blog</a></li> <li><a href='../orgs-using-d.html'>Orgs using D<tæ><vîi> �!�!li><a href='https:]åtwitter.com/search?q=%�(y>�)dlang'>Twitter</a></li> <li class="menu-divider"><a href='https:]ådiscord.gguMZk9Q4'>Discord (community-run)<tæ><vîi> �!�!li><a href='https:]åforum.dlang.org'>Forums<tæ><vîi> �!�!li><a href='irc:]åirc.libera.chat/d'>IRC</a></li> <li><a href='https:/|øiki.dlang.org'>Wiki<tæ><vîi> �!�!li class="menu-divider"><a href='../bugstats.html'>Issues<tæ><vîi> �!�!li><a href='..uontributing.html'>Contributing</a></li> <li class="menu-divider"><a href='..u‹oundation/contributors.html'>Contributors<tæ><vîi> �!�!li><a href='..u‹oundation/index.html'>Foundation</a></li> <li><a href='../ztecurity.html'>Security Team<tæ><vîi> �!�!li class="menu-divider"><a href='https:/zttore.dlang.org[€>Store</a></li> <li><a href='../foundationu(onate.html'>Donate</a></li> <li><a href='../foundationztponsors.html'>Sponsors</a></li> �!�!zùl></li> <li class='expand-container'><a class='expand-toggle' href='..y¾esources.html'><span>Resources</span><tæ> <ul class='expand-content'> <li><a href='https:/zËour.dlang.org'>Tour<tæ><vîi> �!�!li><a href='https:]åwiki.dlang.orge‡ooks'>Books<tæ><vîi> �!�!li><a href='https:]åwiki.dlang.orgl4utorials'>Tutorials<tæ><vîi> �!�!li class="menu-divider"><a href='https:/|øiki.dlang.org/Development_tools'>Tools</a></li> <li><a href='https:/|øiki.dlang.org/Editors'>Editors</a></li> <li><a href='https:/|øiki.dlang.org/IDEs'>IDEs</a></li> <li><a href='https:/y¾un.dlang.io'>run.dlang.io<tæ><vîi> �!�!li><a href='http:/y¾ainers.github.io|sisuald|sisualdltartPage.html'>Visual D<tæ><vîi> �!�!li class="menu-divider"><a href='../acknowledgements.html'>Acknowledgments</a></li> <li><a href='../dstyle.html'>D Style</a></li> <li><a href='../specu’lossary.html'>Glossary</a></li> <li><a href='../sitemap.html'>Sitemap<tæ><vîi> </ul><vîi> <zùl></div> �!�!div class="search-container expand-container"> <a href="../search.html" class="expand-toggle"�4itle="Search"><span>Search</span><tæ> �!�!div id="search-box"> <form method="get" action="https:/u’oogle.com/search"> �!�!input type="hidden" id="domains" name="domains" value="dlang.org"> �!�!input type="hidden" id="sourceid" name="sourceid" value="google-search"> �!�!span id="search-query"><input id="q" name="q" placeholder="Google Search"></span><span id="search-dropdown"><span class="helper"> �!�!select id="sitesearch" name="sitesearch" size="1"> �!�!option�6alue="dlang.org">Entire Site</option> <option selected�6alue="dlang.org/spec">Language</option> <option �6alue="dlang.org/phobos">Library<wóption> �!�!option value="forum.dlang.org">Forums</option> �!�!ztelect> �!�!ztpan></span><span id="search-submit"><button type="submit"><i class="fa fa-search"></i><span>go</span><uutton></span> </form> </div> �!�!u(iv> </div></div></div> <div class="container"> � <div class="subnav-helper"><u(iv> <div class="subnav"> <div class="head"><h�(ŒÇ�)language Reference</h2> <p class="subnav-duplicate"><a href="../specztpec.html">table of contents<tæ><y:><u(iv> <ul> <li><a href=' ../specv}ntro.html'>Introduction<tæ><vîi><li><a href=' ../specvîex.html'>Lexical</a></li><li><a href=' ..ztpec/istring.html'>Interpolation Expression Sequence<tæ><vîi><li><a href=' ../specu’rammar.html'>Grammar</a></li><li><a href=' ..ztpec/module.html'>Modules</a></li><li><a href=' ..ztpec/declaration.html'>Declarations</a></li><li><a href=' ..ztpec/type.html'>Types</a></li><li><a href=' ..ztpec/property.html'>Properties<tæ><vîi><li><a href=' ../spectættribute.html'>Attributes<tæ><vîi><li><a href=' ../specy:ragma.html'>Pragmas<tæ><vîi><li><a href=' ../specu0xpression.html'>Expressions<tæ><vîi><li><a href=' ../speczttatement.html'>Statements<tæ><vîi><li><a href=' ../spectærrays.html'>Arrays</a></li><li><a href=' ..ztpec/hash-map.html'>Associative Arrays<tæ><vîi><li><a href=' ../speczttruct.html'>Structs and Unions</a></li><li><a href=' ..ztpec/class.html'>Classes<tæ><vîi><li><a href=' ../specv}nterface.html'>Interfaces<tæ><vîi><li><a href=' ../specu0num.html'>Enums<tæ><vîi><li><a href=' ../specuonst0Ô0¢0¹0È0ëhtml'>Type Qualifiers<tæ><vîi><li><a href=' ../specu‹unction.html'>Functions<tæ><vîi><li><a href=' ../specwóperatoroverloading.html'>Operator Overloading<tæ><vîi><li><a href=' ../speczËemplate.html'>Templates<tæ><vîi><li><a href=' ../speczËemplate-mixin.html'>Template Mixins<tæ><vîi><li><a href=' ../specuontracts.html'>Contract Programming<tæ><vîi><li><a href=' ../spec|sersion.html'>Conditional Compilation</a></li><li><a href=' ..ztpec/traits.html'>Traits<tæ><vîi><li><a href=' ../specu0rrors.html'>Error Handling</a></li><li><a href=' ..ztpec/unittest.html'>Unit Tests<tæ><vîi><li><a href=' ../specu’arbage.html'>Garbage Collection<tæ><vîi><li><a href=' ../specu‹loat.html'>Floating Point<tæ><vîi><li><a href=' ../specv}asm.html'>D x86 Inline Assembler</a></li><li><a href=' ..ztpec/ddoc.html'>Embedded Documentation<tæ><vîi><li><a href=' ../specv}nterfaceToC.html'>Interfacing to C</a></li><li><a href=' ..ztpec/cpp_interface.html'>Interfacing to C++</a></li><li><a href=' ..ztpec/objc_interface.html'>Interfacing�4o Objective-C<tæ><vîi><li><a href=' ../specy:ortability.html'>Portability�?�?uide</a></li><li><a href=' ..ztpec/entity.html'>Named Character Entities<tæ><vîi><li><a href=' ../specwÛemory-safe-d.html'>Memory Safety</a></li><li><a href=' ..ztpec/abi.html'>Application Binary�!�?nterface</a></li><li><a href=' ..ztpec/simd.html'>Vector Extensions</a></li><li><a href=' ..ztpec/betterc.html'>Better C</a></li><li><a href=' ..ztpec/importc.html'>ImportC<tæ><vîi><li><a href=' ../specwób.html'>Live Functions</a></li><li><a href=' ..ztpec/windows.html'>Windows Programming<tæ><vîi><li><a href=' ../specu’lossary.html'>Glossary</a></li><li><a href=' ..ztpec/legacy.html'>Legacy Code �!�!tæ><vîi> �!�!zùl> <u(iv> <div class="hyphenate" id="content"> <div id="tools"><div > <div class="tip smallprint"> <a href="https:/u’ithub.com/dlang/dlang.org/issueswâew?title=%5BExpressions%5D%20&label=Severity:Enhancement">Report a bug</a> <div > If�9ou spot a problem with�4his page, click here�4o create a Bugzilla issue. <u(iv> <u(iv> <div class="tip smallprint"> <a href="https:]ågithub.comu(langu(lang.orgu0dit/masterztpec/expression.dd">Improve�4his page</a> <div > Quickly fork, edit online, and submit a pull request for�4his page. Requires a signed-in GitHub account. This�7orks�7ell for small changes. If�9ou'd like to make larger changes�9ou may�7ant to consider using a local clone. </div> </div> <u(iv><u(iv> <h´expressions<vv´ � <style> body { counter-reset: h1 �; counter-increment: h1 -1; } h1" counter-reset: h2 h0µ0ó0Á0ü0àp;�( h�(N��){ counter-reset: h0µ0ó0Á0ü0àh4�0; } h3" counter-reset: h4�0; } h4" counter-reset: p;�( h°:before { counter-increment: h± content: counter(h1) ". "; } .hyphenate h�(T|�):before { counter-increment: h�([f�) content: counter(h1) "." counter(h2) " "; �( h0Ú0ó0¹:before { counter-increment: h0Ú0ü0¸ content: counter(h1) "." counter(h2) "." counter(h3) " "; �( h4::before { counter-increment: h4; content: counter(h1) "." counter(h2) "." counter(h3) "." counter(h4) " "; �( �0::before, .spec-boxes::before { counter-increment:�0; content: counter(p) ". "; } h1::before, h2::before, h3::before, h4::before, p::before, .spec-boxes::before " color: #999; font-size: 80%; margin-right: 0.�(ry�)em; } </style> �!�!div class="blankline"></div> <div class="blankline"></div> <div class="blankline"></div> <div class="page-contents�iuickindex"> �!�!div class="page-contents-header"> �!�!b>Contents</b> �!�!u(iv> <ol> <li><a href="#expression">Expressions<tæ><vîi> �!�!li><a href="#definitions-and-terms">Definitions and Terms<tæ><ol> �!�!li><a href="#.define-full-expression">Full Expression<tæ><vîi> �!�!li><a href="#.define-lvalue">Lvalue<tæ><vîi> �!�!li><a href="#.define-rvalue">Rvalue<tæ><vîi> �!�!li><a href="#.define-smallest-short-circuit">Smallest Short-Circuit Expression</a></li> </ol><vîi> �!�!li><a href="#order-af-evaluation">Order Of Evaluation<tæ><ol> �!�!li><a href="#order-increment">Increment and Decrement<tæ><vîi> �!�!li><a href="#order-binary">Binary Expressions<tæ><vîi> �!�!li><a href="#order-conditional">Conditional Expressions<tæ><vîi> �!�!li><a href="#order-calls">Function Calls</a></li> </ol><vîi> �!�!li><a href="#temporary-lifetime">Lifetime of Temporaries</a></li> <li><a href="#comma_expression">Comma Expression</a></li> <li><a href="#assign_expressions">Assign Expressions</a><ol> <li><a href="#simple_assignment_expressions">Simple Assignment Expression<tæ><vîi> �!�!li><a href="#assignment_operator_expressions">Assignment Operator Expressions<tæ><vîi> �!�!wól></li> <li><a href="#conditional_expressions">Conditional Expressions</a></li> <li><a href="#logical_expressions">Logical Expressions</a><ol> <li><a href="#oror_expressions">OrOr Expressions</a></li> <li><a href="#andand_expressions">AndAnd Expressions</a></li> </ol><vîi> �!�!li><a href="#bitwise_expressions">Bitwise Expressions<tæ><ol> �!�!li><a href="#or_expressions">Or Expressions<tæ><vîi> �!�!li><a href="#xor_expressions">Xor Expressions<tæ><vîi> �!�!li><a href="#and_expressions">And Expressions<tæ><vîi> �!�!wól></li> <li><a href="#compare_expressions">Compare Expressions</a><ol> <li><a href="#equality_expressions">Equality Expressions</a></li> <li><a href="#identity_expressions">Identity Expressions</a></li> <li><a href="#relation_expressions">Relational Expressions</a></li> <li><a href="#array_comparisons">Array Comparisons</a></li> <li><a href="#integer_comparisons">Integer Comparisons</a></li> <li><a href="#floating-point-comparisons">Floating Point Comparisons</a></li> <li><a href="#class-comparisons">Class and Struct Comparisons<tæ><vîi> �!�!wól></li> <li><a href="#in_expressions">In Expressions</a></li> <li><a href="#shift_expressions">Shift Expressions</a></li> <li><a href="#additive_expressions">Additive Expressions</a><ol> <li><a href="#add_expressions">Add Expressions</a></li> <li><a href="#cat_expressions">Cat Expressions</a></li> </ol><vîi> �!�!li><a href="#mul_expressions">Mul Expressions<tæ><ol> �!�!li><a href="#division">Division<tæ><vîi> �!�!li><a href="#mul_floating">Floating Point<tæ><vîi> �!�!wól></li> <li><a href="#unary-expression">Unary Expressions<tæ><ol> �!�!li><a href="#complement_expressions">Complement Expressions<tæ><vîi> �!�!li><a href="#delete_expressions">Delete Expressions<tæ><vîi> �!�!li><a href="#cast_expressions">Cast Expressions<tæ><vîi> �!�!wól></li> <li><a href="#throw_expression">Throw Expression</a></li> <li><a href="#pow_expressions">Pow Expressions</a></li> <li><a href="#postfix_expressions">Postfix Expressions</a><ol> <li><a href="#argument-list">Postfix Argument Lists<tæ><vîi> �!�!li><a href="#index_expressions">Index Operations</a></li> <li><a href="#slice_expressions">Slice Operations<tæ><vîi> �!�!wól></li> <li><a href="#primary_expressions">Primary Expressions</a><ol> <li><a href="#this">this</a></li> <li><a href="#super">super</a></li> <li><a href="#null">null</a></li> <li><a href="#string_literals">String Literals</a></li> <li><a href="#hex_string_literals">Hex String Literals</a></li> <li><a href="#array_literals">Array Literals</a></li> <li><a href="#associative_array_literals">Associative Array Literals</a></li> <li><a href="#function_literals">Function Literals</a></li> <li><a href="#uniform_construction_syntax">Uniform construction syntax for built-in scalar�4ypes</a></li> <li><a href="#assert_expressions">Assert Expressions</a></li> <li><a href="#mixin_expressions">Mixin Expressions</a></li> <li><a href="#import_expressions">Import Expressions</a></li> <li><a href="#new_expressions">New Expressions</a></li> <li><a href="#typeid_expressions">Typeid Expressions</a></li> <li><a href="#is_expression">Is Expressions<tæ><vîi> �!�!li><a href="#RvalueExpression">Rvalue Expression</a></li> <li><a href="#specialkeywords">Special Keywords<tæ><vîi> �!�!wól></li> <li><a href="#associativity">Associativity and Commutativity</a></li> </ol> </div> <div class="blankline"></div> <h2><span id="Expression"><a class="anchor"�4itle="Permalink to�4his section" id="expression" href="#expression">Expressions<tæ><ztpan></h2> <div class="blankline"><u(iv> <pre class="bnf notranslate"><a id="Expression"><span class="gname">Expression</span><tæ>: �!�!a href="#CommaExpression"><i>CommaExpression</i></a> <y:re> <div class="blankline"><u(iv> <p>An expression is a sequence of operators and operands�4hat specifies an evaluation. The syntax, order of evaluation, and semantics of expressions are as follows.</p> <div class="blankline"></div> �!�!p>Expressions are used�4o compute values�7ith a resulting type. These values can�4hen be assigned, �4ested, or ignored. Expressions can also have side effects. �!�!y:> <div class="blankline"><u(iv> <h�(ŒÇ�)<a class="anchor" title="Permalink�4o this section" id="definitions-and-terms" href="#definitions-and-terms">Definitions and Terms</a></h2> <div class="blankline"><u(iv> <h0Ü0ë0È<a class="anchor" title="Permalink�4o this section" id=".define-full-expression" href="#.define-full-expression">Full Expression</a></h3> <div class="blankline"><u(iv> <p>For any expression <i>expr<v}>,�4he full expression of <i>expr<v}> is defined as follows.�!�?f <i>expr<v}> parses as a subexpression of another expression <i>expr<sub>²ztub><v}>,�4hen the full expression of�!�!i>expr</i> is the full expression of�!�!i>expr<sub>1</sub></i>. Otherwise,�!�!i>expr</i> is its own full expression.</p> <div class="blankline"></div> <p>Each expression has a unique full expression. Example:</p> <div class="blankline"></div> <pre class="d_code notranslate"><span class="d_keyword">return<ztpan> f() + g() * 2; </pre> <div class="blankline"></div> <p>The full expression of�!�!span class="d_inlinecode donthyphenate notranslate">g() * �(vã�)ztpan> above is <span class="d_inlinecode donthyphenate notranslate">f() + g() * 2</span>, but not�4he full expression of <span class="d_inlinecode donthyphenate notranslate">f() + g()</span> because the latter is not parsed as a subexpression.</p> <div class="blankline"></div> <p>Note: Although�4he definition is straightforward, a few subtleties exist related�4o function literals:</p> <div class="blankline"></div> <pre class="d_code notranslate"><span class="d_keyword">return<ztpan> (() =>�8 + f())() * g(); <y:re> <div class="blankline"><u(iv> <p>The full expression of <span class="d_inlinecode donthyphenate notranslate">f()</span> above is�!�!span class="d_inlinecode donthyphenate notranslate">x + f()<ztpan>, not the expression�0assed to�!�!span class="d_inlinecode donthyphenate notranslate">return</span>. This is because�4he�0arent of�!�!span class="d_inlinecode donthyphenate notranslate">x + f()<ztpan> has function literal type, not expression�4ype.</p> <div class="blankline"></div> <h3><a class="anchor"�4itle="Permalink to�4his section" id=".define-lvalue" href="#.define-lvalue">Lvalue</a></h3> <div class="blankline"><u(iv> <p>The following expressions, and no others, are called <em>lvalue expressions</em> or�!�!em>lvalues</em>:</p> <ol><li><a href="#this"><span class="d_inlinecode donthyphenate notranslate">this</span><tæ> inside�!�!span class="d_inlinecode donthyphenate notranslate">struct</span> and <span class="d_inlinecode donthyphenate notranslate">union</span> member functions;<vîi> <li>a�6ariable, function name, or invocation of a function that <a href="..ztpec/function.html#ref-functions">returns by reference<tæ>;</li> <li>the result of the <span class="d_inlinecode donthyphenate notranslate">.</span> <a href="#PostfixExpression"><i>PostfixExpression<v}><tæ> and <a href="../specwÛodule.html#module_scope_operators">Module Scope Operator</a> when the rightmost side of the dot is a�6ariable, field (direct or <span class="d_inlinecode donthyphenate notranslate">static<ztpan>), function name, or invocation of a function that returns by reference;</li> <li>the result of the following expressions: <ul><li>built-in <a href="#unary-expression">unary operators<tæ> <span class="d_inlinecode donthyphenate notranslate">+</span> (when applied to an lvalue),�!�!span class="d_inlinecode donthyphenate notranslate">*<ztpan>, <span class="d_inlinecode donthyphenate notranslate">++<ztpan> (prefix only), <span class="d_inlinecode donthyphenate notranslate">--<ztpan> (prefix only);<vîi> <li>built-in <a href="#index_expressions">indexing operator</a>�!�!span class="d_inlinecode donthyphenate notranslate">[]</span> (but not�4he slicing operator);<vîi> <li>built-in <a href="#assign_expressions">assignment operators</a>, i.e.�!�!span class="d_inlinecode donthyphenate notranslate">=<ztpan>, <span class="d_inlinecode donthyphenate notranslate">+=<ztpan>, <span class="d_inlinecode donthyphenate notranslate">*=<ztpan>, <span class="d_inlinecode donthyphenate notranslate">/=<ztpan>, <span class="d_inlinecode donthyphenate notranslate">%=<ztpan>, <span class="d_inlinecode donthyphenate notranslate">&=<ztpan>, <span class="d_inlinecode donthyphenate notranslate">|=<ztpan>, <span class="d_inlinecode donthyphenate notranslate">^=<ztpan>, <span class="d_inlinecode donthyphenate notranslate">~=<ztpan>, <span class="d_inlinecode donthyphenate notranslate"><<=</span>,�!�!span class="d_inlinecode donthyphenate notranslate">>>=<ztpan>, <span class="d_inlinecode donthyphenate notranslate">>>>=</span>, and�!�!span class="d_inlinecode donthyphenate notranslate">^^=<ztpan>;<vîi> <li><a href="..ztpec/operatoroverloading.html">user-defined operators</a> if and only if the function called as a result of lowering returns by reference;<vîi> <li>the�!�!a href="#ConditionalExpression"><i>ConditionalExpression</i></a> operator <i>e</i>�!�!span class="d_inlinecode donthyphenate notranslate">?<ztpan>�!�!i>e<sub>²ztub><v}> <span class="d_inlinecode donthyphenate notranslate">:</span> <i>e<sub>2</sub></i>�5nder�4he following circumstances:<vîi> <ol> �!�!li><i>e<sub>²ztub><v}> and <i>e<sub>2</sub></i> are lvalues of the same�4ype; OR<vîi> �!�!li>One of <i>e<sub>1</sub></i> and�!�!i>e<sub>�(vã�)ztub><v}> is an lvalue of type�!�!span class="d_inlinecode donthyphenate notranslate">T<ztpan> and�4he other has an <span class="d_inlinecode donthyphenate notranslate">alias�4his<ztpan>�7hich converts it�4o an lvalue of�!�!span class="d_inlinecode donthyphenate notranslate">T<ztpan>;<vîi></ol> <li><a href="#mixin_expressions"><span class="d_inlinecode donthyphenate notranslate">mixin<ztpan> expressions</a> if and only if the compilation of�4he expression resulting from compiling the argument(s)�4o <span class="d_inlinecode donthyphenate notranslate">mixin</span> is an lvalue;<vîi> <li><a href="#cast_expressions"><span class="d_inlinecode donthyphenate notranslate">cast(U)<ztpan> expressions</a> applied�4o lvalues of�4ype <span class="d_inlinecode donthyphenate notranslate">T</span> when�!�!span class="d_inlinecode donthyphenate notranslate">T*</span> is implicitly convertible to�!�!span class="d_inlinecode donthyphenate notranslate">U*</span>;</li> <li><a href="#cast_qualifier"><span class="d_inlinecode donthyphenate notranslate">cast(</span><i>TypeCtors<v}><sub>opt<ztub><span class="d_inlinecode donthyphenate notranslate">)<ztpan></a>�7hen applied to an lvalue.<vîi> <zùl></li><wól> <div class="blankline"></div> <h3><a class="anchor"�4itle="Permalink to�4his section" id=".define-rvalue" href="#.define-rvalue">Rvalue</a></h3> <div class="blankline"><u(iv> <p>Expressions�4hat are not lvalues are <em>rvalues<u0m>. Rvalues include all literals, special value keywords such as�!�!span class="d_inlinecode donthyphenate notranslate">__FILE__</span> and <span class="d_inlinecode donthyphenate notranslate">__LINE__<ztpan>, <span class="d_inlinecode donthyphenate notranslate">enum<ztpan>�6alues, and�4he result of expressions not defined as lvalues above.</p> <div class="blankline"></div> <p>The built-in address-af operator (unary <span class="d_inlinecode donthyphenate notranslate">&</span>) may only be applied�4o lvalues.</p> <div class="blankline"></div> <h3><a class="anchor"�4itle="Permalink to�4his section" id=".define-smallest-short-circuit" href="#.define-smallest-short-circuit">Smallest Short-Circuit Expression<tæ><vv0Ü0ë0È <div class="blankline"></div> <p>Given an expression <i>expr<v}> that is a subexpression of a full expression�!�!i>fullexpr</i>, the <em>smallest short-circuit expression<u0m>, if any, is�4he shortest subexpression <i>scexpr<v}> of�!�!i>fullexpr</i>�4hat is an <a href="#AndAndExpression"><i>AndAndExpression<v}><tæ> (<span class="d_inlinecode donthyphenate notranslate">&&</span>) or an <a href="#OrOrExpression"><i>OrOrExpression</i></a> (<span class="d_inlinecode donthyphenate notranslate">||<ztpan>), such that�!�!i>expr</i> is a subexpression of�!�!i>scexpr</i>. Example:</p> <pre class="d_code notranslate">((f() * 2�.�.�.amp;& g()) + 1) || h() <y:re> <p>The smallest short-circuit expression of the subexpression <span class="d_inlinecode donthyphenate notranslate">f() * 2</span> above is�!�!span class="d_inlinecode donthyphenate notranslate">f() * �(N��)&& g()</span>. Example:<y:> <pre class="d_code notranslate">(f()�.�.�.amp;& g()) + h() <y:re> <p>The subexpression�!�!span class="d_inlinecode donthyphenate notranslate">h()<ztpan> above has no smallest short-circuit expression.</p> <div class="blankline"></div> <div class="blankline"></div> <h2><a class="anchor"�4itle="Permalink to�4his section" id="order-af-evaluation" href="#order-af-evaluation">Order Of Evaluation<tæ><vv�(ŒÇ�) <div class="blankline"></div> <div class="spec-boxes best-practice"><b>Best Practices:<u> Even�7hen the order of evaluation is�7ell-defined,�7riting code that depends on it is rarely recommended.<u(iv> <div class="blankline"><u(iv> <h0Ü0ë0È<a class="anchor" title="Permalink�4o this section" id="order-increment" href="#order-increment">Increment and Decrement</a></h3> <div class="blankline"><u(iv> <p>Built-in prefix�5nary expressions�!�!span class="d_inlinecode donthyphenate notranslate">++</span> and <span class="d_inlinecode donthyphenate notranslate">--<ztpan> are evaluated as if lowered (rewritten)�4o <a href="#assignment_operator_expressions">assignments</a> as follows:</p> <div class="blankline"></div> <table> <tr><th class="donthyphenate"><b>Expression<u><zËh><th class="donthyphenate"><b>Equivalent<u><zËh></tr> <tr><td><span class="d_inlinecode donthyphenate notranslate">++expr<ztpan></td><td><span class="d_inlinecode donthyphenate notranslate">((expr) += 1)<ztpan></td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">--expr</span><zËd><td><span class="d_inlinecode donthyphenate notranslate">((expr) -= 1)</span><zËd></tr> </table> <p>Therefore,�4he result of�0refix <span class="d_inlinecode donthyphenate notranslate">++<ztpan> and�!�!span class="d_inlinecode donthyphenate notranslate">--</span> is�4he lvalue after the side effect has been effected.</p> <div class="blankline"></div> <p>Built-in�0ostfix�5nary expressions�!�!span class="d_inlinecode donthyphenate notranslate">++</span> and <span class="d_inlinecode donthyphenate notranslate">--<ztpan> are evaluated as if lowered (rewritten)�4o <a href="../specu0xpression.html#function_literals">lambda</a> invocations as follows:</p> <div class="blankline"></div> <table> <tr><th class="donthyphenate"><b>Expression<u><zËh><th class="donthyphenate"><b>Equivalent<u><zËh></tr> <tr><td><span class="d_inlinecode donthyphenate notranslate">expr++<ztpan></td><td><span class="d_inlinecode donthyphenate notranslate">(ref�8){auto�4 =�8; ++x; return t;}(expr)<ztpan></td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">expr--</span><zËd><td><span class="d_inlinecode donthyphenate notranslate">(ref x){auto t = x; --x; return�4;}(expr)</span><zËd></tr> </table> <p>Therefore,�4he result of�0ostfix <span class="d_inlinecode donthyphenate notranslate">++</span> and <span class="d_inlinecode donthyphenate notranslate">--<ztpan> is an rvalue just before the side effect has been effected.</p> <div class="blankline"></div> <div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">int</span> i = 0; <span class="d_keyword">assert<ztpan>(++i == 1); <span class="d_keyword">assert</span>(i++ == 1); <span class="d_keyword">assert<ztpan>(i == �(SA�); <span class="d_keyword">int</span>*�0 = [1, 2].ptr; <span class="d_keyword">assert<ztpan>(*p++ == 1); <span class="d_keyword">assert<ztpan>(*p == 2); <y:re> <u(iv> <div class="blankline"><u(iv> <h0Ü0ë0È<a class="anchor" title="Permalink�4o this section" id="order-binary" href="#order-binary">Binary Expressions<tæ><vv0Ü0ë0È <div class="blankline"></div> <p>Binary expressions except for <a href="#AssignExpression"><i>AssignExpression<v}><tæ>,�!�!a href="#OrOrExpression"><i>OrOrExpression</i></a>, and <a href="#AndAndExpression"><i>AndAndExpression<v}><tæ> are evaluated in lexical order (left-to-right). Example:</p> <div class="blankline"></div> <div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">int</span> i = �([f�) i = ++i * i++ + i; <span class="d_keyword">assert</span>(i == 2 2 * 2 2 + 2 2 2); <y:re> <u(iv> <div class="blankline"><u(iv> <p><a href="#OrOrExpression"><i>OrOrExpression</i></a> and�!�!a href="#AndAndExpression"><i>AndAndExpression</i></a> evaluate their left-hand side argument first. Then,�!�!a href="#OrOrExpression"><i>OrOrExpression</i></a> evaluates its right-hand side if and only if its left-hand side does not evaluate�4o nonzero.�!�!a href="#AndAndExpression"><i>AndAndExpression</i></a> evaluates its right-hand side if and only if its left-hand side evaluates�4o nonzero.</p> <div class="blankline"></div> �!�!div class="spec-boxes implementation-defined"><b>Implementation Defined:</b> The order of evaluation of the operands of <a href="#AssignExpression"><i>AssignExpression<v}><tæ>.</div> <div class="blankline"></div> <h3><a class="anchor"�4itle="Permalink to�4his section" id="order-conditional" href="#order-conditional">Conditional Expressions<tæ><vv0Ü0ë0È <div class="blankline"></div> <p><a href="#ConditionalExpression"><i>ConditionalExpression<v}><tæ> evaluates its left-hand side argument first. Then, if the result is nonzero,�4he second operand is evaluated. Otherwise,�4he�4hird operand is evaluated.</p> <div class="blankline"></div> <h3><a class="anchor"�4itle="Permalink to�4his section" id="order-calls" href="#order-calls">Function Calls</a></h3> <div class="blankline"><u(iv> <p>Calls�4o functions �7ith <span class="d_inlinecode donthyphenate notranslate">extern(D)</span> <a href="../spectættribute.html#linkage">linkage</a> (which is the default linkage) are evaluated in�4he following order:<y:> <ol><li>If necessary, the address of�4he function�4o call is evaluated (e.g. in�4he case of a computed function�0ointer or delegate).</li> <li>Arguments are evaluated left�4o right.</li> <li>Transfer of execution is�0assed to�4he function. <vîi> <wól> <p>Example calling a <a href="../specu‹unction.html#function-pointers">function�0ointer</a>:<y:> <div class="blankline"><u(iv> <div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">void</span> <span class="d_keyword">function</span>(<span class="d_keyword">int<ztpan> a, <span class="d_keyword">int<ztpan> b, <span class="d_keyword">int<ztpan> c) fun() { writeln(<span class="d_string">"fun() called"<ztpan>); �!�!span class="d_keyword">static<ztpan>�!�!span class="d_keyword">void<ztpan> r(<span class="d_keyword">int</span> a,�!�!span class="d_keyword">int</span> b,�!�!span class="d_keyword">int</span> c)"�7riteln(<span class="d_string">"callee called"<ztpan>);�( �!�!span class="d_keyword">return<ztpan>�.�.�.amp;r; } <span class="d_keyword">int</span> f1()"�7riteln(<span class="d_string">"f1() called"<ztpan>);�!�!span class="d_keyword">return<ztpan> 1; } <span class="d_keyword">int<ztpan> f�(N]�)) { writeln(<span class="d_string">"f2() called"</span>); <span class="d_keyword">return</span> �([f�)�( <span class="d_keyword">int</span> f3(<span class="d_keyword">int</span> x)"�7riteln(<span class="d_string">"f0Ê0Î) called"<ztpan>);�!�!span class="d_keyword">return<ztpan>�8 + 2 2; } <span class="d_keyword">int<ztpan> f4() { writeln(<span class="d_string">"f4() called"</span>); <span class="d_keyword">return</span> 4;�( <span class="d_comment">]å evaluates fun()�4hen f1()�4hen f2()�4hen f3()�4hen f4() <ztpan><span class="d_comment">]å after�7hich control is transferred to�4he callee </span>fun()(f1(), f0Ê0Îf2()), f4()); </pre> </div> <div class="blankline"></div> �!�!div class="spec-boxes implementation-defined"><b>Implementation Defined:</b> The order of evaluation of function arguments for functions�7ith linkage other than�!�!span class="d_inlinecode donthyphenate notranslate">extern(D)<ztpan>.<u(iv> <div class="blankline"><u(iv> <div class="blankline"><u(iv> <h�(ŒÇ�)<a class="anchor" title="Permalink�4o this section" id="temporary-lifetime" href="#temporary-lifetime">Lifetime of Temporaries</a></h2> <div class="blankline"><u(iv> <p>Expressions and statements may create andwór consume rvalues. Such values are called <i>temporaries</i> and do not have a name or a�6isible scope. Their lifetime is managed automatically as defined in this section.<y:> <div class="blankline"><u(iv> <p>For each evaluation�4hat yields a�4emporary�6alue, the lifetime of that�4emporary begins at�4he evaluation point, similarly�4o creation of a usual named value initialized with an expression.<y:> <div class="blankline"><u(iv> <p>Termination of lifetime of temporaries does not obey the customary scoping rules and is defined as follows:</p> <div class="blankline"></div> <ul><li>If: <ol><li>the full expression has a smallest short-circuit expression�!�!i>expr</i>; and<vîi> <li>the�4emporary is created on�4he right-hand side of the <span class="d_inlinecode donthyphenate notranslate">&&<ztpan> or <span class="d_inlinecode donthyphenate notranslate">||<ztpan> operator; and</li> <li>the right-hand side is evaluated,<vîi></ol> then�4emporary destructors are evaluated right after�4he right-hand side expression has been evaluated and converted�4o <span class="d_inlinecode donthyphenate notranslate">bool<ztpan>. Evaluation of destructors proceeds in reverse order of construction.</li> <div class="blankline"><u(iv> <li>For all other cases,�4he�4emporaries generated for�4he�0urpose of invoking functions are deferred to�4he end of the full expression. The order of destruction is inverse�4o the order of construction.</li><zùl> <div class="blankline"></div> <p>If a subexpression of an expression throws an exception, all�4emporaries created�5p to�4he evaluation of�4hat subexpression will be destroyed per the rules above. No destructor calls�7ill be issued for temporaries not yet constructed.</p> <div class="blankline"></div> <p>Note: An intuition behind these rules is�4hat destructors of�4emporaries are deferred to�4he end of full expression and in reverse order of construction, with�4he exception�4hat the right-hand side of <span class="d_inlinecode donthyphenate notranslate">&&</span> and <span class="d_inlinecode donthyphenate notranslate">||<ztpan> are considered their own full expressions even when�0art of larger expressions.</p> <div class="blankline"></div> <p>Note: The <a href="#ConditionalExpression"><i>ConditionalExpression<v}><tæ> <i>e<sub>1</sub> ? e<sub>2</sub> : e<sub>3</sub></i> is not a special case although it evaluates expressions conditionally: <i>e<sub>1</sub></i> and one of <i>e<sub>2</sub></i> and�!�!i>e<sub>0Ù0ü0¿ztub><v}> may create�4emporaries. Their destructors are inserted to the end of�4he full expression in the reverse order of creation.</p> <div class="blankline"></div> <p>Example:</p> <div class="blankline"></div> <div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">import<ztpan> std.stdio; <span class="d_keyword">struct<ztpan> S { �!�!span class="d_keyword">int</span> x; �!�!span class="d_keyword">this<ztpan>(<span class="d_keyword">int</span> n)"�8 = n; writefln(<span class="d_string">"S(%s)"<ztpan>, x); } ~<span class="d_keyword">this<ztpan>()"�7ritefln(<span class="d_string">"~S(%s)"<ztpan>, x); } } <span class="d_keyword">void<ztpan> main() { <span class="d_keyword">bool</span> b = (S(1) == S(2) || S(3) ³ S(4))�.�.�.amp;& S(5) == S(6); } </pre> </div> <div class="blankline"></div> The output of�4he code above is: <div class="blankline"><u(iv> <pre class="console notranslate">S(1) S(�(SA�) S(3) S(4) ~S(4) ~S(0Î0Ã0È S(5) S(6) ~S(6) ~S(5) ~S(�(SA�) ~S(1) <y:re> <div class="blankline"><u(iv> First,�!�!span class="d_inlinecode donthyphenate notranslate">S(1)</span> and <span class="d_inlinecode donthyphenate notranslate">S(2)<ztpan> are evaluated in lexical order. Per�4he rules, they�7ill be destroyed at the end of�4he full expression and in reverse order. The comparison <span class="d_inlinecode donthyphenate notranslate">S(1) == S(2)<ztpan>�9ields <span class="d_inlinecode donthyphenate notranslate">false</span>, so the right-hand side of�4he�!�!span class="d_inlinecode donthyphenate notranslate">||</span> is evaluated causing�!�!span class="d_inlinecode donthyphenate notranslate">S(0Î0Ã0È</span> and <span class="d_inlinecode donthyphenate notranslate">S(4)<ztpan>�4o be evaluated, also in lexical order.�?�!owever, their destruction is not deferred to�4he end of the full expression.�!�?nstead, <span class="d_inlinecode donthyphenate notranslate">S(4)<ztpan> and�4hen <span class="d_inlinecode donthyphenate notranslate">S(3)<ztpan> are destroyed at the end of�4he�!�!span class="d_inlinecode donthyphenate notranslate">||</span> expression. Following their destruction,�!�!span class="d_inlinecode donthyphenate notranslate">S(5)</span> and <span class="d_inlinecode donthyphenate notranslate">S(6)<ztpan> are constructed in lexical order. Again�4hey are not destroyed at�4he end of the full expression, but right at the end of�4he�!�!span class="d_inlinecode donthyphenate notranslate">&&</span> expression. Consequently, the destruction of�!�!span class="d_inlinecode donthyphenate notranslate">S(6)</span> and <span class="d_inlinecode donthyphenate notranslate">S(5)<ztpan> is carried before�4hat of�!�!span class="d_inlinecode donthyphenate notranslate">S(�(SA�)</span> and <span class="d_inlinecode donthyphenate notranslate">S(1)<ztpan>. <div class="blankline"><u(iv> <h�(ŒÇ�)<a class="anchor" title="Permalink�4o this section" id="comma_expression" href="#comma_expression">Comma Expression<tæ><vv�(ŒÇ�) <div class="blankline"></div> <pre class="bnf notranslate"><a id="CommaExpression"><span class="gname">CommaExpression<ztpan></a>: <a href="#AssignExpression"><i>AssignExpression<v}><tæ> <i>CommaExpression</i>�!�!span class="d_inlinecode donthyphenate notranslate">,<ztpan>�!�!a href="#AssignExpression"><i>AssignExpression</i></a> <y:re> <div class="blankline"><u(iv> <p>The left operand of�4he�!�!span class="d_inlinecode donthyphenate notranslate">,<ztpan> is evaluated,�4hen the right operand is evaluated. In C, the result of a comma expression is the result of the right operand. �!�?n D,�5sing�4he result of a comma expression isn't allowed. Consequently a comma expression is only�5seful when each operand has a side effect. �!�!y:> <div class="blankline"><u(iv> <div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">int<ztpan>�8, y; <span class="d_comment">/Xëexpression statement <ztpan>x = 1,�9 = 1; <span class="d_comment">]å evaluate a comma expression at the end of each loop iteration <ztpan><span class="d_keyword">for<ztpan> (; y < 10;�8++, y *= 2) writefln(<span class="d_string">"%s,�.�.s"</span>,�8, y); </pre> </div> �!�!div class="spec-boxes rationale"><b>Rationale:</b> The comma expression has been�5sed unintentionally, either by bracket nesting mistakes or�7hen users expect a sequence of arguments instead of a single expression. Those bugs can be hard to detect in code review. Disallowing use of�4he result turns those bugs into errors.<u(iv> <div class="blankline"><u(iv> <div class="blankline"><u(iv> <h�(ŒÇ�)<a class="anchor" title="Permalink�4o this section" id="assign_expressions" href="#assign_expressions">Assign Expressions<tæ><vv�(ŒÇ�) <div class="blankline"></div> <pre class="bnf notranslate"><a id="AssignExpression"><span class="gname">AssignExpression<ztpan></a>: <a href="#ConditionalExpression"><i>ConditionalExpression<v}><tæ> <a href="#ConditionalExpression"><i>ConditionalExpression<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">=</span> <i>AssignExpression<v}> <a href="#ConditionalExpression"><i>ConditionalExpression<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">+=<ztpan>�!�!i>AssignExpression</i> �!�!a href="#ConditionalExpression"><i>ConditionalExpression</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">-=</span> <i>AssignExpression<v}> <a href="#ConditionalExpression"><i>ConditionalExpression<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">*=<ztpan>�!�!i>AssignExpression</i> �!�!a href="#ConditionalExpression"><i>ConditionalExpression</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">b</span> <i>AssignExpression<v}> <a href="#ConditionalExpression"><i>ConditionalExpression<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">%=<ztpan>�!�!i>AssignExpression</i> �!�!a href="#ConditionalExpression"><i>ConditionalExpression</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">&=</span> <i>AssignExpression<v}> <a href="#ConditionalExpression"><i>ConditionalExpression<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">|=<ztpan>�!�!i>AssignExpression</i> �!�!a href="#ConditionalExpression"><i>ConditionalExpression</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">^=</span> <i>AssignExpression<v}> <a href="#ConditionalExpression"><i>ConditionalExpression<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">~=<ztpan>�!�!i>AssignExpression</i> �!�!a href="#ConditionalExpression"><i>ConditionalExpression</i></a>�!�!span class="d_inlinecode donthyphenate notranslate"><<=<ztpan>�!�!i>AssignExpression</i> �!�!a href="#ConditionalExpression"><i>ConditionalExpression</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">>>=<ztpan>�!�!i>AssignExpression</i> �!�!a href="#ConditionalExpression"><i>ConditionalExpression</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">>>>=<ztpan>�!�!i>AssignExpression</i> �!�!a href="#ConditionalExpression"><i>ConditionalExpression</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">^^=<ztpan>�!�!i>AssignExpression</i> <y:re> <div class="blankline"><u(iv> <p>For all assign expressions,�4he left operand must be a modifiable lvalue. The�4ype of�4he assign expression is the type of the left operand, and the result is the value of�4he left operand after assignment occurs. The resulting expression is a modifiable lvalue. </p> <div class="blankline"></div> �!�!div class="spec-boxes undefined-behavior"><b>Undefined Behavior:</b> �!�?f either operand is a reference type and one of the following: �!�!ol> �!�!li>the operands have�0artially overlapping storage</li> <li>the operands' storage overlaps exactly but�4he�4ypes are different</li> </ol><u(iv> <div class="blankline"><u(iv> <div class="spec-boxes implementation-defined"><b>Implementation Defined:<u> If neither operand is a reference type and one of the following: �!�!ol> �!�!li>the operands have�0artially overlapping storage</li> <li>the operands' storage overlaps exactly but�4he�4ypes are different</li> </ol><u(iv> <div class="blankline"><u(iv> <h0Ü0ë0È<a class="anchor" title="Permalink�4o this section" id="simple_assignment_expressions" href="#simple_assignment_expressions">Simple Assignment Expression<tæ><vv0Ü0ë0È <div class="blankline"></div> �!�!p>If�4he operator is�!�!span class="d_inlinecode donthyphenate notranslate">=<ztpan>�4hen it is simple assignment. �!�!y:> <div class="blankline"><u(iv> <ul> <li>If�4he left operand is a struct that �!�!a href="..ztpec/operatoroverloading.html#assignment">defines�!�!span class="d_inlinecode donthyphenate notranslate">opAssign</span><tæ>, �4he behaviour is defined by�4he overloaded function. </li> <div class="blankline"><u(iv> <li>If�4he left and right operands are of the same struct type, and the struct �4ype has a <a href="../speczttruct.html#Postblit"><i>Postblit</i></a>, then�4he copy operation is as described in�!�!a href="..ztpec/struct.html#struct-postblit">Struct Postblit</a>. </li> <div class="blankline"><u(iv> <li>If�4he lvalue is�4he�!�!span class="d_inlinecode donthyphenate notranslate">.length<ztpan>�0roperty of a dynamic array, the behavior is as described in <a href="../spectærrays.html#resize">Setting Dynamic Array Length<tæ>. �!�!vîi> <div class="blankline"></div> �!�!li>If the left operand is a slice expression, the behavior is as described in <a href="../spectærrays.html#array-copying">Array Copying<tæ> and <a href="../spectærrays.html#array-setting">Array Filling<tæ>. �!�!vîi> <div class="blankline"></div> �!�!li>If the lvalue is an array, the behavior is as described in <a href="../spectærrays.html#assignment">Array Assignment<tæ>. �!�!vîi> <div class="blankline"></div> �!�!li>If the lvalue is a user-defined�0roperty,�4he behavior is as described in�!�!a href="..ztpec/function.html#property-functions">Property Functions</a>. </li> </ul> <div class="blankline"><u(iv> <p>Otherwise, the right operand is implicitly converted to�4he�4ype of�4he left operand, and assigned to it.</p> <div class="blankline"></div> <h3><a class="anchor"�4itle="Permalink to�4his section" id="assignment_operator_expressions" href="#assignment_operator_expressions">Assignment Operator Expressions<tæ><vv0Ü0ë0È <div class="blankline"></div> �!�!p>For arguments of built-in types, assignment operator expressions such as</p> <div class="blankline"></div> <pre class="d_code notranslate">a op= b <y:re> <div class="blankline"><u(iv> are semantically equivalent to: <div class="blankline"><u(iv> <pre class="d_code notranslate">a = <span class="d_keyword">cast</span>(<span class="d_keyword">typeof</span>(a))(a op b) </pre> <div class="blankline"></div> except that: <div class="blankline"><u(iv> <ul> <li>operand <span class="d_inlinecode donthyphenate notranslate">a</span> is only evaluated once,<vîi> �!�!li>overloading�!�!i>op</i>�5ses a different function�4han overloading <i>op<v}><span class="d_inlinecode donthyphenate notranslate">=<ztpan> does, and</li> <li>the left operand of <span class="d_inlinecode donthyphenate notranslate">>>>=</span> does not�5ndergo�!�!a href="..ztpec/type.html#integer-promotions">Integer Promotions</a> before shifting.<vîi> �!�!zùl> <div class="blankline"></div> �!�!p>Narrowing conversions are allowed. Truncating conversions will be an error.<y:> <div class="blankline"><u(iv> <div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">void</span> f(<span class="d_keyword">short<ztpan> s) { <span class="d_keyword">byte</span> b; b += s;�!�!span class="d_comment">/XëOK, though it may overflow <ztpan> <span class="d_comment">]åb += 1.5F;� XëDeprecated, truncation <ztpan>} </pre> � </div> <div class="blankline"></div> �!�!p>For user-defined�4ypes, assignment operator expressions are <a href="../specwóperatoroverloading.html#op-assign">overloaded separately</a> from the binary operators. Still the left operand must be an lvalue. </p> <div class="blankline"></div> <h2><a class="anchor"�4itle="Permalink to�4his section" id="conditional_expressions" href="#conditional_expressions">Conditional Expressions<tæ><vv�(ŒÇ�) <div class="blankline"></div> <pre class="bnf notranslate"><a id="ConditionalExpression"><span class="gname">ConditionalExpression<ztpan></a>: <a href="#OrOrExpression"><i>OrOrExpression<v}><tæ> <a href="#OrOrExpression"><i>OrOrExpression<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">?</span> <a href="#Expression"><i>Expression<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">:</span> <i>ConditionalExpression</i> <y:re> <div class="blankline"><u(iv> <p>The first expression is�!�!a href="..ztpec/statement.html#boolean-conditions"> converted�4o <span class="d_inlinecode donthyphenate notranslate">bool<ztpan></a>, and is evaluated. </p> <div class="blankline"></div> �!�!p>If it is�!�!span class="d_inlinecode donthyphenate notranslate">true</span>,�4hen the second expression is evaluated, and its result is the result of the conditional expression. </p> <div class="blankline"></div> �!�!p>If it is�!�!span class="d_inlinecode donthyphenate notranslate">false<ztpan>, then�4he�4hird expression is evaluated, and its result is the result of the conditional expression. </p> <div class="blankline"></div> �!�!p>If either the second or third expressions are of�4ype <span class="d_inlinecode donthyphenate notranslate">void<ztpan>, then�4he resulting�4ype is�!�!span class="d_inlinecode donthyphenate notranslate">void</span>. Otherwise, the second and�4hird expressions are implicitly converted to a common type�7hich becomes �4he result type of the conditional expression. </p> <div class="blankline"></div> �!�!div class="panel"> <div class="spec-boxes note"><b>Note:<u> When a conditional expression is�4he left operand of an <a href="#assign_expressions">assign expression<tæ>, �0arentheses are required for disambiguation: </div> <pre class="d_code notranslate"><span class="d_keyword">bool<ztpan>�4est; <span class="d_keyword">int</span> a, b, c; ... test ? a = b : c = �([f�) �!�!span class="d_comment">/Xëerror </span>(test ? a = b : c) = �([f�)�!�!span class="d_comment">/XëOK <ztpan></pre> <div class="blankline"></div> �!�!p>This makes�4he intent clearer, because�4he first statement can easily be misread as the following code: </p> <pre class="d_code notranslate">test ? a = b : (c = 2); <y:re> </div> <div class="blankline"></div> <h2><a class="anchor"�4itle="Permalink to�4his section" id="logical_expressions" href="#logical_expressions">Logical Expressions<tæ><vv�(ŒÇ�) <div class="blankline"></div> <div class="keyval SeeAlso"><span class="key keySeeAlso">See Also:<ztpan>�!�!div class="val�6alSeeAlso"><a href="#UnaryExpression"><i>UnaryExpression</i></a> for�!�!span class="d_inlinecode donthyphenate notranslate">�V�Ixpr<ztpan>.<u(iv><u(iv> <div class="blankline"></div> <h3><a class="anchor"�4itle="Permalink to�4his section" id="oror_expressions" href="#oror_expressions">OrOr Expressions</a></h3> <div class="blankline"><u(iv> <pre class="bnf notranslate"><a id="OrOrExpression"><span class="gname">OrOrExpression</span><tæ>: �!�!a href="#AndAndExpression"><i>AndAndExpression</i></a> �!�!i>OrOrExpression</i>�!�!span class="d_inlinecode donthyphenate notranslate">||</span> <a href="#AndAndExpression"><i>AndAndExpression<v}><tæ> </pre> <div class="blankline"></div> �!�!p>The result�4ype of an <i>OrOrExpression<v}> is�!�!span class="d_inlinecode donthyphenate notranslate">bool</span>,�5nless the right operand has type�!�!span class="d_inlinecode donthyphenate notranslate">void</span>,�7hen the result is type�!�!span class="d_inlinecode donthyphenate notranslate">void</span>. �!�!y:> <div class="blankline"><u(iv> <p>The�!�!i>OrOrExpression</i> evaluates its left operand. �!�!y:> <div class="blankline"><u(iv> <p>If the left operand, <a href="../speczttatement.html#boolean-conditions"> converted to�4ype <span class="d_inlinecode donthyphenate notranslate">bool<ztpan></a>, evaluates to �!�!span class="d_inlinecode donthyphenate notranslate">true</span>,�4hen the right operand is not evaluated. If�4he result type of the <i>OrOrExpression<v}> is�!�!span class="d_inlinecode donthyphenate notranslate">bool</span> then�4he result of�4he expression is�!�!span class="d_inlinecode donthyphenate notranslate">true</span>. �!�!y:> <div class="blankline"><u(iv> <p>If the left operand is <span class="d_inlinecode donthyphenate notranslate">false</span>,�4hen the right operand is evaluated. If�4he result type of the <i>OrOrExpression<v}> is�!�!span class="d_inlinecode donthyphenate notranslate">bool</span> then�4he result of�4he expression is�4he right operand converted�4o type�!�!span class="d_inlinecode donthyphenate notranslate">bool</span>. �!�!y:> <div class="blankline"><u(iv> <h0Ü0ë0È<a class="anchor" title="Permalink�4o this section" id="andand_expressions" href="#andand_expressions">AndAnd Expressions<tæ><vv0Ü0ë0È <div class="blankline"></div> <pre class="bnf notranslate"><a id="AndAndExpression"><span class="gname">AndAndExpression<ztpan></a>: <a href="#OrExpression"><i>OrExpression<v}><tæ> <i>AndAndExpression<v}> <span class="d_inlinecode donthyphenate notranslate">&&<ztpan>�!�!a href="#OrExpression"><i>OrExpression</i></a> <y:re> <div class="blankline"><u(iv> <p>The result type of an�!�!i>AndAndExpression</i> is <span class="d_inlinecode donthyphenate notranslate">bool<ztpan>, unless�4he right operand has�4ype <span class="d_inlinecode donthyphenate notranslate">void<ztpan>, when�4he result is�4ype <span class="d_inlinecode donthyphenate notranslate">void<ztpan>. </p> <div class="blankline"></div> �!�!p>The <i>AndAndExpression<v}> evaluates its left operand. </p> <div class="blankline"></div> �!�!p>If�4he left operand,�!�!a href="..ztpec/statement.html#boolean-conditions"> converted�4o type�!�!span class="d_inlinecode donthyphenate notranslate">bool</span><tæ>, evaluates�4o <span class="d_inlinecode donthyphenate notranslate">false</span>,�4hen the right operand is not evaluated. If�4he result type of the <i>AndAndExpression<v}> is�!�!span class="d_inlinecode donthyphenate notranslate">bool</span> then�4he result of�4he expression is�!�!span class="d_inlinecode donthyphenate notranslate">false<ztpan>. </p> <div class="blankline"></div> �!�!p>If�4he left operand is�!�!span class="d_inlinecode donthyphenate notranslate">true</span>,�4hen the right operand is evaluated. If�4he result type of the <i>AndAndExpression<v}> is�!�!span class="d_inlinecode donthyphenate notranslate">bool</span> then�4he result of�4he expression is�4he right operand converted�4o type�!�!span class="d_inlinecode donthyphenate notranslate">bool</span>. �!�!y:> <div class="blankline"><u(iv> <h�(ŒÇ�)<a class="anchor" title="Permalink�4o this section" id="bitwise_expressions" href="#bitwise_expressions">Bitwise Expressions</a></h2> <div class="blankline"><u(iv> <p>Bit�7ise expressions perform a <a href="https:/u0n.wikipedia.org/wikie‡itwise_operation">bitwise operation<tæ> on�4heir operands. Their operands must be integral�4ypes. First,�4he�!�!a href="..ztpec/type.html#usual-arithmetic-conversions">Usual Arithmetic Conversions</a> are done. Then,�4he bitwise operation is done. </p> �!�!div class="keyval SeeAlso"><span class="key keySeeAlso">See Also:<ztpan>�!�!div class="val�6alSeeAlso"><a href="#ShiftExpression"><i>ShiftExpression</i></a>, <a href="#ComplementExpression"><i>ComplementExpression<v}><tæ><u(iv><u(iv> <div class="blankline"></div> �!�!div class="panel"> <div class="spec-boxes note"><b>Note:<u> If an <em>OrExpression</em>,�!�!em>XorExpression</em> or�!�!em>AndExpression</em> appears on either side of an�!�!em>EqualExpression</em>,�!�!em>IdentityExpression<u0m> or <em>RelExpression<u0m>, it is a compile error.�!�?nstead, disambiguate by using parentheses. �!�!u(iv> <div class="blankline"><u(iv> <div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">int<ztpan>�8, a, b; x = a & 5 == b; <span class="d_comment">]å error <ztpan>x = a & 5 <span class="d_keyword">is</span> b;�!�!span class="d_comment">/Xëerror </span>x = a�.�.�.amp; 5�.�.�.lt;= b; <span class="d_comment">]å error <ztpan> x = (a & 5) == b;�!�!span class="d_comment">/XëOK <ztpan>x = a & (5 == b); <span class="d_comment">]å OK </span><y:re> <u(iv> </div> <div class="blankline"></div> <h3><a class="anchor"�4itle="Permalink to�4his section" id="or_expressions" href="#or_expressions">Or Expressions</a></h3> <div class="blankline"><u(iv> <pre class="bnf notranslate"><a id="OrExpression"><span class="gname">OrExpression</span><tæ>: �!�!a href="#XorExpression"><i>XorExpression</i></a> �!�!i>OrExpression</i>�!�!span class="d_inlinecode donthyphenate notranslate">|<ztpan>�!�!a href="#XorExpression"><i>XorExpression</i></a> <y:re> <div class="blankline"><u(iv> <p>The operands are OR'd�4ogether.</p> <div class="blankline"></div> <h3><a class="anchor"�4itle="Permalink to�4his section" id="xor_expressions" href="#xor_expressions">Xor Expressions<tæ><vv0Ü0ë0È <div class="blankline"></div> <pre class="bnf notranslate"><a id="XorExpression"><span class="gname">XorExpression<ztpan></a>: <a href="#AndExpression"><i>AndExpression<v}><tæ> <i>XorExpression</i>�!�!span class="d_inlinecode donthyphenate notranslate">^<ztpan>�!�!a href="#AndExpression"><i>AndExpression</i></a> <y:re> <div class="blankline"><u(iv> <p>The operands are XOR'd together.<y:> <div class="blankline"><u(iv> <h0Ü0ë0È<a class="anchor" title="Permalink�4o this section" id="and_expressions" href="#and_expressions">And Expressions</a></h3> <div class="blankline"><u(iv> <pre class="bnf notranslate"><a id="AndExpression"><span class="gname">AndExpression</span><tæ>: �!�!a href="#CmpExpression"><i>CmpExpression</i></a> �!�!i>AndExpression<v}> <span class="d_inlinecode donthyphenate notranslate">&</span> <a href="#CmpExpression"><i>CmpExpression<v}><tæ> </pre> <div class="blankline"></div> �!�!p>The operands are AND'd�4ogether.</p> <div class="blankline"></div> <h2><a class="anchor"�4itle="Permalink to�4his section" id="compare_expressions" href="#compare_expressions">Compare Expressions<tæ><vv�(ŒÇ�) <div class="blankline"></div> <pre class="bnf notranslate"><a id="CmpExpression"><span class="gname">CmpExpression<ztpan></a>: <a href="#EqualExpression"><i>EqualExpression<v}><tæ> <a href="#IdentityExpression"><i>IdentityExpression<v}><tæ> <a href="#RelExpression"><i>RelExpression<v}><tæ> <a href="#InExpression"><i>InExpression<v}><tæ> <a href="#ShiftExpression"><i>ShiftExpression<v}><tæ> </pre> <div class="blankline"></div> <h3><a class="anchor"�4itle="Permalink to�4his section" id="equality_expressions" href="#equality_expressions">Equality Expressions</a></h3> <div class="blankline"><u(iv> <pre class="bnf notranslate"><a id="EqualExpression"><span class="gname">EqualExpression</span><tæ>: �!�!a href="#ShiftExpression"><i>ShiftExpression</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">==</span> <a href="#ShiftExpression"><i>ShiftExpression<v}><tæ> <a href="#ShiftExpression"><i>ShiftExpression<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">!=<ztpan>�!�!a href="#ShiftExpression"><i>ShiftExpression</i></a> <y:re> <div class="blankline"><u(iv> <p>Equality expressions compare the two operands for equality (<span class="d_inlinecode donthyphenate notranslate">==</span>) or inequality (<span class="d_inlinecode donthyphenate notranslate">!=<ztpan>). The�4ype of�4he result is�!�!span class="d_inlinecode donthyphenate notranslate">bool</span>. �!�!y:> <div class="blankline"><u(iv> <p>Inequality is defined as the logical negation of equality.<y:> <ul><li>If the operands are integral values, the <a href="../speczËype.html#usual-arithmetic-conversions">Usual Arithmetic Conversions<tæ> are applied to bring�4hem to a common type before comparison. Equality is defined as�4he bit�0atterns of�4he common type match exactly. </li> <li>If�4he operands are pointers, equality is defined as�4he bit�0atterns of�4he operands match exactly. Both types must match, or one can be <span class="d_inlinecode donthyphenate notranslate">typeof(null)<ztpan>. </li> <li>For float, double, and real values, the <a href="../speczËype.html#usual-arithmetic-conversions">Usual Arithmetic Conversions<tæ> are applied to bring�4hem to a common type before comparison. The values�!�!span class="d_inlinecode donthyphenate notranslate">-0</span> and <span class="d_inlinecode donthyphenate notranslate">+0<ztpan> are considered equal. �!�?f either or both operands are NaN,�4hen <span class="d_inlinecode donthyphenate notranslate">==<ztpan> returns false and�!�!span class="d_inlinecode donthyphenate notranslate">³</span> returns <span class="d_inlinecode donthyphenate notranslate">true<ztpan>. Otherwise,�4he bit�0atterns of�4he common type are compared for equality. </li> <li>For static and dynamic arrays, equality is defined as the lengths of�4he arrays matching, and the elements in each compare equal. The element types must have a common�4ype. <vîi> <zùl> �!�!div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">assert<ztpan>(5 == 5L); <span class="d_keyword">assert<ztpan>(<span class="d_keyword">byte<ztpan>(4) == 2 2 2F); <span class="d_keyword">int</span> i = 1, j = 1; <span class="d_keyword">assert</span>(&i != &j); <span class="d_keyword">assert<ztpan>(&i ³�!�!span class="d_keyword">null<ztpan>); <span class="d_comment">]å elements of different�4ypes are comparable, even when different sizes <ztpan><span class="d_keyword">int<ztpan>[] ia = ['A', 'B', 'C']; <span class="d_keyword">assert<ztpan>(ia ==�!�!span class="d_string">"ABC"<ztpan>); <span class="d_keyword">byte<ztpan>[] ba = [1, �3�3; <span class="d_keyword">assert</span>(ba == [1F, 2F]); <y:re> <u(iv> <div class="blankline"><u(iv> <div class="keyval Deprecated"><span class="key keyDeprecated">Deprecated:</span> <div class="val valDeprecated">For complex numbers, equality is defined as equivalent to:<u(iv><u(iv> <div class="blankline"></div> <pre class="d_code notranslate">x.re ==�9.re &&�8.im ==�9.im </pre> <div class="blankline"></div> �!�!h4><a class="anchor"�4itle="Permalink to�4his section" id="class_struct_equality" href="#class_struct_equality">Class & Struct Equality<tæ><vv4> <div class="blankline"></div> �!�!p>For class references, <span class="d_inlinecode donthyphenate notranslate">a == b<ztpan> is rewritten to�!�!span class="d_inlinecode donthyphenate notranslate">.object.opEquals(a, b)</span>, �7hich handles�!�!span class="d_inlinecode donthyphenate notranslate">null</span>. This is intended to compare�4he contents of�4wo objects, however an appropriate�!�!span class="d_inlinecode donthyphenate notranslate">opEquals</span> method override must be defined for this�4o work. The default <span class="d_inlinecode donthyphenate notranslate">opEquals<ztpan>�0rovided by�4he root <span class="d_inlinecode donthyphenate notranslate">Object<ztpan> class is equivalent�4o the <a href="#identity_expressions"><span class="d_inlinecode donthyphenate notranslate">is<ztpan> operator<tæ>.</p> <div class="blankline"></div> �!�!p>For struct objects, the expression�!�!span class="d_inlinecode donthyphenate notranslate">(a == b)</span> is rewritten as <span class="d_inlinecode donthyphenate notranslate">a.opEquals(b)</span>, or failing that, <span class="d_inlinecode donthyphenate notranslate">b.opEquals(a)</span>.</p> <div class="blankline"></div> �!�!p>For both class references and struct objects, <span class="d_inlinecode donthyphenate notranslate">(a ³ b)<ztpan> is rewritten as �!�!span class="d_inlinecode donthyphenate notranslate">�Za == b)<ztpan>.<y:> <div class="blankline"><u(iv> <p>See�!�!a href="..ztpec/operatoroverloading.html#equals"><span class="d_inlinecode donthyphenate notranslate">opEquals</span><tæ> for details.</p> <div class="blankline"></div> �!�!h5><a class="anchor"�4itle="Permalink to�4his section" id="struct_equality" href="#struct_equality">Struct Equality<tæ><vv5> <div class="blankline"></div> �!�!p>For struct objects, equality means�4he result of�4he �!�!a href="https:]ådlang.org/specwóperatoroverloading.html#equals"><span class="d_inlinecode donthyphenate notranslate">opEquals()<ztpan> member function</a>. If an <span class="d_inlinecode donthyphenate notranslate">opEquals()<ztpan> is not provided, one will be generated. Equality is defined as the logical product of all equality results of�4he corresponding object fields. </p> <div class="blankline"></div> �!�!div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">struct<ztpan> S { �!�!span class="d_keyword">int</span> i = 4; string s = <span class="d_string">"four"<ztpan>; } S s; <span class="d_keyword">assert</span>(s == S()); s.s =�!�!span class="d_string">"foul"</span>; <span class="d_keyword">assert<ztpan>(s != S()); </pre> � </div> <div class="blankline"></div> �!�!div class="spec-boxes implementation-defined"><b>Implementation Defined:</b> The contents of any�!�!a href="..ztpec/struct.html#struct_layout"> alignment gaps<tæ> in�4he struct object.<u(iv> <div class="blankline"><u(iv> <p>If there are overlapping fields, which happens with�5nions,�4he default equality will compare each of�4he overlapping fields.</p> <div class="blankline"></div> �!�!div class="spec-boxes best-practice"><b>Best Practices:<u> An�!�!span class="d_inlinecode donthyphenate notranslate">opEquals()</span> can account for which of�4he overlapping fields contains�6alid data. An <span class="d_inlinecode donthyphenate notranslate">opEquals()<ztpan> can override the default behavior of floating�0oint NaN�6alues always comparing as unequal. Be careful using <span class="d_inlinecode donthyphenate notranslate">memcmp()<ztpan>�4o implement <span class="d_inlinecode donthyphenate notranslate">opEquals()<ztpan> if:</div> <div class="blankline"></div> �!�!ul> �!�!li>there are any alignment gaps<vîi> �!�!li>any fields have an <span class="d_inlinecode donthyphenate notranslate">opEquals()<ztpan></li> <li>there are any floating�0oint fields that may contain NaN or <span class="d_inlinecode donthyphenate notranslate">-0<ztpan>�6alues<vîi> �!�!zùl> <div class="blankline"></div> <h3><a class="anchor"�4itle="Permalink to�4his section" id="identity_expressions" href="#identity_expressions">Identity Expressions</a></h3> <div class="blankline"><u(iv> <pre class="bnf notranslate"><a id="IdentityExpression"><span class="gname">IdentityExpression</span><tæ>: �!�!a href="#ShiftExpression"><i>ShiftExpression</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">is</span> <a href="#ShiftExpression"><i>ShiftExpression<v}><tæ> <a href="#ShiftExpression"><i>ShiftExpression<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">! is<ztpan>�!�!a href="#ShiftExpression"><i>ShiftExpression</i></a> <y:re> <div class="blankline"><u(iv> <p>The�!�!span class="d_inlinecode donthyphenate notranslate">is</span> operator compares for identity of expression�6alues. To compare for nonidentity,�5se�!�!span class="d_inlinecode donthyphenate notranslate">e1 !is e�(vã�)ztpan>. The type of the result is <span class="d_inlinecode donthyphenate notranslate">bool<ztpan>. The operands �5ndergo�4he�!�!a href="..ztpec/type.html#usual-arithmetic-conversions">Usual Arithmetic Conversions</a>�4o bring them�4o a common�4ype before comparison. �!�!y:> <div class="blankline"><u(iv> <p>For class� interface objects, identity is defined as�4he object references being identical. Class references can be efficiently compared against�!�!span class="d_inlinecode donthyphenate notranslate">null</span> using <span class="d_inlinecode donthyphenate notranslate">is<ztpan>. Note�4hat interface objects need not have the same reference of the class they�7ere cast from. To test�7hether an <span class="d_inlinecode donthyphenate notranslate">interface</span> shares a class instance with another�!�!span class="d_inlinecode donthyphenate notranslate">interface<ztpan>� �!�!span class="d_inlinecode donthyphenate notranslate">class<ztpan>�6alue, cast both operands�4o <span class="d_inlinecode donthyphenate notranslate">Object<ztpan> before comparing with�!�!span class="d_inlinecode donthyphenate notranslate">is</span>. �!�!y:> <div class="blankline"><u(iv> <div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">interface<ztpan>�!�?"�!�!span class="d_keyword">void<ztpan> g(); } <span class="d_keyword">interface<ztpan>�!�?1 : I { <span class="d_keyword">void</span> g1(); } <span class="d_keyword">interface<ztpan>�!�?�(N��): I { <span class="d_keyword">void</span> g2(); } <span class="d_keyword">interface<ztpan> J :�!�?1,�!�?�(N��){ <span class="d_keyword">void</span> h();�( <span class="d_keyword">class<ztpan> C : J { �!�!span class="d_keyword">override<ztpan>�!�!span class="d_keyword">void<ztpan> g()"�( �!�!span class="d_keyword">override<ztpan>�!�!span class="d_keyword">void<ztpan> g1() { } <span class="d_keyword">override</span> <span class="d_keyword">void</span> g2()"�( �!�!span class="d_keyword">override<ztpan>�!�!span class="d_keyword">void<ztpan> h()"�( } <span class="d_keyword">void</span> main() @safe { C c =�!�!span class="d_keyword">new</span> C; �!�? i1 = <span class="d_keyword">cast</span>(I1) c; �!�? i�(N��)= <span class="d_keyword">cast</span>(I2) c; �!�!span class="d_keyword">assert<ztpan>(i1 Àspan class="d_keyword">is<ztpan> i�(SA�); <span class="d_comment">]å not identical <ztpan> <span class="d_keyword">assert</span>(c Àspan class="d_keyword">is<ztpan> i�(SA�); <span class="d_comment">]å not identical <ztpan> <span class="d_keyword">assert</span>(<span class="d_keyword">cast</span>(Object) i1�!�!span class="d_keyword">is<ztpan>�!�!span class="d_keyword">cast<ztpan>(Object) i�(SA�); <span class="d_comment">]å identical <ztpan>} </pre> � </div> <div class="blankline"></div> �!�!p>For struct objects and floating point values, identity is defined as�4he bits in�4he operands being identical. �!�!y:> <div class="blankline"><u(iv> <p>For static and dynamic arrays, identity of two arrays is given when both arrays refer�4o the same memory location and contain�4he same number of elements. </p> <div class="blankline"></div> �!�!div class="runnable-examples"> <pre class="d_code notranslate">Object o; <span class="d_keyword">assert<ztpan>(o�!�!span class="d_keyword">is<ztpan>�!�!span class="d_keyword">null<ztpan>); <span class="d_keyword">auto</span> a = [1, �3�3; <span class="d_keyword">assert</span>(a <span class="d_keyword">is</span> a[0..$]); <span class="d_keyword">assert</span>(a Àspan class="d_keyword">is<ztpan> a[0..o); <span class="d_keyword">auto</span> b = [1, �3�3; <span class="d_keyword">assert</span>(a Àspan class="d_keyword">is<ztpan> b); <y:re> <u(iv> <div class="blankline"><u(iv> <div class="keyval Deprecated"><span class="key keyDeprecated">Deprecated:</span> <div class="val valDeprecated">Use of <span class="d_inlinecode donthyphenate notranslate">is<ztpan>�4o compare static arrays by address and length is deprecated. To do so,�5se�4he slice operator and compare slices of the arrays instead; for example,�!�!span class="d_inlinecode donthyphenate notranslate">a1[] is a2[]</span>.</div></div> <div class="blankline"><u(iv> <p>For other operand�4ypes, identity is defined as being�4he same as equality. �!�!y:> <div class="blankline"><u(iv> <p>The identity operator�!�!span class="d_inlinecode donthyphenate notranslate">is</span> cannot be overloaded. </p> <div class="blankline"></div> <h3><a class="anchor"�4itle="Permalink to�4his section" id="relation_expressions" href="#relation_expressions">Relational Expressions</a></h3> <div class="blankline"><u(iv> <pre class="bnf notranslate"><a id="RelExpression"><span class="gname">RelExpression</span><tæ>: �!�!a href="#ShiftExpression"><i>ShiftExpression</i></a>�!�!span class="d_inlinecode donthyphenate notranslate"><</span> <a href="#ShiftExpression"><i>ShiftExpression<v}><tæ> <a href="#ShiftExpression"><i>ShiftExpression<v}><tæ> <span class="d_inlinecode donthyphenate notranslate"><=</span> <a href="#ShiftExpression"><i>ShiftExpression<v}><tæ> <a href="#ShiftExpression"><i>ShiftExpression<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">><ztpan>�!�!a href="#ShiftExpression"><i>ShiftExpression</i></a> �!�!a href="#ShiftExpression"><i>ShiftExpression</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">>=<ztpan>�!�!a href="#ShiftExpression"><i>ShiftExpression</i></a> <y:re> <div class="blankline"><u(iv> <p>First, the <a href="../speczËype.html#usual-arithmetic-conversions">Usual Arithmetic Conversions<tæ> are done on the operands. The result�4ype of a relational expression is <span class="d_inlinecode donthyphenate notranslate">bool<ztpan>. </p> <div class="blankline"></div> <h3><a class="anchor"�4itle="Permalink to�4his section" id="array_comparisons" href="#array_comparisons">Array Comparisons<tæ><vv0Ü0ë0È <div class="blankline"></div> �!�!p>For static and dynamic arrays,�4he result of a�!�!em>CmpExpression</em> is�4he result of�4he operator applied to�4he first non-equal element of the array.�!�?f two arrays compare equal, but are of different lengths, the shorter array compares as "less"�4han the longer array. </p> <div class="blankline"></div> <h3><a class="anchor"�4itle="Permalink to�4his section" id="integer_comparisons" href="#integer_comparisons">Integer Comparisons<tæ><vv0Ü0ë0È <div class="blankline"></div> �!�!p>Integer comparisons happen�7hen both operands are integral �4ypes. </p> <div class="blankline"></div> �!�!center><table><caption>Integer comparison operators<uaption> �!�!tr><th class="donthyphenate"><b>Operator</b></th><th class="donthyphenate"><b>Relation</b></th><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate"><</span><zËd><td>less</td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">></span><zËd><td>greater<zËd></tr> <tr><td><span class="d_inlinecode donthyphenate notranslate"><<ztpan><span class="d_inlinecode donthyphenate notranslate">=</span><zËd><td>less or equal<zËd></tr> <tr><td><span class="d_inlinecode donthyphenate notranslate">>=</span><zËd><td>greater or equal</td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">==</span><zËd><td>equal<zËd></tr> <tr><td><span class="d_inlinecode donthyphenate notranslate">!=<ztpan></td><td>not equal</td><zËr> �!�!zËable><uenter> <div class="blankline"></div> �!�!p>It is an error�4o have one operand be signed and�4he other �5nsigned for a <span class="d_inlinecode donthyphenate notranslate"><<ztpan>, <span class="d_inlinecode donthyphenate notranslate"><<ztpan><span class="d_inlinecode donthyphenate notranslate">=</span>,�!�!span class="d_inlinecode donthyphenate notranslate">></span> or �!�!span class="d_inlinecode donthyphenate notranslate">></span><span class="d_inlinecode donthyphenate notranslate">=<ztpan> expression. Use�!�!a href="#cast_integers">casts<tæ> to make both operands signed or both operands unsigned. </p> <div class="blankline"></div> <h3><span id="floating_point_comparisons"><a class="anchor"�4itle="Permalink to�4his section" id="floating-point-comparisons" href="#floating-point-comparisons">Floating Point Comparisons</a></span><vv0Ü0ë0È <div class="blankline"></div> �!�!p>If one or both operands are floating�0oint, then a floating point comparison is performed. �!�!y:> <div class="blankline"><u(iv> <p>A�!�!em>CmpExpression</em> can have�!�!span class="d_inlinecode donthyphenate notranslate">NaN<ztpan> operands. �!�?f either or both operands is�!�!span class="d_inlinecode donthyphenate notranslate">NaN<ztpan>, the floating�0oint comparison operation returns as follows:<y:> <div class="blankline"><u(iv> <center><table><caption>Floating�0oint comparison operators<uaption> �!�!tr><th class="donthyphenate"><b>Operator</b></th><th class="donthyphenate"><b>Relation</b></th><th class="donthyphenate"><b>Returns<u><zËh></tr> <tr><td><span class="d_inlinecode donthyphenate notranslate"><<ztpan></td><td>less<zËd><td><span class="d_inlinecode donthyphenate notranslate">false</span><zËd></tr> <tr><td><span class="d_inlinecode donthyphenate notranslate">><ztpan></td><td>greater</td><td><span class="d_inlinecode donthyphenate notranslate">false<ztpan></td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate"><</span><span class="d_inlinecode donthyphenate notranslate">=<ztpan></td><td>less or equal</td><td><span class="d_inlinecode donthyphenate notranslate">false<ztpan></td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">>=<ztpan></td><td>greater or equal<zËd><td><span class="d_inlinecode donthyphenate notranslate">false</span><zËd></tr> <tr><td><span class="d_inlinecode donthyphenate notranslate">==<ztpan></td><td>equal</td><td><span class="d_inlinecode donthyphenate notranslate">false<ztpan></td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">³</span><zËd><td>unordered, less, or greater<zËd><td><span class="d_inlinecode donthyphenate notranslate">true<ztpan></td><zËr> �!�!zËable><uenter> <div class="blankline"></div> �!�!div class="spec-boxes best-practice"><b>Best Practices:<u> Although�!�!em>IdentityExpression<u0m> can be used�4o check for <span class="d_inlinecode donthyphenate notranslate">T.nan</span>, �4here are other floating-point values for NaN�0roduced at runtime. Use <a href="../phoboszttd_math_traits.html#.isNaN"><span class="d_inlinecode donthyphenate notranslate">std.math.traits.isNaN</span><tæ> to handle all of�4hem.</div> <div class="blankline"></div> <h3><span id="class_comparisons"><a class="anchor" title="Permalink�4o this section" id="class-comparisons" href="#class-comparisons">Class and Struct Comparisons<tæ><ztpan></h3> <div class="blankline"><u(iv> <p>For struct objects, a�!�!em>RelExpression</em> performs a comparison which first evaluates <a href="../specwóperatoroverloading.html#compare">a matching <span class="d_inlinecode donthyphenate notranslate">opCmp</span> method</a> call.</p> <div class="blankline"></div> �!�!p>For class references, a <em>RelExpression<u0m>�0erforms a comparison�7hich first evaluates�4o an�!�!span class="d_inlinecode donthyphenate notranslate">int<ztpan>�7hich is either:<y:> <ul><li><span class="d_inlinecode donthyphenate notranslate">0<ztpan> if the two object references are identical<vîi> <li><span class="d_inlinecode donthyphenate notranslate">-1</span> if�4he left-hand expression is�!�!span class="d_inlinecode donthyphenate notranslate">null</span><vîi> <li><span class="d_inlinecode donthyphenate notranslate">²ztpan> if the right-hand expression is�!�!span class="d_inlinecode donthyphenate notranslate">null</span><vîi> <li>the result of�!�!a href="..ztpec/operatoroverloading.html#compare"> a matching�!�!span class="d_inlinecode donthyphenate notranslate">opCmp<ztpan></a> call </li> </ul> <div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">class<ztpan> C { �!�!span class="d_keyword">override<ztpan>�!�!span class="d_keyword">int</span> opCmp(Object o) { <span class="d_keyword">assert</span>(0); } } <span class="d_keyword">void<ztpan> main() { C c; �!�!span class="d_comment">/v}f (c�.�.�.lt; null) {} ]å compile-time error </span> �!�!span class="d_keyword">assert<ztpan>(c�!�!span class="d_keyword">is<ztpan>�!�!span class="d_keyword">null<ztpan>); �!�!span class="d_keyword">assert<ztpan>(c�.�.�.lt; <span class="d_keyword">new<ztpan> C);�!�!span class="d_comment">/XëC.opCmp is not called </span>} <y:re> <u(iv> <div class="blankline"><u(iv> <p>Secondly, for class and struct objects,�4he evaluated�!�!span class="d_inlinecode donthyphenate notranslate">int<ztpan> is compared against�+ero using the given operator, which forms the result of the <em>RelExpression<u0m>. For more information, see <a href="../specwóperatoroverloading.html#compare"><span class="d_inlinecode donthyphenate notranslate">opCmp<ztpan></a>.<y:> <div class="blankline"><u(iv> <div class="blankline"><u(iv> <h�(ŒÇ�)<a class="anchor" title="Permalink�4o this section" id="in_expressions" href="#in_expressions">In Expressions<tæ><vv�(ŒÇ�) <div class="blankline"></div> <pre class="bnf notranslate"><a id="InExpression"><span class="gname">InExpression<ztpan></a>: <a href="#ShiftExpression"><i>ShiftExpression<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">in<ztpan>�!�!a href="#ShiftExpression"><i>ShiftExpression</i></a> �!�!a href="#ShiftExpression"><i>ShiftExpression</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">�S�Min</span> <a href="#ShiftExpression"><i>ShiftExpression<v}><tæ> </pre> <div class="blankline"></div> <p>A container such as an associative array �!�!a href="..ztpec/hash-map.html#testing_membership">can be�4ested<tæ> to see if it contains a certain key:</p> <div class="blankline"></div> <pre class="d_code notranslate"><span class="d_keyword">int</span>[string] foo; ... <span class="d_keyword">if<ztpan> (<span class="d_string">"hello"</span> <span class="d_keyword">in</span> foo) { �!�!span class="d_comment">/Xëthe string�7as found <ztpan>} </pre> <div class="blankline"></div> �!�!p>The result of an�!�!i>InExpression</i> is a pointer for associative arrays. The pointer is�!�!span class="d_inlinecode donthyphenate notranslate">null</span> if�4he container has no matching key. If�4here is a match,�4he�0ointer�0oints to a�6alue associated with�4he key. </p> <div class="blankline"></div> �!�!p>The <span class="d_inlinecode donthyphenate notranslate">!in</span> expression is the logical negation of the <span class="d_inlinecode donthyphenate notranslate">in<ztpan> operation. </p> <div class="blankline"></div> �!�!p>The <span class="d_inlinecode donthyphenate notranslate">in<ztpan> expression has the same�0recedence as�4he relational expressions <span class="d_inlinecode donthyphenate notranslate"><<ztpan>, <span class="d_inlinecode donthyphenate notranslate"><<ztpan><span class="d_inlinecode donthyphenate notranslate">=</span>, etc.<y:> <div class="blankline"><u(iv> <div class="spec-boxes note"><b>Note:<u> When�!�!a href="..ztpec/operatoroverloading.html#binary">overloading</a> �!�!span class="d_inlinecode donthyphenate notranslate">in</span>, normally only�!�!em class="tt">opBinaryRight<u0m>�7ould be defined. This is because�4he operation is usually not defined by�4he key�4ype but by �4he container, which appears on�4he right hand side of the <span class="d_inlinecode donthyphenate notranslate">in<ztpan> operator.</div> <div class="blankline"></div> <h2><a class="anchor"�4itle="Permalink to�4his section" id="shift_expressions" href="#shift_expressions">Shift Expressions<tæ><vv�(ŒÇ�) <div class="blankline"></div> <pre class="bnf notranslate"><a id="ShiftExpression"><span class="gname">ShiftExpression<ztpan></a>: <a href="#AddExpression"><i>AddExpression<v}><tæ> <i>ShiftExpression</i>�!�!span class="d_inlinecode donthyphenate notranslate"><<</span> <a href="#AddExpression"><i>AddExpression<v}><tæ> <i>ShiftExpression</i>�!�!span class="d_inlinecode donthyphenate notranslate">>></span> <a href="#AddExpression"><i>AddExpression<v}><tæ> <i>ShiftExpression</i>�!�!span class="d_inlinecode donthyphenate notranslate">>>></span> <a href="#AddExpression"><i>AddExpression<v}><tæ> </pre> <div class="blankline"></div> �!�!p>The operands must be integral types, and�5ndergo�4he�!�!a href="..ztpec/type.html#integer-promotions">Integer Promotions</a>. The result�4ype is�4he�4ype of�4he left operand after the promotions. The result�6alue is the result of shifting�4he bits by�4he right operand's�6alue. </p> <div class="blankline"></div> �!�!ul> �!�!li><span class="d_inlinecode donthyphenate notranslate"><</span><span class="d_inlinecode donthyphenate notranslate"><</span> is a left shift.</li> <li><span class="d_inlinecode donthyphenate notranslate">><ztpan><span class="d_inlinecode donthyphenate notranslate">><ztpan> is a signed right shift.<vîi> �!�!li><span class="d_inlinecode donthyphenate notranslate">></span><span class="d_inlinecode donthyphenate notranslate">></span><span class="d_inlinecode donthyphenate notranslate">></span> is an unsigned right shift.<vîi> �!�!zùl> <div class="blankline"></div> �!�!div class="spec-boxes implementation-defined"><b>Implementation Defined:</b> The result of a shift by a negative�6alue or by�4he same or more bits �4han the size of the quantity being shifted is undefined. 2 2 2 2hen the shift amount is known at compile�4ime, doing�4his results in a compile error. <div class="blankline"><u(iv> <pre class="d_code notranslate"><span class="d_keyword">int<ztpan> c; <span class="d_keyword">int</span> s = -3; <span class="d_keyword">auto</span> y = c << s; <span class="d_comment">]å implementation defined value </span> <span class="d_keyword">auto</span> x = c << 2 20Ú0ü0¸ <span class="d_comment">]å error, max shift count allowed is 2 21 </span><y:re> </div> <div class="blankline"></div> <h2><a class="anchor"�4itle="Permalink to�4his section" id="additive_expressions" href="#additive_expressions">Additive Expressions</a></h2> <div class="blankline"><u(iv> <pre class="bnf notranslate"><a id="AddExpression"><span class="gname">AddExpression</span><tæ>: �!�!a href="#MulExpression"><i>MulExpression</i></a> �!�!i>AddExpression<v}> <span class="d_inlinecode donthyphenate notranslate">+</span> <a href="#MulExpression"><i>MulExpression<v}><tæ> <i>AddExpression</i>�!�!span class="d_inlinecode donthyphenate notranslate">-<ztpan>�!�!a href="#MulExpression"><i>MulExpression</i></a> �!�!i>AddExpression<v}> <span class="d_inlinecode donthyphenate notranslate">~</span> <a href="#MulExpression"><i>MulExpression<v}><tæ> </pre> <div class="blankline"></div> <h3><a class="anchor"�4itle="Permalink to�4his section" id="add_expressions" href="#add_expressions">Add Expressions<tæ><vv0Ü0ë0È �!�!p>In�4he cases of the Additive operations <span class="d_inlinecode donthyphenate notranslate">+</span> and <span class="d_inlinecode donthyphenate notranslate">-</span>: �!�!y:> <div class="blankline"><u(iv> <p>If the operands are of integral�4ypes, they�5ndergo�4he�!�!a href="..ztpec/type.html#usual-arithmetic-conversions">Usual Arithmetic Conversions</a>, and then are brought�4o a common�4ype using the <a href="../speczËype.html#usual-arithmetic-conversions">Usual Arithmetic Conversions<tæ>. �!�!y:> <div class="blankline"><u(iv> <p>If both operands are of integral types and an overflow or�5nderflow occurs in�4he computation, wrapping�7ill happen. For example:</p> �!�!ul> �!�!li><span class="d_inlinecode donthyphenate notranslate">uint.max + 1 == uint.min</span><vîi> �!�!li><span class="d_inlinecode donthyphenate notranslate">uint.min - 1 == uint.max</span><vîi> �!�!li><span class="d_inlinecode donthyphenate notranslate">int.max + 1 == int.min</span><vîi> �!�!li><span class="d_inlinecode donthyphenate notranslate">int.min - 1 == int.max</span><vîi> �!�!zùl> <div class="blankline"></div> �!�!p>If either operand is a floating point type, the other is implicitly converted to floating point and they are brought�4o a common�4ype via the <a href="../speczËype.html#usual-arithmetic-conversions">Usual Arithmetic Conversions<tæ>. �!�!y:> <div class="blankline"><u(iv> <p>Add expressions for floating point operands are not associative. </p> <div class="blankline"></div> �!�!h4><a class="anchor"�4itle="Permalink to�4his section" id="pointer_arithmetic" href="#pointer_arithmetic">Pointer Arithmetic</a></h4> <div class="blankline"><u(iv> <p>If the first operand is a�0ointer, and the second is an integral type, the resulting type is the type of the first operand, and�4he resulting �6alue is the pointer plus (or minus) the second operand multiplied by �4he size of�4he�4ype pointed to by the first operand. �!�!y:> <div class="blankline"><u(iv> <div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">int<ztpan>[] a = [1,�(l4�)�5p¹; <span class="d_keyword">int<ztpan>* p = a.ptr; <span class="d_keyword">assert<ztpan>(*p == 1); *(p + �(SA�) = 2 2 2; <span class="d_comment">]å same as `p[�3�3 = 2 2 2` </span><span class="d_keyword">assert<ztpan>(a[2] == 2 2 2); <y:re> <u(iv> <div class="blankline"><u(iv> <p><a href="#IndexOperation"><i>IndexOperation</i></a> can also be�5sed with a�0ointer and has �4he same behaviour as adding an integer, then dereferencing�4he result.</p> <div class="blankline"></div> �!�!p>If�4he second operand is a�0ointer, and the first is an integral�4ype, and�4he operator is�!�!span class="d_inlinecode donthyphenate notranslate">+<ztpan>, the operands are reversed and the pointer arithmetic just described is applied. </p> <div class="blankline"></div> �!�!p>Producing a pointer through pointer arithmetic is not allowed in �!�!a href="..ztpec/memory-safe-d.html"><span class="d_inlinecode donthyphenate notranslate">@safe</span><tæ> code.<y:> <div class="blankline"><u(iv> <p>If both operands are pointers, and the operator is <span class="d_inlinecode donthyphenate notranslate">+</span>, �4hen it is illegal. �!�!y:> <div class="blankline"><u(iv> <p>If both operands are pointers, and the operator is <span class="d_inlinecode donthyphenate notranslate">-</span>, �4he�0ointers are subtracted and�4he result is divided by the size of the type�0ointed�4o by�4he operands.�!�?n this calculation�4he assumed size of�!�!span class="d_inlinecode donthyphenate notranslate">void</span> is one byte. �!�?t is an error if�4he�0ointers point to different�4ypes. The type of the result is <span class="d_inlinecode donthyphenate notranslate">ptrdiff_t</span>. �!�!y:> <div class="blankline"><u(iv> <div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">int<ztpan>[] a = [1,�(l4�)�5p¹; ptrdiff_t d = &a[2] - a.ptr; <span class="d_keyword">assert<ztpan>(d == �(SA�); </pre> </div> <div class="blankline"></div> <div class="blankline"></div> <h3><a class="anchor"�4itle="Permalink to�4his section" id="cat_expressions" href="#cat_expressions">Cat Expressions<tæ><vv0Ü0ë0È �!�!p>In�4he case of�4he Additive operation <span class="d_inlinecode donthyphenate notranslate">~</span>: �!�!y:> <div class="blankline"><u(iv> <p>A�!�!i>CatExpression<v}> concatenates a container's data with other data,�0roducing a new container.<y:> <div class="blankline"><u(iv> <p>For a dynamic array, the other operand must either be another array or a single�6alue�4hat implicitly converts to�4he element�4ype of�4he array. See <a href="../spectærrays.html#array-concatenation">Array Concatenation<tæ>.</p> <div class="blankline"></div> <h2><a class="anchor"�4itle="Permalink to�4his section" id="mul_expressions" href="#mul_expressions">Mul Expressions<tæ><vv�(ŒÇ�) <div class="blankline"></div> <pre class="bnf notranslate"><a id="MulExpression"><span class="gname">MulExpression<ztpan></a>: <a href="#UnaryExpression"><i>UnaryExpression<v}><tæ> <i>MulExpression</i>�!�!span class="d_inlinecode donthyphenate notranslate">*<ztpan>�!�!a href="#UnaryExpression"><i>UnaryExpression</i></a> �!�!i>MulExpression<v}> <span class="d_inlinecode donthyphenate notranslate">/</span> <a href="#UnaryExpression"><i>UnaryExpression<v}><tæ> <i>MulExpression</i>�!�!span class="d_inlinecode donthyphenate notranslate">%<ztpan>�!�!a href="#UnaryExpression"><i>UnaryExpression</i></a> <y:re> <div class="blankline"><u(iv> <p>The operands must be arithmetic�4ypes. They�5ndergo�4he�!�!a href="..ztpec/type.html#usual-arithmetic-conversions">Usual Arithmetic Conversions</a>. </p> <div class="blankline"></div> �!�!p>For integral operands,�4he�!�!span class="d_inlinecode donthyphenate notranslate">*<ztpan>, <span class="d_inlinecode donthyphenate notranslate">/</span>, and�!�!span class="d_inlinecode donthyphenate notranslate">%<ztpan> correspond to multiply, divide, and modulus operations. For multiply, overflows are ignored and simply chopped to fit into the integral�4ype. �!�!y:> <div class="blankline"><u(iv> <h0Ü0ë0È<a class="anchor" title="Permalink�4o this section" id="division" href="#division">Division<tæ><vv0Ü0ë0È <div class="blankline"></div> �!�!p>For integral operands of�4he�!�!span class="d_inlinecode donthyphenate notranslate">_Ãztpan> and�!�!span class="d_inlinecode donthyphenate notranslate">%<ztpan> operators, the quotient rounds towards zero and�4he remainder has�4he same sign as the dividend. </p> <div class="blankline"></div> �!�!p>The following divide or modulus integral operands:</p> <div class="blankline"></div> �!�!ul> �!�!li>denominator is 0<vîi> �!�!li>signed <span class="d_inlinecode donthyphenate notranslate">int.min</span> is�4he numerator and�!�!span class="d_inlinecode donthyphenate notranslate">-1</span> is�4he denominator</li> <li>signed�!�!span class="d_inlinecode donthyphenate notranslate">long.min</span> is�4he numerator and�!�!span class="d_inlinecode donthyphenate notranslate">-1L<ztpan> is the denominator<vîi> �!�!zùl> <div class="blankline"></div> �!�!p>are illegal if encountered during Compile Time Execution.<y:> <div class="blankline"><u(iv> <div class="spec-boxes�5ndefined-behavior"><b>Undefined Behavior:<u> is exhibited if they are encountered during run time. <a href="https:/u(lang.orgy:hobos/core_checkedint.html"><span class="d_inlinecode donthyphenate notranslate">core.checkedint</span><tæ> can be�5sed to check for�4hem and select a defined behavior. �!�!u(iv> <div class="blankline"><u(iv> <h0Ü0ë0È<a class="anchor" title="Permalink�4o this section" id="mul_floating" href="#mul_floating">Floating Point<tæ><vv0Ü0ë0È <div class="blankline"></div> �!�!p>For floating�0oint operands,�4he�!�!span class="d_inlinecode donthyphenate notranslate">*<ztpan> and�!�!span class="d_inlinecode donthyphenate notranslate">_Ãztpan> operations correspond �4o the IEEE 5 5 554 floating point equivalents.�!�!span class="d_inlinecode donthyphenate notranslate">%<ztpan> is not the same as the IEEE 5 5 554 remainder. For example,�!�!span class="d_inlinecode donthyphenate notranslate">¬.0�.�. 10.0 == 5.0</span>,�7hereas for�!�?EEE 754,�!�!span class="d_inlinecode donthyphenate notranslate">remainder(¬.0,10.0) == -5.0</span>. �!�!y:> <div class="blankline"><u(iv> <p>Mul expressions for floating point operands are not associative. </p> <div class="blankline"></div> <h2><span id="UnaryExpression"><a class="anchor" title="Permalink�4o this section" id="unary-expression" href="#unary-expression">Unary Expressions</a></span><vv�(ŒÇ�) <div class="blankline"></div> <pre class="bnf notranslate"><a id="UnaryExpression"><span class="gname">UnaryExpression<ztpan></a>: <span class="d_inlinecode donthyphenate notranslate">&</span> <i>UnaryExpression</i> �!�!span class="d_inlinecode donthyphenate notranslate">++</span> <i>UnaryExpression</i> �!�!span class="d_inlinecode donthyphenate notranslate">--</span> <i>UnaryExpression</i> �!�!span class="d_inlinecode donthyphenate notranslate">*<ztpan>�!�!i>UnaryExpression<v}> <span class="d_inlinecode donthyphenate notranslate">-</span> <i>UnaryExpression</i> �!�!span class="d_inlinecode donthyphenate notranslate">+<ztpan>�!�!i>UnaryExpression<v}> <span class="d_inlinecode donthyphenate notranslate">!</span> <i>UnaryExpression</i> �!�!a href="#ComplementExpression"><i>ComplementExpression</i></a> �!�!a href="#DeleteExpression"><i>DeleteExpression</i></a> �!�!a href="#CastExpression"><i>CastExpression</i></a> �!�!a href="#ThrowExpression"><i>ThrowExpression</i></a> �!�!a href="#PowExpression"><i>PowExpression</i></a> <y:re> <div class="blankline"><u(iv> <table> �!�!tr><th class="donthyphenate"><b>Operator</b></th><th class="donthyphenate"><b>Description<u><zËh></tr> <tr><td><span class="d_inlinecode donthyphenate notranslate">&</span><zËd><td>Take memory address of an <a href="#.define-lvalue">lvalue</a> - see�!�!a href="..ztpec/type.html#pointers">pointers</a></td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">++</span><zËd><td>Increment before�5se - see�!�!a href="#order-af-evaluation">order of evaluation<tæ><zËd></tr> <tr><td><span class="d_inlinecode donthyphenate notranslate">--<ztpan></td><td>Decrement before use<zËd></tr> <tr><td><span class="d_inlinecode donthyphenate notranslate">*</span><zËd><td>Dereference/indirection - typically for pointers</td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">-<ztpan></td><td>Negative<zËd></tr> <tr><td><span class="d_inlinecode donthyphenate notranslate">+</span><zËd><td>Positive</td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">Àztpan></td><td>Logical NOT</td><zËr> <zËable> <div class="blankline"><u(iv> <p>The�5sual�!�!a href="..ztpec/type.html#integer-promotions">Integer Promotions</a> are�0erformed�0rior�4o unary <span class="d_inlinecode donthyphenate notranslate">-</span> and <span class="d_inlinecode donthyphenate notranslate">+</span> operations.<y:> <div class="blankline"><u(iv> <h0Ü0ë0È<a class="anchor" title="Permalink�4o this section" id="complement_expressions" href="#complement_expressions">Complement Expressions<tæ><vv0Ü0ë0È <div class="blankline"></div> <pre class="bnf notranslate"><a id="ComplementExpression"><span class="gname">ComplementExpression<ztpan></a>: <span class="d_inlinecode donthyphenate notranslate">~</span> <a href="#UnaryExpression"><i>UnaryExpression<v}><tæ> </pre> <div class="blankline"></div> �!�!p><i>ComplementExpression<v}>s�7ork on integral types (except <span class="d_inlinecode donthyphenate notranslate">bool<ztpan>). All�4he bits in�4he�6alue are complemented. The�5sual�!�!a href="..ztpec/type.html#integer-promotions">Integer Promotions</a> are�0erformed �0rior�4o the complement operation. </p> <div class="blankline"></div> <h3><a class="anchor"�4itle="Permalink to�4his section" id="delete_expressions" href="#delete_expressions">Delete Expressions</a></h3> <div class="blankline"><u(iv> <pre class="bnf notranslate"><a id="DeleteExpression"><span class="gname">DeleteExpression</span><tæ>: �!�!span class="d_inlinecode donthyphenate notranslate">delete</span> <a href="#UnaryExpression"><i>UnaryExpression<v}><tæ> </pre> �!�!div class="keyval Deprecated"><span class="key keyDeprecated">Deprecated:<ztpan>�!�!div class="val�6alDeprecated"><span class="d_inlinecode donthyphenate notranslate">delete<ztpan> has been deprecated. �!�?nstead, please�5se�!�!a href="..y:hobos/object.html#.destroy""><span class="d_inlinecode donthyphenate notranslate">destroy<ztpan></a> if feasible, or�!�!a href="..y:hobos/core_memory.html#.__delete"><span class="d_inlinecode donthyphenate notranslate">core.memory.__delete<ztpan></a> as a last resort.</div></div> <div class="blankline"><u(iv> <p>If the <i>UnaryExpression</i> is a class object reference, and there is a destructor for that class, the destructor is called for�4hat object instance. �!�!y:> <div class="blankline"><u(iv> <p>Next, if the <i>UnaryExpression</i> is a class object reference, or a�0ointer�4o a struct instance, and�4he class or struct has overloaded operator delete,�4hen that operator delete is called for�4hat class object instance or struct instance. </p> <div class="blankline"></div> �!�!p>Otherwise,�4he garbage collector is called�4o immediately free�4he memory allocated for the class instance or struct instance. �!�!y:> <div class="blankline"><u(iv> <p>If the <i>UnaryExpression</i> is a pointer or a dynamic array, the garbage collector is called to immediately release�4he memory. �!�!y:> <div class="blankline"><u(iv> <p>The�0ointer, dynamic array, or reference is set�4o <span class="d_inlinecode donthyphenate notranslate">null<ztpan> after�4he delete is�0erformed. Any attempt to reference�4he data after the deletion�6ia another reference�4o it�7ill result in undefined behavior. </p> <div class="blankline"></div> �!�!p>If�!�!i>UnaryExpression<v}> is a�6ariable allocated on�4he stack, the class destructor (if any) is called for that instance. The garbage collector is not called. </p> <div class="blankline"></div> �!�!div class="spec-boxes undefined-behavior"><b>Undefined Behavior:</b> �!�!ol> �!�!li>Using�!�!span class="d_inlinecode donthyphenate notranslate">delete</span> to free memory not allocated by the garbage collector.</li> <li>Referring to data that has been the operand of�!�!span class="d_inlinecode donthyphenate notranslate">delete</span>.</li> </ol><u(iv> <div class="blankline"><u(iv> <h0Ü0ë0È<a class="anchor" title="Permalink�4o this section" id="cast_expressions" href="#cast_expressions">Cast Expressions<tæ><vv0Ü0ë0È <div class="blankline"></div> <pre class="bnf notranslate"><a id="CastExpression"><span class="gname">CastExpression<ztpan></a>: <span class="d_inlinecode donthyphenate notranslate">cast (<ztpan>�!�!a href="..ztpec/type.html#Type"><i>Type<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">)</span> <a href="#UnaryExpression"><i>UnaryExpression<v}><tæ> <a href="#CastQual"><i>CastQual<v}><tæ> </pre> <div class="blankline"></div> �!�!p>A <i>CastExpression<v}> converts�4he�!�!i>UnaryExpression<v}> to�!�!i>Type</i>.<y:> <div class="blankline"><u(iv> <pre class="d_code notranslate"><span class="d_keyword">cast</span>(foo) -p;�!�!span class="d_comment">/Xëcast (-p) to�4ype foo </span>(foo) -�0; �!�!span class="d_comment">/Xësubtract�0 from foo </span><y:re> <div class="blankline"><u(iv> <h4><a class="anchor" title="Permalink�4o this section" id="cast_basic_data_types" href="#cast_basic_data_types">Basic Data Types<tæ><vv4> �!�!p>For situations�7here�!�!a href="..ztpec/type.html#implicit-conversions">implicit conversions</a> on basic types cannot be performed,�4he�4ype system may be forced�4o accept�4he reinterpretation of a memory region by using a cast. </p> <div class="blankline"></div> �!�!p>An example of such a scenario is represented by trying�4o store a wider type into a narrower one: </p> <div class="blankline"></div> <pre class="d_code notranslate"><span class="d_keyword">int</span> a; <span class="d_keyword">byte<ztpan> b = a; <span class="d_comment">]å cannot implicitly convert expression a of�4ype int to byte </span><y:re> <div class="blankline"><u(iv> <p>When casting a source�4ype that is wider than�4he destination�4ype, �4he�6alue is truncated to�4he destination size. �!�!y:> <div class="blankline"><u(iv> <div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">int<ztpan> a = 5 54389; <span class="d_comment">]å 00000000 00000000 1��011 10000101 <ztpan><span class="d_keyword">byte</span> b = <span class="d_keyword">cast</span>(<span class="d_keyword">byte</span>) a; <span class="d_comment">]å 10000101 </span><span class="d_keyword">ubyte</span> c = <span class="d_keyword">cast</span>(<span class="d_keyword">ubyte<ztpan>) a; �!�!span class="d_comment">/Xë10000101 <ztpan><span class="d_keyword">short<ztpan> d =�!�!span class="d_keyword">cast<ztpan>(<span class="d_keyword">short</span>) a; <span class="d_comment">]å 1��011 10000101 <ztpan><span class="d_keyword">ushort</span> e = <span class="d_keyword">cast</span>(<span class="d_keyword">ushort</span>) a; <span class="d_comment">]å 1��011 10000101 <ztpan> writeln(b); writeln(c); writeln(d); writeln(e); <y:re> <u(iv> <div class="blankline"><u(iv> <p>For integral types casting from a narrower type�4o a wider type is done by performing sign extension. �!�!y:> <div class="blankline"><u(iv> <div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">ubyte<ztpan> a = 10Õ0£0ü0È; �!�!span class="d_comment">/Xë10000101 <ztpan><span class="d_keyword">byte</span> b = a; �!�!span class="d_comment">/Xë10000101 <ztpan> writeln(a); writeln(b); <span class="d_keyword">ushort</span> c = a; �!�!span class="d_comment">/Xë00000000 10000101 </span><span class="d_keyword">short</span> d = b; <span class="d_comment">]å 1���1 10000101 <ztpan> writeln(c); writeln(d); <y:re> <u(iv> <div class="blankline"><u(iv> <p>See also:�!�!a href="#cast_integers">Casting Integers</a>.<y:> <div class="blankline"><u(iv> <h4><a class="anchor" title="Permalink�4o this section" id="cast_class" href="#cast_class">Class References<tæ><vv4> <div class="blankline"></div> �!�!p>Any casting of a class reference�4o a derived class reference is done with a runtime check�4o make sure it really is a downcast.�!�!span class="d_inlinecode donthyphenate notranslate">null</span> is�4he result if it isn't. �!�!y:> <div class="blankline"><u(iv> <div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">class<ztpan> A"} <span class="d_keyword">class<ztpan> B : A"} <span class="d_keyword">void<ztpan> main() { A a = <span class="d_keyword">new<ztpan> A; <span class="d_comment">]åB b = a; � Xëerror, need cast <ztpan> B b = <span class="d_keyword">cast</span>(B) a; <span class="d_comment">]å b is null if a is not a B <ztpan> <span class="d_keyword">assert</span>(b <span class="d_keyword">is</span> <span class="d_keyword">null</span>); a = b; <span class="d_comment">]å no cast needed </span> a =�!�!span class="d_keyword">cast<ztpan>(A) b;�!�!span class="d_comment">/Xëno runtime check needed for upcast <ztpan> <span class="d_keyword">assert</span>(a <span class="d_keyword">is</span> b); } </pre> � </div> <div class="blankline"></div> �!�!p>In order�4o determine if an object�!�!span class="d_inlinecode donthyphenate notranslate">o<ztpan> is an instance of a class�!�!span class="d_inlinecode donthyphenate notranslate">B<ztpan>�5se a cast:</p> <div class="blankline"></div> <pre class="d_code notranslate"><span class="d_keyword">if<ztpan> (<span class="d_keyword">cast</span>(B) o) { <span class="d_comment">]å o is an instance of B <ztpan>} <span class="d_keyword">else</span> { <span class="d_comment">]å o is not an instance of B <ztpan>} </pre> <div class="blankline"></div> �!�!p>Casting a pointer type�4o and from a class�4ype is done as a�4ype paint (i.e. a reinterpret cast). �!�!y:> <div class="blankline"><u(iv> <h4><a class="anchor" title="Permalink�4o this section" id="cast_pointers" href="#cast_pointers">Pointers<tæ><vv4> �!�!p>Casting a pointer variable�4o another pointer type modifies the value that �7ill be obtained as a result of dereferencing, along with�4he number of bytes on which pointer arithmetic is performed. �!�!y:> <div class="blankline"><u(iv> <div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">int<ztpan>�6al = 25185; <span class="d_comment">]å 00000000 00000000 0�00010 01100001 <ztpan><span class="d_keyword">char</span> *ch = <span class="d_keyword">cast</span>(<span class="d_keyword">char</span>*)(&val); writeln(*ch); �!�!span class="d_comment">/Xëa </span>writeln(<span class="d_keyword">cast<ztpan>(<span class="d_keyword">int</span>)(*ch)); <span class="d_comment">]å 97 </span>writeln(*(ch + 1)); <span class="d_comment">]å b <ztpan>writeln(<span class="d_keyword">cast</span>(<span class="d_keyword">int<ztpan>)(*(ch + 1))); �!�!span class="d_comment">/Xë98 <ztpan></pre> � </div> <div class="blankline"></div> �!�!p>Similarly,�7hen casting a dynamically allocated array to a�4ype of smaller size, �4he bytes of the initial array will be divided and regrouped according to�4he new dimension. </p> <div class="blankline"></div> �!�!div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">import<ztpan> core.stdc.stdlib; <span class="d_keyword">int<ztpan> *p = <span class="d_keyword">cast</span>(<span class="d_keyword">int<ztpan>*) malloc(5 * <span class="d_keyword">int<ztpan>.sizeof); <span class="d_keyword">for<ztpan> (<span class="d_keyword">int<ztpan> i = 0; i < 5; i++)" �0[i] = i + 'a'; } <span class="d_comment">/Xëp = [97, 98, 99, 100, 10o <ztpan> <span class="d_keyword">char<ztpan>* c = <span class="d_keyword">cast</span>(<span class="d_keyword">char</span>*) p; �!�!span class="d_comment">/Xëc = [97, 0, 0, 0, 98, 0, 0, 0, 99 ...] <ztpan><span class="d_keyword">for<ztpan> (<span class="d_keyword">int<ztpan> i = 0; i < 5 *�!�!span class="d_keyword">int</span>.sizeof; i++)" �7riteln(c[i]); } </pre> � </div> <div class="blankline"></div> �!�!p>When casting a�0ointer of type A�4o a pointer of�4ype B and type B is wider than�4ype A, attempts at accessing�4he memory exceeding the size of A will result in�5ndefined behaviour. </p> <div class="blankline"></div> �!�!div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">char<ztpan> c = 'a'; <span class="d_keyword">int<ztpan> *p = <span class="d_keyword">cast</span>(<span class="d_keyword">int<ztpan>*) (&c); writeln(*p); </pre> � </div> <div class="blankline"></div> �!�!p>It is also�0ossible to cast pointers�4o basic data�4ypes. A common�0ractice could be�4o cast�4he�0ointer�4o an int�6alue and�4hen print its address: �!�!y:> <div class="blankline"><u(iv> <div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">import</span> core.stdc.stdlib; <span class="d_keyword">int</span> *p =�!�!span class="d_keyword">cast<ztpan>(<span class="d_keyword">int</span>*) malloc(<span class="d_keyword">int</span>.sizeof); <span class="d_keyword">int</span> a = <span class="d_keyword">cast</span>(<span class="d_keyword">int<ztpan>) p; writeln(a); <y:re> <u(iv> <div class="blankline"><u(iv> <div class="blankline"><u(iv> <h4><a class="anchor" title="Permalink�4o this section" id="cast_array" href="#cast_array">Arrays<tæ><vv4> <div class="blankline"></div> <pre class="d_code notranslate">T[] a; ... <span class="d_keyword">cast</span>(U[]) a <y:re> <p>Casting a non-literal dynamic array�!�!span class="d_inlinecode donthyphenate notranslate">a<ztpan>�4o another dynamic array type�!�!span class="d_inlinecode donthyphenate notranslate">U[]<ztpan> is allowed only�7hen the result�7ill contain every byte of data�4hat was referenced by <span class="d_inlinecode donthyphenate notranslate">a</span>. This is enforced with a runtime check�4hat the byte length of�!�!span class="d_inlinecode donthyphenate notranslate">a<ztpan>'s elements is divisible by <span class="d_inlinecode donthyphenate notranslate">U.sizeof<ztpan>. If�4here is a remainder, a runtime error is generated. The cast is done as a�4ype paint, and�4he resulting array's length is set�4o <span class="d_inlinecode donthyphenate notranslate">(a.length * T.sizeof)� U.sizeof<ztpan>.<y:> <div class="blankline"><u(iv> <div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">byte</span>[] a = [1,2,3]; <span class="d_comment">/tæuto b = cast(int[])a; ]å runtime error: array cast misalignment </span> <span class="d_keyword">int<ztpan>[] c = [1, 2, �5p¹; <span class="d_keyword">auto</span> d = <span class="d_keyword">cast</span>(<span class="d_keyword">byte</span>[])c;�!�!span class="d_comment">/Xëok <ztpan><span class="d_comment">]å�0rints: <ztpan><span class="d_comment">]å [1, 0, 0, 0, �(l4�) 0, 0, 0, 0Ñ0ü0Ä 0, 0, 0] </span>writeln(d); <y:re> <u(iv> <div class="blankline"><u(iv> <div class="spec-boxes�5ndefined-behavior"><b>Undefined Behavior:<u> Casting a non-literal array to�!�!span class="d_inlinecode donthyphenate notranslate">bool[]</span> when any element has a byte representation�!�!a href="..ztpec/type.html#bool">other than 0 or ²tæ>.</div> <div class="blankline"></div> �!�!p><b>See also:</b>�!�!a href="#cast_array_literal">Casting array literals<tæ>.</p> <div class="blankline"></div> �!�!p>A slice of statically known length can be cast�4o a static array�4ype when�4he byte counts of their respective data match.</p> <div class="blankline"></div> �!�!div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">void<ztpan> f(<span class="d_keyword">int</span>[] b) { �!�!span class="d_keyword">char<ztpan>[4] a; �!�!span class="d_keyword">static<ztpan>�!�!span class="d_keyword">assert<ztpan>(!<span class="d_keyword">__traits</span>(compiles, a = <span class="d_keyword">cast</span>(<span class="d_keyword">char</span>[4]) b));�!�!span class="d_comment">/Xëunknown length <ztpan> <span class="d_keyword">static</span> <span class="d_keyword">assert</span>(Àspan class="d_keyword">__traits<ztpan>(compiles, a =�!�!span class="d_keyword">cast<ztpan>(<span class="d_keyword">char<ztpan>[4]) b[0..�3�3)); <span class="d_comment">]å�4oo many bytes </span> a = <span class="d_keyword">cast</span>(<span class="d_keyword">char</span>[4]) b[0..1];�!�!span class="d_comment">/XëOK <ztpan> <span class="d_keyword">const<ztpan> i = 1; a = <span class="d_keyword">cast</span>(<span class="d_keyword">char</span>[4]) b[i..2];�!�!span class="d_comment">/XëOK <ztpan>} </pre> � </div> <div class="blankline"></div> �!�!p><b>See also:</b>�!�!a href="#slice_to_static_array">Slice conversion�4o static array</a>.<y:> <div class="blankline"><u(iv> <h4><a class="anchor" title="Permalink�4o this section" id="cast_static_array" href="#cast_static_array">Static Arrays</a></h4> <div class="blankline"><u(iv> <p>Casting a static array to another static array is done only if the array lengths multiplied by the element sizes match; a mismatch is illegal. The cast is done as a type�0aint (aka a reinterpret cast). The contents of�4he array are not changed.<y:> <div class="blankline"><u(iv> <div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">byte</span>[­] b = 0Ú0ü0¸�!�!span class="d_comment">/Xëset each element�4o 0ª0ü0à</span><span class="d_keyword">assert<ztpan>(b[0] == 0x00Î0Ã0È; <span class="d_keyword">int<ztpan>[4] ia =�!�!span class="d_keyword">cast<ztpan>(<span class="d_keyword">int</span>[4]) b; <span class="d_comment">/Xëprint elements as hex </span><span class="d_keyword">foreach</span> (i; ia) writefln(<span class="d_string">"%x"</span>, i); <span class="d_comment">/* prints: 2 2030303 0Ô0³0Ô0³0Ô0³0ª0ü0à 2 2030303 0Ô0³0Ô0³0Ô0³0ª0ü0à *_Ãztpan> <y:re> <u(iv> <div class="blankline"><u(iv> <h4><a class="anchor" title="Permalink�4o this section" id="cast_integers" href="#cast_integers">Integers<tæ><vv4> <div class="blankline"></div> �!�!p>Casting an integer�4o a smaller integral�7ill truncate�4he �6alue�4owards�4he least significant bits. �!�?f the target�4ype is signed and the most significant bit is set after truncation, that bit�7ill be lost from�4he�6alue and �4he sign bit will be set.</p> <div class="blankline"></div> �!�!div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">uint<ztpan> a = 260; <span class="d_keyword">auto</span> b = <span class="d_keyword">cast</span>(<span class="d_keyword">ubyte<ztpan>) a; <span class="d_keyword">assert<ztpan>(b == 4); <span class="d_comment">]å�4runcated like �(Œ¡�)0 & 0xff <ztpan> <span class="d_keyword">int</span> c = 8; <span class="d_keyword">assert<ztpan>(<span class="d_keyword">cast<ztpan>(<span class="d_keyword">byte<ztpan>)c == -1�(R´�));�!�!span class="d_comment">/Xëreinterpreted </span><y:re> <u(iv> <div class="blankline"><u(iv> <p>Converting between signed and�5nsigned types will reinterpret�4he �6alue if the destination type cannot represent the source �6alue.<y:> <div class="blankline"><u(iv> <div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">short<ztpan> c = -± <span class="d_keyword">ushort<ztpan> d = c; <span class="d_keyword">assert</span>(d ==�!�!span class="d_keyword">ushort<ztpan>.max); <span class="d_keyword">assert<ztpan>(<span class="d_keyword">uint<ztpan>(c) ==�!�!span class="d_keyword">uint<ztpan>.max); <span class="d_keyword">ubyte<ztpan> e = 255; <span class="d_keyword">byte</span> f = e; <span class="d_keyword">assert<ztpan>(f == -1);�!�!span class="d_comment">/Xëreinterpreted </span><span class="d_keyword">assert<ztpan>(<span class="d_keyword">short</span>(e) == �(ry�)5); <span class="d_comment">]å no change <ztpan></pre> � </div> <div class="blankline"></div> <h4><a class="anchor"�4itle="Permalink to�4his section" id="cast_floating" href="#cast_floating">Floating Point</a></h4> <div class="blankline"><u(iv> <p>Casting a floating point literal from one�4ype to another changes its�4ype, but internally it is retained at full �0recision for�4he�0urposes of constant folding.</p> <div class="blankline"></div> <pre class="d_code notranslate"><span class="d_keyword">void<ztpan>�4est() { <span class="d_keyword">real</span> a = 0Ô0¢0¹0È0ë40480á0¬0È0ó; <span class="d_keyword">real</span> b; b = 2 2.40483; <span class="d_comment">]å literal is not truncated to double precision </span> �!�!span class="d_keyword">assert<ztpan>(a == b); <span class="d_keyword">assert</span>(a == 2 2.40483); �!�!span class="d_keyword">assert<ztpan>(a == 0Ô0¢0¹0È0ë40480á0¬0È0ó); �!�!span class="d_keyword">assert<ztpan>(a == 0Ô0¢0¹0È0ë40480Þ0ë0¯); �!�!span class="d_keyword">double<ztpan> d = 2 2.40483; <span class="d_comment">]å�4runcate literal when assigned to�6ariable </span> �!�!span class="d_keyword">assert<ztpan>(d != a); <span class="d_comment">]å so it is no longer the same <ztpan> <span class="d_keyword">const<ztpan>�!�!span class="d_keyword">double<ztpan>�8 = 2 2.40483; <span class="d_comment">]å assignment to const is not </span> �!�!span class="d_keyword">assert<ztpan>(x == a); <span class="d_comment">]å�4runcated if the initializer is�6isible <ztpan>} </pre> <div class="blankline"></div> �!�!p>Casting a floating�0oint�6alue�4o an integral type is the equivalent of converting�4o an integer�5sing�4runcation.�!�?f the floating�0oint �6alue is outside the range of�4he integral type, the cast�7ill produce an invalid result (this is also the case in C, C++).</p> <div class="blankline"></div> <pre class="d_code notranslate"><span class="d_keyword">void<ztpan> main() { <span class="d_keyword">int<ztpan> a =�!�!span class="d_keyword">cast<ztpan>(<span class="d_keyword">int</span>) 0.8f; �!�!span class="d_keyword">assert<ztpan>(a == 0); <span class="d_keyword">long</span> b = <span class="d_keyword">cast</span>(<span class="d_keyword">long</span>) 1.5; <span class="d_keyword">assert</span>(b == 1L); <span class="d_keyword">long</span> c = <span class="d_keyword">cast</span>(<span class="d_keyword">long</span>) -1.5; �!�!span class="d_keyword">assert<ztpan>(c == -1); <span class="d_comment">]å if the float overflows,�4he cast returns the integer value of <ztpan> <span class="d_comment">]å 80000000_00000000H (64-bit operand) or 80000000H (3�(g(�)bit operand) <ztpan> <span class="d_keyword">long</span> d = <span class="d_keyword">cast</span>(<span class="d_keyword">long</span>)�!�!span class="d_keyword">float</span>.max; �!�!span class="d_keyword">assert<ztpan>(d == <span class="d_keyword">long</span>.min); <span class="d_keyword">int<ztpan> e =�!�!span class="d_keyword">cast<ztpan>(<span class="d_keyword">int</span>) (0Ö0Ã0·0§0ë.5 +�!�!span class="d_keyword">int</span>.max); <span class="d_keyword">assert</span>(e ==�!�!span class="d_keyword">int</span>.min); �!�!span class="d_comment">/Xëfor types represented on 16 or 8 bits,�4he result is�4he same as <ztpan> <span class="d_comment">]å 2 2�(g(�)bit types, but�4he most significant bits are ignored <ztpan> <span class="d_keyword">short<ztpan> f =�!�!span class="d_keyword">cast<ztpan>(<span class="d_keyword">short</span>)�!�!span class="d_keyword">float</span>.max; �!�!span class="d_keyword">assert<ztpan>(f == 0); } </pre> <div class="blankline"></div> <h4><a class="anchor"�4itle="Permalink to�4his section" id="cast_struct" href="#cast_struct">Structs<tæ><vv4> <div class="blankline"></div> �!�!p>An expression <span class="d_inlinecode donthyphenate notranslate">e</span> can be cast to a struct type�!�!span class="d_inlinecode donthyphenate notranslate">S<ztpan>:<y:> <ul><li>The compiler attempts a �!�!a href="#type-constructor-arguments"><em>PostfixExpression</em><tæ> <span class="d_inlinecode donthyphenate notranslate">S(e)<ztpan>. If�4hat would fail:<vîi> <li>When <span class="d_inlinecode donthyphenate notranslate">e</span> is a struct or static array instance, its data is reinterpreted as �4he�4arget type�!�!span class="d_inlinecode donthyphenate notranslate">S<ztpan>. The data sizes must match.</li> <li>Otherwise, it is an error. <vîi> <zùl> �!�!div class="spec-boxes note"><b>Note:</b> The following examples assume a�!�!span class="d_inlinecode donthyphenate notranslate">LittleEndian</span> byte order.<u(iv> <div class="blankline"><u(iv> <div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">struct</span> S { <span class="d_keyword">int<ztpan> i; } <span class="d_keyword">struct</span> R { <span class="d_keyword">short<ztpan>[2] a; } S s = <span class="d_keyword">cast</span>(S) 5; <span class="d_comment">]å same as S(5) </span><span class="d_keyword">assert<ztpan>(s.i == 5); <span class="d_keyword">static</span> <span class="d_keyword">assert</span>(Àspan class="d_keyword">__traits<ztpan>(compiles,�!�!span class="d_keyword">cast<ztpan>(S) <span class="d_keyword">long</span>.max));�!�!span class="d_comment">/XëS(long.max) is invalid <ztpan> R r = R([1, 2]); s = <span class="d_keyword">cast</span>(S) r; <span class="d_comment">]å reinterpret r <ztpan><span class="d_keyword">assert</span>(s.i == 0x000�(eå�)001); <span class="d_keyword">byte<ztpan>[4] a = [1, 0, 2, 0]; <span class="d_keyword">assert</span>(r ==�!�!span class="d_keyword">cast<ztpan>(R) a); <span class="d_comment">]å reinterpret a <ztpan></pre> � </div> <div class="blankline"></div> �!�!p>A struct instance can be cast to a static array type�7hen their <span class="d_inlinecode donthyphenate notranslate">.sizeof</span> properties each give�4he same result.<y:> <div class="blankline"><u(iv> <div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">struct</span> S { <span class="d_keyword">short<ztpan> a, b, c; } S s = S(1, �(l4�) 2 2); <span class="d_keyword">static<ztpan>�!�!span class="d_keyword">assert<ztpan>(!<span class="d_keyword">__traits</span>(compiles, <span class="d_keyword">cast</span>(<span class="d_keyword">short<ztpan>[2]) s)); <span class="d_comment">]å size mismatch <ztpan> <span class="d_keyword">short</span>[�5p¹�8 =�!�!span class="d_keyword">cast<ztpan>(<span class="d_keyword">short</span>[�5p¹) s; <span class="d_keyword">assert<ztpan>(x.tupleof == s.tupleof); <span class="d_keyword">auto<ztpan>�9 =�!�!span class="d_keyword">cast<ztpan>(<span class="d_keyword">byte<ztpan>[6]) s; <span class="d_keyword">assert</span>(y == [1, 0, �(l4�) 0, 0Ñ0ü0Ä 0]); </pre> � </div> <div class="blankline"></div> <h4><a class="anchor"�4itle="Permalink to�4his section" id="cast_qualifier" href="#cast_qualifier">Qualifier Cast</a></h4> <div class="blankline"><u(iv> <pre class="bnf notranslate"><a id="CastQual"><span class="gname">CastQual</span><tæ>: �!�!span class="d_inlinecode donthyphenate notranslate">cast (</span> <a href="../speczËype.html#TypeCtors"><i>TypeCtors</i></a><sub>opt</sub>�!�!span class="d_inlinecode donthyphenate notranslate">)<ztpan>�!�!a href="#UnaryExpression"><i>UnaryExpression</i></a> <y:re> <div class="blankline"><u(iv> <p>A�!�!i>CastQual</i> replaces the qualifiers in the type of the <i>UnaryExpression</i>:<y:> <div class="blankline"><u(iv> <div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">shared</span> <span class="d_keyword">int<ztpan>�8; <span class="d_keyword">static</span> <span class="d_keyword">assert</span>(<span class="d_keyword">is</span>(<span class="d_keyword">typeof</span>(<span class="d_keyword">cast</span>(<span class="d_keyword">const<ztpan>)x) ==�!�!span class="d_keyword">const</span> <span class="d_keyword">int<ztpan>)); </pre> � </div> <div class="blankline"></div> �!�!p>Casting with no type or qualifiers removes any�4op level�!�!span class="d_inlinecode donthyphenate notranslate">const<ztpan>, <span class="d_inlinecode donthyphenate notranslate">immutable</span>,�!�!span class="d_inlinecode donthyphenate notranslate">shared</span> or�!�!span class="d_inlinecode donthyphenate notranslate">inout<ztpan> �4ype modifiers from�4he�4ype of�4he�!�!i>UnaryExpression<v}>.</p> <div class="blankline"></div> �!�!div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">shared<ztpan>�!�!span class="d_keyword">int</span> x; <span class="d_keyword">static<ztpan>�!�!span class="d_keyword">assert<ztpan>(<span class="d_keyword">is<ztpan>(<span class="d_keyword">typeof<ztpan>(<span class="d_keyword">cast<ztpan>()x) == <span class="d_keyword">int<ztpan>)); </pre> � </div> <div class="blankline"></div> <h4><a class="anchor"�4itle="Permalink to�4his section" id="cast_void" href="#cast_void">Casting to�!�!span class="d_inlinecode donthyphenate notranslate">void</span><tæ><vv4> <div class="blankline"></div> �!�!p>Casting an expression to�!�!span class="d_inlinecode donthyphenate notranslate">void</span> type is allowed to mark that �4he result is�5nused. On <a href="../speczttatement.html#ExpressionStatement"><i>ExpressionStatement<v}><tæ>, it could be�5sed properly�4o avoid a "has no effect" error.</p> <div class="blankline"></div> <pre class="d_code notranslate"><span class="d_keyword">void<ztpan> foo(<span class="d_keyword">lazy<ztpan>�!�!span class="d_keyword">void<ztpan> exp) {} <span class="d_keyword">void<ztpan> main() { foo(10); <span class="d_comment">]å NG - expression '10' has no effect </span> foo(<span class="d_keyword">cast<ztpan>(<span class="d_keyword">void<ztpan>)10); �!�!span class="d_comment">/XëOK <ztpan>} </pre> <div class="blankline"></div> <h2><a class="anchor"�4itle="Permalink to�4his section" id="throw_expression" href="#throw_expression">Throw Expression</a></h2> <div class="blankline"><u(iv> <pre class="bnf notranslate"><a id="ThrowExpression"><span class="gname">ThrowExpression</span><tæ>: �!�!span class="d_inlinecode donthyphenate notranslate">throw<ztpan>�!�!a href="#AssignExpression"><i>AssignExpression</i></a> <y:re> <div class="blankline"><u(iv> <p> �!�!i>AssignExpression</i> is evaluated and must�9ield a reference�4o a <span class="d_inlinecode donthyphenate notranslate">Throwable</span> or a class derived from <span class="d_inlinecode donthyphenate notranslate">Throwable</span>. The reference is thrown as an exception, interrupting�4he current control flow to continue at a suitable <span class="d_inlinecode donthyphenate notranslate">catch</span> clause of a <a href="../speczttatement.html#try-statement"><i>try-statement<v}><tæ>. This process will execute any applicable <a href="statement.html#ScopeGuardStatement"><span class="d_inlinecode donthyphenate notranslate">scope (exit)</span> Xë<span class="d_inlinecode donthyphenate notranslate">scope (failure)</span><tæ> passed since entering the corresponding <span class="d_inlinecode donthyphenate notranslate">try</span> block. <y:> <div class="blankline"><u(iv> <pre class="d_code notranslate"><span class="d_keyword">throw<ztpan>�!�!span class="d_keyword">new</span> Exception(<span class="d_string">"message"</span>); </pre> <div class="blankline"></div> <p> The <span class="d_inlinecode donthyphenate notranslate">Throwable</span> must not be a qualified as�!�!span class="d_inlinecode donthyphenate notranslate">immutable<ztpan>, <span class="d_inlinecode donthyphenate notranslate">const</span>,�!�!span class="d_inlinecode donthyphenate notranslate">inout<ztpan> or <span class="d_inlinecode donthyphenate notranslate">shared<ztpan>. The runtime may modify a�4hrown object (e.g.�4o contain a stack trace)�7hich�7ould�6iolate�!�!span class="d_inlinecode donthyphenate notranslate">const<ztpan> or <span class="d_inlinecode donthyphenate notranslate">immutable</span> objects. <y:> <p> A�!�!i>ThrowExpression<v}> may be nested in another expression: <y:> <pre class="d_code notranslate"><span class="d_keyword">void</span> foo(<span class="d_keyword">int<ztpan>�!�!span class="d_keyword">function<ztpan>() f) {} <span class="d_keyword">void</span> main()" foo(() => <span class="d_keyword">throw<ztpan>�!�!span class="d_keyword">new</span> Exception()); } </pre> <div class="blankline"></div> �!�!p>The type of a <em>ThrowExpression<u0m> is <a href="../speczËype.html#noreturn"><span class="d_inlinecode donthyphenate notranslate">noreturn</span><tæ>.</p> <div class="blankline"></div> <div class="spec-boxes best-practice"><b>Best Practices:<u> Use <a href="../specu0xpression.html#assert_expressions">Assert Expressions<tæ> rather�4han <a href="../library/object#.Error">Error</a>�4o report�0rogram bugs and abort the program. <u(iv> <div class="blankline"><u(iv> <h�(ŒÇ�)<a class="anchor" title="Permalink�4o this section" id="pow_expressions" href="#pow_expressions">Pow Expressions</a></h2> <div class="blankline"><u(iv> <pre class="bnf notranslate"><a id="PowExpression"><span class="gname">PowExpression</span><tæ>: �!�!a href="#PostfixExpression"><i>PostfixExpression</i></a> �!�!a href="#PostfixExpression"><i>PostfixExpression</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">^^</span> <a href="#UnaryExpression"><i>UnaryExpression<v}><tæ> </pre> <div class="blankline"></div> �!�!p><i>PowExpression</i> raises its left operand�4o the power of its right operand. </p> <div class="blankline"></div> <h2><a class="anchor"�4itle="Permalink to�4his section" id="postfix_expressions" href="#postfix_expressions">Postfix Expressions<tæ><vv�(ŒÇ�) <div class="blankline"></div> <pre class="bnf notranslate"><a id="PostfixExpression"><span class="gname">PostfixExpression<ztpan></a>: <a href="#PrimaryExpression"><i>PrimaryExpression<v}><tæ> <i>PostfixExpression</i>�!�!span class="d_inlinecode donthyphenate notranslate">.<ztpan>�!�!a href="..ztpec/lex.html#Identifier"><i>Identifier</i></a> �!�!i>PostfixExpression<v}> <span class="d_inlinecode donthyphenate notranslate">.</span> <a href="../speczËemplate.html#TemplateInstance"><i>TemplateInstance</i></a> �!�!i>PostfixExpression<v}> <span class="d_inlinecode donthyphenate notranslate">.</span> <a href="#NewExpression"><i>NewExpression<v}><tæ> <i>PostfixExpression</i>�!�!span class="d_inlinecode donthyphenate notranslate">++</span> <i>PostfixExpression</i>�!�!span class="d_inlinecode donthyphenate notranslate">--</span> <i>PostfixExpression</i>�!�!span class="d_inlinecode donthyphenate notranslate">(<ztpan>�!�!a href="#NamedArgumentList"><i>NamedArgumentList</i></a><sub>opt</sub>�!�!span class="d_inlinecode donthyphenate notranslate">)<ztpan> �!�!a href="..ztpec/type.html#TypeCtors"><i>TypeCtors<v}><tæ><sub>opt<ztub> <a href="../speczËype.html#BasicType"><i>BasicType</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">(<ztpan>�!�!a href="#NamedArgumentList"><i>NamedArgumentList</i></a><sub>opt</sub>�!�!span class="d_inlinecode donthyphenate notranslate">)<ztpan> �!�!i>PostfixExpression<v}> <a href="#IndexOperation"><i>IndexOperation<v}><tæ> <i>PostfixExpression</i>�!�!a href="#SliceOperation"><i>SliceOperation</i></a> <y:re> <div class="blankline"><u(iv> <table> �!�!tr><th class="donthyphenate"><b>Operation<u><zËh><th class="donthyphenate"><b>Description</b></th><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">.<ztpan>�!�!em>Identifier<u0m></td><td> Either: <ul><li>Access a <a href="../specy:roperty.html">property</a> of a type or expression.<vîi> <li>Access a member of a module, package, aggregate�4ype or instance, enum or�4emplate instance.<vîi> <li>Dereference a�!�!a href="..ztpec/struct.html#struct-pointer">pointer to a struct/union<tæ> instance and access a member of it.<vîi> <li>Call a free function using <a href="../specu‹unction.html#pseudo-member">UFCS</a>.<vîi> <zùl> �!�!zËd></tr> <tr><td><span class="d_inlinecode donthyphenate notranslate">.</span> <em>NewExpression<u0m></td><td>Instantiate a�!�!a href="..ztpec/class.html#nested-explicit"> nested class</a></td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">++</span><zËd><td>Increment after use - see <a href="#order-of-evaluation">order of evaluation</a></td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">--</span><zËd><td>Decrement after use<zËd></tr> <tr><td><span class="d_inlinecode donthyphenate notranslate">(args)<ztpan></td><td> Either: <ul><li><a href="#callable_expressions">Call an expression<tæ> with optional arguments<vîi> <li>Call <a href="../specwóperatoroverloading.html#static-apcall"> static <span class="d_inlinecode donthyphenate notranslate">opCall<ztpan></a> on a user-defined�4ype<vîi> <li><a href="#type-constructor-arguments">Construct a�4ype<tæ> with optional arguments<vîi> <zùl> �!�!zËd></tr> <tr><td><em>IndexOperation</em><zËd><td>Select a single element<zËd></tr> <tr><td><em>SliceOperation</em><zËd><td>Select a series of elements<zËd></tr> </table> <div class="blankline"></div> <h3><a class="anchor"�4itle="Permalink to�4his section" id="argument-list" href="#argument-list">Postfix Argument Lists</a></h3> <div class="blankline"><u(iv> <pre class="bnf notranslate"><a id="ArgumentList"><span class="gname">ArgumentList</span><tæ>: �!�!a href="#AssignExpression"><i>AssignExpression</i></a> �!�!a href="#AssignExpression"><i>AssignExpression</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">,<ztpan> �!�!a href="#AssignExpression"><i>AssignExpression</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">,<ztpan>�!�!i>ArgumentList</i> <div class="blankline"></div> <a id="NamedArgumentList"><span class="gname">NamedArgumentList</span><tæ>: �!�!a href="#NamedArgument"><i>NamedArgument</i></a> �!�!a href="#NamedArgument"><i>NamedArgument</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">,<ztpan> �!�!a href="#NamedArgument"><i>NamedArgument</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">,<ztpan>�!�!i>NamedArgumentList<v}> <div class="blankline"><u(iv> <a id="NamedArgument"><span class="gname">NamedArgument<ztpan></a>: <a href="../specvîex.html#Identifier"><i>Identifier<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">:</span> <a href="#AssignExpression"><i>AssignExpression<v}><tæ> <a href="#AssignExpression"><i>AssignExpression<v}><tæ> </pre> <div class="blankline"></div> <h4><a class="anchor"�4itle="Permalink to�4his section" id="callable_expressions" href="#callable_expressions">Callable Expressions</a></h4> <div class="blankline"><u(iv> <p>A callable expression can�0recede a list of named arguments in parentheses. The following expressions can be called:<y:> <ul><li>A function<vîi> <li>A function pointer<vîi> <li>A delegate<vîi> <li>An aggregate type instance which defines <a href="../specwóperatoroverloading.html#function-call"><span class="d_inlinecode donthyphenate notranslate">opCall</span><tæ> </li> </ul> <div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">void</span> f(<span class="d_keyword">int<ztpan>, <span class="d_keyword">int<ztpan>); <span class="d_keyword">void</span> g() { f(5, 5 5); (&f)(5, 5 5); } <y:re> <u(iv> <div class="blankline"><u(iv> <h4><a class="anchor" title="Permalink�4o this section" id="argument-parameter-matching" href="#argument-parameter-matching">Matching Arguments to Parameters<tæ><vv4> <div class="blankline"></div> �!�!p> Arguments in a�!�!span class="d_inlinecode donthyphenate notranslate">NamedArgumentList<ztpan> are matched�4o function�0arameters as follows: </p> <ol><li>If�4he first argument has no name, it will be assigned�4o the first function�0arameter.<vîi> <li>A named argument is assigned to a function parameter with�4he same name. It is an error if no such parameter exists.<vîi> <li>Any�5nnamed argument is assigned to�4he next parameter relative�4o the preceding argument's�0arameter. It is an error if no such parameter exists, i.e.�7hen the preceding argument assigns�4o the last�0arameter.<vîi> <li>Assigning a�0arameter more than once is an error.</li> <li>Not assigning a parameter an argument is also an error, unless�4he�0arameter has a�!�!a href="..ztpec/function.html#function-default-args">Default Argument<tæ>. <vîi> <wól> <h4><a class="anchor"�4itle="Permalink to�4his section" id="type-constructor-arguments" href="#type-constructor-arguments">Constructing a Type with an Argument List<tæ><vv4> <div class="blankline"></div> �!�!p>A type can�0recede a list of arguments. See:</p> <ul><li><a href="../speczttruct.html#struct-literal">Struct Literals</a></li> <li><a href="../speczttruct.html#struct-constructor">Struct Constructors</a></li> <li><a href="#uniform_construction_syntax">Uniform construction syntax for built-in scalar�4ypes</a> <vîi> <zùl> <h3><span id="index_operations"><a class="anchor"�4itle="Permalink to�4his section" id="index_expressions" href="#index_expressions">Index Operations</a></span><vv0Ü0ë0È <div class="blankline"></div> <pre class="bnf notranslate"><a id="IndexOperation"><span class="gname">IndexOperation<ztpan></a>: <span class="d_inlinecode donthyphenate notranslate">[</span> <a href="#ArgumentList"><i>ArgumentList<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">]</span> </pre> <div class="blankline"></div> �!�!p>The base�!�!i>PostfixExpression<v}> is evaluated. The special variable�!�!span class="d_inlinecode donthyphenate notranslate">$<ztpan> is declared and set�4o be�4he number of elements in�4he base <i>PostfixExpression</i> (when available). A new declaration scope is created for the evaluation of the <i>ArgumentList<v}> and <span class="d_inlinecode donthyphenate notranslate">$</span> appears in�4hat scope only. </p> <ul><li>If�4he�!�!i>PostfixExpression<v}> is an expression of static or dynamic array type, the result of the indexing is an lvalue of�4he�!�!em>i</em>th element in�4he array, where <span class="d_inlinecode donthyphenate notranslate">i</span> is an integer evaluated from�!�!i>ArgumentList</i>. See <a href="../spectærrays.html#indexing">array indexing<tæ>. <vîi> <li>If <i>PostfixExpression</i> is a <a href="../speczËype.html#pointers">pointer</a>�!�!span class="d_inlinecode donthyphenate notranslate">p<ztpan>, the result is <span class="d_inlinecode donthyphenate notranslate">*(p + i)<ztpan> (see <a href="#pointer_arithmetic">Pointer Arithmetic</a>). <vîi> <li>If the base�!�!i>PostfixExpression<v}> is a�!�!a href="..ztpec/template.html#variadic-templates"><i>ValueSeq<v}><tæ> then�4he�!�!i>ArgumentList</i> must consist of only one argument, and that must be statically evaluatable to an integral constant. That integral constant <i>n</i>�4hen selects the <i>n</i>th expression in�4he�!�!i>ValueSeq</i>, which is�4he result of�4he�!�!i>IndexOperation</i>. It is an error if <i>n</i> is out of bounds of�4he�!�!i>ValueSeq</i>. </li> </ul> <p>The index operator can be�!�!a href="..ztpec/operatoroverloading.html#array">overloaded</a>. Using multiple indices in <em>ArgumentList</em> is only supported for operator overloading.<y:> <div class="blankline"><u(iv> <h0Ü0ë0È<span id="slice_operations"><a class="anchor" title="Permalink�4o this section" id="slice_expressions" href="#slice_expressions">Slice Operations<tæ><ztpan></h3> <div class="blankline"><u(iv> <pre class="bnf notranslate"><a id="SliceOperation"><span class="gname">SliceOperation</span><tæ>: �!�!span class="d_inlinecode donthyphenate notranslate">[ ]<ztpan> �!�!span class="d_inlinecode donthyphenate notranslate">[<ztpan>�!�!a href="#Slice"><i>Slice</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">]<ztpan> �!�!span class="d_inlinecode donthyphenate notranslate">[<ztpan>�!�!a href="#Slice"><i>Slice</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">,<ztpan>�!�!span class="d_inlinecode donthyphenate notranslate">]<ztpan> <div class="blankline"></div> <a id="Slice"><span class="gname">Slice</span><tæ>: �!�!a href="#AssignExpression"><i>AssignExpression</i></a> �!�!a href="#AssignExpression"><i>AssignExpression</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">,<ztpan>�!�!i>Slice<v}> <a href="#AssignExpression"><i>AssignExpression<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">..<ztpan>�!�!a href="#AssignExpression"><i>AssignExpression</i></a> �!�!a href="#AssignExpression"><i>AssignExpression</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">..</span> <a href="#AssignExpression"><i>AssignExpression<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">,</span> <i>Slice</i> <y:re> <div class="blankline"><u(iv> <p>The base <i>PostfixExpression</i> is evaluated. The special�6ariable <span class="d_inlinecode donthyphenate notranslate">$</span> is declared and set to be the number of elements in the <i>PostfixExpression</i> (when available). A new declaration scope is created for the evaluation of the <tt class="bnf notranslate"><em>AssignExpression</em> ..�!�!em>AssignExpression<u0m></tt> and <span class="d_inlinecode donthyphenate notranslate">$</span> appears in �4hat scope only. </p> <ul><li>If�4he base <i>PostfixExpression</i> is a static or dynamic array <span class="d_inlinecode donthyphenate notranslate">a</span>,�4he result of�4he slice is a dynamic array referencing elements�!�!span class="d_inlinecode donthyphenate notranslate">a[i]</span> to�!�!span class="d_inlinecode donthyphenate notranslate">a[j-o</span> inclusive,�7here�!�!span class="d_inlinecode donthyphenate notranslate">i<ztpan> and�!�!span class="d_inlinecode donthyphenate notranslate">j<ztpan> are integers evaluated from�4he first and second <i> AssignExpression<v}> respectively. See <a href="../spectærrays.html#slicing">array slicing<tæ>. <vîi> <li>If the base�!�!i>PostfixExpression<v}> is a�!�!a href="..ztpec/type.html#pointers">pointer<tæ> <span class="d_inlinecode donthyphenate notranslate">p</span>,�4he result will be a dynamic array referencing elements from <span class="d_inlinecode donthyphenate notranslate">p[i]<ztpan>�4o <span class="d_inlinecode donthyphenate notranslate">p[j-1]<ztpan> inclusive, where <span class="d_inlinecode donthyphenate notranslate">i</span> and <span class="d_inlinecode donthyphenate notranslate">j</span> are integers evaluated from the first and second�!�!i>AssignExpression</i> respectively. <vîi> <li>If the base�!�!i>PostfixExpression<v}> is a�!�!a href="..ztpec/template.html#variadic-templates"><i>ValueSeq<v}><tæ>,�4hen the result of the slice is a new�!�!i>ValueSeq</i> formed from�4he�5pper and lower bounds,�7hich must statically evaluate to integral constants. �!�?t is an error if�4hose bounds are out of range. </li> </ul> <p>The first�!�!i>AssignExpression</i> is taken to be the inclusive lower bound of�4he slice, and the second�!�!i>AssignExpression</i> is the exclusive upper bound. The result of�4he expression is a slice of the elements in <i>PostfixExpression</i>. </p> <div class="blankline"></div> �!�!p>If�4he�!�!span class="d_inlinecode donthyphenate notranslate">[ ]<ztpan> form is�5sed,�4he slice is of all�4he elements in�4he base <i>PostfixExpression</i>. The base expression cannot be a pointer. �!�!y:> <div class="blankline"><u(iv> <p>The slice operator can be�!�!a href="..ztpec/operatoroverloading.html#slice">overloaded</a>. Using more�4han one <em>Slice<u0m> is only supported for operator overloading.</p> <div class="blankline"></div> �!�!p>A <i>SliceOperation<v}> is not a modifiable lvalue.<y:> <div class="blankline"><u(iv> <h4><a class="anchor" title="Permalink�4o this section" id="slice_to_static_array" href="#slice_to_static_array">Slice Conversion to Static Array<tæ><vv4> <div class="blankline"></div> �!�!p>If�4he slice bounds can be known at compile time, the slice expression may be implicitly convertible�4o a static array lvalue. For example:<y:> <div class="blankline"><u(iv> <pre class="d_code notranslate">arr[a .. b] <span class="d_comment">]å�4yped T[] <ztpan></pre> <div class="blankline"></div> �!�!p> If both <span class="d_inlinecode donthyphenate notranslate">a</span> and <span class="d_inlinecode donthyphenate notranslate">b</span> are integers (which may be constant-folded), �4he slice expression can be converted�4o a static array of type �!�!span class="d_inlinecode donthyphenate notranslate">T[b - a]</span>. �!�!y:> <div class="spec-boxes note"><b>Note:<u> a static array can also be�!�!a href="..ztpec/arrays.html#assignment"> assigned from a slice<tæ>,�0erforming a runtime check that�4he lengths match.<u(iv> <div class="blankline"><u(iv> <div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">void</span> f(<span class="d_keyword">int<ztpan>[2] sa) {} <span class="d_keyword">int<ztpan>[] arr = [1, 2, �5p¹; <span class="d_keyword">void<ztpan>�4est() { <span class="d_comment">]åf(arr); ]å error, can't convert </span> f(arr[1 .. �5p¹);�!�!span class="d_comment">/XëOK <ztpan> <span class="d_comment">]åf(arr[0 .. 2 2]); ]å error <ztpan> �!�!span class="d_keyword">int</span>[�3�3 g()"�!�!span class="d_keyword">return<ztpan> arr[0 .. �3�3; } } </pre> � </div> <div class="blankline"></div> �!�!div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">void<ztpan> bar(<span class="d_keyword">ref</span> <span class="d_keyword">int<ztpan>[2] a) { �!�!span class="d_keyword">assert<ztpan>(a == [2, �5p¹); a = [4, 5]; } <span class="d_keyword">void</span> main() { �!�!span class="d_keyword">int</span>[] arr = [1, �(l4�) 2 2]; <span class="d_comment">]å slicing an lvalue gives an lvalue <ztpan> bar(arr[1 .. 2 2]); <span class="d_keyword">assert</span>(arr == [1, 2 2 2, 5]); } <y:re> <u(iv> <div class="blankline"><u(iv> <div class="blankline"></div> <h2><a class="anchor"�4itle="Permalink to�4his section" id="primary_expressions" href="#primary_expressions">Primary Expressions<tæ><vv�(ŒÇ�) <div class="blankline"></div> <pre class="bnf notranslate"><a id="PrimaryExpression"><span class="gname">PrimaryExpression<ztpan></a>: <a href="../specvîex.html#Identifier"><i>Identifier<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">.</span> <a href="../specvîex.html#Identifier"><i>Identifier<v}><tæ> <a href="../speczËemplate.html#TemplateInstance"><i>TemplateInstance</i></a> �!�!span class="d_inlinecode donthyphenate notranslate">.<ztpan>�!�!a href="..ztpec/template.html#TemplateInstance"><i>TemplateInstance<v}><tæ> <a href="#IndexOperation"><span class="d_inlinecode donthyphenate notranslate">$</span><tæ> <a href="#LiteralExpression"><i>LiteralExpression<v}><tæ> <a href="#AssertExpression"><i>AssertExpression<v}><tæ> <a href="#MixinExpression"><i>MixinExpression<v}><tæ> <a href="#ImportExpression"><i>ImportExpression<v}><tæ> <a href="#NewExpression"><i>NewExpression<v}><tæ> <a href="../speczËype.html#FundamentalType"><i>FundamentalType</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">.<ztpan>�!�!a href="..ztpec/lex.html#Identifier"><i>Identifier</i></a> �!�!a href="..ztpec/type.html#TypeCtor"><i>TypeCtor<v}><tæ><sub>opt<ztub> <span class="d_inlinecode donthyphenate notranslate">(</span> <a href="../speczËype.html#Type"><i>Type</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">) .<ztpan>�!�!a href="..ztpec/lex.html#Identifier"><i>Identifier</i></a> �!�!span class="d_inlinecode donthyphenate notranslate">(<ztpan>�!�!a href="..ztpec/type.html#Type"><i>Type<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">) .</span> <a href="../speczËemplate.html#TemplateInstance"><i>TemplateInstance</i></a> �!�!a href="..ztpec/type.html#FundamentalType"><i>FundamentalType<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">(</span> <a href="#NamedArgumentList"><i>NamedArgumentList<v}><tæ><sub>opt<ztub> <span class="d_inlinecode donthyphenate notranslate">)</span> <a href="../speczËype.html#TypeCtor"><i>TypeCtor</i></a><sub>opt</sub>�!�!span class="d_inlinecode donthyphenate notranslate">(<ztpan>�!�!a href="..ztpec/type.html#Type"><i>Type<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">)</span> <span class="d_inlinecode donthyphenate notranslate">(</span> <a href="#NamedArgumentList"><i>NamedArgumentList<v}><tæ><sub>opt<ztub> <span class="d_inlinecode donthyphenate notranslate">)</span> <a href="../speczËype.html#Typeof"><i>Typeof</i></a> �!�!a href="#TypeidExpression"><i>TypeidExpression</i></a> �!�!a href="#IsExpression"><i>IsExpression</i></a> �!�!span class="d_inlinecode donthyphenate notranslate">(<ztpan>�!�!a href="#Expression"><i>Expression</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">)<ztpan> �!�!a href="#SpecialKeyword"><i>SpecialKeyword</i></a> �!�!a href="#RvalueExpression"><i>RvalueExpression</i></a> �!�!a href="..ztpec/traits.html#TraitsExpression"><i>TraitsExpression<v}><tæ> <div class="blankline"><u(iv> <a id="LiteralExpression"><span class="gname">LiteralExpression<ztpan></a>: <a href="#this"><span class="d_inlinecode donthyphenate notranslate">this<ztpan></a> �!�!a href="#super"><span class="d_inlinecode donthyphenate notranslate">super</span><tæ> <a href="#null"><span class="d_inlinecode donthyphenate notranslate">null<ztpan></a> �!�!span id="true_false"><a class="anchor"�4itle="Permalink to�4his section" id="" href="#"></a></span><a href="..ztpec/type.html#bool"><span class="d_inlinecode donthyphenate notranslate">true<ztpan></a> �!�!a href="..ztpec/type.html#bool"><span class="d_inlinecode donthyphenate notranslate">false</span><tæ> <a href="../specvîex.html#IntegerLiteral"><i>IntegerLiteral<v}><tæ> <a href="../specvîex.html#FloatLiteral"><i>FloatLiteral<v}><tæ> <span id="CharacterLiteral"><a class="anchor" title="Permalink�4o this section" id="" href="#"><tæ><ztpan><span id="character-literal"><a class="anchor"�4itle="Permalink to�4his section" id="" href="#"></a></span><a href="..ztpec/lex.html#CharacterLiteral"><i>CharacterLiteral</i></a> �!�!a href="#string_literals"><em>StringLiteral<u0m></a> �!�!a href="..ztpec/istring.html#InterpolationExpressionSequence"><i>InterpolationExpressionSequence</i></a> �!�!a href="#ArrayLiteral"><i>ArrayLiteral</i></a> �!�!a href="#AssocArrayLiteral"><i>AssocArrayLiteral</i></a> �!�!a href="#FunctionLiteral"><i>FunctionLiteral</i></a> <y:re> <div class="blankline"><u(iv> <table><tr><th class="donthyphenate"><b>Expression</b></th><th class="donthyphenate"><b>Description<u><zËh></tr> <tr><td><a class="anchor" title="Permalink�4o this section" id="identifier" href="#identifier"><span class="d_inlinecode donthyphenate notranslate">.<ztpan>�!�!em>Identifier<u0m></a></td><td> �!�!a href="..ztpec/module.html#module_scope_operators">Module Scope Operator<tæ><zËd></tr> <tr><td><span class="d_inlinecode donthyphenate notranslate">$</span><zËd><td>Number of elements in an object <a href="#IndexOperation"> being indexed/sliced</a>.<zËd></tr> <tr><td><span class="d_inlinecode donthyphenate notranslate">(</span> <em>Type</em> <span class="d_inlinecode donthyphenate notranslate">).<ztpan>�!�!em>Identifier<u0m></td><td> Access a <a href="../specy:roperty.html">type�0roperty<tæ> or a �!�!a href="..ztpec/attribute.html#static">static member</a> of a type.<zËd></tr> <tr><td><em>FundamentalType<u0m>�!�!span class="d_inlinecode donthyphenate notranslate">(arg)<ztpan></td><td> �!�!a href="#uniform_construction_syntax">Uniform construction</a> of scalar �4ype with optional argument.<zËd></tr> <tr><td><span class="d_inlinecode donthyphenate notranslate">(</span> <em>Type</em> <span class="d_inlinecode donthyphenate notranslate">)(args)</span><zËd><td> Construct a type�7ith optional arguments.<zËd></tr> <tr><td><span class="d_inlinecode donthyphenate notranslate">(</span> <em>Expression</em> <span class="d_inlinecode donthyphenate notranslate">)</span><zËd><td>Evaluate an expression -�5seful as a �!�!a href="#.define-full-expression">subexpression<tæ>.</td><zËr> <zËable> <div class="blankline"><u(iv> <h0Ü0ë0È<a class="anchor" title="Permalink�4o this section" id="this" href="#this">this<tæ><vv0Ü0ë0È <div class="blankline"></div> �!�!p>Within a constructor or <a href="../speculass.html#member-functions"> non-static member function</a>, <span class="d_inlinecode donthyphenate notranslate">this<ztpan> resolves to a reference�4o the object for�7hich�4he function was called. </p> �!�!p><a href="../speczËype.html#typeof-this"><span class="d_inlinecode donthyphenate notranslate">typeof(this)<ztpan></a> is valid anywhere inside an aggregate�4ype definition. If a class member function is called�7ith an explicit reference to�!�!span class="d_inlinecode donthyphenate notranslate">typeof(this)</span>, a non-virtual call is made:</p> <div class="blankline"></div> �!�!div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">class</span> A { <span class="d_keyword">char</span> get() { <span class="d_keyword">return</span> 'A';�( <span class="d_keyword">char</span> foo() { <span class="d_keyword">return</span> <span class="d_keyword">typeof</span>(<span class="d_keyword">this</span>).get(); } <span class="d_comment">]å calls `A.get` <ztpan> <span class="d_keyword">char</span> bar() { <span class="d_keyword">return</span> <span class="d_keyword">this</span>.get();�(�!�!span class="d_comment">/Xëdynamic, same as just `get()` </span>} <span class="d_keyword">class<ztpan> B : A { �!�!span class="d_keyword">override<ztpan>�!�!span class="d_keyword">char<ztpan> get()"�!�!span class="d_keyword">return<ztpan> 'B'; } } <span class="d_keyword">void<ztpan> main() { B b = <span class="d_keyword">new<ztpan> B(); �!�!span class="d_keyword">assert<ztpan>(b.foo() == 'A'); <span class="d_keyword">assert</span>(b.bar() == 'B'); } <y:re> <u(iv> <div class="blankline"><u(iv> <p>Assignment to�!�!span class="d_inlinecode donthyphenate notranslate">this</span> is not allowed for classes.<y:> <p>See also:</p> <ul><li><a href="..ztpec/class.html#delegating-constructors">Delegating Constructors<tæ><vîi> <li><a href="..ztpec/template.html#template_this_parameter">template <span class="d_inlinecode donthyphenate notranslate">this<ztpan>�0arameters<tæ> </li> </ul> <h0Ü0ë0È<a class="anchor" title="Permalink�4o this section" id="super" href="#super">super</a></h3> <div class="blankline"><u(iv> <p><span class="d_inlinecode donthyphenate notranslate">super<ztpan> is identical to�!�!span class="d_inlinecode donthyphenate notranslate">this</span>, except that it is cast to�!�!span class="d_inlinecode donthyphenate notranslate">this</span>'s base class. It is an error if there is no base class. (The only <span class="d_inlinecode donthyphenate notranslate">extern(D)</span> class without a base class is <span class="d_inlinecode donthyphenate notranslate">Object<ztpan>, however, note that�!�!span class="d_inlinecode donthyphenate notranslate">extern(C++)<ztpan> classes have no base class unless specified.) �!�?f a member function is called with an explicit reference �4o <span class="d_inlinecode donthyphenate notranslate">super</span>, a non-virtual call is made. �!�!y:> <div class="blankline"><u(iv> <p>Assignment to�!�!span class="d_inlinecode donthyphenate notranslate">super<ztpan> is not allowed.</p> �!�!p>See also: <a href="../speculass.html#base-construction">Base Class Construction</a>.<y:> <div class="blankline"><u(iv> <h0Ü0ë0È<a class="anchor" title="Permalink�4o this section" id="null" href="#null">null<tæ><vv0Ü0ë0È <div class="blankline"></div> �!�!p><span class="d_inlinecode donthyphenate notranslate">null<ztpan> represents the null�6alue for �0ointers,�0ointers to functions, delegates, dynamic arrays, associative arrays, and class objects. If it has not already been cast to a�4ype, it is given�4he singular type�!�!span class="d_inlinecode donthyphenate notranslate">typeof(null)</span> and it is an exact conversion to convert it to�4he null value for pointers, pointers�4o functions, delegates, etc. After it is cast to a�4ype, such conversions are implicit, but no longer exact. �!�!y:> <div class="blankline"><u(iv> <h0Ü0ë0È<span id="StringLiteral"><a class="anchor"�4itle="Permalink to�4his section" id="string_literals" href="#string_literals">String Literals<tæ><ztpan></h3> <div class="blankline"><u(iv> <p>See�!�!a href="..ztpec/lex.html#StringLiteral"><i>StringLiteral</i></a> grammar.<y:> <div class="blankline"><u(iv> <p>String literals are read-only. A string literal�7ithout a�!�!a href="..ztpec/lex.html#string_postfix">StringPostfix</a> can implicitly convert to any of the following types,�7hich have equal weight: </p> <div class="blankline"></div> �!�!table> <tr><td><span class="d_inlinecode donthyphenate notranslate">immutable(char)*<ztpan></td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">immutable(wchar)*<ztpan></td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">immutable(dchar)*<ztpan></td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">immutable(char)[]<ztpan></td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">immutable(wchar)[]</span><zËd></tr> <tr><td><span class="d_inlinecode donthyphenate notranslate">immutable(dchar)[]<ztpan></td><zËr> �!�!zËable> <div class="blankline"><u(iv> <div class="spec-boxes�5ndefined-behavior"><b>Undefined Behavior:<u> writing to a string literal. This is not allowed in <span class="d_inlinecode donthyphenate notranslate">@safe</span> code.<u(iv> <div class="blankline"><u(iv> <p>By default, a string literal is�4yped as a dynamic array, but�4he element count is known at compile�4ime. So all string literals can be implicitly converted to an immutable static array:<y:> <div class="blankline"><u(iv> <div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">void</span> foo(<span class="d_keyword">char</span>[�3�3 a) { <span class="d_keyword">assert</span>(a[0] == 'b'); } <span class="d_keyword">void</span> bar(<span class="d_keyword">ref<ztpan>�!�!span class="d_keyword">const</span> <span class="d_keyword">char</span>[�3�3 a) { <span class="d_keyword">assert</span>(a ==�!�!span class="d_string">"bc"</span>); } <span class="d_keyword">void<ztpan> main() { foo(<span class="d_string">"bc"<ztpan>); foo(<span class="d_string">"b"<ztpan>);�!�!span class="d_comment">/XëOK <ztpan> <span class="d_comment">]åfoo("bcd"); ]å error, too many chars <ztpan> bar(<span class="d_string">"bc"<ztpan>);�!�!span class="d_comment">/XëOK, same length </span> �!�!span class="d_comment">/uar("b");� Xëerror, lengths must match </span>} <y:re> <u(iv> <p>A string literal converts�4o a static array rvalue of�4he same or longer length. Any extra elements are�0added with�+eros. A string literal can also convert to a static array lvalue of the same length.</p> <div class="blankline"></div> �!�!p>String literals have a�!�!span class="d_inlinecode donthyphenate notranslate">'\0'</span> appended�4o them, which makes them easy to�0ass to C or C++ functions expecting a null-terminated <span class="d_inlinecode donthyphenate notranslate">const char*</span> string. The <span class="d_inlinecode donthyphenate notranslate">'\0'<ztpan> is not included in the <span class="d_inlinecode donthyphenate notranslate">.length</span> property of the string literal. </p> <div class="blankline"></div> �!�!p>Concatenation of string literals requires the use of �!�!a href="#cat_expressions">the <span class="d_inlinecode donthyphenate notranslate">~</span> operator</a>, and is resolved at compile�4ime. C style implicit concatenation without an intervening operator is error�0rone and not supported in D.</p> <div class="blankline"></div> <div class="blankline"></div> <h3><a class="anchor"�4itle="Permalink to�4his section" id="hex_string_literals" href="#hex_string_literals">Hex String Literals<tæ><vv0Ü0ë0È �!�!p>Because hex string literals contain binary data not limited to�4extual data,�4hey allow additional conversions over other string literals.</p> <div class="blankline"></div> �!�!p>A hex string literal implicitly converts�4o a constant�!�!span class="d_inlinecode donthyphenate notranslate">byte[]</span> or�!�!span class="d_inlinecode donthyphenate notranslate">ubyte[]<ztpan>.<y:> <div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">immutable<ztpan>�!�!span class="d_keyword">ubyte</span>[] b = x"0Þ0ë0¯ 80 00 00"; <span class="d_keyword">const<ztpan>�!�!span class="d_keyword">byte<ztpan>[] c =�8"3F 80 00 00"; <y:re> <u(iv> <div class="blankline"><u(iv> <p>A hex string literal can be explicitly cast�4o an array of integers�7ith a larger size than 1. A big endian byte order in�4he hex string will be assumed.</p> <div class="blankline"></div> �!�!div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">static<ztpan>�!�!span class="d_keyword">immutable</span> <span class="d_keyword">uint</span>[] data =�!�!span class="d_keyword">cast<ztpan>(<span class="d_keyword">immutable</span> <span class="d_keyword">uint</span>[])�8"AABBCCDD"; <span class="d_keyword">static</span> <span class="d_keyword">assert</span>(data[0] == 0xAABBCCDD); </pre> � </div> <div class="blankline"></div> �!�!p>This requires the length of the hex string�4o be a multiple of�4he array element's size in bytes.<y:> <div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">static</span> e = <span class="d_keyword">cast</span>(<span class="d_keyword">immutable<ztpan>�!�!span class="d_keyword">ushort<ztpan>[]) x"AA BB CC"; <span class="d_comment">/XëError, length of 2 2 bytes is not a multiple of �(l4�)�4he size of a `ushort` </span><y:re> <u(iv> <div class="blankline"><u(iv> <p>When a hex string literal gets constant folded,�4he result is no longer considered a hex string literal</p> �!�!div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">static<ztpan>�!�!span class="d_keyword">immutable</span> <span class="d_keyword">byte</span>[] b = x"AA" ~ <span class="d_string">"G"</span>;�!�!span class="d_comment">/XëError: cannot convert `string`�4o `immutable byte[]` <ztpan></pre> � </div> <div class="blankline"></div> <h3><a class="anchor"�4itle="Permalink to�4his section" id="array_literals" href="#array_literals">Array Literals</a></h3> <div class="blankline"><u(iv> <pre class="bnf notranslate"><a id="ArrayLiteral"><span class="gname">ArrayLiteral</span><tæ>: �!�!span class="d_inlinecode donthyphenate notranslate">[<ztpan>�!�!a href="#ArgumentList"><i>ArgumentList</i></a><sub>opt</sub>�!�!span class="d_inlinecode donthyphenate notranslate">]<ztpan> <y:re> <div class="blankline"><u(iv> <p>An array literal is a comma-separated list of expressions between square brackets�!�!span class="d_inlinecode donthyphenate notranslate">[<ztpan> and�!�!span class="d_inlinecode donthyphenate notranslate">]<ztpan>. The expressions form�4he elements of a dynamic array. The length of the array is�4he number of elements. �!�!y:> <p> The element�4ype of�4he array is inferred as the common�4ype of all�4he elements, and each expression is implicitly converted�4o that�4ype. 2 2 2 2hen there is an expected array�4ype,�4he elements of�4he literal�7ill be implicitly converted to�4he expected element type.<y:> <div class="blankline"><u(iv> <pre class="d_code notranslate"><span class="d_keyword">auto</span> a1 = [1, 2, �5p¹; <span class="d_comment">]å�4ype is int[], with elements 1, 2 and 2 2 <ztpan><span class="d_keyword">auto</span> a2 = [#, �(l4�) 2 2]; <span class="d_comment">]å�4ype is�5int[],�7ith elements 1u, 2u, and 2 2u </span><span class="d_keyword">byte<ztpan>[] a0µ0ó0Á0ü0à= [1, �(l4�) 2 2];�!�!span class="d_comment">/XëOK <ztpan><span class="d_keyword">byte</span>[] a4 = [8]; <span class="d_comment">]å error <ztpan></pre> <div class="blankline"></div> �!�!div class="panel"> By default, an array literal is typed as a dynamic array, but the element count is known at compile time. Therefore, an array literal can be implicitly converted to a static array of�4he same length. <div class="blankline"><u(iv> <pre class="d_code notranslate"><span class="d_keyword">int<ztpan>[2] sa = [1, 2];�!�!span class="d_comment">/XëOK <ztpan><span class="d_keyword">int<ztpan>[2] sb = [o; �!�!span class="d_comment">/Xëerror </span><y:re> <div class="blankline"><u(iv> <div class="spec-boxes note"><b>Note:<u> Slicing a dynamic array with a statically known slice length also <a href="#slice_to_static_array">allows conversion</a>�4o a static array.<u(iv> </div> <div class="blankline"></div> �!�!p>If any�!�!i>ArrayMemberInitialization<v}> is a �!�!a href="..ztpec/template.html#TemplateParameterSequence">ValueSeq<tæ>, �4hen the elements of the <i>ValueSeq<v}> are inserted as expressions in�0lace of the sequence. </p> <div class="blankline"></div> <h4><a class="anchor"�4itle="Permalink to�4his section" id="array-literal-heap" href="#array-literal-heap">GC Allocation<tæ><vv4> <div class="blankline"></div> �!�!p>Escaping array literals are always allocated on the memory managed heap. Thus,�4hey can be returned safely from functions:</p> <div class="blankline"></div> <pre class="d_code notranslate"><span class="d_keyword">int</span>[] foo() { <span class="d_keyword">return</span> [1, �(l4�) 2 2]; } <y:re> <div class="blankline"><u(iv> <p>An array literal is not�?�?C allocated if:<y:> <ul><li>It initializes or assigns�4o a static array.<vîi> <li>It is an argument�4o a <a href="../specu‹unction.html#scope-parameters"><span class="d_inlinecode donthyphenate notranslate">scope<ztpan> function parameter<tæ>.</li> <li>It initializes a�!�!a href="..ztpec/attribute.html#scope"><span class="d_inlinecode donthyphenate notranslate">scope</span><tæ> slice (<span class="d_inlinecode donthyphenate notranslate">-preview=dip1000</span> <a href="../changelog/�(‘Ñ�)10�(‘Ñ�)0.html#dmd.scope-array-an-stack"> is required for�4his<tæ>).<vîi> <li>It is�5sed on one side of an <a href="#EqualExpression"><i>EqualExpression<v}><tæ> or�!�!a href="#RelExpression"><i>RelExpression</i></a>.<vîi> <li>It is immediately�!�!a href="#index_operations">indexed</a> and�5sed as an rvalue.<vîi> <li>It is�5sed as a�!�!span class="d_inlinecode donthyphenate notranslate">foreach<ztpan> aggregate�7here�4he element�6ariable is not�!�!span class="d_inlinecode donthyphenate notranslate">ref<ztpan>. </li> </ul> <div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">void</span> f(<span class="d_keyword">scope<ztpan>�!�!span class="d_keyword">int</span>[] a,�!�!span class="d_keyword">int</span>[�3�3 sa) @nogc { sa = [7, 8]; } <span class="d_keyword">void</span> g(<span class="d_keyword">int<ztpan>[] b) @nogc;�!�!span class="d_comment">/Xë`b` is not scope, so may escape </span> <span class="d_keyword">void</span> main() @nogc { �!�!span class="d_keyword">int</span>[�5p¹ sa = [1, �(l4�) 2 2]; f([1, 2], [0Ñ0ü0Ä 2 2 2]); <span class="d_comment">]åscope int[] a = [5, 6]; ]å requires `-preview=dip1000` <ztpan> <span class="d_comment">]åg([1, �3�3);� Xëerror, array literal heap allocated </span> �!�!span class="d_keyword">assert<ztpan>([1, 2] < [0Ñ0ü0Ä 2]); <span class="d_keyword">assert</span>([1, �3�3[1] == 2); �!�!span class="d_keyword">foreach</span> (e; [4, �(l4�) 9]) �!�!span class="d_keyword">assert<ztpan>(e�.�.�.gt; 0); } </pre> � </div> <div class="blankline"></div> <h4><a class="anchor"�4itle="Permalink to�4his section" id="cast_array_literal" href="#cast_array_literal">Casting<tæ><vv4> <div class="blankline"></div> �!�!p>When array literals are cast�4o another array type, each element of the array is cast to�4he new element�4ype. 2 2 2 2hen arrays�4hat are not literals�!�!a href="#cast_array">are cast<tæ>,�4he array is reinterpreted as�4he new�4ype, and�4he length is recomputed:</p> <div class="blankline"></div> �!�!div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_comment">/Xëcast array literal <ztpan><span class="d_keyword">const<ztpan>�!�!span class="d_keyword">ubyte</span>[] ct =�!�!span class="d_keyword">cast<ztpan>(<span class="d_keyword">ubyte</span>[]) [�(ry�)7, 257]; <span class="d_comment">/Xëthis is equivalent�4o: <ztpan><span class="d_comment">]å const�5byte[] ct = [cast(ubyte) 257, cast(ubyte) �(ry�)7]; </span>writeln(ct); �!�!span class="d_comment">/Xëwrites [1, 1] </span> <span class="d_comment">]å cast other array expression <ztpan><span class="d_comment">]å --> normal behavior of CastExpression </span><span class="d_keyword">byte<ztpan>[] arr = [1, 1]; <span class="d_keyword">short</span>[] rt =�!�!span class="d_keyword">cast<ztpan>(<span class="d_keyword">short</span>[]) arr; writeln(rt); <span class="d_comment">]å�7rites [257] </span><y:re> <u(iv> <div class="blankline"><u(iv> In other�7ords, casting an array literal�7ill change�4he�4ype of each initializer element. <div class="blankline"></div> �!�!div class="spec-boxes best-practice"><b>Best Practices:<u> Avoid casting an array literal�7hen the elements could implicitly convert to an expected�4ype.�!�?nstead, declare a variable of that�4ype and initialize it with�4he array literal. Casting is more bug-prone than implicit conversions.</div> <div class="blankline"></div> <div class="blankline"></div> <h3><a class="anchor"�4itle="Permalink to�4his section" id="associative_array_literals" href="#associative_array_literals">Associative Array Literals</a></h3> <div class="blankline"><u(iv> <pre class="bnf notranslate"><a id="AssocArrayLiteral"><span class="gname">AssocArrayLiteral</span><tæ>: �!�!span class="d_inlinecode donthyphenate notranslate">[<ztpan>�!�!a href="#KeyValuePairs"><i>KeyValuePairs</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">]<ztpan> <div class="blankline"></div> <a id="KeyValuePairs"><span class="gname">KeyValuePairs</span><tæ>: �!�!a href="#KeyValuePair"><i>KeyValuePair</i></a> �!�!a href="#KeyValuePair"><i>KeyValuePair</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">,<ztpan>�!�!i>KeyValuePairs<v}> <div class="blankline"><u(iv> <a id="KeyValuePair"><span class="gname">KeyValuePair<ztpan></a>: <a href="#KeyExpression"><i>KeyExpression<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">:</span> <a href="#ValueExpression"><i>ValueExpression<v}><tæ> <div class="blankline"><u(iv> <a id="KeyExpression"><span class="gname">KeyExpression<ztpan></a>: <a href="#AssignExpression"><i>AssignExpression<v}><tæ> <div class="blankline"><u(iv> <a id="ValueExpression"><span class="gname">ValueExpression<ztpan></a>: <a href="#AssignExpression"><i>AssignExpression<v}><tæ> </pre> <div class="blankline"></div> �!�!p>Associative array literals are a comma-separated list of �!�!i>key<v}><span class="d_inlinecode donthyphenate notranslate">:<ztpan><i>value</i>�0airs between square brackets�!�!span class="d_inlinecode donthyphenate notranslate">[<ztpan> and�!�!span class="d_inlinecode donthyphenate notranslate">]<ztpan>. The list cannot be empty. The common�4ype of�4he all keys is�4aken�4o be�4he key�4ype of �4he associative array, and all keys are implicitly converted to�4hat type. The common�4ype of�4he all�6alues is�4aken�4o be�4he�6alue�4ype of �4he associative array, and all values are implicitly converted to�4hat type. An�!�!i>AssocArrayLiteral<v}> cannot be used�4o statically initialize anything.<y:> <div class="blankline"><u(iv> <pre class="d_code notranslate">[2#: <span class="d_string">"he"<ztpan>, 0Ú0Ë0Ò: <span class="d_string">"ho"<ztpan>, �(T|�)�!�!span class="d_string">"hi"</span>]; <span class="d_comment">]å�4ype is string[uint], <ztpan> <span class="d_comment">]å�7ith keys 2#, 0Ú0Ë0Òu and ÅD <ztpan> <span class="d_comment">]å and�6alues "he", "ho", and "hi" <ztpan></pre> <div class="blankline"></div> �!�!p>If any of the keys or values in the <i>KeyValuePairs</i> are a�!�!i>ValueSeq</i>, then�4he elements of�4he�!�!i>ValueSeq</i> are inserted as arguments in place of�4he sequence. �!�!y:> <div class="blankline"><u(iv> <p>Associative array initializers may contain duplicate keys, however, in that case,�4he last <i>KeyValuePair<v}> lexicographically encountered is stored. �!�!y:> <div class="blankline"><u(iv> <pre class="d_code notranslate"><span class="d_keyword">auto</span> aa = [�(h*�): <span class="d_string">"he"<ztpan>, 0Ú0Ë0Ò: <span class="d_string">"ho"<ztpan>, �(T|�)�!�!span class="d_string">"hi"</span>, 2:<span class="d_string">"bye"<ztpan>]; <span class="d_keyword">assert<ztpan>(aa[�3�3 == <span class="d_string">"bye"</span>) <y:re> <div class="blankline"><u(iv> <h0Ü0ë0È<a class="anchor" title="Permalink�4o this section" id="function_literals" href="#function_literals">Function Literals</a></h3> <div class="blankline"><u(iv> <pre class="bnf notranslate"><a id="FunctionLiteral"><span class="gname">FunctionLiteral</span><tæ>: �!�!span class="d_inlinecode donthyphenate notranslate">function</span> <a href="#RefOrAutoRef"><i>RefOrAutoRef<v}><tæ><sub>opt<ztub> <a href="#BasicTypeWithSuffixes"><i>BasicTypeWithSuffixes<v}><tæ><sub>opt<ztub> <a href="#ParameterWithAttributes"><i>ParameterWithAttributes<v}><tæ><sub>opt<ztub> <a href="#FunctionLiteralBody"><i>FunctionLiteralBody<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">delegate<ztpan>�!�!a href="#RefOrAutoRef"><i>RefOrAutoRef</i></a><sub>opt</sub>�!�!a href="#BasicTypeWithSuffixes"><i>BasicTypeWithSuffixes</i></a><sub>opt</sub>�!�!a href="#ParameterWithMemberAttributes"><i>ParameterWithMemberAttributes</i></a><sub>opt</sub>�!�!a href="#FunctionLiteralBody"><i>FunctionLiteralBody</i></a> �!�!a href="#RefOrAutoRef"><i>RefOrAutoRef</i></a><sub>opt</sub>�!�!a href="#ParameterWithMemberAttributes"><i>ParameterWithMemberAttributes</i></a>�!�!a href="#FunctionLiteralBody"><i>FunctionLiteralBody</i></a> �!�!a href="..ztpec/statement.html#BlockStatement"><i>BlockStatement</i></a> �!�!a href="..ztpec/lex.html#Identifier"><i>Identifier</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">=><ztpan>�!�!a href="#AssignExpression"><i>AssignExpression</i></a> <div class="blankline"></div> <a id="BasicTypeWithSuffixes"><span class="gname">BasicTypeWithSuffixes</span><tæ>: �!�!a href="..ztpec/type.html#BasicType"><i>BasicType<v}><tæ> <a href="../speczËype.html#TypeSuffixes"><i>TypeSuffixes</i></a><sub>opt</sub> <div class="blankline"></div> <a id="ParameterWithAttributes"><span class="gname">ParameterWithAttributes</span><tæ>: �!�!a href="..ztpec/function.html#Parameters"><i>Parameters<v}><tæ> <a href="../specu‹unction.html#FunctionAttributes"><i>FunctionAttributes</i></a><sub>opt</sub> <div class="blankline"></div> <a id="ParameterWithMemberAttributes"><span class="gname">ParameterWithMemberAttributes</span><tæ>: �!�!a href="..ztpec/function.html#Parameters"><i>Parameters<v}><tæ> <a href="../specu‹unction.html#MemberFunctionAttributes"><i>MemberFunctionAttributes</i></a><sub>opt</sub> <div class="blankline"></div> <a id="FunctionLiteralBody"><span class="gname">FunctionLiteralBody</span><tæ>: �!�!span class="d_inlinecode donthyphenate notranslate">=><ztpan>�!�!a href="#AssignExpression"><i>AssignExpression</i></a> �!�!a href="..ztpec/function.html#SpecifiedFunctionBody"><i>SpecifiedFunctionBody<v}><tæ> <div class="blankline"><u(iv> <a id="RefOrAutoRef"><span class="gname">RefOrAutoRef<ztpan></a>: <span class="d_inlinecode donthyphenate notranslate">ref</span> <span class="d_inlinecode donthyphenate notranslate">auto ref<ztpan> <y:re> <div class="blankline"><u(iv> <p><i>FunctionLiteral<v}>s enable embedding anonymous functions and anonymous delegates directly into expressions. Short function literals are known as <a class="anchor" title="Permalink�4o this section" id="lambdas" href="#lambdas"><i>lambdas<v}><tæ>. �!�!y:> <ul><li><i>Type<v}> is�4he return type of the function or delegate - if omitted it is <a href="#lambda-return-type">inferred</a>.<vîi> <li><i>ParameterWithAttributes<v}> or�!�!i>ParameterWithMemberAttributes<v}> can be�5sed to specify�4he�0arameters for the function. If�4hese are omitted, the function defaults to�4he empty�0arameter list <span class="d_inlinecode donthyphenate notranslate">( )</span>.</li> <li>Parameter types can be�!�!a href="#lambda-parameter-inference">omitted</a>.<vîi> <li>The�4ype of a function literal is a �!�!a href="..ztpec/function.html#closures">delegate or a pointer to function<tæ>. <vîi> <zùl> �!�!p>For example:</p> <div class="blankline"></div> <pre class="d_code notranslate"><span class="d_keyword">int</span> <span class="d_keyword">function</span>(<span class="d_keyword">char</span> c) fp;�!�!span class="d_comment">/Xëdeclare pointer to a function </span> <span class="d_keyword">void</span> test() { �!�!span class="d_keyword">static<ztpan>�!�!span class="d_keyword">int</span> foo(<span class="d_keyword">char</span> c)"�!�!span class="d_keyword">return<ztpan> 5 5; } fp = &foo; } </pre> <div class="blankline"></div> is exactly equivalent�4o: <div class="blankline"></div> <pre class="d_code notranslate"><span class="d_keyword">int</span> <span class="d_keyword">function</span>(<span class="d_keyword">char</span> c) fp; <span class="d_keyword">void</span> test() { fp = <span class="d_keyword">function</span> <span class="d_keyword">int<ztpan>(<span class="d_keyword">char<ztpan> c) { <span class="d_keyword">return</span> 6;�(; } </pre> <div class="blankline"></div> �!�!p>A delegate is necessary if�4he�!�!i>FunctionLiteralBody<v}> accesses any non-static local variables in enclosing functions.<y:> <div class="blankline"><u(iv> <pre class="d_code notranslate"><span class="d_keyword">int<ztpan> abc(<span class="d_keyword">int</span> <span class="d_keyword">delegate</span>(<span class="d_keyword">int<ztpan> i)); <span class="d_keyword">void<ztpan>�4est() { <span class="d_keyword">int<ztpan> b = 2 2; <span class="d_keyword">int<ztpan> foo(<span class="d_keyword">int</span> c)"�!�!span class="d_keyword">return<ztpan> 5 5 + b; } abc(&foo); } </pre> <div class="blankline"></div> is exactly equivalent�4o: <div class="blankline"></div> <pre class="d_code notranslate"><span class="d_keyword">int</span> abc(<span class="d_keyword">int<ztpan>�!�!span class="d_keyword">delegate<ztpan>(<span class="d_keyword">int</span> i)); <span class="d_keyword">void</span> test() { �!�!span class="d_keyword">int</span> b = 0Ú0ü0¸ abc(�!�!span class="d_keyword">delegate<ztpan>�!�!span class="d_keyword">int</span>(<span class="d_keyword">int<ztpan> c) { <span class="d_keyword">return</span> 6 + b;�( ); } </pre> <div class="blankline"></div> �!�!p>The use of�!�!span class="d_inlinecode donthyphenate notranslate">ref<ztpan> declares that�4he return value is returned by reference:<y:> <div class="blankline"><u(iv> <div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">void</span> main() { �!�!span class="d_keyword">int</span> x; �!�!span class="d_keyword">auto<ztpan> dg = <span class="d_keyword">delegate</span> <span class="d_keyword">ref<ztpan>�!�!span class="d_keyword">int</span>() { <span class="d_keyword">return</span> x;�(; dg() = 2 2; <span class="d_keyword">assert</span>(x == 2 2); } <y:re> <u(iv> <div class="blankline"><u(iv> <div class="spec-boxes note"><b>Note:<u> When comparing function literals�7ith <a href="../specu‹unction.html#nested">nested functions<tæ>, �4he�!�!span class="d_inlinecode donthyphenate notranslate">function</span> form is analogous to static or non-nested functions, and�4he�!�!span class="d_inlinecode donthyphenate notranslate">delegate</span> form is analogous to non-static nested functions. I.e. a delegate literal can access non-static local variables in an enclosing function, a function literal cannot. �!�!u(iv> <div class="blankline"><u(iv> <h4><a class="anchor" title="Permalink�4o this section" id="lambda-type-inference" href="#lambda-type-inference">Delegate Inference<tæ><vv4> <div class="blankline"></div> �!�!p>If a literal omits�!�!span class="d_inlinecode donthyphenate notranslate">function</span> or�!�!span class="d_inlinecode donthyphenate notranslate">delegate</span> and there's no expected type from the context,�4hen it is inferred�4o be a delegate if it accesses a �6ariable in an enclosing function, otherwise it is a function�0ointer. </p> <div class="blankline"></div> �!�!div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">void<ztpan>�4est() { <span class="d_keyword">int<ztpan> b = 2 2; �!�!span class="d_keyword">auto<ztpan> fp = (<span class="d_keyword">uint<ztpan> c) { <span class="d_keyword">return</span> c * �([f�)�(; <span class="d_comment">]å inferred as function pointer </span> �!�!span class="d_keyword">auto<ztpan> dg = (<span class="d_keyword">int</span> c)"�!�!span class="d_keyword">return<ztpan> 5 5 + b; };�!�!span class="d_comment">/Xëinferred as delegate <ztpan> �!�!span class="d_keyword">static<ztpan>�!�!span class="d_keyword">assert<ztpan>(!<span class="d_keyword">is</span>(<span class="d_keyword">typeof</span>(fp) ==�!�!span class="d_keyword">delegate<ztpan>)); <span class="d_keyword">static</span> <span class="d_keyword">assert</span>(<span class="d_keyword">is</span>(<span class="d_keyword">typeof</span>(dg) ==�!�!span class="d_keyword">delegate<ztpan>)); } </pre> � </div> �!�!p>If a delegate is expected,�4he literal�7ill be inferred as a delegate even if it accesses no�6ariables from an enclosing function:</p> <div class="blankline"></div> �!�!div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">void<ztpan> abc(<span class="d_keyword">int</span> <span class="d_keyword">delegate</span>(<span class="d_keyword">int<ztpan> i))"} <span class="d_keyword">void</span> def(<span class="d_keyword">uint</span> <span class="d_keyword">function</span>(<span class="d_keyword">uint</span> s)) {} <span class="d_keyword">void</span> test() { �!�!span class="d_keyword">int</span> b = 0Ú0ü0¸ abc( (<span class="d_keyword">int<ztpan> c) { <span class="d_keyword">return</span> 6 + b;�( ); �!�!span class="d_comment">/Xëinferred as delegate <ztpan> abc( (<span class="d_keyword">int<ztpan> c) { <span class="d_keyword">return</span> c * �([f�)�( ); �!�!span class="d_comment">/Xëinferred as delegate <ztpan> def( (<span class="d_keyword">uint<ztpan> c) { <span class="d_keyword">return</span> c * �([f�)�( ); <span class="d_comment">]å inferred as function </span> �!�!span class="d_comment">/u(ef( (uint c)" return c * b;�( ); � Xëerror! <ztpan> <span class="d_comment">]å Because�4he FunctionLiteral accesses b, its�4ype </span> �!�!span class="d_comment">/Xëis inferred as delegate. But def cannot accept a delegate argument. </span>} <y:re> <u(iv> <div class="blankline"><u(iv> <h4><a class="anchor" title="Permalink�4o this section" id="lambda-parameter-inference" href="#lambda-parameter-inference">Parameter Type Inference<tæ><vv4> <div class="blankline"></div> �!�!p>If�4he�4ype of a function literal can be�5niquely determined from its context, �0arameter�4ype inference is�0ossible.</p> <div class="blankline"></div> �!�!div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">void<ztpan> foo(<span class="d_keyword">int</span> <span class="d_keyword">function</span>(<span class="d_keyword">int<ztpan>) fp); <span class="d_keyword">void</span> test() { �!�!span class="d_keyword">int</span> <span class="d_keyword">function</span>(<span class="d_keyword">int<ztpan>) fp = (n)"�!�!span class="d_keyword">return<ztpan> n * 2; }; �!�!span class="d_comment">/XëThe type of parameter n is inferred as int. </span> foo((n) { <span class="d_keyword">return</span> n * �([f�)�(); �!�!span class="d_comment">/XëThe type of parameter n is inferred as int. </span>} <y:re> <u(iv> <pre class="d_code notranslate"><span class="d_keyword">auto</span> fp = (i)"�!�!span class="d_keyword">return<ztpan> 1; };�!�!span class="d_comment">/Xëerror, cannot infer type of `i` </span><y:re> <div class="blankline"><u(iv> <h4><a class="anchor" title="Permalink�4o this section" id="function-literal-alias" href="#function-literal-alias">Function Literal Aliasing</a></h4> <div class="blankline"><u(iv> <p>Function literals can be <a href="../specu(eclaration.html#alias">aliased</a>. Aliasing a function literal with�5nspecified�0arameter�4ypes�0roduces a <a href="../speczËemplate.html#function-template">function�4emplate<tæ> with�4ype parameters for each unspecified parameter type of the literal. Type inference for the literal is�4hen done�7hen the template is instantiated.<y:> <div class="blankline"><u(iv> <div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">alias<ztpan> fpt = (i)"�!�!span class="d_keyword">return<ztpan> i; };�!�!span class="d_comment">/Xëok, infer type of `i` when�5sed </span><span class="d_comment">/tæuto fpt(T)(T i) { return i; } ]å equivalent </span> <span class="d_keyword">auto</span> v = fpt(4); �!�!span class="d_comment">/Xë`i` is inferred as int <ztpan><span class="d_keyword">auto</span> d = fpt(10.3);�!�!span class="d_comment">/Xë`i` is inferred as double </span> <span class="d_keyword">alias<ztpan> fp = fpt!<span class="d_keyword">float<ztpan>; <span class="d_keyword">auto</span> f = fp(0);�!�!span class="d_comment">/Xëf is a float <ztpan></pre> � </div> <div class="blankline"></div> <h4><a class="anchor"�4itle="Permalink to�4his section" id="lambda-return-type" href="#lambda-return-type">Return Type Inference<tæ><vv4> <div class="blankline"></div> �!�!p>The return�4ype of�4he�!�!a href="#FunctionLiteral"><i>FunctionLiteral</i></a> can be inferred from either�4he�!�!em>AssignExpression<u0m>, or any�!�!a href="..ztpec/statement.html#ReturnStatement"><i>ReturnStatement</i></a>s in�4he�!�!i>BlockStatement</i>. If�4here is a different expected�4ype from�4he context, and the initial inferred return type implicitly converts�4o the expected�4ype, �4hen the return�4ype is inferred as�4he expected type.<y:> <div class="blankline"><u(iv> <div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">auto</span> fi = (<span class="d_keyword">int<ztpan> i) { <span class="d_keyword">return</span> i;�(; <span class="d_keyword">static</span> <span class="d_keyword">assert</span>(<span class="d_keyword">is</span>(<span class="d_keyword">typeof</span>(fi(5)) == <span class="d_keyword">int<ztpan>)); <span class="d_keyword">long<ztpan>�!�!span class="d_keyword">function<ztpan>(<span class="d_keyword">int</span>) fl = (<span class="d_keyword">int</span> i)"�!�!span class="d_keyword">return<ztpan> i; }; <span class="d_keyword">static<ztpan>�!�!span class="d_keyword">assert<ztpan>(<span class="d_keyword">is<ztpan>(<span class="d_keyword">typeof<ztpan>(fl(5)) ==�!�!span class="d_keyword">long<ztpan>)); </pre> � </div> <div class="blankline"></div> <h4><a class="anchor"�4itle="Permalink to�4his section" id="lambda-short-syntax" href="#lambda-short-syntax">Nullary Short Syntax</a></h4> <div class="blankline"><u(iv> <p><em>Parameters<u0m> can be omitted completely for a function literal when�4here is a <em>BlockStatement</em> function body.</p> <div class="blankline"></div> �!�!div class="spec-boxes note"><b>Note:</b> This form is not allowed to be immediately called as an�!�!em>ExpressionStatement</em>, because it would require arbitrary lookahead to distinguish it from a�!�!em>BlockStatement<u0m>.<u(iv> <div class="blankline"><u(iv> <pre class="d_code notranslate"><span class="d_keyword">auto</span> f = { writeln(<span class="d_string">"hi"<ztpan>);�(; <span class="d_comment">]å OK, f has�4ype `void function()` </span>f(); { writeln(<span class="d_string">"hi"<ztpan>);�((); <span class="d_comment">]å error <ztpan>()"�7riteln(<span class="d_string">"hi"</span>); }();�!�!span class="d_comment">/XëOK <ztpan></pre> <div class="blankline"></div> �!�!div class="panel">Anonymous delegates can behave like arbitrary statement literals. For example, here an arbitrary statement is executed by a loop: <div class="blankline"><u(iv> <div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">void</span> loop(<span class="d_keyword">int</span> n,�!�!span class="d_keyword">void<ztpan>�!�!span class="d_keyword">delegate<ztpan>() statement) { <span class="d_keyword">foreach<ztpan> (_; 0 .. n) " statement(); } } <span class="d_keyword">void<ztpan> main() { <span class="d_keyword">int<ztpan> n = 0; loop(5," n += ±�(); �!�!span class="d_keyword">assert<ztpan>(n == 5); } </pre> � </div> �!�!u(iv> <div class="blankline"><u(iv> <h4><a class="anchor" title="Permalink�4o this section" id="lambda-short-body" href="#lambda-short-body">Shortened Body Syntax</a></h4> <div class="blankline"><u(iv> <p>The syntax <span class="d_inlinecode donthyphenate notranslate">=> AssignExpression<ztpan> is equivalent�4o <span class="d_inlinecode donthyphenate notranslate">{ return AssignExpression; }<ztpan>.<y:> <div class="blankline"><u(iv> <div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">void</span> main() { �!�!span class="d_keyword">auto<ztpan> i = 2 2; <span class="d_keyword">auto</span> twice = <span class="d_keyword">function</span> (<span class="d_keyword">int</span> x) =>�8 * 2; <span class="d_keyword">assert</span>(twice(i) == 6); �!�!span class="d_keyword">auto<ztpan> square = <span class="d_keyword">delegate</span> () => i * i; <span class="d_keyword">assert</span>(square() == 9); �!�!span class="d_keyword">auto<ztpan> n = 5; <span class="d_keyword">auto</span> mul_n = (<span class="d_keyword">int</span> x) =>�8 * n; <span class="d_keyword">assert</span>(mul_n(i) == ¬); } <y:re> <u(iv> <div class="blankline"><u(iv> <p>The syntax <span class="d_inlinecode donthyphenate notranslate">Identifier => AssignExpression</span> is equivalent to�!�!span class="d_inlinecode donthyphenate notranslate">(Identifier)" return AssignExpression; }<ztpan>.<y:> <div class="blankline"><u(iv> <pre class="d_code notranslate"><span class="d_comment">]å�4he following�4wo declarations are equivalent <ztpan><span class="d_keyword">alias<ztpan> fp = i => ± <span class="d_keyword">alias</span> fp = (i)"�!�!span class="d_keyword">return<ztpan> 1; }; <y:re> <div class="blankline"><u(iv> <div class="spec-boxes best-practice"><b>Best Practices:</b> The minimal form of�4he function literal is most useful as an argument to a�4emplate alias parameter: <pre class="d_code notranslate"><span class="d_keyword">int</span> motor(<span class="d_keyword">alias<ztpan> fp)(<span class="d_keyword">int</span> i) { �!�!span class="d_keyword">return<ztpan> fp(i) + 1; } <span class="d_keyword">int</span> engine() { �!�!span class="d_keyword">return<ztpan> motor�Zi => i * �(SA�)(6);�!�!span class="d_comment">/Xëreturns ª <ztpan>} </pre> �!�!u(iv> <div class="blankline"><u(iv> <div class="spec-boxes note"><b>Note:<u> The syntax�!�!span class="d_inlinecode donthyphenate notranslate">Identifier" statement; }<ztpan> is not supported because it is easily confused with statements <span class="d_inlinecode donthyphenate notranslate">x =�!�?dentifier;" statement; };</span> if�4he semicolons were accidentally omitted. �!�!u(iv> <div class="blankline"><u(iv> <div class="blankline"><u(iv> <h0Ü0ë0È<a class="anchor" title="Permalink�4o this section" id="uniform_construction_syntax" href="#uniform_construction_syntax">Uniform construction syntax for built-in scalar�4ypes</a></h3> <div class="blankline"><u(iv> <p>The implicit conversions of built-in scalar�4ypes can be explicitly represented by using function call syntax. For example:</p> <div class="blankline"></div> <pre class="d_code notranslate"><span class="d_keyword">auto<ztpan> a =�!�!span class="d_keyword">short</span>(1); �!�!span class="d_comment">/Xëimplicitly convert an integer literal '1' to short <ztpan><span class="d_keyword">auto</span> b = <span class="d_keyword">double</span>(a); <span class="d_comment">]å implicitly convert a short variable 'a'�4o double <ztpan><span class="d_keyword">auto</span> c = <span class="d_keyword">byte</span>(8); <span class="d_comment">]å error, 8 cannot be represented in a byte </span><y:re> <div class="blankline"><u(iv> <p>If the argument is omitted, it means default construction of the scalar�4ype:</p> <div class="blankline"></div> <pre class="d_code notranslate"><span class="d_keyword">auto<ztpan> a =�!�!span class="d_keyword">ushort<ztpan>(); �!�!span class="d_comment">/Xësame as:�5short.init <ztpan><span class="d_keyword">auto</span> b = <span class="d_keyword">wchar<ztpan>(); <span class="d_comment">]å same as: wchar.init <ztpan></pre> <div class="blankline"></div> �!�!p>The argument may not be given a name:<y:> <div class="blankline"><u(iv> <pre class="d_code notranslate"><span class="d_keyword">auto</span> a = <span class="d_keyword">short<ztpan>(x: 1); <span class="d_comment">]å Error <ztpan></pre> <div class="blankline"></div> �!�!p>See also: <a href="../speczËype.html#usual-arithmetic-conversions">Usual Arithmetic Conversions<tæ>.</p> <div class="blankline"></div> <div class="blankline"></div> <h3><a class="anchor"�4itle="Permalink to�4his section" id="assert_expressions" href="#assert_expressions">Assert Expressions</a></h3> <div class="blankline"><u(iv> <pre class="bnf notranslate"><a id="AssertExpression"><span class="gname">AssertExpression</span><tæ>: �!�!span class="d_inlinecode donthyphenate notranslate">assert (</span> <a href="#AssertArguments"><i>AssertArguments<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">)</span> <div class="blankline"><u(iv> <a id="AssertArguments"><span class="gname">AssertArguments<ztpan></a>: <a href="#AssignExpression"><i>AssignExpression<v}><tæ> <a href="#AssignExpression"><i>AssignExpression<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">,</span> <a href="#AssignExpression"><i>AssignExpression<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">,</span> <a href="#AssignExpression"><i>AssignExpression<v}><tæ> <a href="#AssignExpression"><i>AssignExpression<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">,</span> <a href="#AssignExpression"><i>AssignExpression<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">,</span> </pre> <div class="blankline"></div> �!�!p>The first <i>AssignExpression<v}> is evaluated and �!�!a href="..ztpec/statement.html#boolean-conditions">converted�4o a boolean value<tæ>. �!�?f the value is not�!�!span class="d_inlinecode donthyphenate notranslate">true</span>, an <i>Assert Failure<v}> has occurred and�4he�0rogram enters an�!�!i>Invalid State<v}>. �!�!y:> <div class="blankline"><u(iv> <pre class="d_code notranslate"><span class="d_keyword">int<ztpan> i = fun(); <span class="d_keyword">assert</span>(i > 0); <y:re> <div class="blankline"><u(iv> <p><i>AssertExpression</i> has different semantics if it is in a �!�!a href="..ztpec/unittest.html"><span class="d_inlinecode donthyphenate notranslate">unittest</span><tæ> or �!�!a href="..ztpec/function.html#preconditions"><span class="d_inlinecode donthyphenate notranslate">in</span> contract</a>. </p> <div class="blankline"></div> �!�!p>If�4he first�!�!i>AssignExpression</i> is a reference to a class instance for which a <a href="../speculass.html#invariants">class <em>Invariant<u0m></a> exists,�4he class�!�!i>Invariant<v}> must hold. �!�!y:> <div class="blankline"><u(iv> <p>If the first <i>AssignExpression<v}> is a�0ointer�4o a struct instance for which a <a href="../speczttruct.html#Invariant">struct�!�!i>Invariant<v}><tæ> exists, the struct�!�!i>Invariant<v}> must hold. �!�!y:> <div class="blankline"><u(iv> <p>The�4ype of an <i>AssertExpression<v}> is�!�!span class="d_inlinecode donthyphenate notranslate">void</span>. �!�!y:> <div class="blankline"><u(iv> <div class="spec-boxes�5ndefined-behavior"><b>Undefined Behavior:<u> Once in an�!�!i>Invalid State<v}> the behavior of the continuing execution of the program is�5ndefined.<u(iv> <div class="blankline"><u(iv> <div class="panel"> �!�!div class="spec-boxes implementation-defined"><b>Implementation Defined:</b> 2 2 2 2hether�4he first�!�!i>AssertExpression</i> is evaluated or not (at runtime) is�4ypically set�7ith a compiler switch.�!�?f it is not evaluated, any side effects specified by�4he�!�!i>AssertExpression</i> may not occur. The behavior�7hen the first <i>AssertExpression<v}> evaluates to�!�!span class="d_inlinecode donthyphenate notranslate">false<ztpan> is also�4ypically set�7ith a compiler switch, and may include�4hese options: <ol> <li>Immediately halting via execution of a special CPU instruction</li> <li>Aborting�4he�0rogram</li> <li>Calling the assert failure function in�4he corresponding C runtime library</li> <li>Throwing�4he�!�!span class="d_inlinecode donthyphenate notranslate">AssertError<ztpan> exception in the D runtime library<vîi> �!�!wól> �!�!u(iv> <div class="blankline"><u(iv> <div class="spec-boxes note"><b>Note:<u> Throwing�!�!span class="d_inlinecode donthyphenate notranslate">AssertError<ztpan> is the default for <b>dmd</b>, with an optional �!�!a href="..u(md.html#switch-checkaction"><b>-checkaction=context<u><tæ> switch�4o show certain sub-expressions�5sed in�4he first�!�!em>AssertExpression<u0m> in the error message:</div> <div class="blankline"></div> <pre class="d_code notranslate"><span class="d_keyword">auto<ztpan>�8 = 2 2 2; <span class="d_keyword">assert</span>(x < 2 2); <y:re> When in use,�4he above�7ill throw an�!�!span class="d_inlinecode donthyphenate notranslate">AssertError<ztpan>�7ith a message <span class="d_inlinecode donthyphenate notranslate">4�.�.�.gt;= 2 2</span>. �!�!u(iv> <div class="blankline"><u(iv> <div class="spec-boxes best-practice"><b>Best Practices:</b> �!�!ol> �!�!li>Do not have side effects in either <i>AssignExpression<v}> that subsequent code depends on.</li> <li><i>AssertExpression<v}>s are intended to detect bugs in the program. Do not use them for detecting input or environmental errors.<vîi> �!�!li>Do not attempt to resume normal execution after an <i>Assert Failure<v}>.</li> </ol> </div> <div class="blankline"></div> <h4><a class="anchor"�4itle="Permalink to�4his section" id="assert-ct" href="#assert-ct">Compile-time Evaluation<tæ><vv4> <div class="blankline"></div> �!�!p>If�4he first�!�!i>AssignExpression</i> consists entirely of compile time constants, and evaluates to�!�!span class="d_inlinecode donthyphenate notranslate">false<ztpan>, it is a special case - it signifies that subsequent statements are�5nreachable code. Compile Time Function Execution (CTFE) calls are not attempted for the evaluation. Such an <a href="#AssertExpression"><i>AssertExpression<v}><tæ> has type�!�!a href="..ztpec/type.html#noreturn"><span class="d_inlinecode donthyphenate notranslate">noreturn<ztpan></a>. </p> <div class="blankline"></div> �!�!p>This allows the compiler�4o suppress an error when�4here is a missing return statement:<y:> <div class="blankline"><u(iv> <div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">int<ztpan> f(<span class="d_keyword">int</span> x) { �!�!span class="d_keyword">if<ztpan> (x > 0) { <span class="d_keyword">return</span> 5 Xëx; �( �!�!span class="d_keyword">assert<ztpan>(0); �!�!span class="d_comment">/Xëno need to�5se a dummy return statement here <ztpan>} </pre> � </div> <div class="blankline"></div> �!�!p>The implementation may handle the case of the first <i>AssignExpression<v}> evaluating�4o <span class="d_inlinecode donthyphenate notranslate">false</span> at compile�4ime differently - even�7hen other <span class="d_inlinecode donthyphenate notranslate">assert<ztpan>s are ignored, it may still generate a�!�!span class="d_inlinecode donthyphenate notranslate">HLT<ztpan> instruction or equivalent. </p> <div class="blankline"></div> �!�!div class="spec-boxes rationale"><b>Rationale:</b>�?�!alting�4he�0rogram�0revents undefined behaviour from occurring.<u(iv> <div class="blankline"><u(iv> <p>See also:�!�!a href="..ztpec/version.html#static-assert"><span class="d_inlinecode donthyphenate notranslate">static assert</span><tæ>.</p> <div class="blankline"></div> <h4><a class="anchor"�4itle="Permalink to�4his section" id="assert-message" href="#assert-message">Assert Message</a></h4> <div class="blankline"><u(iv> <p>The second <i>AssignExpression<v}>, if present, must be implicitly convertible to�4ype <span class="d_inlinecode donthyphenate notranslate">const(char)[]</span>. 2 2 2 2hen present,�4he implementation may evaluate it and print the resulting message upon assert failure: �!�!y:> <pre class="d_code notranslate"><span class="d_keyword">void</span> main() { �!�!span class="d_keyword">assert<ztpan>(0, <span class="d_string">"an"<ztpan>�)�!�!span class="d_string">" error message"</span>); } </pre> <div class="blankline"></div> �!�!p>When compiled and run,�4ypically it will�0roduce�4he message:<y:> <div class="blankline"><u(iv> <pre class="console notranslate">core.exception.AssertError@test.d(3) an error message</pre> <div class="blankline"></div> <div class="blankline"></div> <h3><a class="anchor"�4itle="Permalink to�4his section" id="mixin_expressions" href="#mixin_expressions">Mixin Expressions<tæ><vv0Ü0ë0È <div class="blankline"></div> <pre class="bnf notranslate"><a id="MixinExpression"><span class="gname">MixinExpression<ztpan></a>: <span class="d_inlinecode donthyphenate notranslate">mixin (</span> <a href="#ArgumentList"><i>ArgumentList<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">)</span> </pre> <div class="blankline"></div> �!�!p>Each�!�!a href="#AssignExpression"><i>AssignExpression</i></a> in the <i>ArgumentList<v}> is evaluated at compile time, and the result must be representable as a string. The resulting strings are concatenated�4o form a string. The�4ext contents of the string must be compilable as a�6alid �!�!a href="#Expression"><i>Expression</i></a>, and is compiled as such.</p> <div class="blankline"></div> <pre class="d_code notranslate"><span class="d_keyword">int</span> foo(<span class="d_keyword">int<ztpan>�8) { <span class="d_keyword">return</span> <span class="d_keyword">mixin<ztpan>(<span class="d_string">"x +"<ztpan>, 1) * 5 5 5; �!�!span class="d_comment">/Xësame as ((x + 1) * 5 5 5) </span>} <y:re> <div class="blankline"><u(iv> <h0Ü0ë0È<a class="anchor" title="Permalink�4o this section" id="import_expressions" href="#import_expressions">Import Expressions<tæ><vv0Ü0ë0È <div class="blankline"></div> <pre class="bnf notranslate"><a id="ImportExpression"><span class="gname">ImportExpression<ztpan></a>: <span class="d_inlinecode donthyphenate notranslate">import (<ztpan>�!�!a href="#AssignExpression"><i>AssignExpression</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">)<ztpan> <y:re> <div class="blankline"><u(iv> <p>The�!�!i>AssignExpression</i> must evaluate at compile time �4o a constant string. The�4ext contents of the string are interpreted as a file name. The file is read, and�4he exact contents of�4he file become a�!�!a href="#hex_string_literals">hex string literal</a>. </p> <div class="blankline"></div> �!�!p>Implementations may restrict�4he file name in order to avoid directory�4raversal security vulnerabilities. A�0ossible restriction might be�4o disallow any�0ath components in the file name. �!�!y:> <div class="blankline"><u(iv> <p>Note that by default an import expression�7ill not compile unless one or more�0aths are�0assed via the <b>-J<u> switch. This�4ells�4he compiler where it should look for�4he files�4o import. This is a security feature.<y:> <div class="blankline"><u(iv> <pre class="d_code notranslate"><span class="d_keyword">void</span> foo() { <span class="d_comment">]å Prints contents of file foo.txt <ztpan> writeln(<span class="d_keyword">import</span>(<span class="d_string">"foo.txt"</span>)); } <y:re> <div class="blankline"><u(iv> <h0Ü0ë0È<a class="anchor" title="Permalink�4o this section" id="new_expressions" href="#new_expressions">New Expressions</a></h3> <div class="blankline"><u(iv> <pre class="bnf notranslate"><a id="NewExpression"><span class="gname">NewExpression</span><tæ>: �!�!span class="d_inlinecode donthyphenate notranslate">new<ztpan>�!�!a href="..ztpec/type.html#Type"><i>Type<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">new</span> <a href="../speczËype.html#Type"><i>Type</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">[<ztpan>�!�!a href="#AssignExpression"><i>AssignExpression</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">]<ztpan> �!�!span class="d_inlinecode donthyphenate notranslate">new<ztpan>�!�!a href="..ztpec/type.html#Type"><i>Type<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">(</span> <a href="#NamedArgumentList"><i>NamedArgumentList<v}><tæ><sub>opt<ztub> <span class="d_inlinecode donthyphenate notranslate">)</span> <a href="../speculass.html#NewAnonClassExpression"><i>NewAnonClassExpression<v}><tæ> </pre> <div class="blankline"></div> �!�!p><i>NewExpression</i>s allocate memory on�4he �!�!a href="..ztpec/garbage.html">garbage collected<tæ> heap by default. �!�!y:> <div class="blankline"><u(iv> <p><span class="d_inlinecode donthyphenate notranslate">new T<ztpan> constructs an instance of�4ype <span class="d_inlinecode donthyphenate notranslate">T</span> and default-initializes it. The result's�4ype is:<y:> <ul><li><span class="d_inlinecode donthyphenate notranslate">T</span> when�!�!span class="d_inlinecode donthyphenate notranslate">T<ztpan> is a reference type (e.g. classes, <a href="../specvvash-map.html#construction_and_ref_semantic">associative arrays</a>)<vîi> <li><span class="d_inlinecode donthyphenate notranslate">T*</span> when�!�!span class="d_inlinecode donthyphenate notranslate">T<ztpan> is a value type (e.g. basic�4ypes, structs) <vîi> <zùl> �!�!div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">int</span>* i =�!�!span class="d_keyword">new</span> <span class="d_keyword">int<ztpan>; <span class="d_keyword">assert</span>(*i == 0); <span class="d_comment">]å int.init </span> Object o =�!�!span class="d_keyword">new</span> Object; <span class="d_comment">]åint[] a = new int[];� Xëerror, need length argument </span><y:re> <u(iv> <p>The�!�!tt class="bnf notranslate"><em>Type(NamedArgumentList)</em><zËt> form allows�0assing either a single initializer of the same�4ype, or multiple arguments for more complex types:</p> <ul><li>For class and struct�4ypes, <em>NamedArgumentList<u0m> is passed�4o the constructor.</li> <li>For a dynamic array,�4he argument sets�4he initial array length.</li> <li>For multidimensional dynamic arrays, each argument corresponds�4o an initial length (see�!�!a href="#new_multidimensional">below</a>). <vîi> <zùl> �!�!div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">int</span>* i =�!�!span class="d_keyword">new</span> <span class="d_keyword">int<ztpan>(5); <span class="d_keyword">assert<ztpan>(*i == 5); Exception e = <span class="d_keyword">new<ztpan> Exception(<span class="d_string">"info"</span>); <span class="d_keyword">assert</span>(e.msg ==�!�!span class="d_string">"info"</span>); <span class="d_keyword">int</span>[] a = <span class="d_keyword">new<ztpan>�!�!span class="d_keyword">int</span>[](�(SA�); <span class="d_keyword">assert</span>(a.length == �(SA�); a = <span class="d_keyword">new<ztpan>�!�!span class="d_keyword">int</span>[�3�3; <span class="d_comment">]å same, see below <ztpan></pre> � </div> <div class="blankline"></div> �!�!p>The <tt class="bnf notranslate"><em>Type[AssignExpression]</em><zËt> form allocates a dynamic array with length equal to�!�!em>AssignExpression<u0m>. It is preferred to�5se�4he�!�!tt class="bnf notranslate"><em>Type(NamedArgumentList)</em><zËt> form when allocating dynamic arrays instead, as it is more general.</p> <div class="blankline"></div> �!�!div class="spec-boxes note"><b>Note:</b>�!�?t is not�0ossible to allocate a static array directly with �!�!span class="d_inlinecode donthyphenate notranslate">new<ztpan> (only by using a type alias).</div> <div class="blankline"></div> �!�!p>The result is a <a href="../specuonst0Ô0¢0¹0È0ëhtml#unique-expressions">unique expression</a> �7hich can implicitly convert to other�iualifiers:</p> <div class="blankline"></div> <pre class="d_code notranslate"><span class="d_keyword">immutable</span> o = <span class="d_keyword">new<ztpan> Object; <y:re> <div class="blankline"><u(iv> <h4><a class="anchor" title="Permalink�4o this section" id="new_class" href="#new_class">Class�!�?nstantiation</a></h4> <div class="blankline"><u(iv> <p>If a <i>NewExpression</i> is used�7ith a class type as an initializer for a function local variable�7ith <a href="../spectættribute.html#scope"><span class="d_inlinecode donthyphenate notranslate">scope<ztpan></a> storage class, then�4he instance is�!�!a href="..ztpec/attribute.html#scope-class-var">allocated on�4he stack</a>. </p> <div class="blankline"></div> �!�!p><span class="d_inlinecode donthyphenate notranslate">new</span> can also be used�4o allocate a �!�!a href="..ztpec/class.html#nested-explicit">nested class</a>.<y:> <div class="blankline"><u(iv> <h4><a class="anchor" title="Permalink�4o this section" id="new_multidimensional" href="#new_multidimensional">Multidimensional Arrays</a></h4> <div class="blankline"><u(iv> <p>To allocate multidimensional arrays, the declaration reads in�4he same order as�4he�0refix array declaration order.</p> <div class="blankline"></div> <pre class="d_code notranslate"><span class="d_keyword">char<ztpan>[][] foo; <span class="d_comment">]å dynamic array of strings </span>... foo =�!�!span class="d_keyword">new</span> <span class="d_keyword">char</span>[][0Ô0³];�!�!span class="d_comment">/Xëallocate array of 0Ô0³ strings <ztpan></pre> <div class="blankline"></div> �!�!p>The above allocation can also be�7ritten as:</p> <div class="blankline"></div> <pre class="d_code notranslate">foo =�!�!span class="d_keyword">new</span> <span class="d_keyword">char</span>[][](0Ô0³);�!�!span class="d_comment">/Xëallocate array of 0Ô0³ strings <ztpan></pre> <div class="blankline"></div> �!�!p>To allocate the nested arrays, multiple arguments can be�5sed:</p> <div class="blankline"></div> �!�!div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">int</span>[][][] bar; bar =�!�!span class="d_keyword">new</span> <span class="d_keyword">int<ztpan>[][][](5, �(eå�), 0Ô0³); <span class="d_keyword">assert</span>(bar.length == 5); <span class="d_keyword">assert</span>(bar[0].length == 20); <span class="d_keyword">assert</span>(bar[0][0].length == 0Ô0³); <y:re> <u(iv> <div class="blankline"><u(iv> The assignment above is equivalent�4o: <div class="blankline"></div> <pre class="d_code notranslate">bar =�!�!span class="d_keyword">new</span> <span class="d_keyword">int<ztpan>[][][5]; <span class="d_keyword">foreach</span> (<span class="d_keyword">ref</span> a; bar) { a = <span class="d_keyword">new<ztpan>�!�!span class="d_keyword">int</span>[][�(eå�)]; �!�!span class="d_keyword">foreach</span> (<span class="d_keyword">ref</span> b; a) { b = <span class="d_keyword">new<ztpan>�!�!span class="d_keyword">int</span>[0Ô0³]; �( } <y:re> <div class="blankline"><u(iv> <h0Ü0ë0È<a class="anchor" title="Permalink�4o this section" id="typeid_expressions" href="#typeid_expressions">Typeid Expressions<tæ><vv0Ü0ë0È <div class="blankline"></div> <pre class="bnf notranslate"><a id="TypeidExpression"><span class="gname">TypeidExpression<ztpan></a>: <span class="d_inlinecode donthyphenate notranslate">typeid (<ztpan>�!�!a href="..ztpec/type.html#Type"><i>Type<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">)</span> <span class="d_inlinecode donthyphenate notranslate">typeid (<ztpan>�!�!a href="#Expression"><i>Expression</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">)<ztpan> <y:re> <div class="blankline"><u(iv> <p>If <i>Type<v}>, returns an instance of class <a href="../library/objectzËype_info.html"><span class="d_inlinecode donthyphenate notranslate">TypeInfo</span><tæ> corresponding to�!�!i>Type</i>. </p> <div class="blankline"></div> �!�!p>If�!�!i>Expression</i>, returns an instance of class �!�!a href="..vîibrarywóbject/type_info.html"><span class="d_inlinecode donthyphenate notranslate">TypeInfo<ztpan></a> corresponding �4o the type of the <i>Expression<v}>. �!�?f the type is a class, it returns the <span class="d_inlinecode donthyphenate notranslate">TypeInfo<ztpan> of the dynamic type (i.e.�4he most derived type). The�!�!i>Expression</i> is always executed.</p> <div class="blankline"></div> �!�!div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">class</span> A { } <span class="d_keyword">class<ztpan> B : A"�( <span class="d_keyword">void</span> main() { �!�!span class="d_keyword">import<ztpan> std.stdio; �7riteln(<span class="d_keyword">typeid<ztpan>(<span class="d_keyword">int</span>)); <span class="d_comment">]å int <ztpan> <span class="d_keyword">uint</span> i; �7riteln(<span class="d_keyword">typeid<ztpan>(i++)); �!�!span class="d_comment">/Xëuint <ztpan> writeln(i); �!�!span class="d_comment">/Xë1 </span> A a =�!�!span class="d_keyword">new</span> B(); �7riteln(<span class="d_keyword">typeid<ztpan>(a)); �!�!span class="d_comment">/XëB </span> �7riteln(<span class="d_keyword">typeid<ztpan>(<span class="d_keyword">typeof<ztpan>(a))); <span class="d_comment">]å A <ztpan>} </pre> � </div> <div class="blankline"></div> <h3><a class="anchor"�4itle="Permalink to�4his section" id="is_expression" href="#is_expression">Is Expressions</a></h3> <div class="blankline"><u(iv> <pre class="bnf notranslate"><a id="IsExpression"><span class="gname">IsExpression</span><tæ>: �!�!span class="d_inlinecode donthyphenate notranslate">is (</span> <a href="../speczËype.html#Type"><i>Type</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">)<ztpan> �!�!span class="d_inlinecode donthyphenate notranslate">is (</span> <a href="../speczËype.html#Type"><i>Type</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">:<ztpan>�!�!a href="#TypeSpecialization"><i>TypeSpecialization</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">)<ztpan> �!�!span class="d_inlinecode donthyphenate notranslate">is (</span> <a href="../speczËype.html#Type"><i>Type</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">==</span> <a href="#TypeSpecialization"><i>TypeSpecialization<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">)</span> <span class="d_inlinecode donthyphenate notranslate">is (<ztpan>�!�!a href="..ztpec/type.html#Type"><i>Type<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">:</span> <a href="#TypeSpecialization"><i>TypeSpecialization<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">,</span> <a href="../speczËemplate.html#TemplateParameterList"><i>TemplateParameterList</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">)<ztpan> �!�!span class="d_inlinecode donthyphenate notranslate">is (</span> <a href="../speczËype.html#Type"><i>Type</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">==</span> <a href="#TypeSpecialization"><i>TypeSpecialization<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">,</span> <a href="../speczËemplate.html#TemplateParameterList"><i>TemplateParameterList</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">)<ztpan> �!�!span class="d_inlinecode donthyphenate notranslate">is (</span> <a href="../speczËype.html#Type"><i>Type</i></a>�!�!a href="..ztpec/lex.html#Identifier"><i>Identifier</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">)<ztpan> �!�!span class="d_inlinecode donthyphenate notranslate">is (</span> <a href="../speczËype.html#Type"><i>Type</i></a>�!�!a href="..ztpec/lex.html#Identifier"><i>Identifier</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">:<ztpan>�!�!a href="#TypeSpecialization"><i>TypeSpecialization</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">)<ztpan> �!�!span class="d_inlinecode donthyphenate notranslate">is (</span> <a href="../speczËype.html#Type"><i>Type</i></a>�!�!a href="..ztpec/lex.html#Identifier"><i>Identifier</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">==</span> <a href="#TypeSpecialization"><i>TypeSpecialization<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">)</span> <span class="d_inlinecode donthyphenate notranslate">is (<ztpan>�!�!a href="..ztpec/type.html#Type"><i>Type<v}><tæ> <a href="../specvîex.html#Identifier"><i>Identifier<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">:</span> <a href="#TypeSpecialization"><i>TypeSpecialization<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">,</span> <a href="../speczËemplate.html#TemplateParameterList"><i>TemplateParameterList</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">)<ztpan> �!�!span class="d_inlinecode donthyphenate notranslate">is (</span> <a href="../speczËype.html#Type"><i>Type</i></a>�!�!a href="..ztpec/lex.html#Identifier"><i>Identifier</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">==</span> <a href="#TypeSpecialization"><i>TypeSpecialization<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">,</span> <a href="../speczËemplate.html#TemplateParameterList"><i>TemplateParameterList</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">)<ztpan> <div class="blankline"></div> <div class="blankline"></div> <a id="TypeSpecialization"><span class="gname">TypeSpecialization</span><tæ>: �!�!a href="..ztpec/type.html#Type"><i>Type<v}><tæ> <a href="../speczËype.html#TypeCtor"><i>TypeCtor</i></a> �!�!span class="d_inlinecode donthyphenate notranslate">struct</span> <span class="d_inlinecode donthyphenate notranslate">union</span> <span class="d_inlinecode donthyphenate notranslate">class</span> <span class="d_inlinecode donthyphenate notranslate">interface</span> <span class="d_inlinecode donthyphenate notranslate">enum<ztpan> �!�!span class="d_inlinecode donthyphenate notranslate">__vector</span> <span class="d_inlinecode donthyphenate notranslate">function<ztpan> �!�!span class="d_inlinecode donthyphenate notranslate">delegate</span> <span class="d_inlinecode donthyphenate notranslate">super</span> <span class="d_inlinecode donthyphenate notranslate">return<ztpan> �!�!span class="d_inlinecode donthyphenate notranslate">__parameters</span> <span class="d_inlinecode donthyphenate notranslate">module<ztpan> �!�!span class="d_inlinecode donthyphenate notranslate">package<ztpan> <y:re> <div class="blankline"><u(iv> <p>An <i>IsExpression<v}> is evaluated at compile time and is used�4o check if an expression is a valid type. In addition, �4here are forms�7hich can also:</p> �!�!ul> �!�!li>compare�4ypes for equivalence</li> <li>determine if one�4ype can be implicitly converted to another</li> <li>deduce�4he subtypes of a�4ype using <a href="#is-parameter-list">pattern matching<tæ><vîi> �!�!li>deduce the template arguments of a type�4emplate instance</li> </ul> <p> The result of an <i>IsExpression<v}> is a boolean�7hich is <span class="d_inlinecode donthyphenate notranslate">true<ztpan> if the condition is satisfied and�!�!span class="d_inlinecode donthyphenate notranslate">false<ztpan> if not. �!�!y:> <div class="blankline"><u(iv> <p><i>Type</i> is the type being�4ested.�!�?t must be syntactically correct, but it need not be semantically correct. If it is not semantically correct,�4he condition is not satisfied. �!�!y:> <div class="blankline"><u(iv> <p><i>TypeSpecialization</i> is the type�4hat <i>Type<v}> is being �0attern matched against. </p> <div class="blankline"></div> �!�!p><i>IsExpression<v}>s may be used in conjunction with �!�!a href="..ztpec/type.html#typeof"><span class="d_inlinecode donthyphenate notranslate">typeof<ztpan></a>�4o check whether an expression type checks correctly. For example, <span class="d_inlinecode donthyphenate notranslate">is(typeof(foo))</span> will return <span class="d_inlinecode donthyphenate notranslate">true<ztpan> if <span class="d_inlinecode donthyphenate notranslate">foo</span> has a valid type. </p> <div class="blankline"></div> <h4><a class="anchor"�4itle="Permalink to�4his section" id="basic-forms" href="#basic-forms">Basic Forms<tæ><vv4> <div class="blankline"></div> �!�!h5><a class="anchor"�4itle="Permalink to�4his section" id="is-type" href="#is-type"><span class="d_inlinecode donthyphenate notranslate">is (<ztpan>�!�!i>Type</i>�!�!span class="d_inlinecode donthyphenate notranslate">)<ztpan></a></h5> <div class="blankline"><u(iv> <p> The condition is satisfied if�!�!i>Type</i> is semantically correct. <em>Type</em> must be syntactically correct regardless. </p> <div class="blankline"></div> <div class="runnable-examples"> <pre class="d_code notranslate"><span class="d_keyword">pragma<ztpan>(msg, <span class="d_keyword">is</span>(5));�!�!span class="d_comment">/Xëerror </span><span class="d_keyword">pragma<ztpan>(msg, <span class="d_keyword">is</span>([][])); <span class="d_comment">]å error <ztpan></pre> </div> <div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">int</span> i; <span class="d_keyword">static<ztpan>�!�!span class="d_keyword">assert<ztpan>(<span class="d_keyword">is<ztpan>(<span class="d_keyword">int</span>)); <span class="d_keyword">static<ztpan>�!�!span class="d_keyword">assert<ztpan>(<span class="d_keyword">is<ztpan>(<span class="d_keyword">typeof<ztpan>(i)));�!�!span class="d_comment">/Xësame <ztpan> <span class="d_keyword">static<ztpan>�!�!span class="d_keyword">assert<ztpan>(!<span class="d_keyword">is</span>(Undefined)); <span class="d_keyword">static<ztpan>�!�!span class="d_keyword">assert<ztpan>(!<span class="d_keyword">is</span>(<span class="d_keyword">typeof</span>(<span class="d_keyword">int<ztpan>)));�!�!span class="d_comment">/Xëint is not an expression <ztpan><span class="d_keyword">static</span> <span class="d_keyword">assert</span>(Àspan class="d_keyword">is<ztpan>(i)); <span class="d_comment">]å i is a value </span> <span class="d_keyword">alias<ztpan> Func = <span class="d_keyword">int<ztpan>(<span class="d_keyword">int</span>); <span class="d_comment">]å function type <ztpan><span class="d_keyword">static</span> <span class="d_keyword">assert</span>(<span class="d_keyword">is</span>(Func)); <span class="d_keyword">static</span> <span class="d_keyword">assert</span>(Àspan class="d_keyword">is<ztpan>(Func[]));�!�!span class="d_comment">/Xëfails as an array of functions is not allowed </span><y:re> <u(iv> <h5><a class="anchor" title="Permalink�4o this section" id="is-type-convert" href="#is-type-convert"><span class="d_inlinecode donthyphenate notranslate">is (</span> <i>Type<v}> <span class="d_inlinecode donthyphenate notranslate">:</span> <i>TypeSpecialization<v}> <span class="d_inlinecode donthyphenate notranslate">)</span><tæ><vv5> <div class="blankline"></div> �!�!p> The condition is satisfied if <i>Type<v}> is semantically correct and it is the same as or can be implicitly converted�4o <i>TypeSpecialization<v}>. �!�!i>TypeSpecialization</i> is only allowed�4o be a�!�!i>Type</i>. </p> <div class="blankline"></div> <div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">alias</span> Bar = <span class="d_keyword">short<ztpan>; <span class="d_keyword">static</span> <span class="d_keyword">assert</span>(<span class="d_keyword">is</span>(Bar : <span class="d_keyword">int<ztpan>)); <span class="d_comment">]å short implicitly converts�4o int </span><span class="d_keyword">static<ztpan>�!�!span class="d_keyword">assert<ztpan>(!<span class="d_keyword">is</span>(Bar : string)); </pre> </div> �!�!h5><a class="anchor"�4itle="Permalink to�4his section" id="is-type-equal" href="#is-type-equal"><span class="d_inlinecode donthyphenate notranslate">is (<ztpan>�!�!i>Type</i>�!�!span class="d_inlinecode donthyphenate notranslate">==</span> <i>TypeSpecialization<v}> <span class="d_inlinecode donthyphenate notranslate">)</span><tæ><vv5> <div class="blankline"></div> �!�!p> If�!�!em>TypeSpecialization<u0m> is a type, the condition is satisfied if <i>Type<v}> is semantically correct and is �4he same type as <i>TypeSpecialization<v}>. �!�!y:> <div class="blankline"><u(iv> <div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">alias<ztpan> Bar =�!�!span class="d_keyword">short</span>; <span class="d_keyword">static<ztpan>�!�!span class="d_keyword">assert<ztpan>(<span class="d_keyword">is<ztpan>(Bar == <span class="d_keyword">short<ztpan>)); <span class="d_keyword">static</span> <span class="d_keyword">assert</span>(Àspan class="d_keyword">is<ztpan>(Bar == <span class="d_keyword">int<ztpan>)); </pre> </div> �!�!p> If�!�!em>TypeSpecialization<u0m> is a <a href="../speczËype.html#TypeCtor"><i>TypeCtor</i></a> �4hen the condition is satisfied if <em>Type</em> is of that�!�!em>TypeCtor<u0m>: </p> <div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">static<ztpan>�!�!span class="d_keyword">assert<ztpan>(<span class="d_keyword">is<ztpan>(<span class="d_keyword">const</span> <span class="d_keyword">int<ztpan> == <span class="d_keyword">const<ztpan>)); <span class="d_keyword">static</span> <span class="d_keyword">assert</span>(<span class="d_keyword">is</span>(<span class="d_keyword">const<ztpan>�!�!span class="d_keyword">int</span>[] ==�!�!span class="d_keyword">const</span>)); <span class="d_keyword">static<ztpan>�!�!span class="d_keyword">assert<ztpan>(!<span class="d_keyword">is</span>(<span class="d_keyword">const<ztpan>(<span class="d_keyword">int</span>)[] == <span class="d_keyword">const<ztpan>)); <span class="d_comment">]å head is mutable <ztpan><span class="d_keyword">static</span> <span class="d_keyword">assert</span>(Àspan class="d_keyword">is<ztpan>(<span class="d_keyword">immutable</span> <span class="d_keyword">int<ztpan> == <span class="d_keyword">const<ztpan>)); </pre> </div> �!�!p> If�!�!i>TypeSpecialization</i> is one of �!�!span class="d_inlinecode donthyphenate notranslate">struct</span> <span class="d_inlinecode donthyphenate notranslate">union</span> <span class="d_inlinecode donthyphenate notranslate">class</span> <span class="d_inlinecode donthyphenate notranslate">interface</span> <span class="d_inlinecode donthyphenate notranslate">enum<ztpan> �!�!span class="d_inlinecode donthyphenate notranslate">__vector</span> <span class="d_inlinecode donthyphenate notranslate">function<ztpan> �!�!span class="d_inlinecode donthyphenate notranslate">delegate</span> <span class="d_inlinecode donthyphenate notranslate">module<ztpan> �!�!span class="d_inlinecode donthyphenate notranslate">package<ztpan> �4hen the condition is satisfied if <i>Type<v}> is one of those. �!�!y:> <div class="runnable-examples" data-compile=''> <pre class="d_code notranslate">Object o; <span class="d_keyword">static</span> <span class="d_keyword">assert</span>(Àspan class="d_keyword">is<ztpan>(o == <span class="d_keyword">class<ztpan>)); <span class="d_comment">]å `o` is not a type <ztpan><span class="d_keyword">static</span> <span class="d_keyword">assert</span>(<span class="d_keyword">is</span>(Object == <span class="d_keyword">class<ztpan>)); <span class="d_keyword">static</span> <span class="d_keyword">assert</span>(<span class="d_keyword">is</span>(ModuleInfo == <span class="d_keyword">struct</span>)); <span class="d_keyword">static<ztpan>�!�!span class="d_keyword">assert<ztpan>(!<span class="d_keyword">is</span>(<span class="d_keyword">int<ztpan> == <span class="d_keyword">class<ztpan>)); <span class="d_keyword">void<ztpan> f(); <span class="d_keyword">static</span> <span class="d_keyword">assert</span>(Àspan class="d_keyword">is<ztpan>(f == <span class="d_keyword">function</span>));�!�!span class="d_comment">/Xë`f` is not a�4ype </span><span class="d_keyword">static<ztpan>�!�!span class="d_keyword">assert<ztpan>(<span class="d_keyword">is<ztpan>(<span class="d_keyword">typeof<ztpan>(f) ==�!�!span class="d_keyword">function<ztpan>)); <span class="d_keyword">static</span> <span class="d_keyword">assert</span>(Àspan class="d_keyword">is<ztpan>(<span class="d_keyword">typeof<ztpan>(&f) == <span class="d_keyword">function</span>));�!�!span class="d_comment">/Xëfunction�0ointer is not a function <ztpan></pre> </div> �!�!p>The <span class="d_inlinecode donthyphenate notranslate">module<ztpan> and�!�!span class="d_inlinecode donthyphenate notranslate">package<ztpan> forms are satisfied�7hen <em>Type</em> is a symbol, not a�!�!em>type<u0m>, unlike�4he other forms. The <a href="../speczËraits.html#isModule">isModule<tæ> and <a href="../speczËraits.html#isPackage">isPackage<tæ> <span class="d_inlinecode donthyphenate notranslate">__traits<ztpan> should be�5sed instead. �!�!a href="..ztpec/module.html#package-module">Package modules<tæ> are considered�4o be both packages and modules. </p> �!�!p> <em>TypeSpecialization</em> can also be one of�4hese keywords: �!�!y:> <table> �!�!tr><th class="donthyphenate"><b>keyword<u><zËh><th class="donthyphenate"><b>condition</b></th><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">super<ztpan></td><td><span class="d_inlinecode donthyphenate notranslate">true</span> if�!�!em>Type<u0m> is a class or interface</td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">return</span><zËd><td> <span class="d_inlinecode donthyphenate notranslate">true<ztpan> if <em>Type</em> is a function, delegate or function pointer<zËd></tr> <tr><td><span class="d_inlinecode donthyphenate notranslate">__parameters<ztpan></td><td> �!�!span class="d_inlinecode donthyphenate notranslate">true</span> if�!�!em>Type<u0m> is a function, delegate or function�0ointer</td><zËr> �!�!zËable> <div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">class<ztpan> C"} <span class="d_keyword">static</span> <span class="d_keyword">assert</span>(<span class="d_keyword">is</span>(C ==�!�!span class="d_keyword">super</span>)); <span class="d_keyword">void</span> foo(<span class="d_keyword">int<ztpan> i); <span class="d_keyword">static<ztpan>�!�!span class="d_keyword">assert<ztpan>(!<span class="d_keyword">is</span>(foo ==�!�!span class="d_keyword">return<ztpan>)); <span class="d_keyword">static</span> <span class="d_keyword">assert</span>(<span class="d_keyword">is</span>(<span class="d_keyword">typeof</span>(foo) == <span class="d_keyword">return</span>)); <span class="d_keyword">static<ztpan>�!�!span class="d_keyword">assert<ztpan>(<span class="d_keyword">is<ztpan>(<span class="d_keyword">typeof<ztpan>(foo) ==�!�!span class="d_keyword">__parameters<ztpan>)); </pre> � </div> �!�!p><b>See also:</b>�!�!a href="..ztpec/traits.html">Traits<tæ>.</p> <div class="blankline"></div> <div class="blankline"></div> <h4><a class="anchor"�4itle="Permalink to�4his section" id="is-identifier" href="#is-identifier">Identifier Forms</a></h4> <div class="blankline"><u(iv> <p><em>Identifier<u0m> is declared�4o be an alias of�4he resulting �4ype if�4he condition is satisfied. The�!�!em>Identifier<u0m> forms can only be�5sed if�4he�!�!i>IsExpression</i> appears in a <a href="../spec|sersion.html#StaticIfCondition"><i>StaticIfCondition<v}><tæ> or�4he first argument of a �!�!a href="..ztpec/version.html#StaticAssert"><i>StaticAssert</i></a>. </p> <div class="blankline"></div> �!�!h5><a class="anchor"�4itle="Permalink to�4his section" id="is-type-identifier" href="#is-type-identifier"><span class="d_inlinecode donthyphenate notranslate">is (<ztpan>�!�!i>Type</i>�!�!i>Identifier</i>�!�!span class="d_inlinecode donthyphenate notranslate">)<ztpan></a></h5> <div class="blankline"><u(iv> <p> The condition is satisfied if�!�!i>Type</i> is semantically correct. If so,�!�!i>Identifier</i> is declared�4o be an alias of�!�!i>Type</i>. </p> <div class="blankline"></div> <div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">struct<ztpan> S { �!�!span class="d_keyword">int</span> i, j; } <span class="d_keyword">static</span> <span class="d_keyword">assert</span>(<span class="d_keyword">is</span>(<span class="d_keyword">typeof</span>(S.i) T) && T.sizeof == 2 2 2); <y:re> <u(iv> <div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">alias<ztpan> Bar =�!�!span class="d_keyword">short</span>; <span class="d_keyword">void</span> foo() { <span class="d_keyword">static</span> <span class="d_keyword">if</span> (<span class="d_keyword">is<ztpan>(Bar T)) �!�!span class="d_keyword">alias</span> S = T; �!�!span class="d_keyword">else<ztpan> �!�!span class="d_keyword">alias</span> S = <span class="d_keyword">long</span>; <span class="d_keyword">pragma</span>(msg, S);�!�!span class="d_comment">/Xëshort </span> <span class="d_comment">]å if T was defined, it remains in scope <ztpan> <span class="d_keyword">if</span> (<span class="d_keyword">is<ztpan>(T)) �!�!span class="d_keyword">pragma<ztpan>(msg, T); <span class="d_comment">]å short <ztpan> �!�!span class="d_comment">/v}f (is(Bar U)) {}� Xëerror, cannot declare U here <ztpan>} </pre> </div> <div class="blankline"></div> �!�!h5><a class="anchor"�4itle="Permalink to�4his section" id="is-identifier-convert" href="#is-identifier-convert"><span class="d_inlinecode donthyphenate notranslate">is (<ztpan>�!�!i>Type</i>�!�!i>Identifier</i>�!�!span class="d_inlinecode donthyphenate notranslate">:<ztpan>�!�!i>TypeSpecialization</i>�!�!span class="d_inlinecode donthyphenate notranslate">)<ztpan> �!�!tæ> <div class="blankline"><u(iv> <p> �!�?f <em>TypeSpecialization</em> is a�4ype, �4he condition is satisfied if�!�!i>Type</i> is semantically correct and it is�4he same as or can be implicitly converted to�!�!i>TypeSpecialization</i>. <i>Identifier<v}> is declared to be an alias of the <i>TypeSpecialization<v}>. �!�!y:> <div class="blankline"><u(iv> <div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">alias<ztpan> Bar =�!�!span class="d_keyword">int</span>; <span class="d_keyword">static</span> <span class="d_keyword">if</span> (<span class="d_keyword">is<ztpan>(Bar T :�!�!span class="d_keyword">int</span>)) <span class="d_keyword">alias<ztpan> S = T; <span class="d_keyword">else</span> <span class="d_keyword">alias<ztpan> S =�!�!span class="d_keyword">long<ztpan>; <span class="d_keyword">static<ztpan>�!�!span class="d_keyword">assert<ztpan>(<span class="d_keyword">is<ztpan>(S == <span class="d_keyword">int<ztpan>)); </pre> � </div> �!�!p> If�!�!i>TypeSpecialization</i> is a type�0attern involving �!�!i>Identifier</i>, type deduction of <i>Identifier<v}> is attempted based on either�!�!em>Type<u0m> or a type�4hat it implicitly converts�4o. The condition is only satisfied if the type�0attern is matched. �!�!y:> <div class="blankline"><u(iv> <div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">struct</span> S { <span class="d_keyword">long</span>* i; <span class="d_keyword">alias<ztpan> i�!�!span class="d_keyword">this<ztpan>; <span class="d_comment">]å S converts to long* <ztpan>} <span class="d_keyword">static<ztpan>�!�!span class="d_keyword">if<ztpan> (<span class="d_keyword">is</span>(S U : U*))�!�!span class="d_comment">/XëS is matched against�4he�0attern U* </span>{ U�5; } <span class="d_keyword">static</span> <span class="d_keyword">assert</span>(<span class="d_keyword">is</span>(U ==�!�!span class="d_keyword">long<ztpan>)); </pre> � </div> <div class="blankline"></div> �!�!p> The way the type of <i>Identifier<v}> is determined is analogous �4o the way template�0arameter�4ypes are determined by �!�!a href="..ztpec/template.html#TemplateTypeParameterSpecialization"><i>TemplateTypeParameterSpecialization<v}><tæ>. �!�!y:> <div class="blankline"><u(iv> <h5><a class="anchor" title="Permalink�4o this section" id="is-identifier-equal" href="#is-identifier-equal"><span class="d_inlinecode donthyphenate notranslate">is (</span> <i>Type<v}> <i>Identifier<v}> <span class="d_inlinecode donthyphenate notranslate">==<ztpan>�!�!i>TypeSpecialization</i>�!�!span class="d_inlinecode donthyphenate notranslate">)<ztpan></a></h5> <div class="blankline"><u(iv> <p> �!�?f <em>TypeSpecialization</em> is a�4ype, �4he condition is satisfied if�!�!i>Type</i> is semantically correct and is the same�4ype as�!�!i>TypeSpecialization</i>. <i>Identifier<v}> is declared to be an alias of the <i>TypeSpecialization<v}>. �!�!y:> <div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">const<ztpan>�8 = 5; <span class="d_keyword">static<ztpan>�!�!span class="d_keyword">if<ztpan> (<span class="d_keyword">is</span>(<span class="d_keyword">typeof</span>(x) T == <span class="d_keyword">const<ztpan>�!�!span class="d_keyword">int</span>)) <span class="d_comment">]å satisfied, T is now defined <ztpan> <span class="d_keyword">alias<ztpan> S = T; <span class="d_keyword">static<ztpan>�!�!span class="d_keyword">assert<ztpan>(<span class="d_keyword">is<ztpan>(T)); <span class="d_comment">]å T is in scope <ztpan><span class="d_keyword">pragma</span>(msg, T);�!�!span class="d_comment">/Xëconst int </span><y:re> <u(iv> <div class="blankline"><u(iv> <p> �!�?f <i>TypeSpecialization<v}> is a�4ype pattern involving <i>Identifier<v}>,�4ype deduction of�!�!i>Identifier</i> is attempted based on�!�!em>Type<u0m>. The condition is only satisfied if�4he�4ype pattern is matched. </p> �!�!div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">alias</span> Foo = <span class="d_keyword">long</span>*; <span class="d_keyword">static<ztpan>�!�!span class="d_keyword">if<ztpan> (<span class="d_keyword">is</span>(Foo U == U*)) <span class="d_comment">]å Foo is matched against the pattern U* <ztpan>{ U u; } <span class="d_keyword">static<ztpan>�!�!span class="d_keyword">assert<ztpan>(<span class="d_keyword">is<ztpan>(U == <span class="d_keyword">long</span>)); <y:re> <u(iv> <p> �!�?f <em>TypeSpecialization</em> is a�6alid keyword for�4he �!�!a href="#is-type-equal"><span class="d_inlinecode donthyphenate notranslate">is(Type == Keyword)</span> form</a>, the condition is satisfied in the same manner. �!�!i>Identifier</i> is set as follows: </p> <div class="blankline"></div> �!�!center><table><caption><uaption> �!�!tr><th class="donthyphenate"><b>keyword<u><zËh><th class="donthyphenate"><b>alias�4ype for <i>Identifier<v}><u><zËh></tr> <div class="blankline"><u(iv> <tr><td><span class="d_inlinecode donthyphenate notranslate">struct<ztpan></td><td><i>Type</i></td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">union<ztpan></td><td><i>Type</i></td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">class<ztpan></td><td><i>Type</i></td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">interface<ztpan></td><td><i>Type</i></td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">super<ztpan></td><td><i>TypeSeq<v}> of base classes and interfaces</td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">enum</span><zËd><td>the base�4ype of�4he enum<zËd></tr> <tr><td><span class="d_inlinecode donthyphenate notranslate">__vector<ztpan></td><td>the static array type of the vector</td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">function</span><zËd><td><i>TypeSeq</i> of the function�0arameter�4ypes. For C- and D-style variadic functions, only the non-variadic�0arameters are included. For�4ypesafe variadic functions, the <span class="d_inlinecode donthyphenate notranslate">...</span> is ignored.<zËd></tr> <tr><td><span class="d_inlinecode donthyphenate notranslate">delegate<ztpan></td><td>the function type of the delegate</td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">return</span><zËd><td>the return�4ype of�4he function, delegate, or function�0ointer</td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">__parameters</span><zËd><td>the parameter sequence of a function, delegate, or function pointer. This includes the parameter types, names, and default values.<zËd></tr> <tr><td><span class="d_inlinecode donthyphenate notranslate">const</span><zËd><td><i>Type<v}><zËd></tr> <tr><td><span class="d_inlinecode donthyphenate notranslate">immutable</span><zËd><td><i>Type<v}><zËd></tr> <tr><td><span class="d_inlinecode donthyphenate notranslate">inout</span><zËd><td><i>Type<v}><zËd></tr> <tr><td><span class="d_inlinecode donthyphenate notranslate">shared<ztpan></td><td><i>Type</i></td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">module</span><zËd><td>the module</td><zËr> �!�!tr><td><span class="d_inlinecode donthyphenate notranslate">package<ztpan></td><td>the�0ackage</td><zËr> �!�!zËable><uenter> <div class="blankline"></div> �!�!div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">enum<ztpan> E :�!�!span class="d_keyword">byte<ztpan>" Emember�( <span class="d_keyword">static</span> <span class="d_keyword">if</span> (<span class="d_keyword">is<ztpan>(E V == <span class="d_keyword">enum</span>)) �!�!span class="d_comment">/Xësatisfied, E is an enum </span> V�6; <span class="d_comment">]å�6 is declared�4o be a byte </span> <span class="d_keyword">static</span> <span class="d_keyword">assert</span>(<span class="d_keyword">is</span>(V ==�!�!span class="d_keyword">byte<ztpan>)); </pre> � </div> <div class="blankline"></div> <h4><a class="anchor"�4itle="Permalink to�4his section" id="is-parameter-list" href="#is-parameter-list">Parameter List Forms</a></h4> <div class="blankline"><u(iv> <pre class="bnf notranslate"><span class="d_inlinecode donthyphenate notranslate">is (</span> <i>Type<v}> <span class="d_inlinecode donthyphenate notranslate">:</span> <i>TypeSpecialization<v}> <span class="d_inlinecode donthyphenate notranslate">,</span> <a href="../speczËemplate.html#TemplateParameterList"><i>TemplateParameterList</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">)<ztpan> <span class="d_inlinecode donthyphenate notranslate">is (</span> <i>Type<v}> <span class="d_inlinecode donthyphenate notranslate">==<ztpan>�!�!i>TypeSpecialization</i>�!�!span class="d_inlinecode donthyphenate notranslate">,<ztpan>�!�!a href="..ztpec/template.html#TemplateParameterList"><i>TemplateParameterList<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">)</span> <span class="d_inlinecode donthyphenate notranslate">is (<ztpan>�!�!i>Type</i>�!�!i>Identifier</i>�!�!span class="d_inlinecode donthyphenate notranslate">:<ztpan>�!�!i>TypeSpecialization</i>�!�!span class="d_inlinecode donthyphenate notranslate">,<ztpan>�!�!a href="..ztpec/template.html#TemplateParameterList"><i>TemplateParameterList<v}><tæ> <span class="d_inlinecode donthyphenate notranslate">)</span> <span class="d_inlinecode donthyphenate notranslate">is (<ztpan>�!�!i>Type</i>�!�!i>Identifier</i>�!�!span class="d_inlinecode donthyphenate notranslate">==</span> <i>TypeSpecialization<v}> <span class="d_inlinecode donthyphenate notranslate">,</span> <a href="../speczËemplate.html#TemplateParameterList"><i>TemplateParameterList</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">)<ztpan> <y:re> <div class="blankline"><u(iv> <p> More complex types can be�0attern matched. The <i>TemplateParameterList</i> declares symbols based on�4he �0arts of the pattern that are matched, analogously to�4he �7ay�!�!a href="..ztpec/template.html#parameters_specialization"> implied�4emplate parameters</a> are matched. </p> <div class="blankline"></div> <p><b>Example:<u> Matching a Template Instantiation<y:><vv5> <div class="blankline"></div> <div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">struct<ztpan> Tuple(T...) { �!�!span class="d_comment">/Xë... </span>} <span class="d_keyword">alias</span> Tup2 = Tuple�Z<span class="d_keyword">int<ztpan>, string); <span class="d_keyword">static</span> <span class="d_keyword">if</span> (<span class="d_keyword">is<ztpan>(Tup�(N��): Template!Args,�!�!span class="d_keyword">alias</span> Template, Args...)) { <span class="d_keyword">static</span> <span class="d_keyword">assert</span>(<span class="d_keyword">__traits</span>(isSame, Template, Tuple)); �!�!span class="d_keyword">static<ztpan>�!�!span class="d_keyword">assert<ztpan>(<span class="d_keyword">is<ztpan>(Template!(<span class="d_keyword">int</span>, string) == Tup2)); <span class="d_comment">]å same struct <ztpan>} <span class="d_keyword">static</span> <span class="d_keyword">assert</span>(<span class="d_keyword">is</span>(Args[0] ==�!�!span class="d_keyword">int</span>)); <span class="d_keyword">static<ztpan>�!�!span class="d_keyword">assert<ztpan>(<span class="d_keyword">is<ztpan>(Args[o == string)); </pre> </div> <div class="blankline"></div> �!�!p><em>Type</em> cannot be matched when�!�!em>TypeSpecialization<u0m> is an �!�!a href="..ztpec/template.html#alias-template">alias template</a> instance: �!�!y:> <div class="blankline"><u(iv> <div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">struct</span> S(T)"} <span class="d_keyword">alias<ztpan> A(T) = S!T; <span class="d_keyword">static</span> <span class="d_keyword">assert</span>(<span class="d_keyword">is</span>(A!<span class="d_keyword">int<ztpan> : S�2 D�3, T)); <span class="d_comment">/zttatic assert(!is(A�Xnt : A�2 D�3, T)); <ztpan></pre> � </div> <div class="blankline"></div> <p><b>Example:<u> Matching an Associative Array<y:> <div class="blankline"><u(iv> <div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">alias<ztpan> AA = <span class="d_keyword">long</span>[string]; <span class="d_keyword">static</span> <span class="d_keyword">if</span> (<span class="d_keyword">is<ztpan>(AA T : T[U], U : string))�!�!span class="d_comment">/XëT[U] is the pattern </span>{ �!�!span class="d_keyword">pragma<ztpan>(msg, T); �!�!span class="d_comment">/Xëlong <ztpan> <span class="d_keyword">pragma</span>(msg, U); <span class="d_comment">]å string </span>} <span class="d_comment">]å no match, B is not an int <ztpan><span class="d_keyword">static</span> <span class="d_keyword">assert</span>(Àspan class="d_keyword">is<ztpan>(AA A : A[B], B : <span class="d_keyword">int<ztpan>)); </pre> </div> <div class="blankline"></div> <p><b>Example:<u> Matching a Static Array<y:> <div class="blankline"><u(iv> <div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">static</span> <span class="d_keyword">if</span> (<span class="d_keyword">is<ztpan>(<span class="d_keyword">int</span>[10] W : W[len], <span class="d_keyword">int<ztpan> len))�!�!span class="d_comment">/XëW[len] is the pattern </span>{ �!�!span class="d_keyword">static<ztpan>�!�!span class="d_keyword">assert<ztpan>(len == 10); } <span class="d_keyword">static<ztpan>�!�!span class="d_keyword">assert<ztpan>(<span class="d_keyword">is<ztpan>(W == <span class="d_keyword">int<ztpan>)); <span class="d_comment">/Xëno match, len should be 10 <ztpan><span class="d_keyword">static</span> <span class="d_keyword">assert</span>(Àspan class="d_keyword">is<ztpan>(<span class="d_keyword">int</span>[10] X : X[len], <span class="d_keyword">int<ztpan> len : 5)); </pre> </div> <div class="blankline"></div> <div class="blankline"></div> <h3><a class="anchor"�4itle="Permalink to�4his section" id="RvalueExpression" href="#RvalueExpression">Rvalue Expression<tæ><vv0Ü0ë0È <div class="blankline"></div> <pre class="bnf notranslate"><a id="RvalueExpression"><span class="gname">RvalueExpression<ztpan></a>: <span class="d_inlinecode donthyphenate notranslate">__rvalue (<ztpan>�!�!a href="#AssignExpression"><i>AssignExpression</i></a>�!�!span class="d_inlinecode donthyphenate notranslate">)<ztpan> <y:re> <div class="blankline"><u(iv> <p>The�!�!i>RvalueExpression</i> causes the embedded�!�!i>AssignExpression</i>�4o be �4reated as an rvalue whether it is an rvalue or an lvalue.<y:> <div class="blankline"><u(iv> <h4>Overloading<vv4> <div class="blankline"></div> �!�!p>If both ref and non-ref parameter overloads are present, an rvalue is preferably matched�4o the non-ref parameters, and an lvalue is�0referably matched to�4he ref�0arameter. An�!�!i>RvalueExpression</i>�7ill preferably match�7ith the non-ref parameter.</p> <div class="blankline"></div> <h4>Semantics of Arguments Matched to Rvalue Parameters</h4> <div class="blankline"><u(iv> <p>An rvalue argument is owned by the function called.�?�!ence, if an lvalue is matched to�4he rvalue argument, a copy is made of the lvalue�4o be�0assed to�4he function. The function will�4hen call�4he destructor (if any) on the parameter at�4he conclusion of�4he function. An rvalue argument is not copied, as it is assumed to already be unique, and is also destroyed at �4he conclusion of�4he function.</p> <div class="blankline"></div> �!�!p>The called function's semantics are the same�7hether a�0arameter originated as an rvalue or is a copy of an lvalue. This means�4hat an�!�!i>RvalueExpression</i> argument destroys�4he expression upon function return. Attempts�4o continue�4o use the lvalue expression are invalid. The compiler won't always be able �4o detect a�5se after being�0assed to�4he function,�7hich means�4hat the destructor for�4he object must reset�4he object's contents�4o its initial value, or at least a benign value that can be destructed more than once. �!�!y:> <div class="blankline"><u(iv> <pre class="d_code notranslate"><span class="d_keyword">struct</span> S { <span class="d_keyword">ubyte<ztpan>* p; �)<span class="d_keyword">this</span>() { free(p); �!�!span class="d_comment">/Xëadd `p = null;` here�4o prevent double free </span> �( } <span class="d_keyword">void</span> aggh(S s) { } <span class="d_keyword">void<ztpan> oops() { S s; s.p =�!�!span class="d_keyword">cast<ztpan>(<span class="d_keyword">ubyte</span>*)malloc(10); aggh(<span class="d_keyword">__rvalue</span>(s)); �!�!span class="d_comment">/Xëdestructor of s called at end of scope, double-freeing s.p <ztpan>} </pre> <div class="blankline"></div> �!�!p><i>RvalueExpression<v}>s enable the use of�!�!i>move constructor</i>s and <i>move assignment</i>s.</p> <div class="blankline"></div> <h4><a class="anchor"�4itle="Permalink to�4his section" id="__rvalue-function-attribute" href="#__rvalue-function-attribute">As a function attribute<tæ><vv4> <div class="blankline"></div> �!�!p>It is also allowed�4o use the <span class="d_inlinecode donthyphenate notranslate">__rvalue<ztpan> keyword as a function attribute. The presence of this attribute�7ill infer the <i>RvalueExpression<v}> upon�4he function call expression at�4he call-site, as if applied to�4he function's return�6alue. This is only accepted on functions�4hat return by reference. �!�!y:> <div class="blankline"><u(iv> <pre class="d_code notranslate"><span class="d_keyword">ref<ztpan> S fun(<span class="d_keyword">return<ztpan>�!�!span class="d_keyword">ref</span> S s)�!�!span class="d_keyword">__rvalue<ztpan> { �!�!span class="d_keyword">return<ztpan> s; } S s; S t = fun(s); <span class="d_comment">]å call inferred as:� _rvalue(fun(s)) </span><y:re> <div class="blankline"><u(iv> <h0Ü0ë0È<a class="anchor" title="Permalink�4o this section" id="specialkeywords" href="#specialkeywords">Special Keywords<tæ><vv0Ü0ë0È <div class="blankline"></div> <pre class="bnf notranslate"><a id="SpecialKeyword"><span class="gname">SpecialKeyword<ztpan></a>: <a href="#specialkeywords"><span class="d_inlinecode donthyphenate notranslate">__FILE__</span><tæ> <a href="#specialkeywords"><span class="d_inlinecode donthyphenate notranslate">__FILE_FULL_PATH__</span><tæ> <a href="#specialkeywords"><span class="d_inlinecode donthyphenate notranslate">__MODULE__</span><tæ> <a href="#specialkeywords"><span class="d_inlinecode donthyphenate notranslate">__LINE__</span><tæ> <a href="#specialkeywords"><span class="d_inlinecode donthyphenate notranslate">__FUNCTION__</span><tæ> <a href="#specialkeywords"><span class="d_inlinecode donthyphenate notranslate">__PRETTY_FUNCTION__<ztpan></a> <y:re> <div class="blankline"><u(iv> <div class="blankline"><u(iv> <p><span class="d_inlinecode donthyphenate notranslate">__FILE__</span> and <span class="d_inlinecode donthyphenate notranslate">__LINE__<ztpan> expand to�4he source file name and line number at�4he�0oint of instantiation. The�0ath of �4he source file is left�5p to�4he compiler.�!�!y:> <div class="blankline"><u(iv> <p><span class="d_inlinecode donthyphenate notranslate">__FILE_FULL_PATH__</span> expands to�4he absolute source file name at the point of instantiation.<y:> <div class="blankline"><u(iv> <p><span class="d_inlinecode donthyphenate notranslate">__MODULE__</span> expands to�4he module name at the point of instantiation.<y:> <div class="blankline"><u(iv> <p><span class="d_inlinecode donthyphenate notranslate">__FUNCTION__</span> expands to�4he fully�iualified name of�4he function at�4he�0oint of instantiation.</p> <div class="blankline"></div> �!�!p><span class="d_inlinecode donthyphenate notranslate">__PRETTY_FUNCTION__</span> is similar�4o <span class="d_inlinecode donthyphenate notranslate">__FUNCTION__<ztpan>, but also expands�4he function return�4ype, its�0arameter�4ypes, and its attributes.<y:> <div class="blankline"><u(iv> <p>Example:<y:> <div class="blankline"><u(iv> <div class="runnable-examples" data-compile=''> <pre class="d_code notranslate"><span class="d_keyword">module</span> test; <span class="d_keyword">import</span> std.stdio; <span class="d_keyword">void</span> test(string file =�!�!span class="d_keyword">__FILE__<ztpan>, size_t line = <span class="d_keyword">__LINE__</span>, string mod = <span class="d_keyword">__MODULE__</span>, string func =�!�!span class="d_keyword">__FUNCTION__<ztpan>, string�0retty = <span class="d_keyword">__PRETTY_FUNCTION__<ztpan>, string fileFullPath = <span class="d_keyword">__FILE_FULL_PATH__</span>) { �7ritefln(<span class="d_string">"file: '%s', line: '%s', module: '%s',\nfunction: '%s', "</span> ~ <span class="d_string">"pretty function: '%s',\nfile full path: '%s'"<ztpan>, file, line, mod, func,�0retty, fileFullPath); } <span class="d_keyword">int</span> main(string[] args) { test(); <span class="d_keyword">return</span> 0; } <y:re> <u(iv> <div class="blankline"><u(iv> <p>Assuming the file�7as at u0xamplezËest.d,�4his will output:</p> <div class="blankline"></div> <pre class="console notranslate">file: 'test.d', line: '10È0ó, module: 'test', function: 'test.main',�0retty function: 'int�4est.main(string[] args)', file full path: '/example/test.d' </pre> <div class="blankline"></div> �!�!div class="panel"><p><span class="red">Warning</span>: Do not use <span class="d_inlinecode donthyphenate notranslate">mixin(__FUNCTION__)</span> to get�4he symbol for the current function. This seems�4o be a common thing for programmers to attempt�7hen trying�4o get the symbol for�4he current function in order�4o do introspection on it, since D does not currently have a direct�7ay�4o get that symbol.�?�!owever, using <span class="d_inlinecode donthyphenate notranslate">mixin(__FUNCTION__)</span> means that�4he symbol for the function�7ill be looked up by name, which means that it's subject to�4he�6arious rules�4hat go�7ith symbol lookup,�7hich can cause�6arious�0roblems. One such problem would that if a function is overloaded, the result�7ill be�4he first overload whether that's�4he current function or not.<y:> <div class="blankline"><u(iv> �!�!p>Given that D doesn't currently have a way to directly get the symbol for the current function, the best�7ay�4o do it is�4o get�4he�0arent symbol of a symbol�7ithin the function, since �4hat avoids any issues surrounding symbol lookup rules. An example of that�7hich doesn't rely on any other symbols is �!�!span class="d_inlinecode donthyphenate notranslate">__traits(parent {})<ztpan>. It declares an anonymous, nested function, whose parent is then�4he current function. So, getting its parent gets the symbol for�4he current function.</p></div> <div class="blankline"></div> <h2><a class="anchor"�4itle="Permalink to�4his section" id="associativity" href="#associativity">Associativity and Commutativity<tæ><vv�(ŒÇ�) <div class="blankline"></div> �!�!p>An implementation may rearrange the evaluation of expressions according to arithmetic associativity and commutativity rules as long as, within�4hat thread of execution, no observable difference is�0ossible. �!�!y:> <div class="blankline"><u(iv> <p>This rule�0recludes any associative or commutative reordering of floating�0oint expressions. </p> <div style="float: left"><i class="fa fa-angle-left" aria-hidden="true"></i>�!�!a href="..ztpec/pragma.html">Pragmas</a></div> <div style="float: right"><a href="..ztpec/statement.html">Statements</a>�!�!i class="fa fa-angle-right" aria-hidden="true"><v}><u(iv> <div style="clear:both"></div> <div class="smallprint" id="copyright">Copyright�.�.�.copy; 99-2025 by�4he�!�!a href="..u‹oundation_overview.html">D Language Foundation</a>�= Page generated by <a href="..ztpec/ddoc.html">Ddoc<tæ> on Fri Oct 10 �(g �):16:56 2025 �<u(iv> </div> <u(iv> �!�!script�4ype="text/javascript" src="https:/tæjax.googleapis.comtæjax/libsv®query/1.7.�(W�)jquery.min.js"><ztcript> �!�!script�4ype="text/javascript">window.jQuery || document.write('\x3Cscript src="../jsv®query-1.7.�(‘Ñ�)min.js">\x0Þ0¤0¯0íztcript>');<ztcript> �!�!script�4ype="text/javascript" src="../jsu(lang.js"><ztcript> � <script type="textv®avascript" src="..v®s/codemirror-compressed.js"></script> <script type="textv®avascript" src="..v®s/run.js"></script> <script type="textv®avascript" src="..v®s/listanchors.js"></script> <script type="textv®avascript">jQuery(document).ready(addVersionSelector);</script> <link rel="stylesheet" href="https:/wÛaxcdn.bootstrapcdn.comu‹ont-awesome/4.4.0/css/font-awesome.min.css"> <uody> <vvtml>�<�/�d�i�v�>�<�d�i�v� �c�l�a�s�s�=�"�n�a�k�e�d�_�c�t�r�l�"�>� �<�f�o�r�m� �a�c�t�i�o�n�=�"�/�i�n�d�e�x�.�c�g�i�/�l�a�r�g�e�r�-�t�e�x�t�"� �m�e�t�h�o�d�=�"�g�e�t�"� �n�a�m�e�=�"�g�a�t�e�"�>� �<�p�>�<�a� �h�r�e�f�=�"�h�t�t�p�:�/�/�a�l�t�s�t�y�l�e�.�a�l�f�a�s�a�d�o�.�n�e�t�"�>�A�l�t�S�t�y�l�e�<�/�a�>� 0k0ˆ0c0fY cÛ0U0Œ0_0Ú0ü0¸� �<�a� �h�r�e�f�=�"�h�t�t�p�s�:�/�/�d�l�a�n�g�.�o�r�g�/�s�p�e�c�/�e�x�p�r�e�s�s�i�o�n�.�h�t�m�l�"�>�(�-�&�g�t�;0ª0ê0¸0Ê0ë�)�<�/�a�>� �/� �<�l�a�b�e�l�>0¢0É0ì0¹�:� �<�i�n�p�u�t� �t�y�p�e�=�"�t�e�x�t�"� �n�a�m�e�=�"�n�a�k�e�d�_�p�o�s�t�_�u�r�l�"� �v�a�l�u�e�=�"�h�t�t�p�s�:�/�/�d�l�a�n�g�.�o�r�g�/�s�p�e�c�/�e�x�p�r�e�s�s�i�o�n�.�h�t�m�l�"� �s�i�z�e�=�"�2�2�"� �/�>�<�/�l�a�b�e�l�>� �<�l�a�b�e�l�>0â0ü0É�:� �<�s�e�l�e�c�t� �n�a�m�e�=�"�n�a�k�e�d�_�p�o�s�t�_�m�o�d�e�"�>� �<�o�p�t�i�o�n� �v�a�l�u�e�=�"�d�e�f�a�u�l�t�"�>0Ç0Õ0©0ë0È�<�/�o�p�t�i�o�n�>� �<�o�p�t�i�o�n� �v�a�l�u�e�=�"�s�p�e�e�c�h�"�>—óXð0Ö0é0¦0¶�<�/�o�p�t�i�o�n�>� �<�o�p�t�i�o�n� �v�a�l�u�e�=�"�r�u�b�y�"�>0ë0ÓNØ0M�<�/�o�p�t�i�o�n�>� �<�o�p�t�i�o�n� �v�a�l�u�e�=�"�c�o�n�t�r�a�s�t�"�>‘M‚rSÍŽâ�<�/�o�p�t�i�o�n�>� �<�o�p�t�i�o�n� �v�a�l�u�e�=�"�l�a�r�g�e�r�-�t�e�x�t�"� �s�e�l�e�c�t�e�d�=�"�s�e�l�e�c�t�e�d�"�>e‡[WbáY'�<�/�o�p�t�i�o�n�>� �<�o�p�t�i�o�n� �v�a�l�u�e�=�"�m�o�b�i�l�e�"�>0â0Ð0¤0ë�<�/�o�p�t�i�o�n�>� �<�/�s�e�l�e�c�t�>� �<�i�n�p�u�t� �t�y�p�e�=�"�s�u�b�m�i�t�"� �v�a�l�u�e�=�"ˆhy:�"� �/�>� �<�/�p�>� �<�/�f�o�r�m�>� �<�/�d�i�v�>� � �