www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - lost and mixed in

reply Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
Why doesn't this work:

# template MFoo(T)
# {
# 	T foo(T a)
# 	{
# 		return a;
# 	}
# }
#
# class Foo
# {
# 	mixin MFoo!(int);
# 	mixin MFoo!(int*);
# }
#
# void main()
# {
# 	Foo f = new Foo;
# 	f.foo(2);
# }

Error:
"console_main.d(3): function console_main.Foo.MFoo!(int) MFoo_i.foo 
conflicts with console_main.Foo.MFoo!(int*) MFoo_Pi.foo at 
console_main.d(3)"


On the other hand, this code is just fine:

# class Foo
# {
# 	int foo(int a)
# 	{
# 		return a;
# 	}
#
# 	int* foo(int* a)
# 	{
# 		return a;
# 	}
# }
#
# void main()
# {
# 	Foo f = new Foo;
# 	f.foo(2);
# }


Am I missing/mixing something about mixins ? I'd really like the mixin 
code to work... right now I have to make huge cut'n'pasting in my project :(


-- 
Tomasz Stachowiak  /+ a.k.a. h3r3tic +/
May 06 2005
next sibling parent Sean Kelly <sean f4.ca> writes:
See if this works:

# class Foo
# {
# 	mixin MFoo!(int) MFooMix1;
#       alias MFooMix.foo foo;
# 	mixin MFoo!(int*) MFooMix2;
#       alias MFooMix.foo foo;
# }
May 06 2005
prev sibling parent reply Derek Parnell <derek psych.ward> writes:
On Fri, 06 May 2005 19:04:31 +0200, Tom S wrote:

 Why doesn't this work:
 
 # template MFoo(T)
 # {
 # 	T foo(T a)
 # 	{
 # 		return a;
 # 	}
 # }
 #
 # class Foo
 # {
 # 	mixin MFoo!(int);
 # 	mixin MFoo!(int*);
 # }
 #
 # void main()
 # {
 # 	Foo f = new Foo;
 # 	f.foo(2);
 # }
 
 Error:
 "console_main.d(3): function console_main.Foo.MFoo!(int) MFoo_i.foo 
 conflicts with console_main.Foo.MFoo!(int*) MFoo_Pi.foo at 
 console_main.d(3)"
 
 
 On the other hand, this code is just fine:
 
 # class Foo
 # {
 # 	int foo(int a)
 # 	{
 # 		return a;
 # 	}
 #
 # 	int* foo(int* a)
 # 	{
 # 		return a;
 # 	}
 # }
 #
 # void main()
 # {
 # 	Foo f = new Foo;
 # 	f.foo(2);
 # }
 
 
 Am I missing/mixing something about mixins ? I'd really like the mixin 
 code to work... right now I have to make huge cut'n'pasting in my project :(

If one reads the mixin documents, one gets the impression that it is a method of inserting code _as if it had been coded in_. However, mixins are not exactly boilerplate code snippets. Which is a crying shame, if you ask me. Using your example, one could have thought that it was as if you had coded ... # class Foo # { # int foo(int a) # { # return a; # } # int* foo(int* a) # { # return a; # } #} #void main() #{ # Foo f = new Foo; # f.foo(2); #} which compiles just fine. But using mixins you have to go over some stupid looking hurdles. #template MFoo(T) #{ # T foo(T a) # { # return a; # } #} #class Foo #{ # mixin MFoo!(int) mint; alias mint.foo foo; # mixin MFoo!(int*) mintp; alias mintp.foo foo; #} #void main() #{ # Foo f = new Foo; # f.foo(2); #} So, to repeat myself, mixins are not macros. mixin MFoo!(int) does not exactly generate ... int foo(int a) { return a; } as you would naturally expect. Its more like creating a variable declaration, and having two variables with the same name in the same scope is a conflict. I think it is a mistake that one day Walter will see fit to correct. -- Derek Parnell Melbourne, Australia http://www.dsource.org/projects/build v2.06 is now available. 04/May/2005 7/05/2005 4:25:35 AM
May 06 2005
parent Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
Derek Parnell wrote:
 So, to repeat myself, mixins are not macros. 
 
   mixin MFoo!(int)
 
 does not exactly generate ...
 
   int foo(int a)
   {
     return a;
   }
 
 as you would naturally expect. Its more like creating a variable
 declaration, and having two variables with the same name in the same scope
 is a conflict.
 
 I think it is a mistake that one day Walter will see fit to correct.

Sorry I haven't replied earlier, but only now I could get back to my comp for a longer while. Thank you (and Sean) for helping me out with this issue, but I don't quite like the solution... I hope this problem will be fixed / resolved in future (or rather: Walter resolves it) I've gone with another solution, which at least looks better in the implementation /* I'd have to define many of these mixin FooMixin1 .. FooMixinN as I'm using it for custom classes rather than for int and int* ... */ I'm just mixing in a single template which has the desired code generated by a python script (like a marco -> shame), lies in a separate file/module and at least works exactly as I want it to... -- Tomasz Stachowiak /+ a.k.a. h3r3tic +/
May 08 2005