Movatterモバイル変換


[0]ホーム

URL:


D Logo
Menu
Search

Library Reference

version 2.112.0

overview

Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.Requires a signed-in GitHub account. This works well for small changes.If you'd like to make larger changes you may want to consider usinga local clone.

core.interpolation

This module provides definitions to support D's interpolated expression sequence literal, sometimes called string interpolation.
string str;int num;// the compiler uses this module to implement the// i"..." literal used here.auto a =i"$​(str) has $​(num) items.";
The variablea is a sequence of expressions:
a[0] == InterpolationHeader()a[$-1] == InterpolationFooter()
First and last, you see the header and footer, to clearly indicate where interpolation begins and ends. Note that there may be nested interpolated sequences too, each with their own header and footer. Think of them as a set of balanced parenthesis around the contents.
Inside, you will find three general categories of content:InterpolatedLiteral!"string" for string expressions,InterpolatedExpression!"code" for code expressions, and then the values themselves as their own type.
In the example:
auto a =i"$​(str) has $​(num) items.";
We will find:
a[0] == InterpolationHeader()a[1] == InterpolatedExpression!"str"a[2] == stra[3] == InterpolatedLiteral!" has ";a[4] == InterpolatedExpression!"num";a[5] == numa[6] == InterpolatedLiteral!" items.";a[7] == InterpolationFooter()a.length == 8;
You can see the correspondence with the original input: when you write$​(expression), the string of the expression is passed asInterpolatedExpression!ThatString, (excluding any parenthesis around the expression), and everything else is passed asInterpolatedLiteral!str, in the same sequence as they appeared in the source.
After anInterpolatedExpression!..., you will find the actual value(s) in the tuple. (If the expression expanded to multiple values - for example, if it was itself a tuple, there will be multiple values for a single expression.)
Library functions should NOT attempt to mixin the code from anInterpolatedExpression themselves. Doing so will fail, since it is coming from a different scope anyway. The string is provided to you only for informational purposes and as a sentinel to separate things the user wrote.
Your code should be able to handle an empty code string inInterpolatedExpression or even an entirely missingInterpolatedExpression, in case an implementation decides to not emit these.
ThetoString members on these returnnull, except for theInterpolatedLiteral, which returns the literal string. This is to ease processing by generic functions likestd.stdio.write orstd.conv.text, making them effectively transparently skipped.
To extract the string from anInterpolatedLiteral, you can use anis expression or the.toString method.
To extract the string from aInterpolatedExpression, you can use anis expression or the.expression member.
None of these structures have runtime state.
History:
Added in dmd 2.10x frontend, released in late 2023.
pure nothrow @nogc @safe string__getEmptyString();
Common implementation for returning an empty string, to avoid storing multiple versions of the same function based on templated types below.
structInterpolationHeader;

structInterpolationFooter;
Sentinel values to indicate the beginning and end of an interpolated expression sequence.
Note that these can nest, so while processing a sequence, it may be helpful to keep a nesting count if that knowledge is important to your application.
aliastoString = .__getEmptyString;
Returnsnull for easy compatibility with existing functions likestd.stdio.writeln andstd.conv.text.
structInterpolatedLiteral(string text);
Represents a fragment of a string literal in between expressions passed as part of an interpolated expression sequence.
static pure nothrow @nogc @safe stringtoString();
Returns the text of the interpolated string literal for this segment of the tuple, for easy access and compatibility with existing functions likestd.stdio.writeln andstd.conv.text.
structInterpolatedExpression(string text);
Represents the source code of an expression passed as part of an interpolated expression sequence.
enum autoexpression;
Returns the text of an interpolated expression used in the original literal, if provided by the implementation.
aliastoString = __getEmptyString;
Returnsnull for easy compatibility with existing functions likestd.stdio.writeln andstd.conv.text.
Copyright © 1999-2026 by theD Language Foundation | Page generated byDdoc on Sat Feb 21 00:05:16 2026

[8]ページ先頭

©2009-2026 Movatter.jp