Thank you, Martin_fr, for your insightful feedback. My proposal is about memory management of the objects and not their specific features.
But that is a really minor usecase...
The more important memory management becomes, the more an app should avoid throw-away instances. But without throw away instances it becomes all about resource management. And those are done via the features like close/disconnect/...
That is why there are things like pools for resource using objects. But in order to return them, the using code must finish up its work.
That said, yes a lot of code uses throw away instances when it really shouldn't. But should that be encouraged?
Looking at your proposal, I think it goes way to far from what we have.
The top part (calling constructors) really does not need anything that try/finally does not have. And try/finally does not make it more complex... In the case that a constructor can fail
a := nil; b:= nil; //....
try
a:= foo.create;
b:= foo.create;
//....
Yes, it is some extra nil assignments. But for something that is a rare special case, those are not enough to justify a new feature....
For the finally block, there is a difference.
You need a finally block, that does execute each statement (or top level compound statement) in it.
Currently finally only runs to the first failure. And wrapping each line in finally into its own try/except or try/finally.... Well, there is a case that might have enough value to discuss.
So then, if anything at all
try
//...
finally do each
b.free;
a.free;
begin foo; bar; end; // one statement, not guarding the sub statements
end;
And it would run each of the statement, even if any one in there raises an exception
Of course it creates the
same problem like your proposal.
If there is an exception in each of the destructors, then at the end of that block you have several exceptions... Which one to raise for the code that follows?