digitalmars.D.learn - How does this template work?
- =?iso-8859-1?Q?Robert_M._M=FCnch?= (18/18) Jun 16 2019 How does the observerObject Template and function work? I'm struggling
- rikki cattermole (5/24) Jun 16 2019 observerObject is an eponymous template.
- =?iso-8859-1?Q?Robert_M._M=FCnch?= (7/11) Jun 17 2019 Hmm... ok. Is there any reason to have these "eponymous templates"? I
- aliak (3/10) Jun 17 2019 Less typing for one. Otherwise you'd have to write:
- =?iso-8859-1?Q?Robert_M._M=FCnch?= (12/15) Jun 17 2019 Since code is many times more read than written I will never understand
- XavierAP (26/30) Jun 18 2019 Eponymous templates:
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
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
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
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:Less typing for one. Otherwise you'd have to write: auto observer = observerObject!int.observerObject(TestObserver());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...
Jun 17 2019
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
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