-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Darren New wrote:
> Christopher Smith wrote:
>> using (Foo foo = new Foo(...))
>> {
>> ...
>> }
>>
>> What's not reliable there? Either foo gets initialized or it doesn't.
>
> But Foo can get halfway through initialization, throw an error, and not
> be finalized by the time you leave the function.
First of all, "Foo" is a class, so it's is the construction of an
instance of Foo that gets half way through initialization. Yes, it could
throw an error, in which case "foo" never gets assigned to and then the
instance that might have been never was and no finalization happens.
This is both entirely reliable and exactly what you want to have happen.
> using (Foo foo = new ComplexSqlSocket()) { .... }
> CrashIfSqlSocketStillOpen();
>
> If ComplexSqlSocket calles {new SqlSocket()} in its constructor, and
> then the constructor throws, neither ~SqlSocket() nor
> ~ComplexSqlSocket() gets invoked before you get to the
> CrashIfSqlSocketStillOpen.
Yes, that would be true, but of course the SqlSocket and
ComplexSqlSocket constructors are responsible for handling that case and
are at liberty to employ "using" or try/catch blocks to do so.
> In C++, it does, using the RIAA idiom. If a constructor throws after
> having constructed subcomponents, it deconstructs the subcomponents
> properly for you.
Yes, I quite like that about C++. I hadn't realized this about C#, but
then again, C# constructors tend to do their work in the constructor
body rather than in the initialization block. What happens if you have a
derived class that successfully invokes the its base class constructor
but later throws an exception in the constructor body?
>> If it does, then finalize() is reliably called. If it doesn't, then
>> finalize() is reliably *not* called. Either way, entirely reliable.
>
> Well, yes. It's stupidly defined to not do what you'd expect, which is
> that you always call the finalizer if the initializer completes.
Hopefully when Java adopts its own version of using, it will learn from
C#'s experiences.
- --Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFGbhcgOagjPOywMBARAgxIAKC1Wgieph55UL5ddr6TCbBSxyr+MwCgyEb7
kjr3Vk4oZicVi2YxoCS/UH0=
=ySrh
-----END PGP SIGNATURE-----
--
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-lpsg