From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,d8a4797a79f9c90f X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-05-28 14:47:42 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news.uchicago.edu!newsswitch.lcs.mit.edu!nntp.TheWorld.com!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: I/O - exception handling Date: 28 May 2003 17:47:39 -0400 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls4.std.com 1054158459 21180 199.172.62.241 (28 May 2003 21:47:39 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Wed, 28 May 2003 21:47:39 +0000 (UTC) User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 Xref: archiver1.google.com comp.lang.ada:37949 Date: 2003-05-28T17:47:39-04:00 List-Id: Hyman Rosen writes: > Sergey Koshcheyev wrote: > > I myself miss constructors and destructors in Ada a lot. > > Well, Ada has them, albeit in an inconvenient form. You need a > type which inherits from Controlled, and that type must be > declared at library level for some safety reason that Ada folks > appreciate. > > I don't know if there's a way to use access discriminants in > combination with generics to get a "universal" cleanup object > that could forward to a function. Ada experts? The problem is that finalization code has to be at library level, so it can't easily get its hands on local objects. A solution/workaround is to declare a Limited_Controlled type with an access discriminant, and make that discrim point at the local object. It's kind of ugly, but it's exception-safe and abort-safe. You have to make the local object aliased. And if the "local object" is a parameter (which it often is, in my experience), then you have to make the type tagged so it will be aliased, or you have to copy the param into a local aliased object. And you have to use 'Unchecked_Access, even though there is little danger of creating dangling pointers. I suppose you could wrap this "design pattern" in a generic, but I doubt if it would be pretty. Note that a generic instantiation must be at library level if it contains a controlled type. - Bob