www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - spec: template instantiations

reply Dibyendu Majumdar <mobile majumdar.org.uk> writes:
I don't under this:

https://dlang.org/spec/template.html#instantiation_scope

The text seems to contradict the example.

If a template declares multiple entities then is it possible to 
instantiate all of them at one or is each entity instantiated 
separately?

Suppose we have:

template Transformer(From, To) // From and To are types, too
{
     To transform(From from)
     {
         import std.conv;
         return to!(To)(from);
     }

     class Modificator
     {
         From f;
         To t;
         this(From f) { /*...*/ }
     }
}

Then is there way to instantiate both 'transform' and 
'Modificator' at once?
Nov 20 2020
next sibling parent Dibyendu Majumdar <mobile majumdar.org.uk> writes:
On Friday, 20 November 2020 at 20:59:56 UTC, Dibyendu Majumdar 
wrote:
 I don't under this:

 https://dlang.org/spec/template.html#instantiation_scope

 The text seems to contradict the example.
Please ignore - my mistake.
Nov 20 2020
prev sibling parent reply Dibyendu Majumdar <mobile majumdar.org.uk> writes:
On Friday, 20 November 2020 at 20:59:56 UTC, Dibyendu Majumdar 
wrote:
 If a template declares multiple entities then is it possible to 
 instantiate all of them at one or is each entity instantiated 
 separately?

 Suppose we have:

 template Transformer(From, To) // From and To are types, too
 {
     To transform(From from)
     {
         import std.conv;
         return to!(To)(from);
     }

     class Modificator
     {
         From f;
         To t;
         this(From f) { /*...*/ }
     }
 }

 Then is there way to instantiate both 'transform' and 
 'Modificator' at once?
I suppose answer is no? So when instantiating, it is always a particular member of the template that is instantiated?
Nov 20 2020
parent Paul Backus <snarwin gmail.com> writes:
On Saturday, 21 November 2020 at 00:24:43 UTC, Dibyendu Majumdar 
wrote:
 On Friday, 20 November 2020 at 20:59:56 UTC, Dibyendu Majumdar 
 wrote:
 Suppose we have:

 template Transformer(From, To) // From and To are types, too
 {
     To transform(From from)
     {
         import std.conv;
         return to!(To)(from);
     }

     class Modificator
     {
         From f;
         To t;
         this(From f) { /*...*/ }
     }
 }

 Then is there way to instantiate both 'transform' and 
 'Modificator' at once?
I suppose answer is no? So when instantiating, it is always a particular member of the template that is instantiated?
Quite the opposite. When you instantiate a template, the entire template body is instantiated at the same time. You can then access individual members using the `.` operator. So for example: alias Instance = Transformer!(int, string); Instance.Modificator m = new Instance.Modificator(123); string s = Instance.transform(123);
Nov 20 2020