How to export a deduced template type to the enclosing scope?

kiran kumari via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 23 23:54:08 PDT 2014


On Tuesday, 23 September 2014 at 22:09:11 UTC, Ali Çehreli wrote:
> I think similar questions were asked by others in different 
> contexts before.
>
> I played with core.thread.Fibre a little bit. As others have 
> done a number of times before, I tried to make the following 
> syntax possible inside fiber code:
>
>     yield(42);
>
> I wonder whether there is a clever trick to pull out a deduced 
> type from a template. I don't think it is possible, because 
> there may be many instantiations of the template and it would 
> not be clear which one to pull out. (I don't think there is any 
> way of getting all of the instantiations of a template because 
> the compiler has only a partial view of the program at a time.)
>
> However, what if there is exactly one instantiation allowed?
>
> struct S(alias Func)
> {
>     /* This template mixin will instantiate the yield(T) 
> template. */
>     mixin Func!();
> see more example
http://techgurulab.com/course/java-quiz-online/
>     void yield(T)(T)
>     {
>         /* As expected and demonstrated by the following 
> pragma, in
>          * this case T happens to be 'double'. Assuming that 
> Func is
>          * not allowed to call yield() with more than one type, 
> can we
>          * pull the actual type of T out into S's scope? */
>
>         alias YieldedT = T;
>         pragma(msg, YieldedT);    /* Prints 'double'. */
>     }
>
>     /* QUESTION: Can we know the type for the single 
> instantiation of
>      *           yield(T) here? */
>
>     YieldedT data;    /* What is YieldedT? */
> }
>
> mixin template MyFunc()
> {
>     void foo()
>     {
>         double d;
>         yield(d);    /* <-- The single instantiation */
>     }
> }
>
> void main()
> {
>     auto s = S!MyFunc();
>     s.foo();
> }
>
> So near and yet so far... :)
>
> Ali


More information about the Digitalmars-d-learn mailing list