digitalmars.D.learn - CRTP in D?
- bearophile (4/4) Aug 19 2009 I don't know much C++. Can CRTP be used in D1 too, to improve the perfor...
- grauzone (4/9) Aug 19 2009 Why don't you just go and try?
- bearophile (4/5) Aug 19 2009 Mostly because C++ isn't one of the languages I know. I don't fully unde...
- Kagamin (24/28) Aug 20 2009 There's really not much to understand there. The difference from mixin i...
- downs (2/8) Aug 19 2009 We have this, except we call it "template mixin" :)
- div0 (15/24) Aug 19 2009 -----BEGIN PGP SIGNED MESSAGE-----
- Bill Baxter (18/32) Aug 19 2009 Mixins can be used to do a lot (most? all?) of things CRTP is used for:
- div0 (27/66) Aug 19 2009 -----BEGIN PGP SIGNED MESSAGE-----
- bearophile (5/13) Aug 19 2009 Thank you to all the people that have answered me.
- div0 (19/45) Aug 19 2009 -----BEGIN PGP SIGNED MESSAGE-----
- John C (5/14) Aug 20 2009 import std.typetuple;
- div0 (42/59) Aug 20 2009 -----BEGIN PGP SIGNED MESSAGE-----
- Bill Baxter (14/37) Aug 20 2009 This doesn't work either:
- div0 (27/72) Aug 20 2009 -----BEGIN PGP SIGNED MESSAGE-----
- Saaa (1/10) Aug 19 2009 where can I read about class parameters?
- Bill Baxter (9/21) Aug 19 2009 It's just a class template. Short for
-
Saaa
(12/24)
Aug 19 2009
"Bill Baxter"
wrote in message
I don't know much C++. Can CRTP be used in D1 too, to improve the performance of some D1 code? http://en.wikipedia.org/wiki/Curiously_Recurring_Template_Pattern Bye, bearophile
Aug 19 2009
bearophile wrote:I don't know much C++. Can CRTP be used in D1 too, to improve the performance of some D1 code? http://en.wikipedia.org/wiki/Curiously_Recurring_Template_PatternWhy don't you just go and try? If you hit forward referencing errors when using structs, try classes with final methods.Bye, bearophile
Aug 19 2009
grauzone:Why don't you just go and try?<Mostly because C++ isn't one of the languages I know. I don't fully understand that code. Bye, bearophile
Aug 19 2009
bearophile Wrote:grauzone:There's really not much to understand there. The difference from mixin is that Base can inherit an interface so that Derived exposes the interface just by deriving from Base. class Base(Derived) { void interface() { // ... (cast(Derived)this).implementation(); // ... } static void static_func() { // ... Derived.static_sub_func(); // ... } } class Derived : Base!(Derived) { void implementation(){...} static void static_sub_func(){...} }Why don't you just go and try?<Mostly because C++ isn't one of the languages I know. I don't fully understand that code.
Aug 20 2009
bearophile wrote:I don't know much C++. Can CRTP be used in D1 too, to improve the performance of some D1 code? http://en.wikipedia.org/wiki/Curiously_Recurring_Template_Pattern Bye, bearophileWe have this, except we call it "template mixin" :)
Aug 19 2009
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 downs wrote:bearophile wrote:No, template mixins are not CRTP. And yes CRTP does work in D. - -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iD8DBQFKjDcnT9LetA9XoXwRAiN8AJ4ghcTj9sYdU7tWdQRj1vVSxPzywgCeKP6f FKwMutENm221/YmhvopIEDk= =1Zt5 -----END PGP SIGNATURE-----I don't know much C++. Can CRTP be used in D1 too, to improve the performance of some D1 code? http://en.wikipedia.org/wiki/Curiously_Recurring_Template_Pattern Bye, bearophileWe have this, except we call it "template mixin" :)
Aug 19 2009
On Wed, Aug 19, 2009 at 10:32 AM, div0<div0 users.sourceforge.net> wrote:-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 downs wrote:Mixins can be used to do a lot (most? all?) of things CRTP is used for: class Class(alias MixMe) { mixin MixMe impl; ... void doSomething { impl.doIt(); } }bearophile wrote:No, template mixins are not CRTP.I don't know much C++. Can CRTP be used in D1 too, to improve the performance of some D1 code? http://en.wikipedia.org/wiki/Curiously_Recurring_Template_Pattern Bye, bearophileWe have this, except we call it "template mixin" :)And yes CRTP does work in D.That's fine if you don't need to use the one inheritance slot for something else. I also seem to recall for things like policy based design, you end up doing CRTP inheritance from several different policy classes: class Derived : Policy1<Derived>, Policy2<Derived> So I think DK is right -- more often than not CRTP is used as a substitute for lack of actual mixin support in C++. --bb
Aug 19 2009
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Bill Baxter wrote:On Wed, Aug 19, 2009 at 10:32 AM, div0<div0 users.sourceforge.net> wrote:Yes true, but there are subtle differences. I guess my no was a little over egging the differences on reflection.-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 downs wrote:Mixins can be used to do a lot (most? all?) of things CRTP is used for: class Class(alias MixMe) { mixin MixMe impl; ... void doSomething { impl.doIt(); } }bearophile wrote:No, template mixins are not CRTP.I don't know much C++. Can CRTP be used in D1 too, to improve the performance of some D1 code? http://en.wikipedia.org/wiki/Curiously_Recurring_Template_Pattern Bye, bearophileWe have this, except we call it "template mixin" :)CRTP is often used that way, but it's not the only use of it. It's also used as a mechanism to create a meta interface which deriving classes need to implement. This is used quite a lot in template meta programming in c++. (and that's where you often see the multiple inheritance from policy classes you mentioned). Mixins do pretty much same thing but in reverse, mixins pull stuff right into the class, and with a mixin it's the class which is imposing the meta interface on the mixin. As you pointed out though, CRTP nukes your inheritance slot in D, so you'd generally prefer mixins in D. CRTP isn't going to give you any advantage that I can see, you just have to do your design the other way round than you would in c++. - -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iD8DBQFKjFZWT9LetA9XoXwRAu7jAKDN8lOVjr1JW9F+HpWvu/rWScQIqwCguYED fVaoenX/OgKfsmXR4etVSas= =9QPj -----END PGP SIGNATURE-----And yes CRTP does work in D.That's fine if you don't need to use the one inheritance slot for something else. I also seem to recall for things like policy based design, you end up doing CRTP inheritance from several different policy classes: class Derived : Policy1<Derived>, Policy2<Derived> So I think DK is right -- more often than not CRTP is used as a substitute for lack of actual mixin support in C++. --bb
Aug 19 2009
div0:Mixins do pretty much same thing but in reverse, mixins pull stuff right into the class, and with a mixin it's the class which is imposing the meta interface on the mixin. As you pointed out though, CRTP nukes your inheritance slot in D, so you'd generally prefer mixins in D. CRTP isn't going to give you any advantage that I can see, you just have to do your design the other way round than you would in c++.Thank you to all the people that have answered me. Mixins have an advantage: I have understood them in minutes and then I have used them. While I haven't undertood CRTP that quickly :-) Bye, bearophile
Aug 19 2009
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Bill Baxter wrote:On Wed, Aug 19, 2009 at 10:32 AM, div0<div0 users.sourceforge.net> wrote:While we're on the subject, is it possible to mixin in a tuple? Doesn't seem like you can... class C(M...) { mixin M; } Doesn't work. - -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iD8DBQFKjGJVT9LetA9XoXwRApgoAJ9hEW/qAJ5uCR+186MfH2ebDD1zIQCaA8RT jdlbCp7Tj0oX1BMievFVXOk= =GtuO -----END PGP SIGNATURE----------BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 downs wrote:Mixins can be used to do a lot (most? all?) of things CRTP is used for: class Class(alias MixMe) { mixin MixMe impl; ... void doSomething { impl.doIt(); } }bearophile wrote:No, template mixins are not CRTP.I don't know much C++. Can CRTP be used in D1 too, to improve the performance of some D1 code? http://en.wikipedia.org/wiki/Curiously_Recurring_Template_Pattern Bye, bearophileWe have this, except we call it "template mixin" :)
Aug 19 2009
div0 Wrote:import std.typetuple; class C(M...) { mixin TypeTuple!(M); }While we're on the subject, is it possible to mixin in a tuple? Doesn't seem like you can... class C(M...) { mixin M; } Doesn't work.
Aug 20 2009
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 John C wrote:div0 Wrote:Unfortunately that doesn't work. It stops the immediate compile error, but the mixin doesn't do anything: template a() { void funcA() { writefln("funcA"); } } template b() { void funcB()() { writefln("funcB"); } } class theClass0(M ...) { mixin TypeTuple!(M); } void test() { auto g = new theClass0!(a, b); g.funcA(); g.funcB(); } main.d(31): Error: no property 'funcA' for type 'main.theClass0!(a,b).theClass0' main.d(31): Error: function expected before (), not __error of type int main.d(32): Error: no property 'funcB' for type 'main.theClass0!(a,b).theClass0' main.d(32): Error: function expected before (), not __error of type int main.d(33): Error: no property '_a' for type 'main.theClass0!(a,b).theClass0' main.d(33): Error: no property '_b' for type 'main.theClass0!(a,b).theClass0' Same with both 1.046 & 2.031 TY though. - -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iD8DBQFKjaDUT9LetA9XoXwRAoo6AKDBMZC2Sc59UfDof1SSM5G9i9en6QCgk2DJ 1FMBI1zOfbuokGDx2M4ahDg= =wDXk -----END PGP SIGNATURE-----While we're on the subject, is it possible to mixin in a tuple? Doesn't seem like you can... class C(M...) { mixin M; } Doesn't work.import std.typetuple; class C(M...) { mixin TypeTuple!(M); }
Aug 20 2009
On Thu, Aug 20, 2009 at 12:15 PM, div0<div0 users.sourceforge.net> wrote:-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 John C wrote:This doesn't work either: class C(M) { mixin M; } template Foo() { void blarf() {} } auto x =3D new C!(Foo); x.blarf; because a parameter that is itself a template needs to be an alias template argument like so: class C(alias M) { ... } As far as I know you can't have an alias variadic argument or pass template aliases to a regular variadic template arg. --bbdiv0 Wrote:Unfortunately that doesn't work. It stops the immediate compile error, but the mixin doesn't do anything:While we're on the subject, is it possible to mixin in a tuple? Doesn't seem like you can... class C(M...) { =A0mixin M; } Doesn't work.import std.typetuple; class C(M...) { =A0 mixin TypeTuple!(M); }
Aug 20 2009
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Bill Baxter wrote:On Thu, Aug 20, 2009 at 12:15 PM, div0<div0 users.sourceforge.net> wrote:Yup, bit of a silly limitation. I found a way to do it using our old friend: string mixins class C(string mixins) { mixin (mixins); } auto c = new C!("mixin a; mixin b;"); It's bloody ugly though. I suppose you could add some compile time functions to clean up the arg list a bit so you could do: class C(string mixins) { mixin (CrackMixins!(mixins)); } new C!("a;b"); but it's not as convenient as MI and requires class C to be written with that use in mind. - -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iD8DBQFKjcKUT9LetA9XoXwRApBMAJ0bkFN9zFmxyIhgAUgKy1q8I3aScQCggr1Q UNLHcjpF/uXbwm7+CtzsDzk= =krsn -----END PGP SIGNATURE----------BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 John C wrote:This doesn't work either: class C(M) { mixin M; } template Foo() { void blarf() {} } auto x = new C!(Foo); x.blarf; because a parameter that is itself a template needs to be an alias template argument like so: class C(alias M) { ... } As far as I know you can't have an alias variadic argument or pass template aliases to a regular variadic template arg. --bbdiv0 Wrote:Unfortunately that doesn't work. It stops the immediate compile error, but the mixin doesn't do anything:While we're on the subject, is it possible to mixin in a tuple? Doesn't seem like you can... class C(M...) { mixin M; } Doesn't work.import std.typetuple; class C(M...) { mixin TypeTuple!(M); }
Aug 20 2009
Mixins can be used to do a lot (most? all?) of things CRTP is used for: class Class(alias MixMe) { mixin MixMe impl; ... void doSomething { impl.doIt(); } }where can I read about class parameters?
Aug 19 2009
On Wed, Aug 19, 2009 at 2:59 PM, Saaa<empty needmail.com> wrote:It's just a class template. Short for template Class(alias MixMe) { class Class { .... } } See http://www.digitalmars.com/d/2.0/template.html under "Class Templates". --bbMixins can be used to do a lot (most? all?) of things CRTP is used for: class Class(alias MixMe) { =A0 mixin MixMe impl; =A0 ... =A0 void doSomething { =A0 =A0 =A0 =A0 impl.doIt(); =A0 } }where can I read about class parameters?
Aug 19 2009
"Bill Baxter" <wbaxter gmail.com> wrote in message news:mailman.344.1250719235.14071.digitalmars-d-learn puremagic.com... On Wed, Aug 19, 2009 at 2:59 PM, Saaa<empty needmail.com> wrote:It's just a class template. Short for template Class(alias MixMe) { class Class { .... } } See http://www.digitalmars.com/d/2.0/template.html under "Class Templates". --bb Thanks ;)Mixins can be used to do a lot (most? all?) of things CRTP is used for: class Class(alias MixMe) { mixin MixMe impl; ... void doSomething { impl.doIt(); } }where can I read about class parameters?
Aug 19 2009