comp.lang.ada
 help / color / mirror / Atom feed
From: Shark8 <onewingedshark@gmail.com>
Subject: Re: Calling a record type's methods (functions or procedure) when record is in an array
Date: Tue, 21 Jan 2020 12:51:45 -0800 (PST)
Date: 2020-01-21T12:51:45-08:00	[thread overview]
Message-ID: <6dbb0da9-8c68-435e-945d-d0e1eefeda4c@googlegroups.com> (raw)
In-Reply-To: <e99b708d-d0bd-4ae6-b099-3bcfb9cadc77@googlegroups.com>

On Saturday, January 18, 2020 at 11:02:32 AM UTC-7, Mace Ayres wrote:
> 
> In your code below with my mark up “ * ?”,  are the sub programs private in any way since they are below Type Thing is private, or the privacy is applied to Thing?

Ada has a concept called private types, "TYPE X IS PRIVATE;", where the type's implementation is hidden from the clients of the package. Thus you could have the following:

Package Example is
   Type Point is private;
  
   -- Getters:
   Function  X( Object : in     Point ) return Integer;
   Function  Y( Object : in     Point ) return Integer;
  
   -- Setters:
   Procedure X( Object : in out Point; Value : in     Integer );
   Procedure Y( Object : in out Point; Value : in     Integer );
Private
  
   Type Point is record
     X_Value, Y_Value : -- Integer;
                         Real;
   End record;
End Example;

Package Body Example is
   Function  X( Object : in     Point ) return Integer is
   Begin
    Return Integer(Object.X_Value);
   End X;
  
   Procedure X( Object : in out Point; Value : in     Integer )
   Begin
    Object.X_Value:= Real( Value );
   End X;

   Function  Y( Object : in     Point ) return Integer is
   Begin
    Return Integer(Object.Y_Value);
   End Y;
  
   Procedure Y( Object : in out Point; Value : in     Integer ) is
   Begin
    Object.Y_Value:= Real( Value );
   End Y;

End Example;

And now, you could change the type of the components to Point to Integer, remove the type-casts, and recompile the package Example **without having to recompile dependent packages** because they are dependent only on the visible portion of the package.

> 
> That is, what is the full scope of privacy in declaring type thing as private, to type thing only,  or the sub programs too. Or is this a confused question anyway?

See above.
Private types are Ada's method of hiding implementation-details. For example, you could have a Password type that, internally, is a string but none of the package-clients can see that:

Package Other_Example is
  Min_Length : Constant Natural;
  Function Valid_Characters( Input : String ) return Boolean;
  
  Type Password(<>) is private;
  
  
  Function Create( Input : String ) return Password
   with Pre => Valid_Characters(Input) and Input'Length >= Min_Length;
  Function "="( Left, Right : Password ) return Boolean;
  Function "="( Object : Password; Value : String ) return Boolean;
  
Private
  
  -- A Password must be at least 5 characters,
  -- and may contain only alphanumeric characters.
  Min_Length : Constant Natural:= 5;
  Function Valid_Characters( Input : String ) return Boolean is
  (for all C of Password => C in 'a'..'z'|'A'..'Z'|'0'..'9');
  
  Type Password is new String
    with Dynamic_Predicate => Password'Length >= Min_Length
        and then Valid_Characters( String(Password) );
  
    Function Create( Input : String ) return Password is
    ( Password(Input) );
End Other_Example;


  parent reply	other threads:[~2020-01-21 20:51 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-17 23:30 Calling a record type's methods (functions or procedure) when record is in an array Mace Ayres
2020-01-18 12:31 ` Simon Wright
2020-01-18 18:02 ` Mace Ayres
2020-01-18 20:53   ` Simon Wright
2020-01-21 20:51   ` Shark8 [this message]
2020-01-21 23:17     ` Jeffrey R. Carter
2020-01-23 15:00     ` joakimds
2020-01-23 15:02       ` joakimds
2020-01-23 16:51         ` Simon Wright
2020-01-24  9:47           ` joakimds
2020-01-23 20:15       ` Optikos
2020-01-19 15:06 ` Mace Ayres
replies disabled

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