August 19, 2009
On Wed, Aug 19, 2009 at 2:59 PM, Saaa<empty@needmail.com> 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();
>>   }
>> }
>>
>
> where can I read about class parameters?

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
August 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:
>
>> 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?

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


August 20, 2009
bearophile Wrote:

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

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(){...}
}

August 20, 2009
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);

}
August 20, 2009
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:

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
August 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:
>> 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
August 20, 2009
Bill Baxter wrote:
> On Thu, Aug 20, 2009 at 12:15 PM, div0<div0@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
1 2
Next ›   Last »