Thread overview
[Issue 1732] New: Mixin can not be instantiated
Dec 16, 2007
d-bugmail
Dec 16, 2007
d-bugmail
Dec 16, 2007
d-bugmail
Dec 16, 2007
d-bugmail
Dec 16, 2007
d-bugmail
Dec 16, 2007
d-bugmail
December 16, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1732

           Summary: Mixin can not be instantiated
           Product: D
           Version: 1.024
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: aarti@interia.pl


template init() {
   a = 5;
}

void main() {
  int a;
  mixin init;
}

it doesn't compile, but it should. According to specs mixin should be evaluated in the context of its instantiation. Also another examples, in specs suggest that it should be allowed.

Probably also 2.0 issue.


-- 

December 16, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1732


matti.niemenmaa+dbugzilla@iki.fi changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID




------- Comment #1 from matti.niemenmaa+dbugzilla@iki.fi  2007-12-16 08:59 -------
Templates can only contain declarations, not statements. The template alone is invalid.

As http://www.digitalmars.com/d/1.0/template.html says:

"The body of the TemplateDeclaration must be syntactically correct even if never instantiated. Semantic analysis is not done until instantiated. A template forms its own scope, and the template body can contain classes, structs, types, enums, variables, functions, and other templates."

Your template is syntactically invalid, as it doesn't contain a DeclDef but an AssignExpression.


-- 

December 16, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1732





------- Comment #2 from aarti@interia.pl  2007-12-16 09:24 -------
Maybe, but I still find it very strange that following works:

----
import std.stdio;

template init() {
   void func() {writefln(a);}
}

void main() {
  int a;
  mixin init;
  func();
}

I see no real (practical) difference between both examples. I would say that they should be both invalid or both valid.


-- 

December 16, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1732





------- Comment #3 from matti.niemenmaa+dbugzilla@iki.fi  2007-12-16 10:14 -------
The difference is that one imports a declaration while the other imports a statement. The init() with a function could be mixed in at global scope or class scope, whereas the "a = 5" one only works at function scope.

I see where you're coming from, though: some sort of implicitness here might be handy. For instance, if a template contains statements, it is allowed only as a mixin at function scope. That is:

template T() { statements }
void foo() { mixin T; }

Could be equivalent to:

template T() { void __unique_internal_name() { statements } }
void foo() { mixin T; __unique_internal_name(); }

And you get inlining for free.


-- 

December 16, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1732





------- Comment #4 from aarti@interia.pl  2007-12-16 12:02 -------
I think about the issue differently. For me its more consistency issue.

It should be just enough when compiler checks:
1. if template body is syntacticly correct (but not as a whole, so variables
doesn't have to be declared)
2. compiler instantiates template or paste mixin, then it tries to compile it
as a whole

That should be enough to use it.

But when I think about it, this probably will cause problems for compiler (more compilations than necessary). And probably that's why macros will be introduced...


-- 

December 16, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1732





------- Comment #5 from aarti@interia.pl  2007-12-16 13:03 -------
I will make from this improvement request as I think current state is just wrong. Will at least work as issue tracker...


--