Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
D (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Metaprogramming==== [[Metaprogramming]] is supported through templates, compile-time function execution, [[tuple]]s, and string mixins. The following examples demonstrate some of D's compile-time features. Templates in D can be written in a more imperative style compared to the C++ functional style for templates. This is a regular function that calculates the [[factorial]] of a number: <syntaxhighlight lang="D"> ulong factorial(ulong n) { if (n < 2) return 1; else return n * factorial(n-1); } </syntaxhighlight> Here, the use of <code>static if</code>, D's compile-time conditional construct, is demonstrated to construct a template that performs the same calculation using code that is similar to that of the function above: <syntaxhighlight lang="D"> template Factorial(ulong n) { static if (n < 2) enum Factorial = 1; else enum Factorial = n * Factorial!(n-1); } </syntaxhighlight> In the following two examples, the template and function defined above are used to compute factorials. The types of constants need not be specified explicitly as the compiler [[type inference|infers their types]] from the right-hand sides of assignments: <syntaxhighlight lang="D"> enum fact_7 = Factorial!(7); </syntaxhighlight> This is an example of [[compile-time function execution]] (CTFE). Ordinary functions may be used in constant, compile-time expressions provided they meet certain criteria: <syntaxhighlight lang="D"> enum fact_9 = factorial(9); </syntaxhighlight> The <code>std.string.format</code> function performs [[printf|<code>printf</code>]]-like data formatting (also at compile-time, through CTFE), and the "msg" [[Directive (programming)|pragma]] displays the result at compile time: <syntaxhighlight lang="D"> import std.string : format; pragma(msg, format("7! = %s", fact_7)); pragma(msg, format("9! = %s", fact_9)); </syntaxhighlight> String mixins, combined with compile-time function execution, allow for the generation of D code using string operations at compile time. This can be used to parse [[domain-specific language]]s, which will be compiled as part of the program: <syntaxhighlight lang="D"> import FooToD; // hypothetical module which contains a function that parses Foo source code // and returns equivalent D code void main() { mixin(fooToD(import("example.foo"))); } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)