Templates, D way

Corey Lubin via Digitalmars-d digitalmars-d at puremagic.com
Fri Sep 8 03:27:57 PDT 2017


On Tuesday, 5 September 2017 at 12:41:45 UTC, Computermatronic 
wrote:
>
> I find it very strange that this works, as a non-mixin template 
> should not be able to capture the context of where it was 
> instantiated. If you take the alias template parameters out it 
> behaves how it should (that is an error message saying this is 
> not accessible).

Computermatronic (and crimaniak and John Colvin):

The behavior is as should be expected and is part of the 
definition of how templates' alias parameters are supposed to 
work.

The error that occurs when you stop using an alias parameter 
occurs because you're no longer forcing the capture of an 
enclosing scope by passing in an alias parameter that requires 
such a scope; not because the error should have occurred the 
first time as well.

Circumstances where some "context" is captured include:

  - template instantiations where an alias parameter requires a 
context in order to resolve the parameter correctly [[the 
instantiated function/struct/class will effectively become nested 
within whichever scope is determined to be needed in order to 
resolve the alias parameter -- whether the alias parameter is 
needed, used, or not]]

  - template instantiation of functions/structs/classes whose 
enclosing templates were declared within a function/struct/class 
[[the template instance's functions, structs, and classes will 
effectively become nested within the function/struct/class that 
the template was declared within -- whether the instantiated 
function/struct/class's original declaration strictly implied a 
need for an outer function/struct/class or not]]

  - and --hrmm... it's 6:20 AM; I haven't yet slept for the night; 
and I'm supposed to continue preparing to save myself from the 
strongest hurricane ever seen in the Atlantic Ocean in a couple 
hours, so I think that's where I'll end this partial list.

Some assorted details related to some miscellaneous capture 
situations:
  - https://dlang.org/spec/template.html#nested-templates
  - https://dlang.org/spec/template.html#nested_template_limitation
  - https://dlang.org/spec/template.html#aliasparameters
  - https://dlang.org/spec/template.html#limitations
  - https://dlang.org/spec/function.html#closures



More information about the Digitalmars-d mailing list