struct mixins

Leandro Lucarella llucax at gmail.com
Mon Nov 16 10:03:58 PST 2009


Steven Schveighoffer, el 16 de noviembre a las 10:44 me escribiste:
> On Mon, 16 Nov 2009 10:32:31 -0500, Leandro Lucarella
> <llucax at gmail.com> wrote:
> 
> >What if one could use a struct directly with mixin?
> 
> This is a very good idea.  BTW, I think conflicts should be a
> compile-time error.

I think compile-time error is nice and clean, but you are lost if you're
using a third-party struct, and the maintainer adds a new field that
collides with yours.

Example:

struct ThirdParty {
	int x;
}

struct Mine {
	mixin ThirdParty;
	int y;
}

void myCode(Mine m) {
	m.y = 1;
}

If ThirdParty is changed like this:

struct ThirdParty {
	int x;
	int y;
}

Then your code don't compile anymore and you can't fix it. Either you stop
using mixin ThirdParty or you have to change your struct and *all* the
code written for it.

On the other hand, if you allow explicit disambiguation, you code still
compiles and work as expected. You just won't use the new y variable from
ThirdParty (you didn't use it before either, so that's OK). If you need to
start using the new y attribute from ThirdParty, you can do it with:

void myCode(Mine m) {
	m.y = m.ThirdParty.y;
}


-- 
Leandro Lucarella (AKA luca)                     http://llucax.com.ar/
----------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------
Yeah, I'm a great quitter. It's one of the few things I do well. I come from a
long line of quitters. My father was a quitter, my grandfather was a quitter...
I was raised to give up.
	-- George Constanza



More information about the Digitalmars-d mailing list