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-Thread: 103376,12a63150f4f961a X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,UTF8 Received: by 10.68.211.136 with SMTP id nc8mr6766656pbc.6.1336157334512; Fri, 04 May 2012 11:48:54 -0700 (PDT) Path: pr3ni3976pbb.0!nntp.google.com!news1.google.com!goblin2!goblin.stu.neva.ru!news.internetdienste.de!news.tu-darmstadt.de!news.belwue.de!rz.uni-karlsruhe.de!feed.news.schlund.de!schlund.de!news.online.de!not-for-mail From: Felix Krause Newsgroups: comp.lang.ada Subject: Re: OOP in Ada: Alternatives for "protected" scope Date: Fri, 4 May 2012 20:48:53 +0200 Organization: 1&1 Internet AG Message-ID: References: <1vn9nv4aqm9a3.1tqzc91idjlbs$.dlg@40tude.net> NNTP-Posting-Host: port-92-203-3-111.dynamic.qsc.de Mime-Version: 1.0 X-Trace: online.de 1336157334 15510 92.203.3.111 (4 May 2012 18:48:54 GMT) X-Complaints-To: abuse@einsundeins.com NNTP-Posting-Date: Fri, 4 May 2012 18:48:54 +0000 (UTC) User-Agent: Unison/2.1.7 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Date: 2012-05-04T20:48:53+02:00 List-Id: On 2012-05-03 21:56:51 +0000, Dmitry A. Kazakov said: > > If Do_Something varies in children they should override it. The reusable > parts common for all children can be factored out as a private class-wide > operation. Compared to my version (with redispatching issues fixed), this alternative enforces a weaker contract. It does not enforce the child classes to call Do_Prologue and Do_Epilogue when implementing Do_Something. While this fixes the problem that I cannot have an abstract method in the private part, I think the mix-in solution you suggested in your second post is a better way to handle this. > All children having access to private operations must be declared in > children packages. > > package P is > type A is abstract tagged private; > procedure Do_Something (Object : in out A) is abstract; > private > ... > procedure Do_Prologue (Object : in out A'Class); > procedure Do_Epilogue (Object : in out A'Class); > end P; > > package P.Q is > type B is new A with private; > overriding procedure Do_Something (Object : in out B); > ... > end P.Q; > > package body P.Q is > procedure Do_Something (Object : in out B) is > begin > Do_Prologue (Object); > ... -- Do specific calculations > Do_Epilogue (Object); > end Do_Something; > end P.Q; > >> In languages like Java and C#, there is a "protected" scope to cope >> with this situation. > > This is exactly what Ada's private is. The correspondence goes as follows: > > "Public" <-> Ada' public operation > "Protected" <-> Ada's private operation > "Private" <-> An operation defined in the package body So, to be able to use these visibility scopes, the class hierarchy must follow the layout of the package hierarchy. I think this is a disadvantage for some reasons: * Given the scenario that you use an Ada software library containing tagged types, you have to subclass these types in child-packages within the package layout of the library. I for myself would rather want to place my code in the package hierarchy of my application. * It doesn't work with generics: generic -- ... package P is type A is tagged private; -- … end P; package Q is package P_Instance is new P (...); type B is new P_Instance.A with private; end P.Q.R; Any non-generic tagged type derived from an instance of a generic parent type cannot access that type's "protected" methods - as all child packages of a generic package also must be generic.