www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4265] New: It should be possible to query template parameters with __traits

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4265

           Summary: It should be possible to query template parameters
                    with __traits
           Product: D
           Version: future
          Platform: Other
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: nfxjfg gmail.com



Consider you have

struct(T1, T2) Foo {
} 

alias Foo!(int, float) X;

You should be able to extract (int, float) from X.
Often it is possible to get the parameters indirectly from members, but there
doesn't seem to be a way to do this in the general case.

I suggest:

__traits(getTemplateParameters, X)

which would yield  a (int, float) tuple.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 02 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4265


Trass3r <mrmocool gmx.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mrmocool gmx.de



What would you use that for?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 03 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4265




Trass3r: meta programming. It often happened to me that I wanted to get the
template params. Sometimes I had to add alias declarations just to get access
to them. I think this feature would make meta programming code simpler in some
cases.

PS: the example above should start with:

struct Foo(T1, T2) {
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 03 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4265




Well of course it's for metaprogramming.
But I can't imagine a usecase at the moment so I asked for some examples ;)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 03 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4265


nfxjfg gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 06 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4265


Brad Roberts <braddr puremagic.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
                 CC|                            |braddr puremagic.com
         Resolution|INVALID                     |


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 06 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4265


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich gmail.com



14:55:16 PST ---
As a partial workaround if you know the original template a type was
instantiated with you can use:

struct Foo(T1, T2)
{
}

alias Foo!(int, float) X;

template GetParams(R, alias X)
{
    static if (is(R x == X!T, T...))
    {
        alias T GetParams;
    }
}

void main()
{
    pragma(msg, GetParams!(X, Foo));
}

There's also an 'isTemplateInstance' now in std.traits.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 20 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4265




15:09:13 PST ---
I can have a pull ready soon for 2 traits:

1. Get the template out of the template instance (Foo!int => Foo)
2. Get the arguments used to instantiate the template (Foo!int => (int))

However I need some good names for these. I thought about using
'__traits(getTemplateArgs, T)' and '__traits(getTemplateParent, T)', any better
suggestions?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 22 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4265




15:24:32 PST ---

 I can have a pull ready soon for 2 traits:
 
 1. Get the template out of the template instance (Foo!int => Foo)
 2. Get the arguments used to instantiate the template (Foo!int => (int))
 
 However I need some good names for these. I thought about using
 '__traits(getTemplateArgs, T)' and '__traits(getTemplateParent, T)', any better
 suggestions?
nazriel suggested: 'getTemplateName' 'getTemplateArgs' I think it's better because they line up nicely. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 22 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4265


Andrei Alexandrescu <andrei erdani.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrei erdani.com



PDT ---
C++ lacks this particular ability and makes up for it by requiring templates to
define internal aliases for type parameters. However, this is more tenuous for
value parameters and simply impossible for template template parameters (in our
case alias parameters). This has led to certain contortions and limitations;
based on that experience I think we should add this ability.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 24 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4265


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |k.hara.pg gmail.com



06:40:55 PDT ---

 I can have a pull ready soon for 2 traits:
I managed to lose the branch where I implemented this. I think I only did it partially though. kenji: If you have this: template T(Args...) { } struct S(Args...) { } alias Tint = T!int; alias Sint = S!int; How do you get to the 'S' declaration from the instance? I want to implement these traits: static assert(is(__traits(getTemplateSymbol, SInt) == S)); static assert(is(__traits(getTemplateArgs, SInt) == int)); I've tried this in traits.c: Dsymbol *s = getDsymbol(o); TemplateInstace *ti = s->isTemplateInstance(); However that only works for Tint, and not for Sint. 's' is actually a StructDeclaration for Sint, not a TemplateInstance. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 06 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4265


thelastmammoth gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |thelastmammoth gmail.com




 As a partial workaround if you know the original template a type was
 instantiated with you can use:
 
 struct Foo(T1, T2)
 {
 }
 
 alias Foo!(int, float) X;
 
 template GetParams(R, alias X)
 {
     static if (is(R x == X!T, T...))
     {
         alias T GetParams;
     }
 }
 
 void main()
 {
     pragma(msg, GetParams!(X, Foo));
 }
 
 There's also an 'isTemplateInstance' now in std.traits.
I don't see it ? But I added it in https://github.com/D-Programming-Language/phobos/pull/1367. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jun 23 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4265






 I can have a pull ready soon for 2 traits:
I managed to lose the branch where I implemented this. I think I only did it partially though. kenji: If you have this: template T(Args...) { } struct S(Args...) { } alias Tint = T!int; alias Sint = S!int; How do you get to the 'S' declaration from the instance? I want to implement these traits: static assert(is(__traits(getTemplateSymbol, SInt) == S)); static assert(is(__traits(getTemplateArgs, SInt) == int)); I've tried this in traits.c: Dsymbol *s = getDsymbol(o); TemplateInstace *ti = s->isTemplateInstance(); However that only works for Tint, and not for Sint. 's' is actually a StructDeclaration for Sint, not a TemplateInstance.
I made a pull request for this and other traits. It's in std.traits, which IMO is better than adding it to the compiler. https://github.com/D-Programming-Language/phobos/pull/1367 -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jun 23 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4265


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc



15:57:09 PDT ---
*** Issue 10890 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 25 2013