526 lines
18 KiB
Plaintext
526 lines
18 KiB
Plaintext
<!DOCTYPE html>
|
|
<head>
|
|
<link href="https://fonts.googleapis.com/css?family=Roboto+Slab:300,400" rel="stylesheet" type="text/css"/>
|
|
<link rel="stylesheet" href="/static/css/common.css"/>
|
|
<link rel="shortcut icon" href="/static/img/favicon.ico"/>
|
|
<link rel="stylesheet" type="text/css" href="/static/css/menu.css"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.1, maximum-scale=10.0"/>
|
|
<title>Compile Time Function Evaluation (CTFE) - Dlang Tour</title>
|
|
<link rel="stylesheet" href="/static/css/tour.css"/>
|
|
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css"/>
|
|
<link rel="stylesheet" href="/static/lib/hotkeys.min.css"/>
|
|
<link rel="stylesheet" href="/static/lib/codemirror/lib/codemirror.css"/>
|
|
<link rel="stylesheet" href="/static/lib/codemirror/addon/lint/lint.css"/>
|
|
<link rel="stylesheet" href="/static/lib/codemirror/theme/elegant.css"/>
|
|
<link rel="stylesheet" href="/static/lib/grid12.min.css"/>
|
|
<link rel="stylesheet" href="/static/css/ansi.css"/>
|
|
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
|
|
</head>
|
|
<body ng-app="DlangTourApp" class="ng-cloak">
|
|
<div id="top">
|
|
<div class="helper">
|
|
<div class="helper expand-container active">
|
|
<div class="logo">
|
|
<a href="https://dlang.org">
|
|
<img id="logo" alt="DLang Logo" src="/static/img/dlogo.svg"/>
|
|
</a>
|
|
<a href="../../../" id="title">
|
|
<span>DLang Tour</span>
|
|
</a>
|
|
</div>
|
|
<a href="#" title="Menu" class="hamburger expand-toggle">
|
|
<span>Menu</span>
|
|
</a>
|
|
<div id="cssmenu">
|
|
<ul>
|
|
<li class="expand-container">
|
|
<a class="expand-toggle" href="#">
|
|
<span>Welcome</span>
|
|
</a>
|
|
<ul class="expand-content">
|
|
<li class="">
|
|
<a href="/tour/en/welcome/welcome-to-d">
|
|
<span>Welcome to D</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/welcome/languages">
|
|
<span>Read in your language</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/welcome/install-d-locally">
|
|
<span>Install D locally</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/welcome/run-d-program-locally">
|
|
<span>Run D program locally</span>
|
|
</a>
|
|
</li>
|
|
</ul>
|
|
</li>
|
|
<li class="expand-container">
|
|
<a class="expand-toggle" href="#">
|
|
<span>D's Basics</span>
|
|
</a>
|
|
<ul class="expand-content">
|
|
<li class="">
|
|
<a href="/tour/en/basics/imports-and-modules">
|
|
<span>Imports and modules</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/basics/basic-types">
|
|
<span>Basic types</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/basics/memory">
|
|
<span>Memory</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/basics/type-qualifiers">
|
|
<span>Mutability</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/basics/controlling-flow">
|
|
<span>Control flow</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/basics/functions">
|
|
<span>Functions</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/basics/structs">
|
|
<span>Structs</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/basics/arrays">
|
|
<span>Arrays</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/basics/slices">
|
|
<span>Slices</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/basics/alias-strings">
|
|
<span>Alias & Strings</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/basics/loops">
|
|
<span>Loops</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/basics/foreach">
|
|
<span>Foreach</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/basics/ranges">
|
|
<span>Ranges</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/basics/associative-arrays">
|
|
<span>Associative Arrays</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/basics/classes">
|
|
<span>Classes</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/basics/interfaces">
|
|
<span>Interfaces</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/basics/templates">
|
|
<span>Templates</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/basics/delegates">
|
|
<span>Delegates</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/basics/exceptions">
|
|
<span>Exceptions</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/basics/further-reading">
|
|
<span>Further Reading</span>
|
|
</a>
|
|
</li>
|
|
</ul>
|
|
</li>
|
|
<li class="expand-container active">
|
|
<a class="expand-toggle" href="#">
|
|
<span>D's Gems</span>
|
|
</a>
|
|
<ul class="expand-content">
|
|
<li class="active">
|
|
<a href="/tour/en/gems/uniform-function-call-syntax-ufcs">
|
|
<span>Uniform Function Call Syntax (UFCS)</span>
|
|
</a>
|
|
</li>
|
|
<li class="active">
|
|
<a href="/tour/en/gems/scope-guards">
|
|
<span>Scope guards</span>
|
|
</a>
|
|
</li>
|
|
<li class="active">
|
|
<a href="/tour/en/gems/range-algorithms">
|
|
<span>Range algorithms</span>
|
|
</a>
|
|
</li>
|
|
<li class="active">
|
|
<a href="/tour/en/gems/unittesting">
|
|
<span>Unit Testing</span>
|
|
</a>
|
|
</li>
|
|
<li class="active">
|
|
<a href="/tour/en/gems/string-mixins">
|
|
<span>String Mixins</span>
|
|
</a>
|
|
</li>
|
|
<li class="active">
|
|
<a href="/tour/en/gems/compile-time-function-evaluation-ctfe">
|
|
<span>Compile Time Function Evaluation (CTFE)</span>
|
|
</a>
|
|
</li>
|
|
<li class="active">
|
|
<a href="/tour/en/gems/functional-programming">
|
|
<span>Functional programming</span>
|
|
</a>
|
|
</li>
|
|
<li class="active">
|
|
<a href="/tour/en/gems/contract-programming">
|
|
<span>Contract programming</span>
|
|
</a>
|
|
</li>
|
|
<li class="active">
|
|
<a href="/tour/en/gems/subtyping">
|
|
<span>Subtyping</span>
|
|
</a>
|
|
</li>
|
|
<li class="active">
|
|
<a href="/tour/en/gems/attributes">
|
|
<span>Attributes</span>
|
|
</a>
|
|
</li>
|
|
<li class="active">
|
|
<a href="/tour/en/gems/opdispatch-opapply">
|
|
<span>opDispatch & opApply</span>
|
|
</a>
|
|
</li>
|
|
<li class="active">
|
|
<a href="/tour/en/gems/documentation">
|
|
<span>Documentation</span>
|
|
</a>
|
|
</li>
|
|
<li class="active">
|
|
<a href="/tour/en/gems/traits">
|
|
<span>Traits</span>
|
|
</a>
|
|
</li>
|
|
<li class="active">
|
|
<a href="/tour/en/gems/template-meta-programming">
|
|
<span>Template meta programming</span>
|
|
</a>
|
|
</li>
|
|
<li class="active">
|
|
<a href="/tour/en/gems/bit-manipulation">
|
|
<span>Bit manipulation</span>
|
|
</a>
|
|
</li>
|
|
<li class="active">
|
|
<a href="/tour/en/gems/unicode">
|
|
<span>Unicode in D</span>
|
|
</a>
|
|
</li>
|
|
</ul>
|
|
</li>
|
|
<li class="expand-container">
|
|
<a class="expand-toggle" href="#">
|
|
<span>Multithreading</span>
|
|
</a>
|
|
<ul class="expand-content">
|
|
<li class="">
|
|
<a href="/tour/en/multithreading/thread-local-storage">
|
|
<span>Thread local storage</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/multithreading/message-passing">
|
|
<span>Message Passing</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/multithreading/synchronization-sharing">
|
|
<span>Synchronization & Sharing</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/multithreading/std-parallelism">
|
|
<span>std.parallelism</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/multithreading/fibers">
|
|
<span>Fibers</span>
|
|
</a>
|
|
</li>
|
|
</ul>
|
|
</li>
|
|
<li class="expand-container">
|
|
<a class="expand-toggle" href="#">
|
|
<span>Vibe.d</span>
|
|
</a>
|
|
<ul class="expand-content">
|
|
<li class="">
|
|
<a href="/tour/en/vibed/vibe-d-web-framework">
|
|
<span>Vibe.d web framework</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/vibed/basics-asynchronous-i-o">
|
|
<span>Basics & Asynchronous I/O</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/vibed/web-server">
|
|
<span>Web server</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/vibed/diet-templates">
|
|
<span>DIET Templates</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/vibed/json-rest-interface">
|
|
<span>JSON REST Interface</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/vibed/database-connectivity">
|
|
<span>Database connectivity</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/vibed/deploy-on-heroku">
|
|
<span>Deploy on Heroku</span>
|
|
</a>
|
|
</li>
|
|
</ul>
|
|
</li>
|
|
<li class="expand-container">
|
|
<a class="expand-toggle" href="#">
|
|
<span>D by Examples</span>
|
|
</a>
|
|
<ul class="expand-content">
|
|
<li class="">
|
|
<a href="/tour/en/byexample/code-generation-parser">
|
|
<span>Code generation (Parser)</span>
|
|
</a>
|
|
</li>
|
|
</ul>
|
|
</li>
|
|
<li class="expand-container">
|
|
<a class="expand-toggle" href="#">
|
|
<span>DUB packages</span>
|
|
</a>
|
|
<ul class="expand-content">
|
|
<li class="">
|
|
<a href="/tour/en/dub/mir-algorithm">
|
|
<span>Mir Algorithm</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/dub/mir-random">
|
|
<span>Mir Random</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/dub/mir">
|
|
<span>Mir</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/dub/emsi_containers">
|
|
<span>EMSI Containers</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/dub/vibe-d">
|
|
<span>Vibe-d</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/dub/libdparse">
|
|
<span>libdparse</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/dub/pegged">
|
|
<span>Pegged</span>
|
|
</a>
|
|
</li>
|
|
<li class="">
|
|
<a href="/tour/en/dub/lubeck">
|
|
<span>Lubeck</span>
|
|
</a>
|
|
</li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
<div id="github_avatar">
|
|
<a href="https://github.com/dlang-tour/core">
|
|
<i class="fa fa-github" aria-hidden="true"></i>
|
|
</a>
|
|
<a ng-href="https://github.com/dlang-tour/core/issues/new">
|
|
<i class="fa fa-question-circle" aria-hidden="true"></i>
|
|
</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="row" ng-controller="DlangTourAppCtrl as ctrl" ng-init="initTour('en', 'dlang-tour/english', 'gems', 'compile-time-function-evaluation-ctfe', true, '/tour/en/gems/string-mixins', '/tour/en/gems/functional-programming')">
|
|
<div id="tour-content" ng-show="showContent" ng-class="{'col-md-12': !showSourceCode, 'col-md-7 col-sm-12': showSourceCode}">
|
|
<div ng-hide="showProgramOutput">
|
|
<div class="content-command-box">
|
|
<button class="btn btn-default" ng-click="editOnGithub()">
|
|
<div class="fa fa-edit"></div>
|
|
</button>
|
|
<span class="hidden-xs hidden-sm">
|
|
<button class="btn btn-default btn-sm" ng-click="showSourceCode = true" ng-hide="showSourceCode">
|
|
<div class="fa fa-eye"></div>
|
|
</button>
|
|
</span>
|
|
</div>
|
|
<h1 id="compile-time-function-evaluation-ctfe"> Compile Time Function Evaluation (CTFE)</h1>
|
|
<p>CTFE is a mechanism which allows the compiler to execute
|
|
functions at <strong>compile time</strong>. There is no special set of the D
|
|
language necessary to use this feature - whenever
|
|
a function just depends on compile time known values
|
|
the D compiler might decide to interpret
|
|
it during compilation.
|
|
</p>
|
|
<pre class="prettyprint"><code>// result will be calculated at compile
|
|
// time. Check the machine code, it won't
|
|
// contain a function call!
|
|
static val = sqrt(50);
|
|
</code></pre>
|
|
<p>Keywords like <code class="prettyprint">static</code>, <code class="prettyprint">immutable</code> or <code class="prettyprint">enum</code>
|
|
instruct the compiler to use CTFE whenever possible.
|
|
The great thing about this technique is that
|
|
functions don't need to be rewritten to use
|
|
it, and the same code can perfectly be shared:
|
|
</p>
|
|
<pre class="prettyprint"><code>int n = doSomeRuntimeStuff();
|
|
// same function as above but this
|
|
// time it is just called the classical
|
|
// run-time way.
|
|
auto val = sqrt(n);
|
|
</code></pre>
|
|
<p>One prominent example in D is the <a href="https://dlang.org/phobos/std_regex.html">std.regex</a>
|
|
library. It provides the <code class="prettyprint">ctRegex</code> type which uses
|
|
<em>string mixins</em> and CTFE to generate a highly optimized
|
|
regular expression automaton that is generated during
|
|
compilation. The same code base is re-used for
|
|
the run-time version <code class="prettyprint">regex</code> that allows regular
|
|
expressions only available at run-time to be compiled.
|
|
</p>
|
|
<pre class="prettyprint"><code>auto ctr = ctRegex!(`^.*/([^/]+)/?$`);
|
|
auto tr = regex(`^.*/([^/]+)/?$`);
|
|
// ctr and tr can be used interchangely
|
|
// but ctr will be faster!
|
|
</code></pre>
|
|
<p>Not all language features are available
|
|
during CTFE but the supported feature set is increased
|
|
with every release of the compiler.
|
|
</p>
|
|
<h3 id="in-depth"> In-depth</h3>
|
|
<ul>
|
|
<li><a href="https://dlang.org/regular-expression.html">Introduction to regular expressions in D</a>
|
|
</li>
|
|
<li><a href="https://dlang.org/phobos/std_regex.html">std.regex</a>
|
|
</li>
|
|
<li><a href="https://dlang.org/spec/version.html">Conditional compilation</a>
|
|
</li>
|
|
</ul>
|
|
|
|
</div>
|
|
<div ng-show="showProgramOutput">
|
|
<div class="content-command-box">
|
|
<button class="btn btn-danger btn-sm" ng-click="showProgramOutput = !showProgramOutput">
|
|
<span class="fa fa-close"></span>
|
|
</button>
|
|
</div>
|
|
<h2 class="program-output-title">rdmd playground.d</h2><div id="nanobar" ng-show="inProgress"></div><pre id="program-output" ng-bind-html="programOutput"></pre></div>
|
|
</div>
|
|
<div ng-class="{'col-md-5 col-sm-12': showContent, 'col-md-12': !showContent}" ng-show="showSourceCode" style="padding-left: 0px; padding-right: 0px">
|
|
<div id="command-box" class="text-right">
|
|
<button class="btn btn-default" ng-click="showContent = !showContent">
|
|
<i class="fa fa-expand" ng-show="showContent" aria-hidden="true"></i><i class="fa fa-compress" ng-hide="showContent" aria-hidden="true"></i>
|
|
</button>
|
|
<button class="btn btn-default btn-sm hidden-xs hidden-sm" ng-click="showSourceCode = false">
|
|
<div class="fa fa-eye-slash"></div>
|
|
</button>
|
|
<button class="btn btn-primary" ng-click="run()">
|
|
<span>Run</span><i class="fa fa-play" aria-hidden="true"></i>
|
|
</button>
|
|
<button class="btn btn-default" ng-click="format()">
|
|
<span>Format</span><i class="fa fa-magic" aria-hidden="true"></i>
|
|
</button>
|
|
<button class="btn btn-default" ng-click="reset()">
|
|
<span>Reset</span><i class="fa fa-undo" aria-hidden="true"></i>
|
|
</button>
|
|
<button class="btn btn-default" ng-click="export()">
|
|
<span>Export</span><i class="fa fa-share" aria-hidden="true"></i>
|
|
</button>
|
|
</div>
|
|
<ui-codemirror ui-codemirror-opts="editorOptions" ui-codemirror="{ onLoad : codemirrorLoaded }" ng-model="sourceCode"></ui-codemirror>
|
|
</div>
|
|
</div>
|
|
<nav class="navbar navbar-bottom">
|
|
<div class="container text-center h1">
|
|
<a href="/tour/en/gems/string-mixins" ng-show="'' != '/tour/en/gems/string-mixins'" target="_self">
|
|
<
|
|
<span class="h4">String Mixins</span>
|
|
</a>
|
|
6 /
|
|
<span class="small">16</span>
|
|
<a href="/tour/en/gems/functional-programming" ng-show="'' != '/tour/en/gems/functional-programming'" target="_self">
|
|
<span class="h4">Functional programming</span>>
|
|
</a>
|
|
</div>
|
|
<div class="container hidden-xs hidden-sm">
|
|
<p class="text-muted text-center">
|
|
<kbd>?</kbd>Keyboard Shortcuts
|
|
</p>
|
|
</div>
|
|
</nav>
|
|
<script type="application/javascript" src="/static/js/menu.js"></script><script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
|
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
|
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
|
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
|
|
ga('create', '', 'auto');
|
|
ga('send', 'pageview');
|
|
</script><script src="/static/js/tour-controller.js"></script><script src="/static/js/swipe.js"></script><script src="/static/lib/codemirror/lib/codemirror.js"></script><script src="/static/lib/codemirror/mode/d/d.js"></script><script src="/static/lib/codemirror/addon/lint/lint.js"></script><script src="/static/lib/codemirror/addon/runmode/runmode.js"></script><script src="/static/lib/ui-codemirror.min.js"></script><script src="/static/lib/hotkeys.min.js"></script><script src="/static/lib/nanobar.min.js"></script><script src="/static/lib/ansi_up.min.js"></script>
|
|
</body> |