www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why are statements not allowed in mixin templates?

reply Andrej Mitrovic <none none.none> writes:
If they're evaluated at the instantiation scope, and not in the point of
definition, then why are statements disallowed?

Where does the limitation come from?
Apr 09 2011
next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
 If they're evaluated at the instantiation scope, and not in the point of
 definition, then why are statements disallowed?
 
 Where does the limitation come from?
Probably because statements are only valid in functions, and a template is a set of declarations. It certainly wouldn't make any sense to have a normal template include statements, and the whole template mixin thing grew out of normal templates, so even if it makes perfect sense for template mixins to include statements, since they came out of normal templates in the first place (which can't have statements, since it would make no sense), they wouldn't wouldn't be able to have statements unless Walter thought to add that capabality specifically with mixing in in mind. But while templates that you mixin are generally designed for that and therefore designed differently than normal templates, I don't believe that there's any really difference in them except in how you instantiate them, so the compiler is going to end up treating them the same as far as the lexer and parser go. And since it makes no sense for normal templates to have statements, templates that you mixin don't either. But think of the mess that you'd have if you tried to instantiate a template as a normal template when it held statements? You'd probably get some pretty weird errors if that were allowed. So, I think that the limitation comes primarily from the fact that prior to mixins it made no sense for templates to contain naked statements, and templates intended for being used in mixins aren't really any different from normal templates from the compiler's perspective. - Jonathan M Davis
Apr 09 2011
prev sibling parent Cliff Hudson <cliff.s.hudson gmail.com> writes:
Couldn't you also just have your mixin define a function which - as a nested
function - would have access to the stack of the enclosing block?  Slightly
less pretty, and slightly different (since variables declared in the mixin
wouldn't exist in the enclosing scope)... ok, yeah, rather different.

On Sat, Apr 9, 2011 at 3:07 PM, Jonathan M Davis <jmdavisProg gmx.com>wrote:

 If they're evaluated at the instantiation scope, and not in the point of
 definition, then why are statements disallowed?

 Where does the limitation come from?
Probably because statements are only valid in functions, and a template is a set of declarations. It certainly wouldn't make any sense to have a normal template include statements, and the whole template mixin thing grew out of normal templates, so even if it makes perfect sense for template mixins to include statements, since they came out of normal templates in the first place (which can't have statements, since it would make no sense), they wouldn't wouldn't be able to have statements unless Walter thought to add that capabality specifically with mixing in in mind. But while templates that you mixin are generally designed for that and therefore designed differently than normal templates, I don't believe that there's any really difference in them except in how you instantiate them, so the compiler is going to end up treating them the same as far as the lexer and parser go. And since it makes no sense for normal templates to have statements, templates that you mixin don't either. But think of the mess that you'd have if you tried to instantiate a template as a normal template when it held statements? You'd probably get some pretty weird errors if that were allowed. So, I think that the limitation comes primarily from the fact that prior to mixins it made no sense for templates to contain naked statements, and templates intended for being used in mixins aren't really any different from normal templates from the compiler's perspective. - Jonathan M Davis
Apr 09 2011