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.1 required=5.0 tests=BAYES_00, PP_MIME_FAKE_ASCII_TEXT autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII X-Google-Thread: 103376,ad988eb0a9545c86 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-04-17 02:11:35 PST Path: supernews.google.com!sn-xit-02!supernews.com!news.gv.tsc.tdk.com!news.iac.net!news-out.cwix.com!newsfeed.cwix.com!newsfeed.icl.net!news-x.support.nl!isdnet!195.25.12.36.MISMATCH!oleane.net!oleane!nnrp.oleane.net!not-for-mail From: "Jean-Pierre Rosen" Newsgroups: comp.lang.ada Subject: Re: Problem trying to implement generics. Date: Tue, 17 Apr 2001 10:50:33 +0200 Organization: Adalog Message-ID: <9bh0uk$3rc$1@s1.read.news.oleane.net> References: <9b46dr$cd8$1@taliesin.netcom.net.uk> <9b6jtu$4is$2@taliesin.netcom.net.uk> <9b6m27$68e$1@taliesin.netcom.net.uk> <0JBB6.10484$FD1.1197250@news6-win.server.ntlworld.com> <9b7tce$laf$2@taliesin.netcom.net.uk> NNTP-Posting-Host: mailhost.axlog.fr X-Trace: s1.read.news.oleane.net 987498260 3948 195.25.228.57 (17 Apr 2001 09:04:20 GMT) X-Complaints-To: abuse@oleane.net NNTP-Posting-Date: Tue, 17 Apr 2001 09:04:20 +0000 (UTC) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2919.6700 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6700 Xref: supernews.google.com comp.lang.ada:6941 Date: 2001-04-17T10:50:33+02:00 List-Id: "Brian Rogoff" a �crit dans le message news: Pine.BSF.4.21.0104131658480.17102-100000@shell5.ba.best.com... > > Another workaround is to declare a limited type, with a component that > > always points to itself: > > > > type T is tagged limited > > record > > Self_Reference: T_Ptr := T'Unchecked_Access; > > ... > > end record; > > > > If you pass this thing as an 'in' parameter to a function, the function > > can modify it by following the Self_Reference pointer. This works > > because no constant objects of type T can exist. It seems a lot like > > "cheating", doesn't it? > Actually, my version is slightly more sophisticated than that . It uses an access discriminant, avoiding the need for Unchecked_Access. See http://www.adapower.com/lang/modifyin.html If you consider that functions should not modify their parameters, then any trick that allows you to modify parameters is "cheating". Now, there are two nice things with this trick: 1) various rules of the language make sure that it can be used only where it is safe (no dangling pointers, no pointers to the wrong thing, etc.). 2) It requires a special structure in the type itself. Therefore, it's not the function that decides to modify the parameter, it is the parameter that tells the function that it accepts to be modified. It's like the designer ("owner") of the type putting some special notice: "by exception to the rule, I allow functions to modify this data". I think it removes most of the "evil" of functions modifying parameters. > Did anyone see this during the Ada 95 design? I thought J.P. Rosen (who > showed most of us this evil trick :) said something about having noticed > it early but never actually implementing it until later. I'm sure I'm > botching the story: J.P, what's the history here? > I realized this possibility initially during a review meeting of tests for access discriminants (I was a member of the ACVC/9X reviewers team). Did I mention it at that time ? I don't remember, but if I did, it apparently went unnoticed. Then we had that discussion about functions-modifying-parameters on c.l.a, and I took the opportunity to address a wider audience... BTW: one of the fascinating things with Ada is that you discover every day paradigms, design patterns, whatever, that show that the language is even *more* powerful than you thought. You think "wow, this is allowed by the language. Could it possibly work?". You try it, and it's OK. Well, I can't resist. One of my favorite ones (back in Ada83 times) is when you have a common treatment for all exceptions, then you want to dispatch according to the exception actually raised. For example, you have a local file open, and you want to close it in case of an exception, then do something. procedure proc is F : File_Type; begin ... Open (F, ...); ... exception when others => if Is_Open (F) then Close (F); end if; begin raise; -- Reraise current exception exception when Constraint_Error => ... when Data_Error => ... ... end; end Proc; -- --------------------------------------------------------- J-P. Rosen (rosen@adalog.fr) Visit Adalog's web site at http://www.adalog.fr