digitalmars.D - How to stringify a template instantiation expression?
- Yuxuan Shui (4/4) Feb 28 2018 For a template instantiation expression like A!(B, C!(D, E)), I
- aliak (4/8) Feb 28 2018 A!(B, C!(D, E)).stringof I guess. Will print the former.
- Yuxuan Shui (11/21) Mar 01 2018 Did you actually try that? With dmd 2.079-rc1, this:
- Yuxuan Shui (5/29) Mar 01 2018 Even worse, if the template instantiation yields another
- Simen =?UTF-8?B?S2rDpnLDpXM=?= (45/69) Mar 01 2018 string TemplateStringOf(T...)()
- Yuxuan Shui (7/21) Mar 02 2018 Ah, thanks. I was trying to match template instantiation using
- aliak (8/18) Mar 01 2018 Yep, though not all possible combinations of course:
For a template instantiation expression like A!(B, C!(D, E)), I want to get a string "A!(B, C!(D, E))", better if A, B, C, D, E is replaced by fully qualified name. Is this possible?
Feb 28 2018
On Wednesday, 28 February 2018 at 15:09:56 UTC, Yuxuan Shui wrote:For a template instantiation expression like A!(B, C!(D, E)), I want to get a string "A!(B, C!(D, E))", better if A, B, C, D, E is replaced by fully qualified name. Is this possible?A!(B, C!(D, E)).stringof I guess. Will print the former. There's a Learn forum as well btw :) Cheers
Feb 28 2018
On Wednesday, 28 February 2018 at 15:49:25 UTC, aliak wrote:On Wednesday, 28 February 2018 at 15:09:56 UTC, Yuxuan Shui wrote:Did you actually try that? With dmd 2.079-rc1, this: template A(T...) {} struct B {} struct D {} struct E {} template C(T...) {} pragma(msg, (A!(B, C!(D, E))).stringof); Prints: A!(B, __T1CTS2ax1DTSQh1EZ) When compiledFor a template instantiation expression like A!(B, C!(D, E)), I want to get a string "A!(B, C!(D, E))", better if A, B, C, D, E is replaced by fully qualified name. Is this possible?A!(B, C!(D, E)).stringof I guess. Will print the former. There's a Learn forum as well btw :) Cheers
Mar 01 2018
On Thursday, 1 March 2018 at 16:46:30 UTC, Yuxuan Shui wrote:On Wednesday, 28 February 2018 at 15:49:25 UTC, aliak wrote:Even worse, if the template instantiation yields another template, e.g: template A(T) { template A(T) {} } A!int.stringof returns "A(T)", which is not useful at all.On Wednesday, 28 February 2018 at 15:09:56 UTC, Yuxuan Shui wrote:Did you actually try that? With dmd 2.079-rc1, this: template A(T...) {} struct B {} struct D {} struct E {} template C(T...) {} pragma(msg, (A!(B, C!(D, E))).stringof); Prints: A!(B, __T1CTS2ax1DTSQh1EZ) When compiledFor a template instantiation expression like A!(B, C!(D, E)), I want to get a string "A!(B, C!(D, E))", better if A, B, C, D, E is replaced by fully qualified name. Is this possible?A!(B, C!(D, E)).stringof I guess. Will print the former. There's a Learn forum as well btw :) Cheers
Mar 01 2018
On Thursday, 1 March 2018 at 16:46:30 UTC, Yuxuan Shui wrote:On Wednesday, 28 February 2018 at 15:49:25 UTC, aliak wrote:string TemplateStringOf(T...)() if (T.length == 1) { import std.traits : TemplateOf, TemplateArgsOf; import std.meta : AliasSeq, staticMap; import std.string : indexOf; import std.conv : text; static if (is(typeof({ alias a = TemplateOf!T; }))) { alias Tmp = TemplateOf!T; alias Args = TemplateArgsOf!T; enum tmpFullName = Tmp.stringof; enum tmpName = tmpFullName[0..tmpFullName.indexOf('(')]; alias AddCommas(T...) = AliasSeq!(T, ", "); alias ArgNames = staticMap!(.TemplateStringOf, Args); alias SeparatedArgNames = staticMap!(AddCommas, ArgNames)[0..$-1]; return text(tmpName, "!(", SeparatedArgNames, ")"); } else { return T[0].stringof; } } unittest { template A(T...) {} struct B {} struct D {} struct E {} template C(T...) {} assert(TemplateStringOf!(A!(B, C!(D, E))) == "A!(B, C!(D, E))"); } unittest { template A(T) { template A(T) {} } assert(TemplateStringOf!(A!int) == "A!(int)"); } Probably still some corner cases I haven't thought of, but it seems to cover what you're asking for. One thing I didn't bother with is disambiguation - if B exists in modules foo and bar, the above will not specify which foo it's referring to. This is left as an exercise for the reader. -- SimenOn Wednesday, 28 February 2018 at 15:09:56 UTC, Yuxuan Shui wrote:Did you actually try that? With dmd 2.079-rc1, this: template A(T...) {} struct B {} struct D {} struct E {} template C(T...) {} pragma(msg, (A!(B, C!(D, E))).stringof); Prints: A!(B, __T1CTS2ax1DTSQh1EZ) When compiledFor a template instantiation expression like A!(B, C!(D, E)), I want to get a string "A!(B, C!(D, E))", better if A, B, C, D, E is replaced by fully qualified name. Is this possible?A!(B, C!(D, E)).stringof I guess. Will print the former. There's a Learn forum as well btw :) Cheers
Mar 01 2018
On Thursday, 1 March 2018 at 17:48:02 UTC, Simen Kjærås wrote:On Thursday, 1 March 2018 at 16:46:30 UTC, Yuxuan Shui wrote:Ah, thanks. I was trying to match template instantiation using is() (i.e. is(T == S!R, S, R...)). But this doesn't work if T is a template. Reading the code of TemplateOf, I realized I have to use template constraints for that. Maybe we can add support for something like is(alias T == ...)?[...]string TemplateStringOf(T...)() if (T.length == 1) { import std.traits : TemplateOf, TemplateArgsOf; import std.meta : AliasSeq, staticMap; import std.string : indexOf; import std.conv : text; static if (is(typeof({ alias a = TemplateOf!T; }))) { alias Tmp = TemplateOf!T; alias Args = TemplateArgsOf!T; [...]
Mar 02 2018
On Thursday, 1 March 2018 at 16:46:30 UTC, Yuxuan Shui wrote:Did you actually try that? With dmd 2.079-rc1, this: template A(T...) {} struct B {} struct D {} struct E {} template C(T...) {} pragma(msg, (A!(B, C!(D, E))).stringof); Prints: A!(B, __T1CTS2ax1DTSQh1EZ) When compiledYep, though not all possible combinations of course: struct X(T...) {} void main() { writeln(X!(int, X!(X!(int), float), char).stringof); } Seems like if you throw in template templates then things get a little more ... complicated.
Mar 01 2018