Encapsulate arguments
alex burton
alexibu.remove at me.com
Wed Jan 1 15:00:33 PST 2014
struct Init
{
Factory * factory;
Other arg1;
Another arg2;
};
void foo(const Init& init);
void bar(const Init& init)
{
foo(init);
other stuff;
};
bar(Init(factory,other args));
In C++ I often wrap some arguments that are common to multiple
functions in a struct.
This idiom can reduce typing when the args change.
It is particularly useful in testable code for constructors and
factory methods.
It adds minimal overhead and is really just syntax.
In D there are no header files to change making it slightly less
useful. In D you dont need to write the trivial ctor for Init
making it easier.
In D this would be void bar(in Init init) which makes init const
making all of the parts of init const. This means this idiom
cannot be applied as often args will be refences to mutable
objects.
Does anyone know an equivalent idiom for D ?
More information about the Digitalmars-d
mailing list