www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - wrong mixin function being called

reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
So here is the code:

<CODE>
import std.c.stdio;

template Tocka()
{
 int x=2,y=7;
 void print(){printf("x = %d y = %d\n",x,y);}
}

class Line
{
 mixin Tocka T1;
 mixin Tocka T2;

 void play()
 {
  T1.print();
  T2.print();
  printf("\n");
  T1.x = 100;
  T1.y = 50;
  T2.x = 25;
  T2.y = 5;
  T1.print();
  T2.print();
 }
}

int main(char[][] args)
{
 Line T = new Line();
 T.play();

 getch();
 return 1;
}
</CODE>

this program prints:
x = 25 y = 5
x = 25 y = 5

instead of the expected if every mixin has its own scope:
x = 100 y = 50
x = 25 y = 5
May 18 2004
parent reply "Walter" <newshound digitalmars.com> writes:
This works as expected if you name the function something other than
'print'. The trouble is, both T1.print and T2.print override Object.print,
and an error should be diagnosed.

"Ivan Senji" <ivan.senji public.srce.hr> wrote in message
news:c8cdfk$2neh$1 digitaldaemon.com...
 So here is the code:

 <CODE>
 import std.c.stdio;

 template Tocka()
 {
  int x=2,y=7;
  void print(){printf("x = %d y = %d\n",x,y);}
 }

 class Line
 {
  mixin Tocka T1;
  mixin Tocka T2;

  void play()
  {
   T1.print();
   T2.print();
   printf("\n");
   T1.x = 100;
   T1.y = 50;
   T2.x = 25;
   T2.y = 5;
   T1.print();
   T2.print();
  }
 }

 int main(char[][] args)
 {
  Line T = new Line();
  T.play();

  getch();
  return 1;
 }
 </CODE>

 this program prints:
 x = 25 y = 5
 x = 25 y = 5

 instead of the expected if every mixin has its own scope:
 x = 100 y = 50
 x = 25 y = 5

May 18 2004
parent "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Walter" <newshound digitalmars.com> wrote in message
news:c8cfs9$2rks$1 digitaldaemon.com...
 This works as expected if you name the function something other than
 'print'. The trouble is, both T1.print and T2.print override Object.print,
 and an error should be diagnosed.

Now (in 0.90) error is being diagnosed but it is a bit misleading. The error points to the line where the template (that is used as a mixin) appears and not at the code where it is mixed in. This shouldn't be reported as an error in a mixin, but better in the place where it is used.
 "Ivan Senji" <ivan.senji public.srce.hr> wrote in message
 news:c8cdfk$2neh$1 digitaldaemon.com...
 So here is the code:

 <CODE>
 import std.c.stdio;

 template Tocka()
 {
  int x=2,y=7;
  void print(){printf("x = %d y = %d\n",x,y);}
 }

 class Line
 {
  mixin Tocka T1;
  mixin Tocka T2;

  void play()
  {
   T1.print();
   T2.print();
   printf("\n");
   T1.x = 100;
   T1.y = 50;
   T2.x = 25;
   T2.y = 5;
   T1.print();
   T2.print();
  }
 }

 int main(char[][] args)
 {
  Line T = new Line();
  T.play();

  getch();
  return 1;
 }
 </CODE>

 this program prints:
 x = 25 y = 5
 x = 25 y = 5

 instead of the expected if every mixin has its own scope:
 x = 100 y = 50
 x = 25 y = 5


May 21 2004