CRTP in D?

div0 div0 at users.sourceforge.net
Thu Aug 20 14:39:32 PDT 2009


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Bill Baxter wrote:
> On Thu, Aug 20, 2009 at 12:15 PM, div0<div0 at users.sourceforge.net> wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> John C wrote:
>>> div0 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.
>>> import std.typetuple;
>>>
>>> class C(M...) {
>>>
>>>   mixin TypeTuple!(M);
>>>
>>> }
>> Unfortunately that doesn't work.
>>
>> It stops the immediate compile error, but the mixin doesn't
>> do anything:
> 
> 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.
> 
> --bb

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-----


More information about the Digitalmars-d-learn mailing list