CINXE.COM
class Routine | Raku Documentation
<!DOCTYPE html> <html lang="en" class="fontawesome-i2svg-active fontawesome-i2svg-complete" style="scroll-padding-top:60px"> <head> <title>class Routine | Raku Documentation</title> <meta charset="UTF-8" /> <link href="/assets/images/Camelia.ico" rel="icon" type="image/x-icon"/> <link rel="stylesheet" href="/assets/css/Website.css"/> <link rel="stylesheet" href="/assets/css/typegraph-styling.css"/> <link rel="stylesheet" href="/assets/css/typegraph-dark.css" title="dark"/> <link rel="stylesheet" href="/assets/css/typegraph-light.css" title="light"/> <link rel="stylesheet" href="/assets/css/css/filtered-toc-dark.css" title="dark"/> <link rel="stylesheet" href="/assets/css/css/filtered-toc-light.css" title="light"/> <link rel="stylesheet" href="/assets/css/css/announce-light.css" title="light"/> <link rel="stylesheet" href="/assets/css/css/announce-dark.css" title="dark"/> <link rel="stylesheet" href="/assets/css/all.min.css"/> <link rel="stylesheet" href="/assets/css/listf-styling-light.css" title="light"/> <link rel="stylesheet" href="/assets/css/listf-styling-dark.css" title="dark"/> <link rel="stylesheet" href="/assets/css/css/options-search-light.css" title="light"/> <link rel="stylesheet" href="/assets/css/css/options-search-dark.css" title="dark"/> <link rel="stylesheet" href="/assets/css/css/page-styling-main.css"/> <link rel="stylesheet" href="/assets/css/css/page-styling-dark.css" title="dark"/> <link rel="stylesheet" href="/assets/css/css/page-styling-light.css" title="light"/> <link rel="stylesheet" href="/assets/css/css/chyronToggle-dark.css" title="dark"/> <link rel="stylesheet" href="/assets/css/css/chyronToggle-light.css" title="light"/> <link rel="stylesheet" href="/assets/css/css/centreToggle-dark.css" title="dark"/> <link rel="stylesheet" href="/assets/css/css/centreToggle-light.css" title="light"/> <link rel="stylesheet" href="/assets/css/tm-styling.css"/> <link rel="stylesheet" href="/assets/css/tm-light.css" title="light"/> <link rel="stylesheet" href="/assets/css/tm-dark.css" title="dark"/> <link rel="stylesheet" href="/assets/css/css/rainbow-dark.css" title="dark"/> <link rel="stylesheet" href="/assets/css/css/rainbow-light.css" title="light"/> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@tarekraafat/autocomplete.js@10.2.7/dist/css/autoComplete.min.css" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/atom-one-light.min.css" title="light" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/atom-one-dark.min.css" title="dark" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script src="/assets/scripts/all.min.js"></script><script src="/assets/scripts/filter-script.js"></script><script src="/assets/scripts/tableManager.js"></script><script src="https://cdn.jsdelivr.net/npm/fuzzysort@2.0.4/fuzzysort.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@tarekraafat/autocomplete.js@10.2.7/dist/autoComplete.min.js"></script><script src="/assets/scripts/filtered-toc.js"></script><script src="/assets/scripts/options-search.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/haskell.min.js"></script><script src="/assets/scripts/page-styling.js"></script><script src="/assets/scripts/announcements.js"></script><script src="/assets/scripts/rainbow.js"></script> </head> <body class="has-navbar-fixed-top"> <div id="class_Routine" class="top-of-page"></div> <nav class="navbar is-fixed-top is-flex-touch" role="navigation" aria-label="main navigation"> <div class="navbar-item" style="margin-left: auto;"> <div class="left-bar-toggle" title="Toggle Table of Contents & Index"> <label class="chyronToggle left"> <input id="navbar-left-toggle" type="checkbox"> <span class="text">Contents</span> </label> </div> </div> <div class="container is-justify-content-space-around"> <div class="navbar-brand"> <div class="navbar-logo"> <a class="navbar-item" href="/"> <img src="/assets/images/camelia-recoloured.png" alt="Raku" width="52.83" height="38"> </a> <span class="navbar-logo-tm">tm</span> </div> <a role="button" class="navbar-burger burger" aria-label="menu" aria-expanded="false" data-target="navMenu"> <span aria-hidden="true"></span> <span aria-hidden="true"></span> <span aria-hidden="true"></span> </a> </div> <div id="navMenu" class="navbar-menu"> <div class="navbar-start"> <a class="navbar-item" href="/introduction" title="Getting started, Tutorials, Migration guides"> Introduction </a> <a class="navbar-item" href="/reference" title="Fundamentals, General reference"> Reference </a> <a class="navbar-item" href="/miscellaneous" title="Programs, Experimental"> Miscellaneous </a> <a class="navbar-item" href="/types" title="The core types (classes) available"> Types </a> <a class="navbar-item" href="/routines" title="Searchable table of routines"> Routines </a> <a class="navbar-item" href="https://raku.org" title="Home page for community"> Raku<sup>®</sup> </a> <a class="navbar-item" href="https://web.libera.chat/#raku" title="IRC live chat"> Chat </a> <div class="navbar-item has-dropdown is-hoverable"> <a class="navbar-link"> More </a> <div class="navbar-dropdown is-right is-rounded"> <hr class="navbar-divider"> <a class="navbar-item js-modal-trigger" data-target="download-ebook"> Download E-Book (epub) </a> <hr class="navbar-divider"> <a class="navbar-item" href="/about"> About </a> <hr class="navbar-divider"> <a class="navbar-item has-text-red" href="https://github.com/raku/doc-website/issues"> Report an issue with this site </a> <hr class="navbar-divider"> <a class="navbar-item" href="https://github.com/raku/doc/issues"> Report an issue with the documentation content </a> <hr class="navbar-divider"> <label class="navbar-item centreToggle" title="Enable/Disable Announcements" style="--switch-width: 18"> <input id="cancelAnnouncements" type="checkbox"> <span class="text">Announcements</span> <span class="on">suppressed</span> <span class="off">allowed</span> </label> </div> </div> </div> <div class="navbar-end navbar-search-wrapper"> <div class="navbar-item"> <div class="field has-addons"> <div class="autoComplete_options"> <input class="control input" id="autoComplete" type="search" dir="ltr" spellcheck=false autocorrect="off" autocomplete="off" autocapitalize="off" placeholder="🔍 Type f to search for ..."> </div> <div class="control" title="Search options"> <a class="button is-primary js-modal-trigger" data-target="options-search-info"> <span class="icon"> <i class="fas fa-cogs"></i> </span> </a> </div> </div> </div> </div> <div id="options-search-info" class="modal"> <div class="modal-background"></div> <div class="modal-content"> <div class="box"> <p>The last search was: <span id="selected-candidate" class="ss-selected"></span></p> <div class="control is-grouped is-grouped-centered options-search-controls"> <label class="centreToggle" title="Include extra information (Alt-E)" style="--switch-width: 10.5"> <input id="options-search-extra" type="checkbox"> <span class="text">Extra info</span> <span class="on">yes</span> <span class="off">no</span> </label> <p>The search response can be shortened by excluding the extra information line (Alt-E)</p> <label class="centreToggle" title="Search engine type Strict/Loose (Alt-L)" style="--switch-width: 10.5"> <input id="options-search-loose" type="checkbox"> <span class="text">Search type</span> <span class="on">loose</span> <span class="off">strict</span> </label> <p> The search engine can perform a strict search (only the characters in the search box) or a loose search (Alt-L)</p> <label class="centreToggle" title="Search in headings (Alt-H)" style="--switch-width: 10.5"> <input id="options-search-headings" type="checkbox"> <span class="text">Headings</span> <span class="on">yes</span> <span class="off">no</span> </label> <p>Search through headings in all web-pages (Alt-H)</p> <label class="centreToggle" title="Search indexed items (Alt-I)" style="--switch-width: 10.5"> <input id="options-search-indexed" type="checkbox"> <span class="text">Indexed</span> <span class="on">yes</span> <span class="off">no</span> </label> <p>Search through all indexed items (Alt-I)</p> <label class="centreToggle" title="Search composite pages (Alt-C)" style="--switch-width: 10.5"> <input id="options-search-composite" type="checkbox"> <span class="text">Composite</span> <span class="on">yes</span> <span class="off">no</span> </label> <p>Search in the names of composite pages, which combine similar information from the main web pages (Alt-C)</p> <label class="centreToggle" title="Search primary sources (Alt-P)" style="--switch-width: 10.5"> <input id="options-search-primary" type="checkbox"> <span class="text">Primary</span> <span class="on">yes</span> <span class="off">no</span> </label> <p>Search through the names of the main web pages (Alt-P)</p> <label class="centreToggle" title="Open in new tab (Alt-Q)" style="--switch-width: 10.5"> <input id="options-search-newtab" type="checkbox"> <span class="text">New tab</span> <span class="on">yes</span> <span class="off">no</span> </label> <p>Once a search candidate has been chosen, it can be opened in a new tab or in the current tab (Alt-Q)</p> <p>If all else fails, an item is added to use the Google search engine on the whole site</p> <button class="button is-warning" id="options-search-reset-defaults">Clear options, reset to defaults</button> <p>Exit this page by pressing <Escape>, or clicking on X or on the background.</p> </div> </div> </div> <button class="modal-close is-large" aria-label="close"></button> </div> </div> <div id="download-ebook" class="modal"> <div class="modal-background"></div> <div class="modal-content"> <div class="box"> <p><a href="/RakuDocumentation.epub" download>RakuDocumentation.epub</a> is a work in progress e-book. It targets the <a href="https://www.w3.org/publishing/epub3/">EPUB v3 specification</a>. It needs testing on a variety of ereaders (some of which may still implicitly expect compliance with EPUB v2). The CSS definitely needs enhancing (especially for code snippets). The Ebook opens in a Calibre reader, which is available on all operating systems.</p> <p>Suggestions are welcome and should be addressed by opening an issue on the Raku/doc-website repository</p> <p>Exit this popup by pressing <Escape>, or clicking on X or on the background.</p> </div> </div> <button class="modal-close is-large" aria-label="close"></button> </div> <div id="announcement-modal" class="modal"> <div class="modal-background"></div> <div class="modal-content"> <div class="box"> <div id="raku-doc-announcement"></div> <p>For more see <a href="/announcements">Announcements page</a>.</p> <p>Exit this popup by pressing <Escape>, or clicking on X or on the background.</p> </div> </div> <button class="modal-close is-large" aria-label="close"></button> </div> </div> </nav> <div class="tile is-ancestor section"> <div class="page-edit"> <a class="button page-edit-button" href="https://github.com/Raku/doc/edit/main/doc/Type/Routine.rakudoc" title="Edit this page. Commit: 99c751d5d 2024-05-28"> <span class="icon is-right"> <i class="fas fa-pen-alt is-medium"></i> </span> </a> </div> <div id="left-column" class="tile is-parent is-2 is-hidden"> <div id="left-col-inner"> <div class="tabs" id="tabs"> <ul> <li class="is-active" id="toc-tab"> <a>Table of Contents</a> </li> <li id="index-tab"> <a>Index</a> </li> </ul> </div> <div class="field"> <div class="control has-icons-right"> <input id="toc-filter" class="input" type="text" placeholder="Filter"> <span class="icon is-right has-text-grey"> <i class="fas fa-search is-medium"></i> </span> </div> </div> <div class="raku-sidebar"> <aside id="toc-menu" class="menu"> <ul class="menu-list"> <li><a href="#Methods">Methods</a></li> <ul> <li><a href="#method_name">method name</a></li> <li><a href="#method_package">method package</a></li> <li><a href="#method_multi">method multi</a></li> <li><a href="#method_candidates">method candidates</a></li> <li><a href="#method_cando">method cando</a></li> <li><a href="#method_wrap">method wrap</a></li> <li><a href="#method_unwrap">method unwrap</a></li> <li><a href="#method_is-wrapped">method is-wrapped</a></li> <li><a href="#method_yada">method yada</a></li> <li><a href="#trait_is_cached">trait is cached</a></li> <li><a href="#trait_is_pure">trait is pure</a></li> <li><a href="#trait_is_rw">trait is rw</a></li> <li><a href="#trait_is_export">trait is export</a></li> <li><a href="#trait_is_DEPRECATED">trait is DEPRECATED</a></li> <li><a href="#trait_is_hidden-from-backtrace">trait is hidden-from-backtrace</a></li> <li><a href="#trait_is_default">trait is default</a></li> <li><a href="#trait_is_raw">trait is raw</a></li> <li><a href="#trait_is_test-assertion">trait is test-assertion</a></li> </ul> </aside> <aside id="index-menu" class="menu is-hidden"> <ul class="menu-list"> <li>Reference<ul> <li><a href="#index-entry-return-rw-return-rw">return-rw</a></li> </ul></li> <li>Traits<ul> <li><a href="#index-entry-is_default">is default</a></li> </ul></li> </ul> </aside> </div> </div> </div> <div id="main-column" class="tile is-parent" style="overflow-x: hidden;"> <div id="main-col-inner"> <section class="raku page-header"> <div class="container px-4"> <div class="raku page-title has-text-centered"> class Routine </div> <div class="raku page-subtitle has-text-centered"> <p>Code object with its own lexical scope and <code>return</code> handling</p> </div> </div> </section> <section class="raku page-content"><div class="container px-4"><div class="columns one-col"> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">class</span> <span class="highlite-TYPE">Routine</span> <span class="highlite-KEYWORD">is</span> <span class="highlite-TYPE">Block</span> { }</pre> </div> </div> <p>A <code>Routine</code> is a code object meant for units of code larger than <a href="/type/Block"><code>Block</code></a>. Routine is the common superclass for <a href="/type/Sub"><code>Sub</code></a> (and therefore operators) and <a href="/type/Method"><code>Method</code></a>, the two primary code objects for code reuse, as well as <a href="/type/Macro"><code>Macro</code></a> and <a href="/type/Submethod"><code>Submethod</code></a>.</p><p>Routines serve as a scope limiter for <code>return</code> (i.e. a <code>return</code> returns from the innermost outer Routine).</p><p>The routine level is also the one at which <a href="/language/glossary#Multi-dispatch">multiness</a> (multi subs and multi methods) are handled. Subroutines can also be declared <code>anon</code>. See the <a href="/language/variables#The_anon_declarator">documentation on the <code>anon</code> declarator</a> for more information.</p><p>Routines are the lowest objects in the <a href="/type/Code"><code>Code</code></a> hierarchy that can introspect through the <a href="/language/variables#%26%3FROUTINE"><code>&?ROUTINE</code> variable</a>, which is defined automatically, and contains the corresponding <code>Routine</code> object.</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">class</span> Foo { <span class="highlite-KEYWORD">submethod</span> bar { <span class="highlite-NAME_CODE">&?ROUTINE</span>.^<span class="highlite-ROUTINE">name</span> } }; <span class="highlite-ROUTINE">say</span> Foo.bar; <span class="highlite-COMMENT"># OUTPUT: «Submethod» </span></pre> </div> </div> <h1 id="Methods" class="raku-h1"><a href="#class_Routine" title="go to top of document">Methods<a class="raku-anchor" title="direct link" href="#Methods">§</a></a></h1> <h2 id="method_name" class="raku-h2"><a href="#class_Routine" title="go to top of document">method name<a class="raku-anchor" title="direct link" href="#method_name">§</a></a></h2> <!-- defnmark method_name 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">method</span> <span class="highlite-ROUTINE">name</span>(Routine:D: <span class="highlite-KEYWORD">--></span> Str:D)</pre> </div> </div> <p>Returns the name of the sub or method.</p> <h2 id="method_package" class="raku-h2"><a href="#class_Routine" title="go to top of document">method package<a class="raku-anchor" title="direct link" href="#method_package">§</a></a></h2> <!-- defnmark method_package 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">method</span> <span class="highlite-ROUTINE">package</span>(Routine:D:)</pre> </div> </div> <p>Returns the package in which the routine is defined.</p> <h2 id="method_multi" class="raku-h2"><a href="#class_Routine" title="go to top of document">method multi<a class="raku-anchor" title="direct link" href="#method_multi">§</a></a></h2> <!-- defnmark method_multi 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">method</span> <span class="highlite-KEYWORD">multi</span>(Routine:D: <span class="highlite-KEYWORD">--></span> Bool:D)</pre> </div> </div> <p>Returns <code>True</code> if the routine is a multi sub or method. Note that the name of a multi sub refers to its <a href="/syntax/proto"><code>proto</code></a> and this method would return false if called on it. It needs to be called on the candidates themselves:</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">multi</span> foo ($<span class="highlite-OPERATOR">,</span> $) {}; <span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_CODE">&foo</span>.<span class="highlite-KEYWORD">multi</span>; <span class="highlite-COMMENT"># OUTPUT: «False» </span><span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_CODE">&foo</span>.<span class="highlite-ROUTINE">candidates</span>».<span class="highlite-KEYWORD">multi</span>; <span class="highlite-COMMENT"># OUTPUT: «(True)»</span></pre> </div> </div> <h2 id="method_candidates" class="raku-h2"><a href="#class_Routine" title="go to top of document">method candidates<a class="raku-anchor" title="direct link" href="#method_candidates">§</a></a></h2> <!-- defnmark method_candidates 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">method</span> <span class="highlite-ROUTINE">candidates</span>(Routine:D: <span class="highlite-KEYWORD">--></span> Positional:D)</pre> </div> </div> <p>Returns a list of multi candidates, or a one-element list with itself if it's not a multi</p> <h2 id="method_cando" class="raku-h2"><a href="#class_Routine" title="go to top of document">method cando<a class="raku-anchor" title="direct link" href="#method_cando">§</a></a></h2> <!-- defnmark method_cando 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">method</span> <span class="highlite-ROUTINE">cando</span>(<span class="highlite-TYPE">Capture</span> <span class="highlite-NAME_SCALAR">$c</span>)</pre> </div> </div> <p>Returns a possibly-empty list of candidates that can be called with the given <a href="/type/Capture"><code>Capture</code></a>, ordered by narrowest candidate first. For methods, the first element of the Capture needs to be the invocant:</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights">.<span class="highlite-ROUTINE">signature</span>.<span class="highlite-ROUTINE">say</span> <span class="highlite-KEYWORD">for</span> <span class="highlite-STRING_DELIMITER">"</span><span class="highlite-STRING">foo</span><span class="highlite-STRING_DELIMITER">"</span>.^<span class="highlite-ROUTINE">can</span>(<span class="highlite-STRING_DELIMITER">"</span><span class="highlite-STRING">comb</span><span class="highlite-STRING_DELIMITER">"</span>)[0].cando: \(<span class="highlite-TYPE">Cool</span><span class="highlite-OPERATOR">,</span> <span class="highlite-STRING_DELIMITER">"</span><span class="highlite-STRING">o</span><span class="highlite-STRING_DELIMITER">"</span>); <span class="highlite-COMMENT"># OUTPUT: «(Cool $: Str $matcher, $limit = Inf, *%_)»</span></pre> </div> </div> <h2 id="method_wrap" class="raku-h2"><a href="#class_Routine" title="go to top of document">method wrap<a class="raku-anchor" title="direct link" href="#method_wrap">§</a></a></h2> <!-- defnmark method_wrap 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">method</span> <span class="highlite-ROUTINE">wrap</span>(Routine:D: <span class="highlite-NAME_CODE">&wrapper</span>)</pre> </div> </div> <p>Wraps (i.e. in-place modifies) the routine. That means a call to this routine first calls <code>&wrapper</code>, which then can (but doesn't have to) call the original routine with the <code>callsame</code>, <code>callwith</code>, <code>nextsame</code> and <code>nextwith</code> dispatchers. The return value from the routine is also the return value from the wrapper.</p><p><code>wrap</code> returns an instance of a private class called <a href="/type/Routine/WrapHandle"><code>Routine::WrapHandle</code></a>, which you can pass to <a href="/routine/unwrap">unwrap</a> to restore the original routine.</p> <h2 id="method_unwrap" class="raku-h2"><a href="#class_Routine" title="go to top of document">method unwrap<a class="raku-anchor" title="direct link" href="#method_unwrap">§</a></a></h2> <!-- defnmark method_unwrap 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">method</span> <span class="highlite-ROUTINE">unwrap</span>(<span class="highlite-NAME_SCALAR">$wraphandle</span>)</pre> </div> </div> <p>Restores the original routine after it has been wrapped with <a href="/routine/wrap">wrap</a>. While the signature allows any type to be passed, only the <a href="/type/Routine/WrapHandle"><code>Routine::WrapHandle</code></a> type returned from <code>wrap</code> can usefully be.</p> <h2 id="method_is-wrapped" class="raku-h2"><a href="#class_Routine" title="go to top of document">method is-wrapped<a class="raku-anchor" title="direct link" href="#method_is-wrapped">§</a></a></h2> <!-- defnmark method_is-wrapped 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">method</span> is-wrapped()</pre> </div> </div> <p>Returns <code>True</code> or <code>False</code>, depending on the whether or not the <code>Routine</code> is wrapped.</p> <h2 id="method_yada" class="raku-h2"><a href="#class_Routine" title="go to top of document">method yada<a class="raku-anchor" title="direct link" href="#method_yada">§</a></a></h2> <!-- defnmark method_yada 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">method</span> <span class="highlite-ROUTINE">yada</span>(Routine:D: <span class="highlite-KEYWORD">--></span> Bool:D)</pre> </div> </div> <p>Returns <code>True</code> if the routine is a stub</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-ROUTINE">say</span> (<span class="highlite-KEYWORD">sub</span> <span class="highlite-ROUTINE">f</span>() { <span class="highlite-OPERATOR">...</span> }).<span class="highlite-ROUTINE">yada</span>; <span class="highlite-COMMENT"># OUTPUT: «True» </span><span class="highlite-ROUTINE">say</span> (<span class="highlite-KEYWORD">sub</span> g() { 1; }).<span class="highlite-ROUTINE">yada</span>; <span class="highlite-COMMENT"># OUTPUT: «False»</span></pre> </div> </div> <h2 id="trait_is_cached" class="raku-h2"><a href="#class_Routine" title="go to top of document">trait is cached<a class="raku-anchor" title="direct link" href="#trait_is_cached">§</a></a></h2> <!-- defnmark trait_is_cached 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">multi</span> trait_mod:<span class="highlite-STRING_DELIMITER"><</span><span class="highlite-STRING">is</span><span class="highlite-STRING_DELIMITER">></span>(<span class="highlite-TYPE">Routine</span> <span class="highlite-NAME_SCALAR">$r</span><span class="highlite-OPERATOR">,</span> <span class="highlite-OPERATOR">:</span><span class="highlite-NAME_SCALAR">$cached</span>!)</pre> </div> </div> <p>Causes the return value of a routine to be stored, so that when subsequent calls with the same list of arguments are made, the stored value can be returned immediately instead of re-running the routine.<sup class="content-footnote"><a name="fnret1" href="#fn1">[1]</a></sup> </p><p>Useful when storing and returning the computed value is much faster than re-computing it every time, and when the time saved trumps the cost of the use of more memory.</p><p>Even if the arguments passed to the routine are "reference types" (such as objects or arrays), then for the purpose of caching they will only be compared based on their contents. Thus the second invocation will hit the cache in this case:</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-ROUTINE">say</span> foo( [1<span class="highlite-OPERATOR">,</span> 2<span class="highlite-OPERATOR">,</span> 3] ); <span class="highlite-COMMENT"># runs foo </span><span class="highlite-ROUTINE">say</span> foo( [1<span class="highlite-OPERATOR">,</span> 2<span class="highlite-OPERATOR">,</span> 3] ); <span class="highlite-COMMENT"># doesn't run foo, uses cached value </span></pre> </div> </div> <p>Since it's still at the experimental stage, you will have to insert the <code>use experimental :cached;</code> statement in any module or script that uses it.</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">use</span> <span class="highlite-TYPE">experimental</span> :cached; <span class="highlite-KEYWORD">sub</span> nth-prime(Int:D <span class="highlite-NAME_SCALAR">$x</span> <span class="highlite-KEYWORD">where</span> <span class="highlite-OPERATOR">*</span> <span class="highlite-OPERATOR">></span> 0) <span class="highlite-KEYWORD">is</span> <span class="highlite-KEYWORD">cached</span> { <span class="highlite-ROUTINE">say</span> <span class="highlite-STRING_DELIMITER">"</span><span class="highlite-STRING">Calculating </span><span class="highlite-ESCAPE">{</span><span class="highlite-NAME_SCALAR">$x</span><span class="highlite-ESCAPE">}</span><span class="highlite-STRING">th prime</span><span class="highlite-STRING_DELIMITER">"</span>; <span class="highlite-KEYWORD">return</span> (2.<span class="highlite-OPERATOR">.*</span>).<span class="highlite-ROUTINE">grep</span>(<span class="highlite-OPERATOR">*</span>.<span class="highlite-ROUTINE">is-prime</span>)[<span class="highlite-NAME_SCALAR">$x</span> <span class="highlite-OPERATOR">-</span> 1]; } <span class="highlite-ROUTINE">say</span> nth-prime(43); <span class="highlite-ROUTINE">say</span> nth-prime(43); <span class="highlite-ROUTINE">say</span> nth-prime(43); </pre> </div> </div> <p>produces this output:</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>「text」 without highlighting</label> <div> <pre class="nohighlights"><pre class="nohighlights"> Calculating 43th prime 191 191 191 </pre> </pre> </div> </div> <p><strong>Note</strong>: This feature is not thread-safe.</p> <h2 id="trait_is_pure" class="raku-h2"><a href="#class_Routine" title="go to top of document">trait is pure<a class="raku-anchor" title="direct link" href="#trait_is_pure">§</a></a></h2> <!-- defnmark trait_is_pure 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">multi</span> trait_mod:<span class="highlite-STRING_DELIMITER"><</span><span class="highlite-STRING">is</span><span class="highlite-STRING_DELIMITER">></span>(<span class="highlite-TYPE">Routine</span> <span class="highlite-NAME_SCALAR">$r</span><span class="highlite-OPERATOR">,</span> <span class="highlite-OPERATOR">:</span><span class="highlite-NAME_SCALAR">$pure</span>!)</pre> </div> </div> <p>Marks a subroutine as <em>pure</em>, that is, it asserts that for the same input, it will always produce the same output without any additional side effects.</p><p>The <code>is pure</code> trait is a promise by the programmer to the compiler that it can constant-fold calls to such functions when the arguments are known at compile time.</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">sub</span> syllables() <span class="highlite-KEYWORD">is</span> <span class="highlite-KEYWORD">pure</span> { <span class="highlite-ROUTINE">say</span> <span class="highlite-STRING_DELIMITER">"</span><span class="highlite-STRING">Generating syllables</span><span class="highlite-STRING_DELIMITER">"</span>; <span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@vowels</span> <span class="highlite-OPERATOR">=</span> <span class="highlite-STRING_DELIMITER"><</span><span class="highlite-STRING">a e i o u</span><span class="highlite-STRING_DELIMITER">></span>; <span class="highlite-KEYWORD">return</span> <span class="highlite-NAME_ARRAY">@vowels</span>.append: <span class="highlite-STRING_DELIMITER"><</span><span class="highlite-STRING">k m n sh d r t y</span><span class="highlite-STRING_DELIMITER">></span> <span class="highlite-OPERATOR">X</span>~ <span class="highlite-NAME_ARRAY">@vowels</span>; }</pre> </div> </div> <p>You can mark function as pure even if they throw exceptions in edge cases or if they modify temporary objects; hence the <code>is pure</code> trait can cover cases that the compiler cannot deduce on its own. On the other hand, you might not want to constant-fold functions that produce a large return value (such as the string or list repetition operators, infix <code>x</code> and <code>xx</code>) even if they are pure, to avoid large precompilation files.</p><p>To see it an action with a particular compiler you can try this example:</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">BEGIN</span> { <span class="highlite-ROUTINE">say</span> ‘Begin’ } <span class="highlite-ROUTINE">say</span> ‘Start’; <span class="highlite-ROUTINE">say</span> (^100).map: { syllables().<span class="highlite-ROUTINE">pick</span>(4).<span class="highlite-ROUTINE">join</span>(<span class="highlite-STRING_DELIMITER">"</span><span class="highlite-STRING_DELIMITER">"</span>) }; <span class="highlite-COMMENT"># Example output: </span><span class="highlite-COMMENT"># Begin </span><span class="highlite-COMMENT"># Generating syllables </span><span class="highlite-COMMENT"># Start </span><span class="highlite-COMMENT"># (matiroi yeterani shoriyuru... </span></pre> </div> </div> <p>Essentially this allows the compiler to perform some operations at compile time. The benefits of constant-folding <em>may</em> include better performance, especially in cases when the folded code is precompiled.</p><p>In addition, using a pure function or operator in sink context (that is, where the result is discarded) may lead to a warning. The code</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">sub</span> double(<span class="highlite-NAME_SCALAR">$x</span>) <span class="highlite-KEYWORD">is</span> <span class="highlite-KEYWORD">pure</span> { 2 <span class="highlite-OPERATOR">*</span> <span class="highlite-NAME_SCALAR">$x</span> }; double(21); <span class="highlite-ROUTINE">say</span> <span class="highlite-STRING_DELIMITER">"</span><span class="highlite-STRING">anything</span><span class="highlite-STRING_DELIMITER">"</span>; <span class="highlite-COMMENT"># WARNING: «Useless use of "double(21)" in expression "double(21)" in sink context (line 2)»</span></pre> </div> </div> <p>If you want to apply this trait to a <code>multi</code>, you need to apply it to the <code>proto</code>; it will not work otherwise, at least in versions 2018.08 and below.</p> <h2 id="trait_is_rw" class="raku-h2"><a href="#class_Routine" title="go to top of document">trait is rw<a class="raku-anchor" title="direct link" href="#trait_is_rw">§</a></a></h2> <!-- defnmark trait_is_rw 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">multi</span> trait_mod:<span class="highlite-STRING_DELIMITER"><</span><span class="highlite-STRING">is</span><span class="highlite-STRING_DELIMITER">></span>(<span class="highlite-TYPE">Routine</span> <span class="highlite-NAME_SCALAR">$r</span><span class="highlite-OPERATOR">,</span> <span class="highlite-OPERATOR">:</span><span class="highlite-NAME_SCALAR">$rw</span>!)</pre> </div> </div> <p>When a routine is modified with this trait, its return value will be writable. This is useful when returning variables or writable elements of hashes or arrays, for example:</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">sub</span> walk(\thing<span class="highlite-OPERATOR">,</span> <span class="highlite-OPERATOR">*</span><span class="highlite-NAME_ARRAY">@keys</span>) <span class="highlite-KEYWORD">is</span> <span class="highlite-KEYWORD">rw</span> { <span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_SCALAR">$current</span> <span class="highlite-OPERATOR">:=</span> thing; <span class="highlite-KEYWORD">for</span> <span class="highlite-NAME_ARRAY">@keys</span> <span class="highlite-KEYWORD">-></span> <span class="highlite-NAME_SCALAR">$k</span> { <span class="highlite-KEYWORD">if</span> <span class="highlite-NAME_SCALAR">$k</span> <span class="highlite-OPERATOR">~~</span> <span class="highlite-TYPE">Int</span> { <span class="highlite-NAME_SCALAR">$current</span> <span class="highlite-OPERATOR">:=</span> <span class="highlite-NAME_SCALAR">$current</span>[<span class="highlite-NAME_SCALAR">$k</span>]; } <span class="highlite-KEYWORD">else</span> { <span class="highlite-NAME_SCALAR">$current</span> <span class="highlite-OPERATOR">:=</span> <span class="highlite-NAME_SCALAR">$current</span>{<span class="highlite-NAME_SCALAR">$k</span>}; } } <span class="highlite-NAME_SCALAR">$current</span>; } <span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_HASH">%hash</span>; walk(<span class="highlite-NAME_HASH">%hash</span><span class="highlite-OPERATOR">,</span> <span class="highlite-STRING_DELIMITER">'</span><span class="highlite-STRING">some</span><span class="highlite-STRING_DELIMITER">'</span><span class="highlite-OPERATOR">,</span> <span class="highlite-STRING_DELIMITER">'</span><span class="highlite-STRING">key</span><span class="highlite-STRING_DELIMITER">'</span><span class="highlite-OPERATOR">,</span> 1<span class="highlite-OPERATOR">,</span> 2) <span class="highlite-OPERATOR">=</span> <span class="highlite-STRING_DELIMITER">'</span><span class="highlite-STRING">autovivified</span><span class="highlite-STRING_DELIMITER">'</span>; <span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_HASH">%hash</span>.<span class="highlite-ROUTINE">raku</span>; </pre> </div> </div> <p>produces</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights">(<span class="highlite-STRING_DELIMITER">"</span><span class="highlite-STRING">some</span><span class="highlite-STRING_DELIMITER">"</span> <span class="highlite-OPERATOR">=></span> {<span class="highlite-STRING_DELIMITER">"</span><span class="highlite-STRING">key</span><span class="highlite-STRING_DELIMITER">"</span> <span class="highlite-OPERATOR">=></span> [<span class="highlite-TYPE">Any</span><span class="highlite-OPERATOR">,</span> [<span class="highlite-TYPE">Any</span><span class="highlite-OPERATOR">,</span> <span class="highlite-TYPE">Any</span><span class="highlite-OPERATOR">,</span> <span class="highlite-STRING_DELIMITER">"</span><span class="highlite-STRING">autovivified</span><span class="highlite-STRING_DELIMITER">"</span>]]}).<span class="highlite-ROUTINE">hash</span> </pre> </div> </div> <p>Note that <code>return</code> marks return values as read only; if you need an early exit from an <code>is rw</code> routine, you have to use <a name="index-entry-return-rw-return-rw" class="index-entry"><span class="glossary-entry" data-index-text="⦃〚Reference〛 〚return-rw〛⦄"><code>return-rw</code></span></a> instead.</p> <h2 id="trait_is_export" class="raku-h2"><a href="#class_Routine" title="go to top of document">trait is export<a class="raku-anchor" title="direct link" href="#trait_is_export">§</a></a></h2> <!-- defnmark trait_is_export 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">multi</span> trait_mod:<span class="highlite-STRING_DELIMITER"><</span><span class="highlite-STRING">is</span><span class="highlite-STRING_DELIMITER">></span>(<span class="highlite-TYPE">Routine</span> <span class="highlite-NAME_SCALAR">$r</span><span class="highlite-OPERATOR">,</span> <span class="highlite-OPERATOR">:</span><span class="highlite-NAME_SCALAR">$export</span>!)</pre> </div> </div> <p>Marks a routine as exported to the rest of the world</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">module</span> Foo { <span class="highlite-KEYWORD">sub</span> double(<span class="highlite-NAME_SCALAR">$x</span>) <span class="highlite-KEYWORD">is</span> <span class="highlite-KEYWORD">export</span> { 2 <span class="highlite-OPERATOR">*</span> <span class="highlite-NAME_SCALAR">$x</span> } } <span class="highlite-KEYWORD">import</span> Foo; <span class="highlite-COMMENT"># makes sub double available </span><span class="highlite-ROUTINE">say</span> double 21; <span class="highlite-COMMENT"># 42 </span></pre> </div> </div> <p>From inside another file you'd say <code>use Foo;</code> to load a module and import the exported functions.</p><p>See <a href="/language/modules#Exporting_and_selective_importing">Exporting and Selective Importing Modules</a> for more details.</p> <h2 id="trait_is_DEPRECATED" class="raku-h2"><a href="#class_Routine" title="go to top of document">trait is DEPRECATED<a class="raku-anchor" title="direct link" href="#trait_is_DEPRECATED">§</a></a></h2> <!-- defnmark trait_is_DEPRECATED 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">multi</span> trait_mod:<span class="highlite-STRING_DELIMITER"><</span><span class="highlite-STRING">is</span><span class="highlite-STRING_DELIMITER">></span>(Routine:D <span class="highlite-NAME_SCALAR">$r</span><span class="highlite-OPERATOR">,</span> <span class="highlite-OPERATOR">:</span><span class="highlite-NAME_SCALAR">$DEPRECATED</span>!)</pre> </div> </div> <p>Marks a <code>Routine</code> as deprecated; that is, it should no longer be used going forward, and will eventually be removed. An optional message specifying the replacement functionality can be specified</p><p>By having both the original (deprecated) and new <code>Routine</code> available simultaneously, you can avoid a breaking change in a single release, by allowing users time and instructions on how to update their code. Remove the deprecated version only after at least one release that includes both the warning and the new <code>Routine</code>.</p><p>This code</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">sub</span> <span class="highlite-ROUTINE">f</span>() <span class="highlite-KEYWORD">is</span> <span class="highlite-KEYWORD">DEPRECATED</span>(<span class="highlite-STRING_DELIMITER">'</span><span class="highlite-STRING">the literal 42</span><span class="highlite-STRING_DELIMITER">'</span>) { 42 } <span class="highlite-ROUTINE">say</span> <span class="highlite-ROUTINE">f</span>(); </pre> </div> </div> <p>produces this output:</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>「text」 without highlighting</label> <div> <pre class="nohighlights"><pre class="nohighlights"> 42 Saw 1 occurrence of deprecated code. ================================================================================ Sub f (from GLOBAL) seen at: deprecated.raku, line 2 Please use the literal 42 instead. -------------------------------------------------------------------------------- Please contact the author to have these occurrences of deprecated code adapted, so that this message will disappear! </pre> </pre> </div> </div> <h2 id="trait_is_hidden-from-backtrace" class="raku-h2"><a href="#class_Routine" title="go to top of document">trait is hidden-from-backtrace<a class="raku-anchor" title="direct link" href="#trait_is_hidden-from-backtrace">§</a></a></h2> <!-- defnmark trait_is_hidden-from-backtrace 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">multi</span> trait_mod:<span class="highlite-STRING_DELIMITER"><</span><span class="highlite-STRING">is</span><span class="highlite-STRING_DELIMITER">></span>(Routine:D<span class="highlite-OPERATOR">,</span> <span class="highlite-OPERATOR">:</span><span class="highlite-NAME_SCALAR">$hidden-from-backtrace</span>!)</pre> </div> </div> <p>Hides a routine from showing up in a default backtrace. For example</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">sub</span> inner { <span class="highlite-ROUTINE">die</span> <span class="highlite-STRING_DELIMITER">"</span><span class="highlite-STRING">OH NOEZ</span><span class="highlite-STRING_DELIMITER">"</span> }; <span class="highlite-KEYWORD">sub</span> outer { inner() }; outer(); </pre> </div> </div> <p>produces the error message and backtrace</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>「text」 without highlighting</label> <div> <pre class="nohighlights"><pre class="nohighlights"> OH NOEZ in sub inner at bt.raku:1 in sub outer at bt.raku:2 in block <unit> at bt.raku:3 </pre> </pre> </div> </div> <p>but if <code>inner</code> is marked with <code>hidden-from-backtrace</code></p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">sub</span> inner <span class="highlite-KEYWORD">is</span> <span class="highlite-KEYWORD">hidden-from-backtrace</span> { <span class="highlite-ROUTINE">die</span> <span class="highlite-STRING_DELIMITER">"</span><span class="highlite-STRING">OH NOEZ</span><span class="highlite-STRING_DELIMITER">"</span> }; <span class="highlite-KEYWORD">sub</span> outer { inner() }; outer(); </pre> </div> </div> <p>the error backtrace does not show it:</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>「text」 without highlighting</label> <div> <pre class="nohighlights"><pre class="nohighlights"> OH NOEZ in sub outer at bt.raku:2 in block <unit> at bt.raku:3 </pre> </pre> </div> </div> <p><a name="index-entry-is_default" class="index-entry"></a></p> <h2 id="trait_is_default" class="raku-h2"><a href="#class_Routine" title="go to top of document">trait is default<a class="raku-anchor" title="direct link" href="#trait_is_default">§</a></a></h2> <!-- defnmark trait_is_default 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">multi</span> trait_mod:<span class="highlite-STRING_DELIMITER"><</span><span class="highlite-STRING">is</span><span class="highlite-STRING_DELIMITER">></span>(Routine:D <span class="highlite-NAME_SCALAR">$r</span><span class="highlite-OPERATOR">,</span> <span class="highlite-OPERATOR">:</span><span class="highlite-NAME_SCALAR">$default</span>!)</pre> </div> </div> <p>There is a special trait for <code>Routine</code>s called <code>is default</code>. This trait is designed as a way to disambiguate <code>multi</code> calls that would normally throw an error because the compiler would not know which one to use. This means that given the following two <code>Routine</code>s, the one with the <code>is default</code> trait will be called.</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">multi</span> <span class="highlite-ROUTINE">f</span>() <span class="highlite-KEYWORD">is</span> <span class="highlite-KEYWORD">default</span> { <span class="highlite-ROUTINE">say</span> <span class="highlite-STRING_DELIMITER">"</span><span class="highlite-STRING">Hello there</span><span class="highlite-STRING_DELIMITER">"</span> } <span class="highlite-KEYWORD">multi</span> <span class="highlite-ROUTINE">f</span>() { <span class="highlite-ROUTINE">say</span> <span class="highlite-STRING_DELIMITER">"</span><span class="highlite-STRING">Hello friend</span><span class="highlite-STRING_DELIMITER">"</span> } <span class="highlite-ROUTINE">f</span>(); <span class="highlite-COMMENT"># OUTPUT: «"Hello there"»</span></pre> </div> </div> <p>The <code>is default</code> trait can become very useful for debugging and other uses but keep in mind that it will only resolve an ambiguous dispatch between two <code>Routine</code>s of the same precedence. If one of the <code>Routine</code>s is narrower than another, then that one will be called. For example:</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">multi</span> <span class="highlite-ROUTINE">f</span>() <span class="highlite-KEYWORD">is</span> <span class="highlite-KEYWORD">default</span> { <span class="highlite-ROUTINE">say</span> <span class="highlite-STRING_DELIMITER">"</span><span class="highlite-STRING">Hello there</span><span class="highlite-STRING_DELIMITER">"</span> } <span class="highlite-KEYWORD">multi</span> <span class="highlite-ROUTINE">f</span>(<span class="highlite-OPERATOR">:</span><span class="highlite-NAME_SCALAR">$greet</span>) { <span class="highlite-ROUTINE">say</span> <span class="highlite-STRING_DELIMITER">"</span><span class="highlite-STRING">Hello </span><span class="highlite-STRING_DELIMITER">"</span> <span class="highlite-OPERATOR">~</span> <span class="highlite-NAME_SCALAR">$greet</span> } <span class="highlite-ROUTINE">f</span>(); <span class="highlite-COMMENT"># "Use of uninitialized value $greet..." </span></pre> </div> </div> <p>In this example, the <code>multi</code> without <code>is default</code> was called because it was actually narrower than the <a href="/type/Sub"><code>Sub</code></a> with it.</p> <h2 id="trait_is_raw" class="raku-h2"><a href="#class_Routine" title="go to top of document">trait is raw<a class="raku-anchor" title="direct link" href="#trait_is_raw">§</a></a></h2> <!-- defnmark trait_is_raw 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">multi</span> trait_mod:<span class="highlite-STRING_DELIMITER"><</span><span class="highlite-STRING">is</span><span class="highlite-STRING_DELIMITER">></span>(Routine:D <span class="highlite-NAME_SCALAR">$r</span><span class="highlite-OPERATOR">,</span> <span class="highlite-OPERATOR">:</span><span class="highlite-NAME_SCALAR">$raw</span>!)</pre> </div> </div> <p>Gives total access to the data structure returned by the routine.</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">my</span> <span class="highlite-NAME_ARRAY">@zipi</span> <span class="highlite-OPERATOR">=</span> <span class="highlite-STRING_DELIMITER"><</span><span class="highlite-STRING">zape zapatilla</span><span class="highlite-STRING_DELIMITER">></span>; <span class="highlite-KEYWORD">sub</span> þor() <span class="highlite-KEYWORD">is</span> <span class="highlite-KEYWORD">raw</span> { <span class="highlite-KEYWORD">return</span> <span class="highlite-NAME_ARRAY">@zipi</span> }; þor()[1] <span class="highlite-OPERATOR">=</span> <span class="highlite-STRING_DELIMITER">'</span><span class="highlite-STRING">pantuflo</span><span class="highlite-STRING_DELIMITER">'</span>; <span class="highlite-ROUTINE">say</span> <span class="highlite-NAME_ARRAY">@zipi</span>; <span class="highlite-COMMENT"># OUTPUT: «[zape pantuflo]» </span></pre> </div> </div> <h2 id="trait_is_test-assertion" class="raku-h2"><a href="#class_Routine" title="go to top of document">trait is test-assertion<a class="raku-anchor" title="direct link" href="#trait_is_test-assertion">§</a></a></h2> <!-- defnmark trait_is_test-assertion 2 --> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">multi</span> trait_mod:<span class="highlite-STRING_DELIMITER"><</span><span class="highlite-STRING">is</span><span class="highlite-STRING_DELIMITER">></span>(Routine:D<span class="highlite-OPERATOR">,</span> <span class="highlite-OPERATOR">:</span><span class="highlite-NAME_SCALAR">$test-assertion</span>!)</pre> </div> </div> <p>Declares that a routine generates test output (aka TAP). When failures are reported, the calling routine's location is used instead of this routine. For example:</p> <div class="raku-code raku-lang"> <button class="copy-code" title="Copy code"><i class="far fa-clipboard"></i></button> <label>Raku highlighting</label> <div> <pre class="nohighlights"><span class="highlite-KEYWORD">use</span> <span class="highlite-TYPE">Test</span>; <span class="highlite-KEYWORD">sub</span> foo-test(<span class="highlite-NAME_SCALAR">$value</span>) <span class="highlite-KEYWORD">is</span> test-assertion { <span class="highlite-KEYWORD">is</span> <span class="highlite-NAME_SCALAR">$value</span><span class="highlite-OPERATOR">,</span> 42<span class="highlite-OPERATOR">,</span> <span class="highlite-STRING_DELIMITER">"</span><span class="highlite-STRING">is the value 42?</span><span class="highlite-STRING_DELIMITER">"</span>; } foo-test(666); <span class="highlite-COMMENT"># <-- error is reported on this line </span></pre> </div> </div> <h1 id="typegraphrelations" class="raku-h1"><a href="#" title="go to top of document">Typegraph<a class="raku-anchor" title="direct link" href="#typegraphrelations">§</a></a></h1> <figure class="typegraph" > <figcaption>Type relations for <code>Routine</code></figcaption> <svg width="409pt" height="476pt" viewBox="0.00 0.00 408.74 476.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 472)"> <title>raku-type-graph</title> <polygon fill="#ffffff" stroke="transparent" points="-4,4 -4,-472 404.74,-472 404.74,4 -4,4"/> <!-- Routine --> <g id="node1" class="node"> <title>Routine</title> <g id="a_node1"><a xlink:href="/type/Routine" xlink:title="Routine"> <ellipse fill="none" stroke="#000000" cx="233.04" cy="-162" rx="44.69" ry="18"/> <text text-anchor="middle" x="233.04" y="-158.3" font-family="FreeSans" font-size="14.00" fill="#000000">Routine</text> </a> </g> </g> <!-- Block --> <g id="node6" class="node"> <title>Block</title> <g id="a_node6"><a xlink:href="/type/Block" xlink:title="Block"> <ellipse fill="none" stroke="#000000" cx="233.04" cy="-234" rx="35.19" ry="18"/> <text text-anchor="middle" x="233.04" y="-230.3" font-family="FreeSans" font-size="14.00" fill="#000000">Block</text> </a> </g> </g> <!-- Routine->Block --> <g id="edge1" class="edge"> <title>Routine->Block</title> <path fill="none" stroke="#000000" d="M233.04,-180.3C233.04,-188.02 233.04,-197.29 233.04,-205.89"/> <polygon fill="#000000" stroke="#000000" points="229.54,-205.9 233.04,-215.9 236.54,-205.9 229.54,-205.9"/> </g> <!-- Mu --> <g id="node2" class="node"> <title>Mu</title> <g id="a_node2"><a xlink:href="/type/Mu" xlink:title="Mu"> <ellipse fill="none" stroke="#000000" cx="187.04" cy="-450" rx="27" ry="18"/> <text text-anchor="middle" x="187.04" y="-446.3" font-family="FreeSans" font-size="14.00" fill="#000000">Mu</text> </a> </g> </g> <!-- Any --> <g id="node3" class="node"> <title>Any</title> <g id="a_node3"><a xlink:href="/type/Any" xlink:title="Any"> <ellipse fill="none" stroke="#000000" cx="187.04" cy="-378" rx="28.7" ry="18"/> <text text-anchor="middle" x="187.04" y="-374.3" font-family="FreeSans" font-size="14.00" fill="#000000">Any</text> </a> </g> </g> <!-- Any->Mu --> <g id="edge2" class="edge"> <title>Any->Mu</title> <path fill="none" stroke="#000000" d="M187.04,-396.3C187.04,-404.02 187.04,-413.29 187.04,-421.89"/> <polygon fill="#000000" stroke="#000000" points="183.54,-421.9 187.04,-431.9 190.54,-421.9 183.54,-421.9"/> </g> <!-- Callable --> <g id="node4" class="node"> <title>Callable</title> <g id="a_node4"><a xlink:href="/type/Callable" xlink:title="Callable"> <ellipse fill="none" stroke="#6666ff" cx="280.04" cy="-378" rx="46.29" ry="18"/> <text text-anchor="middle" x="280.04" y="-374.3" font-family="FreeSans" font-size="14.00" fill="#6666ff">Callable</text> </a> </g> </g> <!-- Code --> <g id="node5" class="node"> <title>Code</title> <g id="a_node5"><a xlink:href="/type/Code" xlink:title="Code"> <ellipse fill="none" stroke="#000000" cx="233.04" cy="-306" rx="33.6" ry="18"/> <text text-anchor="middle" x="233.04" y="-302.3" font-family="FreeSans" font-size="14.00" fill="#000000">Code</text> </a> </g> </g> <!-- Code->Any --> <g id="edge3" class="edge"> <title>Code->Any</title> <path fill="none" stroke="#000000" d="M222.38,-323.24C216.69,-331.89 209.57,-342.73 203.21,-352.4"/> <polygon fill="#000000" stroke="#000000" points="200.22,-350.57 197.66,-360.85 206.07,-354.42 200.22,-350.57"/> </g> <!-- Code->Callable --> <g id="edge10" class="edge"> <title>Code->Callable</title> <path fill="none" stroke="#6666ff" d="M243.95,-323.24C249.68,-331.77 256.82,-342.42 263.25,-351.98"/> <polygon fill="#6666ff" stroke="#6666ff" points="260.4,-354.02 268.88,-360.37 266.21,-350.11 260.4,-354.02"/> </g> <!-- Block->Code --> <g id="edge4" class="edge"> <title>Block->Code</title> <path fill="none" stroke="#000000" d="M233.04,-252.3C233.04,-260.02 233.04,-269.29 233.04,-277.89"/> <polygon fill="#000000" stroke="#000000" points="229.54,-277.9 233.04,-287.9 236.54,-277.9 229.54,-277.9"/> </g> <!-- Submethod --> <g id="node7" class="node"> <title>Submethod</title> <g id="a_node7"><a xlink:href="/type/Submethod" xlink:title="Submethod"> <ellipse fill="none" stroke="#000000" cx="63.04" cy="-90" rx="63.09" ry="18"/> <text text-anchor="middle" x="63.04" y="-86.3" font-family="FreeSans" font-size="14.00" fill="#000000">Submethod</text> </a> </g> </g> <!-- Submethod->Routine --> <g id="edge5" class="edge"> <title>Submethod->Routine</title> <path fill="none" stroke="#000000" d="M97.45,-105.17C125.25,-116.62 164.42,-132.74 193.51,-144.72"/> <polygon fill="#000000" stroke="#000000" points="192.38,-148.04 202.96,-148.61 195.05,-141.57 192.38,-148.04"/> </g> <!-- Method --> <g id="node8" class="node"> <title>Method</title> <g id="a_node8"><a xlink:href="/type/Method" xlink:title="Method"> <ellipse fill="none" stroke="#000000" cx="188.04" cy="-90" rx="44.39" ry="18"/> <text text-anchor="middle" x="188.04" y="-86.3" font-family="FreeSans" font-size="14.00" fill="#000000">Method</text> </a> </g> </g> <!-- Method->Routine --> <g id="edge6" class="edge"> <title>Method->Routine</title> <path fill="none" stroke="#000000" d="M198.71,-107.59C204.07,-115.92 210.67,-126.2 216.66,-135.51"/> <polygon fill="#000000" stroke="#000000" points="213.79,-137.52 222.14,-144.04 219.68,-133.74 213.79,-137.52"/> </g> <!-- Sub --> <g id="node9" class="node"> <title>Sub</title> <g id="a_node9"><a xlink:href="/type/Sub" xlink:title="Sub"> <ellipse fill="none" stroke="#000000" cx="279.04" cy="-90" rx="28.7" ry="18"/> <text text-anchor="middle" x="279.04" y="-86.3" font-family="FreeSans" font-size="14.00" fill="#000000">Sub</text> </a> </g> </g> <!-- Sub->Routine --> <g id="edge7" class="edge"> <title>Sub->Routine</title> <path fill="none" stroke="#000000" d="M268.61,-106.88C263,-115.41 255.97,-126.11 249.64,-135.75"/> <polygon fill="#000000" stroke="#000000" points="246.65,-133.92 244.08,-144.2 252.5,-137.77 246.65,-133.92"/> </g> <!-- Macro --> <g id="node10" class="node"> <title>Macro</title> <g id="a_node10"><a xlink:href="/type/Macro" xlink:title="Macro"> <ellipse fill="none" stroke="#000000" cx="363.04" cy="-90" rx="37.89" ry="18"/> <text text-anchor="middle" x="363.04" y="-86.3" font-family="FreeSans" font-size="14.00" fill="#000000">Macro</text> </a> </g> </g> <!-- Macro->Routine --> <g id="edge8" class="edge"> <title>Macro->Routine</title> <path fill="none" stroke="#000000" d="M338.9,-104C318.89,-114.77 290.2,-130.22 267.74,-142.32"/> <polygon fill="#000000" stroke="#000000" points="265.99,-139.29 258.84,-147.11 269.31,-145.45 265.99,-139.29"/> </g> <!-- Regex --> <g id="node11" class="node"> <title>Regex</title> <g id="a_node11"><a xlink:href="/type/Regex" xlink:title="Regex"> <ellipse fill="none" stroke="#000000" cx="188.04" cy="-18" rx="38.19" ry="18"/> <text text-anchor="middle" x="188.04" y="-14.3" font-family="FreeSans" font-size="14.00" fill="#000000">Regex</text> </a> </g> </g> <!-- Regex->Method --> <g id="edge9" class="edge"> <title>Regex->Method</title> <path fill="none" stroke="#000000" d="M188.04,-36.3C188.04,-44.02 188.04,-53.29 188.04,-61.89"/> <polygon fill="#000000" stroke="#000000" points="184.54,-61.9 188.04,-71.9 191.54,-61.9 184.54,-61.9"/> </g> </g> </svg> <p class="fallback"> <a rel="alternate" href="/assets/typegraphs/Routine.svg"> Expand chart above </a></p> </figure> </div></div></section> <section class="page-footnotes"> <div class="container"> <div id="_Footnotes" class="footnotes"> <div class="footnote" id="fn1"><span class="footnote-number">1</span><a class="footnote-linkback" href="#fnret1"> [↑] </a>This is still in experimental stage. Please check <a href="/language/experimental#cached">the corresponding section in the experimental features document</a></div> </div> </div> </section> </div> </div> </div> <footer class="footer main-footer"> <div class="container px-4"> <nav class="level"> <div class="level-left"> <div class="level-item"> <a href="/about">About</a> </div> <div class="level-item"> <a id="toggle-theme">Toggle theme</a> </div> <div class="level-item" title="99c751d5d 2024-05-28"> <a>Commit</a> </div> </div> <div class="level-right"> <div class="level-item"> <a href="/license">License</a> </div> </div> </nav> </div> </footer> </body> </html>