www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How does this template work?

reply =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
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.


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

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

struct TestObserver {...}

auto observer = observerObject!int(TestObserver());


-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster
Jun 16 2019
next sibling parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 17/06/2019 3:11 AM, 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.
 
 
 class ObserverObject(R, E...){...}
 
 template observerObject(E)
 {
     ObserverObject!(R, E) observerObject(R)(R range)
     {
         return new ObserverObject!(R, E)(range);
     }
 }
 
 struct TestObserver {...}
 
 auto observer = observerObject!int(TestObserver());
observerObject is an eponymous template. What this means (in essence) is the symbol inside the template block == template block. Yes R is being inferred by the argument.
Jun 16 2019
parent reply =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
On 2019-06-16 15:14:37 +0000, rikki cattermole said:

 observerObject is an eponymous template.
 
 What this means (in essence) is the symbol inside the template block == 
 template block.
Hmm... ok. Is there any reason to have these "eponymous templates"? I don't see any benefit... -- Robert M. Münch http://www.saphirion.com smarter | better | faster
Jun 17 2019
parent reply aliak <something something.com> writes:
On Monday, 17 June 2019 at 18:25:24 UTC, Robert M. Münch wrote:
 On 2019-06-16 15:14:37 +0000, rikki cattermole said:

 observerObject is an eponymous template.
 
 What this means (in essence) is the symbol inside the template 
 block == template block.
Hmm... ok. Is there any reason to have these "eponymous templates"? I don't see any benefit...
Less typing for one. Otherwise you'd have to write: auto observer = observerObject!int.observerObject(TestObserver());
Jun 17 2019
parent =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
On 2019-06-17 20:53:28 +0000, aliak said:

 Less typing for one. Otherwise you'd have to write:
 
 auto observer = observerObject!int.observerObject(TestObserver());
Since code is many times more read than written I will never understand why the syntax is polluted to save some keystrokes, making it much harded for others who don't have 800 pages special cases in their mind to read the code. One explicit alias or so would be OK too for cases where such a declaration is needed more than once. But anyway, thanks. -- Robert M. Münch http://www.saphirion.com smarter | better | faster
Jun 17 2019
prev sibling parent XavierAP <n3minis-git yahoo.es> writes:
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());
Jun 18 2019