Decision on container design
Tomek Sowiński
just at ask.me
Sat Jan 29 02:48:26 PST 2011
Michel Fortin napisał:
> > Is there anything implementation specific in the outer struct that provides
> > ref semantics to Impl? If not, Container could be generic, parametrized by
> > Impl type.
>
> You could provide an implementation-specific version of some functions
> as an optimization. For instance there is no need to create the Impl
> when asking for the length, if the pointer is null, length is zero.
> Typically, const function can be implemented in the outward container
> with a shortcut checking for null.
I think the reference struct can still be orthogonal to the container.
struct Ref(Impl)
{
private Impl* _impl;
ref Impl impl() @property
{
if (!impl) impl = new Impl;
return *impl;
}
static if (hasLength!Impl)
{
auto length() @property
{
return impl ? impl.length : 0;
}
}
alias impl this;
}
Reusability lightens the burden of the container's author (less fuss for user implementations) and somewhat standardizes containers as they all must exhibit a certain API with certain semantics to be able to fit into Ref.
The downside is that the syntax for the most common case (ref semantics) is a little nosier than for value-like behavior.
--
Tomek
More information about the Digitalmars-d
mailing list