Bug 6207 – Mixin template evaluated to string can convert to string mixin expression implicitly
Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-06-24T03:48:00Z
Last change time
2017-07-02T16:44:53Z
Keywords
patch
Assigned to
nobody
Creator
k.hara.pg
Comments
Comment #0 by k.hara.pg — 2011-06-24T03:48:28Z
If mixin template instantiation makes manifest constant string and it appears in expression context, the compiler converts it implicitly to string mixin expression.
This enhancement feature does not conflict with any existing features.
Because normal instantiating with mixin template is illegal.
Sample code:
----
mixin template expand(string code)
{
static if (code.length >= 2 && code[0..2] == "$x")
{
enum expand = `x` ~ code[2..$];
pragma(msg, expand);
}
else
enum expand = code;
}
void main()
{
int x = 1;
int y = expand!q{$x+2};
// Rhs is implicitly converted to mixin(expand!(q{$x+2}))
assert(y == 3);
}
----
Comment #1 by k.hara.pg — 2011-10-16T18:58:34Z
We can improve the string lambda features in std.algorithm!
mixin template map(string pred)
{
enum map = `map!((a){ return `~pred~`; })`;
}
template map(alias pred)
{
auto map(E)(E[] r)
{
E[] result;
foreach (e; r)
result ~= pred(e);
return result;
}
}
void main()
{
int b = 10;
auto r = map!q{ a * b }([1,2,3]);
// --> mixin(`map!((a){ return ` ~ q{ a * b } ~ `; })`)([1,2,3])
// --> map!((a){ return a * b ; })([1,2,3]);
assert(r == [10,20,30]);
}
What if we add some special syntax for mixin template instantiation.
I mean something like this:
int y = expand#"$x+2"; // instead of mixin(expand!"$x+2")
What do you think?
Comment #5 by dlang-bugzilla — 2017-07-02T16:44:53Z
(In reply to Walter Bright from comment #3)
> See the pull request for more discussion.
I believe such a change today would require a DIP, so I'm going to close this.