Thread overview
[Issue 9608] New: Add introspection for templates
Feb 27, 2013
Andrej Mitrovic
Mar 23, 2013
Andrej Mitrovic
February 27, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9608

           Summary: Add introspection for templates
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: andrei@erdani.com


--- Comment #0 from Andrei Alexandrescu <andrei@erdani.com> 2013-02-27 05:12:46 PST ---
Currently given a template symbol it's not possible to do simple introspection on it, such as getting arity, distinguishing alias vs. type parameters, and figuring out variadics.

Example:

    template A(alias sym)
    {
        ...
    }
    A!((a, b) => a.name < b.name);

A is unable to tell the number of parameters of the lambda. It could if it knew what type to instantiate it, and in order to know that it needs the actual instantiation type. There should be enough introspection chops to compute the number of parameters without instantiating the lambda.

There would be a fair amount of design involved because currently there's no kind that expresses a template's parameters outside the template.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 27, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9608


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

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


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-02-27 15:16:48 PST ---
Is Issue 4265 relevant in any way (does it help?)?

I think I almost have a pull ready for Issue 4265, but it depends on whether people really want the feature.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 23, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9608



--- Comment #2 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-03-23 11:32:36 PDT ---
(In reply to comment #0)
> There would be a fair amount of design involved because currently there's no kind that expresses a template's parameters outside the template.

I don't think we need to design much. We could provide traits. For example:

    template A(alias sym)
    {
        __traits(getArity, sym);  // 2 - or paramCount
        __traits(isAliasParam, 0, sym);  // 0 == index of param
    }
    A!((a, b) => a.name < b.name);

    template B(alias sym)
    {
        __traits(isValueParam, 0, sym);  // true
        __traits(isTypeParam, 1, sym);  // true
        __traits(isVariadicParam, 2, sym);  // true
    }

    void X(int val, Type, Args...)(Args t) { return 0; }

    B!(X);

And then you could wrap all of this into a Phobos template which collects all the data. let's call it TemplateInfo:

    template B(alias sym)
    {
        alias ti = TemplateInfo!sym;
        static assert(ti.params.count == 2);
        static assert(ti.params[0].isValueParam);
    }

    void X(Type, int val)() { }
    B!(X);

That sort of stuff.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 24, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9608



--- Comment #3 from Andrei Alexandrescu <andrei@erdani.com> 2013-03-24 05:34:07 PDT ---
Yah, issue 4265 is related. For this one, the challenge is defining a minimal and complete set of traits.

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