How does this template work?

XavierAP n3minis-git at yahoo.es
Tue Jun 18 07:44:07 UTC 2019


On Sunday, 16 June 2019 at 15:11:29 UTC, Robert M. Münch wrote:
> How does the observerObject Template and function work? I'm 
> struggling because both use the same name and how is the 
> template parameter R deduced/where is it coming from? Looks 
> like it's somehow implicitly deduced.

Eponymous templates:
https://dlang.org/spec/template.html#implicit_template_properties

"Templated types" are actually particular cases of eponymous 
templates:
https://dlang.org/spec/template.html#StructTemplateDeclaration

    class ObserverObject(R, E...) {...}

is equivalent to

    tempalte ObserverObject(R, E...)
    {
       class ObserverObject(R, E...) {...}
    }

So this is I think how everything is made to work with the same 
compiler engine, both individual "templated types" and "eponymous 
templates".

It's considered idiomatic, but if you don't like it in your case, 
it's very easy for the author to avoid it: just make the names 
different in any way.

    template Observer(E)
    {
       ObserverObject!(R, E) Object(R)(R range)
       {
           return new ObserverObject!(R, E)(range);
       }
    }

    auto observer = Observer!int.Object(TestObserver());


More information about the Digitalmars-d-learn mailing list