> On 8/9/11 12:25 AM, Jonathan M Davis wrote:
> >> On 8/8/11 10:41 PM, Jonathan M Davis wrote:
> >>> In this particular example, what goes in initFromParameters is specific
> >>> to Foo anyway. So, you'd have to write it by hand regardless.
> >> 
> >> If Andrei intended his answer this way, I don't quite see how it would
> >> be an appropriate solution, as Andrej explicitly stated in his original
> >> post: »I see that as needlessly implementing in user-code what the
> >> compiler can already do on its own.«
> > 
> > [snip ginormous reply]
> 
> So you assumed initFromParameters would contain the initialization code
> for sum and average? In this case, using a mixin doesn't make any sense
> in the first place to me, since it would indeed be very specific to Foo
> and could be just a private function…

Well, it has to include that code or there was no point in creating the 
constructor in the first place, since as Andrej notes, Foo(1, 2, 3, 4) works 
withou declaring a constructor. So, all-in-all, I don't think that Andrei's 
suggestion works. If you mix in the entire constructor, then you can do it, 
but just doing a mixin inside of the constructor like Andrei suggested doesn't 
buy you anything over just putting it directly in the constructor as far as I 
can see, unless you have multiple constructors. But then the mixin probably 
wouldn't work anyway, since it was mixing in everything, not just the common 
stuff. And if all you're doing is the common stuff, then you might as well 
have a separate, private function to call.

So, it looks like the best solution to this is to use either a string mixin or 
a template mixin to mix in the entire constructor where it's given the number 
of member variables to put in the constructor's parameters and the function to 
be the "post constructor" to call after the member variables have been 
initialized from the function parameters.

- Jonathan M Davis

Reply via email to