Collections question

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Sat Nov 28 13:19:30 PST 2015


On Saturday, 28 November 2015 at 12:20:36 UTC, Timon Gehr wrote:
> 3. (Non-internal) factory function:
>
> auto c1 = myCollection(1,2,3);
> auto c2 = myCollection!int();
> auto c3 = c2; // refers to the same collection as c2

Yeah. In general, I prefer that approach. It's what we currently 
do with RedBlackTree. It's more flexible (e.g. it can infer the 
element type like we do with dynamic arrays) and less verbose. 
The only downside that I can think of is that it doesn't work as 
well in generic code that's creating a container (as in where the 
container type is a template argument), but that's not something 
that's done normally. And if the factory function is just making 
using templated constructors cleaner, then generic code that's 
constructing such a container can still use the constructors. It 
just wouldn't be as nice as using the factory function.

But for almost all cases, a non-internal factory function named 
after the container is less verbose and more flexible.

- Jonathan M Davis


More information about the Digitalmars-d mailing list