Marc C has the right approach for trying to emulate this in straightforward procedural code.
However, IMHO, this structure is basically a way to hack the Java OO system, for those who want one of the structural advantages of OO in old-fashioned procedural programming. Even in Java, you are almost always better at creating the right class.
Therefore, I donโt think it is too much to say that the right way to get this functionality in Ada would be to create a suitable object and make your object a child of Ada.Finalization.Controlled .
If you donโt want to worry about creating a real object, you can simply create dummy code, put the completion code in it and declare it on the stack at the top of the block for which you want to run it. The disadvantage of this is that the controlled types themselves (at least the last time I used them) must be declared in the package level area. In this case, you will not be able to directly refer to objects with a lower ad. They said they were going to fix it in a future revision of the language, but I have not tried it lately to make sure they did.
TED
source share