www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Question about Mixin.

reply "Agustin" <wolftein1 gmail.com> writes:
Hello guys, my question is, its possible to write a mixin in a 
class, then if that class is inherited, the mixin will be written 
again instead of written the mixin again in the class child, for 
example:

Class A(T)
{
  mixin(WriteFunctionFor!(A));
}

Class B : A(B)
{
   ... -> mixin is written for B without need to write 
("mixin(Write...))")
}

Class C : A(C)
{
   ... -> mixin is written for C without need to write 
("mixin(Write...))")
}
Jun 19 2013
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/19/2013 04:29 PM, Agustin wrote:
 Hello guys, my question is, its possible to write a mixin in a class,
 then if that class is inherited, the mixin will be written again instead
 of written the mixin again in the class child, for example:

 Class A(T)
 {
   mixin(WriteFunctionFor!(A));
 }

 Class B : A(B)
 {
    ... -> mixin is written for B without need to write ("mixin(Write...))")
 }

 Class C : A(C)
 {
    ... -> mixin is written for C without need to write ("mixin(Write...))")
 }
Yes: import std.stdio; template WriteFunctionFor(T) { T data; void foo() { writefln("I am working with a %s.", T.stringof); } } class A(T) { mixin WriteFunctionFor!T; } class B : A!B {} class C : A!C {} void main() { auto b = new B(); b.foo(); auto c = new C(); c.foo(); } The output: I am working with a B. I am working with a C. Ali
Jun 19 2013
next sibling parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Wednesday, June 19, 2013 16:35:16 Ali Çehreli wrote:
 On 06/19/2013 04:29 PM, Agustin wrote:
 Hello guys, my question is, its possible to write a mixin in a class,
 then if that class is inherited, the mixin will be written again instead
 of written the mixin again in the class child, for example:
 
 Class A(T)
 {
 
 mixin(WriteFunctionFor!(A));
 
 }
 
 Class B : A(B)
 {
 
 ... -> mixin is written for B without need to write
 ("mixin(Write...))")
 
 }
 
 Class C : A(C)
 {
 
 ... -> mixin is written for C without need to write
 ("mixin(Write...))")
 
 }
Yes: import std.stdio; template WriteFunctionFor(T) { T data; void foo() { writefln("I am working with a %s.", T.stringof); } } class A(T) { mixin WriteFunctionFor!T; } class B : A!B {} class C : A!C {} void main() { auto b = new B(); b.foo(); auto c = new C(); c.foo(); } The output: I am working with a B. I am working with a C.
Ah, you're right. That will work. I misread the question. I thought that he was asking whether mixins in the base class magically got mixed in again into the derived class, which they don't. - Jonathan M Davis
Jun 19 2013
parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Wednesday, 19 June 2013 at 23:44:12 UTC, Jonathan M Davis 
wrote:
 Ah, you're right. That will work. I misread the question. I 
 thought that he
 was asking whether mixins in the base class magically got mixed 
 in again into
 the derived class, which they don't.

 - Jonathan M Davis
yeah, that's the "curiously recursive template pattern". The thing is that all these classes don't actually derive from a common base. The fact that they have a base class is only an implementation detail. This trick is used a lot in C++, but in D, I really don't see what you get doing this over a simple template mixin (appart from not being able to have a "true" base class)...
Jun 20 2013
prev sibling parent reply "Agustin" <wolftein1 gmail.com> writes:
On Wednesday, 19 June 2013 at 23:35:16 UTC, Ali Çehreli wrote:
 On 06/19/2013 04:29 PM, Agustin wrote:
 Hello guys, my question is, its possible to write a mixin in a 
 class,
 then if that class is inherited, the mixin will be written 
 again instead
 of written the mixin again in the class child, for example:

 Class A(T)
 {
  mixin(WriteFunctionFor!(A));
 }

 Class B : A(B)
 {
   ... -> mixin is written for B without need to write 
 ("mixin(Write...))")
 }

 Class C : A(C)
 {
   ... -> mixin is written for C without need to write 
 ("mixin(Write...))")
 }
Yes: import std.stdio; template WriteFunctionFor(T) { T data; void foo() { writefln("I am working with a %s.", T.stringof); } } class A(T) { mixin WriteFunctionFor!T; } class B : A!B {} class C : A!C {} void main() { auto b = new B(); b.foo(); auto c = new C(); c.foo(); } The output: I am working with a B. I am working with a C. Ali
Thanks!, now i'm trying to do that but its not working :(. template Eventable(T) { final static __gshared public HandlerList!T getHandler() { if( handler_ is null ) { handler_ = new HandlerList!T(); } return handler_; } } public class EventTemplate(T) : Event { mixin Eventable!T; } class TestEvent : EventTemplate!(TestEvent) { double x = 0.0; } TestEvent.getHandler() -> wont work.
Jun 19 2013
parent "Agustin" <wolftein1 gmail.com> writes:
On Thursday, 20 June 2013 at 00:20:36 UTC, Agustin wrote:
 On Wednesday, 19 June 2013 at 23:35:16 UTC, Ali Çehreli wrote:
 On 06/19/2013 04:29 PM, Agustin wrote:
 Hello guys, my question is, its possible to write a mixin in 
 a class,
 then if that class is inherited, the mixin will be written 
 again instead
 of written the mixin again in the class child, for example:

 Class A(T)
 {
 mixin(WriteFunctionFor!(A));
 }

 Class B : A(B)
 {
  ... -> mixin is written for B without need to write 
 ("mixin(Write...))")
 }

 Class C : A(C)
 {
  ... -> mixin is written for C without need to write 
 ("mixin(Write...))")
 }
Yes: import std.stdio; template WriteFunctionFor(T) { T data; void foo() { writefln("I am working with a %s.", T.stringof); } } class A(T) { mixin WriteFunctionFor!T; } class B : A!B {} class C : A!C {} void main() { auto b = new B(); b.foo(); auto c = new C(); c.foo(); } The output: I am working with a B. I am working with a C. Ali
Thanks!, now i'm trying to do that but its not working :(. template Eventable(T) { final static __gshared public HandlerList!T getHandler() { if( handler_ is null ) { handler_ = new HandlerList!T(); } return handler_; } } public class EventTemplate(T) : Event { mixin Eventable!T; } class TestEvent : EventTemplate!(TestEvent) { double x = 0.0; } TestEvent.getHandler() -> wont work.
public class EventTemplate(T) : Event { mixin Eventable!T; protected __gshared HandlerList!T handler_; }
Jun 19 2013
prev sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Thursday, June 20, 2013 01:29:48 Agustin wrote:
 Hello guys, my question is, its possible to write a mixin in a
 class, then if that class is inherited, the mixin will be written
 again instead of written the mixin again in the class child
No. If you want to put the same mixin in each of the derived classes, you must do so manually (though any public or protected functions mixed into the base class will be callable by the derived classes without mixing anything into them - just the same as if those functions were written directly in the base class). - Jonathan M Davis
Jun 19 2013