www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - assignments in mixins

reply Serg Kovrov <kovrov no.spam> writes:
Hello everybody,

If this mixin example works:

 template Foo(T)
 {
     T x = 5;
 }

Why this one not:
 template Foo(T)
 {
     T x;
     x = 5; // <- error: no identifier for declarator x
 }

On page describing mixins I never saw an example with assignment inside mixin body, is it limitation of language or bug? Thanks. -- serg.
Aug 15 2006
parent Pragma <ericanderton yahoo.removeme.com> writes:
Serg Kovrov wrote:
 Hello everybody,
 
 If this mixin example works:
 
 template Foo(T)
 {
     T x = 5;
 }

Why this one not:
 template Foo(T)
 {
     T x;
     x = 5; // <- error: no identifier for declarator x
 }

On page describing mixins I never saw an example with assignment inside mixin body, is it limitation of language or bug? Thanks. -- serg.

The problem here is that a template() declaration is a scope that contains other declarations - it is not a scope that contains code. This is why it complains about their being 'no identifier for declarator', as it was expecting a type before the 'x'. -- - EricAnderton at yahoo
Aug 15 2006