Patch to add template parameters to class .stringof
text/plain
506
Comments
Comment #0 by casantander1 — 2007-12-23T22:13:00Z
I understand that "class A(T)" is syntactic sugar for "template A(T){class A...", but I still think this is wrong behavior:
---
class A(T) {}
alias A!(int) Aint;
A!(int) a;
pragma(msg, typeof(a).stringof);
pragma(msg, Aint.stringof);
static assert(typeof(a).stringof != "A", "typeof(a).stringof shouldn't be A");
---
Both pragmas print A, when I think it should be A!(int).
Tested with GDC rev 198, but this is part of the front end.
Comment #1 by dhasenan — 2008-11-10T10:02:32Z
This is really hurting me with CTFE + mixins. I can't use templated classes without creating a subclass for each template instantiation, and I can't use templated structs, if I want to use some of my testing code for mock objects. (That is, these objects cannot have methods that reference templated classes or templated structs.)
Comment #2 by dhasenan — 2008-11-10T13:35:28Z
It looks like fixing this will require giving Dsymbol a reference to a TemplateInstance, eliminating the nesting part (or giving an option to omit it), then modifying *::toChars to check if it's in a template instance and, if so, prepend the template instance's string representation.
You can add the reference at template.c:3026
Only types (typedefs, classes, structs) need modification in their toChars methods.
Expected time required to make this change: 30-60 minutes. I'll make it tonight, if I can get llvm to compile.
Comment #3 by dhasenan — 2008-11-15T22:04:22Z
This isn't a straightforward fix after all. I've seen some situations in which a string representation closer to what we would need is shown...
With structs, I'm seeing:
typeof(a).stringof prints the correct result
A!(int).stringof prints the wrong result
This will give at least a workaround with a minimum of effort.
Comment #4 by dhasenan — 2008-11-15T22:52:41Z
Created attachment 279
Patch to add template parameters to class .stringof
Comment #5 by dhasenan — 2008-11-15T22:54:08Z
If you have a templated struct, it partially works already:
---
struct S(T) {}
S!(int) s;
pragma (msg, typeof(s).stringof); // prints S!(int)
pragma (msg, S!(int).stringof); // prints S
pragma (msg, (S!(int)).stringof); // prints S!(int)
---
The patch I just added gives the same output if you change 'struct' to 'class'.
Comment #6 by dhasenan — 2008-11-23T08:43:09Z
The patch I submitted fixes the case of:
class S(T){}
I don't think it fixes any other cases:
class S(T)
{
class B {}
}
S!(int).B.stringof == B
template Template(T)
{
struct S {}
}
Template!(int).S.stringof == S
However, it does cover the basic case, which is something of an improvement. I'll see what I can do about everything else. Additionally, I think there might be something going on with nested template .stringof that I have to look into.