May 16, 2004
I have this mostly implemented. It's been based on a lot of your suggestions. The way I did it is, I think, pretty unexplored territory. That means there may be some pretty cool uses for it I've never thought of. Let me know what you think, and if I missed the boat completely <g>.

www.digitalmars.com/d/mixin.html


May 16, 2004
Walter wrote:

>I have this mostly implemented. It's been based on a lot of your
>suggestions. The way I did it is, I think, pretty unexplored territory. That
>means there may be some pretty cool uses for it I've never thought of. Let
>me know what you think, and if I missed the boat completely <g>.
>
>www.digitalmars.com/d/mixin.html
>  
>
Wow, this is really nice.

-- 
-Anderson: http://badmama.com.au/~anderson/
May 16, 2004
>www.digitalmars.com/d/mixin.html
Coooool!!

Is it possible to write something like this? :

template Foo() {}
class Bar(alias Tmpl)
{
mixin Tmpl;
}

Bar!(Foo) x;

k.inaba
May 16, 2004
"k.inaba" <k.inaba_member@pathlink.com> wrote in message news:c879v9$15fg$1@digitaldaemon.com...
> >www.digitalmars.com/d/mixin.html
> Coooool!!
>
> Is it possible to write something like this? :
>
> template Foo() {}
> class Bar(alias Tmpl)
> {
> mixin Tmpl;
> }
>
> Bar!(Foo) x;

Yes <g>.


May 16, 2004
Walter wrote:
> I have this mostly implemented. It's been based on a lot of your
> suggestions. The way I did it is, I think, pretty unexplored territory. That
> means there may be some pretty cool uses for it I've never thought of. Let
> me know what you think, and if I missed the boat completely <g>.
> 
> www.digitalmars.com/d/mixin.html

Looks good so far. But two things:

First: there is a typo at the top.

mixin TemplateIdentifier (! TemplateArgumentList ) ;
mixin TemplateIdentifier (! TemplateArgumentList ) MixinIdentifier ;

should be (judging from the examples):

mixin TemplateIdentifier !( TemplateArgumentList ) ;
mixin TemplateIdentifier !( TemplateArgumentList ) MixinIdentifier ;

For a moment I thought you'd have to use !( for templates and (! for mixins. I had already started to formulate a flame mail in my head ;).


The second thing is a question. Can mixins mix-in other mixins? I.e.

template Mix1(T)
{
	void foo(T a) { return 2*a; }
}

template Mix2(T)
{
	mixin Mix1!(T);

	void bar(T a) { return foo(a); }
}


And another thing comes to my mind: can you mixin all kinds of templates? Including specialized ones?


Hauke
May 16, 2004
Walter wrote:

> I have this mostly implemented. It's been based on a lot of your
> suggestions. The way I did it is, I think, pretty unexplored territory. That
> means there may be some pretty cool uses for it I've never thought of. Let
> me know what you think, and if I missed the boat completely <g>.
> 
> www.digitalmars.com/d/mixin.html
> 
> 

Oh yeah, forgot to mention: THANKS very much for implementing this! I think this is a kick-ass feature!

Hauke
May 16, 2004
so i guess this means that structs can have inheritance now (in a way)? Thats
great.

In article <c876sh$10r7$1@digitaldaemon.com>, Walter says...
>
>I have this mostly implemented. It's been based on a lot of your suggestions. The way I did it is, I think, pretty unexplored territory. That means there may be some pretty cool uses for it I've never thought of. Let me know what you think, and if I missed the boat completely <g>.
>
>www.digitalmars.com/d/mixin.html
>
>


May 16, 2004
Walter wrote:

> I have this mostly implemented. It's been based on a lot of your suggestions. The way I did it is, I think, pretty unexplored territory. That means there may be some pretty cool uses for it I've never thought of. Let me know what you think, and if I missed the boat completely <g>.
> 
> www.digitalmars.com/d/mixin.html

Cool! Will something like this work:

// helper to make an array "literal" with value semantics
// Example:
//   uintn!(3)(100,200,300)
struct InlineArray(T,int N) {

  static .InlineArray!(T,N) opCall(T x0,...) {
    .InlineArray!(T,N) res;
    res.array[] = (&x0)[0..N][];
    return res;
  }

  mixin CommonInlineArray!(T,N); // add generic N-D members
}

struct InlineArray(T,int N:2) {

  // avoid using varargs for safety and speed
  static .InlineArray!(T,N) opCall(T x0, T x1) {
    .InlineArray!(T,N) res;
    res.array[0] = x0;
    res.array[1] = x1;
    return res;
  }

  T x() { return array[0]; }
  void x(T val) { array[0] = val; }
  T y() { return array[1]; }
  void y(T val) { array[1] = val; }

  mixin CommonInlineArray!(T,N); // add generic N-D members
}

// InlineArray members that are independent of dimension
private template CommonInlineArray(T,int N) {
  T[N] array;

  static .InlineArray!(T,N) opCall(T[N] x) {
    .InlineArray!(T,N) res;
    res.array[] = x[];
    return res;
  }

  T opIndex(int i) {
    return array[i];
  }

  void opIndex(int i, T val) {
    array[i] = val;
  }
  // todo: arithmetic, cmp, etc
}

template intn(N) { alias InlineArray!(int,N) uintn; }

int main() {
 intn!(2) p = intn!(2)(4,5);
 printf("x=%d y=%d array=%p\n", p.x, p[1], p.array);
}

May 16, 2004
"Hauke Duden" <H.NS.Duden@gmx.net> wrote in message news:c87gmb$1f8j$1@digitaldaemon.com...
> First: there is a typo at the top.
>
> mixin TemplateIdentifier (! TemplateArgumentList ) ;
> mixin TemplateIdentifier (! TemplateArgumentList ) MixinIdentifier ;
>
> should be (judging from the examples):
>
> mixin TemplateIdentifier !( TemplateArgumentList ) ;
> mixin TemplateIdentifier !( TemplateArgumentList ) MixinIdentifier ;
>
> For a moment I thought you'd have to use !( for templates and (! for
> mixins. I had already started to formulate a flame mail in my head ;).

Fixed.

> The second thing is a question. Can mixins mix-in other mixins? I.e.
>
> template Mix1(T)
> {
> void foo(T a) { return 2*a; }
> }
>
> template Mix2(T)
> {
> mixin Mix1!(T);
>
> void bar(T a) { return foo(a); }
> }

Yes!

> And another thing comes to my mind: can you mixin all kinds of templates? Including specialized ones?

Yes, just like for regular template instantiations.


May 16, 2004
"Hauke Duden" <H.NS.Duden@gmx.net> wrote in message news:c87go0$1f8j$2@digitaldaemon.com...
> Walter wrote:
>
> > I have this mostly implemented. It's been based on a lot of your suggestions. The way I did it is, I think, pretty unexplored territory.
That
> > means there may be some pretty cool uses for it I've never thought of.
Let
> > me know what you think, and if I missed the boat completely <g>.
> >
> > www.digitalmars.com/d/mixin.html
> >
> >
>
> Oh yeah, forgot to mention: THANKS very much for implementing this! I think this is a kick-ass feature!

You're welcome! Now, I want to see a kick-ass use for this, so I can write a magazine article about it! A use that is cool, not easilly expressed in other languages, and so would highlight the capabilities of D.


« First   ‹ Prev
1 2 3 4
Top | Discussion index | About this forum | D home