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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,bde430ece5c51468 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,UTF8 Path: g2news2.google.com!news1.google.com!goblin1!goblin.stu.neva.ru!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Tagged type more type safe than access to subprogram ? Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: Date: Wed, 28 Oct 2009 09:55:08 +0100 Message-ID: NNTP-Posting-Date: 28 Oct 2009 09:55:08 CET NNTP-Posting-Host: 85ca28bf.newsspool4.arcor-online.net X-Trace: DXC=>U6G;3F8GQ2NTD55K=4IUK8^;Jm2Cok9[6LHn;2LCV>7enW;^6ZC`4IXm65S@:3>?LHEIKFEZ63< X-Complaints-To: usenet-abuse@arcor.de Xref: g2news2.google.com comp.lang.ada:8824 Date: 2009-10-28T09:55:08+01:00 List-Id: On Wed, 28 Oct 2009 00:51:19 -0700 (PDT), Hibou57 (Yannick Duchêne) wrote: > Hello Ada Artists, > > I was reading the Ada 95 Quality and Style Guide, seeking for some > inspiration about a design / style doubt. > > I came into Chapter 5, “ CHAPTER 5: Programming Practices ”, > > 5.3.4 Subprogram Access Types says : >> You can achieve the same effect as access-to-subprogram types for >> dynamic selection by using abstract tagged types. You declare an >> abstract type with one abstract operation and then use an >> access-to-class-wide type to get the dispatching effect. This technique >> provides greater flexibility and type safety than access-to-subprogram types > Here : http://www.iste.uni-stuttgart.de/ps/ada-doc/style_guide/sec_5a.html#5.3.4 > > I agree about the “ greater flexibility” (I've recently meet such a > case), but I do not understand the “ and [greater] type safety ”. > > If it's Ok for me to assert that tagged type is a more flexible way > than access to subprogram, I do not see a case where access to > subprogram would be less type safe than tagged type. > > If there is something I do not understand, this may means I have > something to learn about it (the purpose of the question then). > > Does any one know a case which match this assertion ? An access to subprogram is a poor-man's closure. Let's ignore "access" part and consider a pure downward closure (as it should have been in Ada). I argue that a helper type with an abstract primitive subprogram is safer than a closure, both safer and type-safer. The general safety comes from the fact that a closure brings a context with it, which an object normally does not. An abstract type is better encapsulated and there is less chances to run into occasional side effects. Even if the side effects are desired I would argue that it is better and safer to limit them to the object rather than to the closure's context. The crucial point is that closure's effects are "there", while effects on the object are localized to the object's state, there are "here", at the call point. The type safety is gained through different types derived from the abstract parent. There might be no difference at the caller side, e.g. whether you mistakenly call to the closure C1 instead of C2, or mistakenly pass an instance of S1 instead of S2 (both from the S'Class). Yet it is type safer with regard to the parameters required to construct S2. Its constructor can have a different signature, so that the parameters you pass in order to create it were different from S1. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de