[cap-talk] Composability and RAII
William ML Leslie
william.leslie.ttg at gmail.com
Sun Mar 6 22:28:44 PST 2011
On 7 March 2011 17:03, Rob Meijer <capibara at xs4all.nl> wrote:
> The problem with finaly/with in these cases comes with the fact that for
> the finaly/with solution being a resource that requires this solution to
> be used is transitive to composition. David-Sarah argues that this
> transitive property is correct, I argue that it brakes encapsulation.
> For these cases C++ stile RAII allows for full encapsulation of the
> knowledge of the presence of deep resources.
It does break encapsulation if you implement it as an IDisposable that
delegates dispose to its attributes; but that doesn't necessarily
solve the problems we are talking about - such as correctly sequencing
the release of resources. I don't think anyone here is advocating
that as a one-size-fits-all solution. It doesn't work when you want
to return one of those attributes from the function that will dispose
of this object, the example I gave.
>> It is the other cases that are interesting.
> The other cases are where C++ RAII also breaks down. I tried to argue that
> cPython (reference counted GC) shows that a combination of reference
> counted GC with RAII can be a good solution there.
It works for a certain subset of resource usage, and file usage is one
case where it has worked fairly well traditionally. But, as the zen of
python says, explicit is better than implicit. And especially where
the GC is concerned, because programmers have difficulty reasoning
about its behaviour in even small projects.
More information about the cap-talk