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,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 2002:a24:9411:: with SMTP id j17mr627474ite.18.1550103711936; Wed, 13 Feb 2019 16:21:51 -0800 (PST) X-Received: by 2002:a9d:654f:: with SMTP id q15mr9372otl.6.1550103711403; Wed, 13 Feb 2019 16:21:51 -0800 (PST) Path: eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!weretis.net!feeder6.news.weretis.net!feeder.usenetexpress.com!feeder-in1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!p15no16060itb.0!news-out.google.com!r11ni68ita.0!nntp.google.com!u140no16582ita.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Wed, 13 Feb 2019 16:21:51 -0800 (PST) In-Reply-To: <5e42642d-3dd4-4c53-8b24-50bde70485f8@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=70.109.61.2; posting-account=QF6XPQoAAABce2NyPxxDAaKdAkN6RgAf NNTP-Posting-Host: 70.109.61.2 References: <5e42642d-3dd4-4c53-8b24-50bde70485f8@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: Mixing public and private member of a class. Dealing with what to hide and not From: Jere Injection-Date: Thu, 14 Feb 2019 00:21:51 +0000 Content-Type: text/plain; charset="UTF-8" Xref: reader01.eternal-september.org comp.lang.ada:55516 Date: 2019-02-13T16:21:51-08:00 List-Id: On Wednesday, February 13, 2019 at 12:40:24 PM UTC-5, Daniel wrote: > > > Any help or ideas of how to do it? (if it possible respecting 1 "with" dependency in the outter world side) > > Thank you very much and Best regards. After fixing syntax errors, the example you gave compiled fine in GCC 7.1.1 (there were quite a few syntax errors however). That said, I'm not a fan of the two stage extension method. I prefer one of two ways (depending on if the "public" component is limited or not: 1. Non-limited built in components: I just make them private but use getter and setter functions with the same name as the component. 2. Complex or limited components: I use "reference types" to get access to them. In order to make this work, the record component needs to be aliased. I'll give an example of this below. I'm using simple built in types for the example, but hopefully you can extrapolate the example to types like your notebook type. EXAMPLE: with Ada.Text_IO; use Ada.Text_IO; procedure Hello is package Thing is type Instance is tagged limited private; subtype Class is Instance'Class; -- This defines a reference type for a -- component that can be modified by -- a client package type Natural_Holder (Element : not null access Natural) is limited null record with Implicit_Dereference => Element; -- This function returns that reference -- type. Notice the name of the function -- matches the field (not required, but -- I like this nomenclature) function Value (Self : aliased in out Instance) return Natural_Holder; -- This defines a reference type for a -- component that cannot be modified by -- a client package. It is essentially -- constant type Constant_Character_Holder (Element : not null access constant Character) is limited null record with Implicit_Dereference => Element; -- This function returns that reference -- type. Notice the name of the function -- matches the field (not required, but -- I like this nomenclature) function Constant_Value (Self : aliased Instance) return Constant_Character_Holder; private type Instance is tagged limited record Value : aliased Natural := 0; Constant_Value : aliased Character := 'W'; end record; function Value (Self : aliased in out Instance) return Natural_Holder is (Element => Self.Value'Access); function Constant_Value (Self : aliased Instance) return Constant_Character_Holder is (Element => Self.Constant_Value'Access); end Thing; Something : Thing.Instance; begin Put_Line("Value =" & Natural'Image(Something.Value)); Something.Value := 23; Put_Line("Value =" & Natural'Image(Something.Value)); Put_Line("Constant_Value =" & Character'Image(Something.Constant_Value)); -- Fails to compile next line since the value is constant --Something.Constant_Value := 'C'; end Hello; RM section on user defined references: http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-4-1-5.html#I2450