comp.lang.ada
 help / color / mirror / Atom feed
From: card@syr.lmco.com
To: card@syr.lmco.com
Subject: Visibility and access to "public" attributes
Date: 1997/08/29
Date: 1997-08-29T00:00:00+00:00	[thread overview]
Message-ID: <872873007.3110@dejanews.com> (raw)


Earlier today, I saw an interesting discussion about the way Ada and
Eiffel perform "information hiding". In Ada, a type is is either
public (i.e. its attributes can be read and written by a client of
the type) or private (the attributes of the type are not visible
and can only be accessed using the type's public methods).

In Eiffel, by contrast, a type can be "semi-private", by which I mean
that its attributes can be made visible for read purposes but not
write purposes. This was said to be superior to the Ada method of
using a private type with access functions to get at its attributes
because in Ada the access functions couldn't have the same name as
the attributes, i.e. you'd need a function named "Get_X" to read the
value of attribute X, while in Eiffel you could just use "X". I
excerpt the following from Don Harrison's post:

>To get similar read-only semantics in Ada, you have to declare the
>attribute in the package body (hiding it) and return it via an
exported
>function (which can't have the same name):
>
>  the_a: SOME_TYPE
>  ...
>  function a return SOME_TYPE is
>  begin
>    return the_a;
>  end;

First of all, let me say that in Ada the closest analogue to an
Eiffel/C++ style class (one in which the unit of modularity is
the same as the type) is a tagged type and its dispatching
operations. In Ada, it is certainly possible to have access
functions which have the same names as the attributes of their
tagged type. The following is some compilable Ada code that
illustrates this:

package Test1 is
   type My_Type is tagged private;           -- attributes invisible
   function A(Obj : My_Type) return Boolean; -- access function
private
   type My_Type is tagged
      record
         A : Boolean;                        -- attribute
      end record;
end Test1;


package body Test1 is
   function A(Obj : My_Type) return Boolean
   is
   begin
      return Obj.A;
   end A;
end Test1;

Thus, there is no restriction in Ada that prevents an access function
from having the same name as a class' (tagged type's) attribute.

That said, I think it is fair to say that I believe the idea behind
Ada's "all or nothing" approach is that you may want to ensure that
a client never depends on some of a type's attributes. For instance,
you might have an Aircraft class with a Take_Off method. Should a
client be able to view (and thus potentially depend upon) attributes
in the Aircraft type that might change but would not impact the
interface to Take_Off?

I know that Eiffel can totally hide the attributes of an object (i.e.
accessed via methods only) as well. When writing Eiffel programs, how
do you decide when to make a type's attributes "read-only visible"
and when to hide them?

- Mike

---------------
Michael P. Card
Lockheed Martin Ocean, Radar and Sensor Systems Division
Syracuse, NY 13221
voice: (315)-456-3022  FAX: (315)-456-2414  e-mail:card@syr.lmco.com

-------------------==== Posted via Deja News ====-----------------------
      http://www.dejanews.com/     Search, Read, Post to Usenet




             reply	other threads:[~1997-08-29  0:00 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-08-29  0:00 card [this message]
1997-08-29  0:00 ` Visibility and access to "public" attributes Patrick Doyle
     [not found]   ` <JSA.97Aug29190453@alexandria.organon.com>
1997-08-30  0:00     ` Patrick Doyle
1997-08-30  0:00       ` Jon S Anthony
1997-09-01  0:00         ` Patrick Doyle
1997-08-30  0:00 ` Darren New
1997-09-02  0:00 ` Don Harrison
1997-09-02  0:00   ` Peter Horan
1997-09-02  0:00   ` Don Harrison
1997-09-02  0:00     ` Jon S Anthony
1997-09-02  0:00     ` Gavin Collings
1997-09-02  0:00       ` Nick Leaton
1997-09-02  0:00         ` Gavin Collings
1997-09-02  0:00       ` Patrick Doyle
1997-09-03  0:00       ` Don Harrison
1997-09-05  0:00       ` Nick Leaton
1997-09-05  0:00         ` Patrick Doyle
     [not found]         ` <01bcba0e$418f7380$2001df0a@gavinspc>
1997-09-05  0:00           ` Nick Leaton
  -- strict thread matches above, loose matches on Subject: below --
1997-08-29  0:00 card
1997-08-29  0:00 ` Ted Velkoff
1997-08-30  0:00 ` Darren New
1997-08-30  0:00 ` Patrick Doyle
1997-09-02  0:00 card
replies disabled

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