comp.lang.ada
 help / color / mirror / Atom feed
From: Jere <jhb.chat@gmail.com>
Subject: Re: Mixing public and private member of a class. Dealing with what to hide and not
Date: Wed, 13 Feb 2019 16:21:51 -0800 (PST)
Date: 2019-02-13T16:21:51-08:00	[thread overview]
Message-ID: <f635a481-ed51-4cba-a8b1-fca6dbd3670e@googlegroups.com> (raw)
In-Reply-To: <5e42642d-3dd4-4c53-8b24-50bde70485f8@googlegroups.com>

On Wednesday, February 13, 2019 at 12:40:24 PM UTC-5, Daniel wrote:
> <SNIPPED>
> 
> 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

  parent reply	other threads:[~2019-02-14  0:21 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-13 17:40 Mixing public and private member of a class. Dealing with what to hide and not Daniel Norber
2019-02-13 17:46 ` Daniel
2019-02-13 20:05 ` Dmitry A. Kazakov
2019-02-13 20:39 ` J-P. Rosen
2019-02-14  0:21 ` Jere [this message]
2019-02-14 18:04 ` G.B.
2019-02-22  8:42 ` Daniel
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox