File: interpolatedexpressionsequence.d

package info (click to toggle)
gcc-arm-none-eabi 15%3A14.2.rel1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,099,328 kB
  • sloc: cpp: 3,627,108; ansic: 2,571,498; ada: 834,230; f90: 235,082; makefile: 79,231; asm: 74,984; xml: 51,692; exp: 39,736; sh: 33,298; objc: 15,629; python: 15,069; fortran: 14,429; pascal: 7,003; awk: 5,070; perl: 3,106; ml: 285; lisp: 253; lex: 204; haskell: 135
file content (51 lines) | stat: -rw-r--r-- 2,040 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import core.interpolation;

alias AliasSeq(T...) = T;

string simpleToString(T...)(T thing) {
    string s;
    foreach(item; thing)
        // all the items provided by core.interpolation have
        // toString to return an appropriate value
        //
        // then this particular example only has embedded strings
        // and chars, to we can append them directly
        static if(__traits(hasMember, item, "toString"))
            s ~= item.toString();
        else
            s ~= item;

    return s;
}

void main() {
	int a = 1;
	string b = "one";
	// parser won't permit alias = i".." directly; i"..." is meant to
	// be used as a function/template parameter at this time.
	alias expr = AliasSeq!i"$(a) $(b)";
	// elements from the source code are available at compile time, so
	// we static assert those, but the values, of course, are different
	static assert(expr[0] == InterpolationHeader());
	static assert(expr[1] == InterpolatedExpression!"a"());
	assert(expr[2] == a); // actual value not available at compile time
	static assert(expr[3] == InterpolatedLiteral!" "());
	// the parens around the expression are not included
	static assert(expr[4] == InterpolatedExpression!"b"());
	assert(expr[5] == b); // actual value not available at compile time
	static assert(expr[6] == InterpolationFooter());

	// it does currently allow `auto` to be used, it creates a value tuple
	// you can embed any D expressions inside the parenthesis, and the
	// token is not ended until you get the *outer* ) and ".
	auto thing = i"$(b) $("$" ~ ')' ~ `"`)";
	assert(simpleToString(thing) == "one $)\"");

        assert(simpleToString(i"$b") == "$b"); // support for $ident removed by popular demand

        // i`` and iq{} should also work
        assert(simpleToString(i` $(b) is $(b)!`) == " one is one!");
        assert(simpleToString(iq{ $(b) is $(b)!}) == " one is one!");
        assert(simpleToString(i`\$('$')`) == "\\$"); // no \ escape there
        assert(simpleToString(iq{{$('$')}}) == "{$}"); // {} needs to work
}