www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Compile-time reflection and templates

reply Jean-Louis Leroy <jl leroy.nyc> writes:
I can identify the templates in a module:

   module modtemp;

   import std.stdio;
   import std.traits;

   void foo(T)(T x) {}

   void main()
   {
     foreach (m; __traits(allMembers, modtemp)) {
       if (__traits(isTemplate, mixin(m))) {
         mixin("alias F = " ~ m ~ ";");
         writeln(m);
       }
     }
   }
   // output:
   // foo

I'd like to go further: find the template arguments and the 
function arguments and return types. Looking at __traits and 
std.traits, it doesn't seem feasible, but maybe I overlooked 
something?
Sep 27
next sibling parent jmh530 <john.michael.hall gmail.com> writes:
On Wednesday, 27 September 2017 at 19:47:32 UTC, Jean-Louis Leroy 
wrote:
 I'd like to go further: find the template arguments and the 
 function arguments and return types. Looking at __traits and 
 std.traits, it doesn't seem feasible, but maybe I overlooked 
 something?
You can use TemplateArgsOf https://dlang.org/phobos/std_traits.html#TemplateArgsOf to get the template arguments. You can test if it's a function https://dlang.org/phobos/std_traits.html#isFunction You can get the Parameter and Return types https://dlang.org/phobos/std_traits.html#Parameters https://dlang.org/phobos/std_traits.html#ReturnType You can get the names of parameters https://dlang.org/phobos/std_traits.html#ParameterIdentifierTuple
Sep 27
prev sibling next sibling parent reply jmh530 <john.michael.hall gmail.com> writes:
On Wednesday, 27 September 2017 at 19:47:32 UTC, Jean-Louis Leroy 
wrote:
 I'd like to go further: find the template arguments and the 
 function arguments and return types. Looking at __traits and 
 std.traits, it doesn't seem feasible, but maybe I overlooked 
 something?
You can use TemplateArgsOf https://dlang.org/phobos/std_traits.html#TemplateArgsOf to get the template arguments. For the stuff below, I think you can't just use foo, it has to be a specific foo!T. You can test if it's a function https://dlang.org/phobos/std_traits.html#isFunction You can get the Parameter and Return types https://dlang.org/phobos/std_traits.html#Parameters https://dlang.org/phobos/std_traits.html#ReturnType You can get the names of parameters https://dlang.org/phobos/std_traits.html#ParameterIdentifierTuple
Sep 27
parent reply Jean-Louis Leroy <jl leroy.nyc> writes:
On Wednesday, 27 September 2017 at 20:04:42 UTC, jmh530 wrote:
 On Wednesday, 27 September 2017 at 19:47:32 UTC, Jean-Louis 
 Leroy wrote:
 I'd like to go further: find the template arguments and the 
 function arguments and return types. Looking at __traits and 
 std.traits, it doesn't seem feasible, but maybe I overlooked 
 something?
You can use TemplateArgsOf https://dlang.org/phobos/std_traits.html#TemplateArgsOf to get the template arguments. For the stuff below, I think you can't just use foo, it has to be a specific foo!T. You can test if it's a function https://dlang.org/phobos/std_traits.html#isFunction You can get the Parameter and Return types https://dlang.org/phobos/std_traits.html#Parameters https://dlang.org/phobos/std_traits.html#ReturnType You can get the names of parameters https://dlang.org/phobos/std_traits.html#ParameterIdentifierTuple
I am aware of these but TemplateArgsOf takes a template *instantiation* and returns the arguments. I want to reflect the *template*. Here what I am trying to achieve (for openmethods). Given: Matrix!T times(T, virtual!(Matrix!T)); ...inject, at compile time (in 'mixin(registerMethods)'), the following code: Matrix!T times(T)(T s, Matrix!T m) { return Method!("times", "deallocator", Matrix!T, T, virtual!(Matrix!T)).dispatcher(s, m); } Method!("times", "deallocator", Matrix!T, T, virtual!(Matrix!T)) times(T)(MethodTag, T s, Matrix!T m);
Sep 27
parent jmh530 <john.michael.hall gmail.com> writes:
On Wednesday, 27 September 2017 at 21:18:54 UTC, Jean-Louis Leroy 
wrote:
 I am aware of these but TemplateArgsOf takes a template 
 *instantiation* and returns the arguments. I want to reflect 
 the *template*.
Yeah, I had kind of realized the point about instantiate mid-post.
Sep 27
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2017-09-27 21:47, Jean-Louis Leroy wrote:

 I'd like to go further: find the template arguments 
There was a pull request that implemented this [1]. But it was closed, added to many new traits. [1] https://github.com/dlang/dmd/pull/5201 -- /Jacob Carlborg
Sep 28