comp.lang.ada
 help / color / mirror / Atom feed
From: Mace Ayres <maceayres0@gmail.com>
Subject: Re: Calling a record type's methods (functions or procedure) when record is in an array
Date: Sat, 18 Jan 2020 10:02:31 -0800 (PST)
Date: 2020-01-18T10:02:31-08:00	[thread overview]
Message-ID: <e99b708d-d0bd-4ae6-b099-3bcfb9cadc77@googlegroups.com> (raw)
In-Reply-To: <b820f7d0-1b5e-4e1d-9dd1-3f873e2f13e1@googlegroups.com>

Thank you. I see, we can pass the record (type) thing as a parameter, along with any other parameters (like value) and then interact with the record ‘thing’  in sub programs. 

Since my objects (type of Thing) are only instantiated as elements in my array, thing_array, I can only/ would I address them like this ?

looping.. i
 looping  j
   set_attribute(my_thing_array(i,j).value, 6);
  ...
end loop;
end loop;

In this way I only access (not ACCESS type) my thing via set_attribute procedure and not with...  thing.value := 5, which is the direct access we want to avoid. 

And with type thing being private, its attributes are only accessible via sub programs in package Mace.

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?

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?


? this code does indeed access the value fields directly!

-- I have to add the private part too, to hide the inners of type
a_trunk.

You're looking for an accessor (though accessors aren't always a good
idea; there might be checks needed, attributes may be related so you
need to deal with more than one at the same time, eg coordinates):

 package Mace is
    type Thing is private;
 * ?    procedure Set_Attribute (Into : in out Thing; Value : Integer);
 * ?   function Get_Attribute (From : Thing) return Integer;
 private
    type Thing is record
       Attribute : Integer;
    end record;
 end Mace;

 package body Mace is
    procedure Set_Attribute (Into : in out Thing; Value : Integer) is
    begin
       Into.Attribute := Value;
    end Set_Attribute;
    function Get_Attribute (From : Thing) return Integer
    is (From.Attribute);
 end Mace;


  parent reply	other threads:[~2020-01-18 18:02 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 [this message]
2020-01-18 20:53   ` Simon Wright
2020-01-21 20:51   ` Shark8
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