Re: How does this template work?

2019-06-18 Thread XavierAP via Digitalmars-d-learn

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());


Re: How does this template work?

2019-06-18 Thread Robert M. Münch via Digitalmars-d-learn

On 2019-06-17 20:53:28 +, 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



Re: How does this template work?

2019-06-17 Thread aliak via Digitalmars-d-learn

On Monday, 17 June 2019 at 18:25:24 UTC, Robert M. Münch wrote:

On 2019-06-16 15:14:37 +, 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());




Re: How does this template work?

2019-06-17 Thread Robert M. Münch via Digitalmars-d-learn

On 2019-06-16 15:14:37 +, 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



How does this template work?

2019-06-16 Thread Robert M. Münch via Digitalmars-d-learn
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



Re: How does this template work?

2019-06-16 Thread rikki cattermole via Digitalmars-d-learn

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.