comp.lang.ada
 help / color / mirror / Atom feed
* Re: Visibility and access to "public" attributes
@ 1997-08-29  0:00 card
  1997-08-29  0:00 ` Ted Velkoff
                   ` (2 more replies)
  0 siblings, 3 replies; 23+ messages in thread
From: card @ 1997-08-29  0:00 UTC (permalink / raw)
  To: card


In my original post on this subject, I wrote:

>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?

To which Patrick Doyle replied:

>If the attributes change, then what used to be an attribute
>can be changed into a function with absolutely no effect on
>client code.  (And, if it turns out to be impossible to do
>this, then you have a problem on your hands even in Ada.)
>The Eiffel approach is *no different* from using accessors.

The issue I'm raising here is the ability of a client to
depend on a class' attribute (feature). To expand on the
Aircraft example a bit (pardon my Eiffel- I don't write the
langauge fluenty; I'm just a tourist! ;) )

class AIRCRAFT
   Take_Off; -- I don't know how you write methods in Eiffel!
feature

  Propeller_RPM: Integer;

end

Now, suppose you change the AIRCRAFT class to have jet engines and
thus remove or change the Propeller_RPM attribute? Because it was
*visible*, someone may have written code to read it and now if you
change the spec of the class their code will break. It doesn't
matter that they can't update the attribute! If they can refer to it
at all and you remove the attribute from the class definition, their
code is broken! In this case, the only way to avoid breaking client
code written for the original definition of AIRCRAFT would be to
add a useless Propeller_RPM accessor that returns zero or something.

In this case, the AIRCRAFT class' features/attributes would have been
better off hidden, since the Take_Off method's interface didn't
change when the Propeller_RPM attribute was changed/removed.

Now, I would have the same problem in Ada if my Aircraft type were
declared with public attributes:

package X is
   type Aircraft is tagged
      record
         Propeller_RPM : Integer;
      end record;
   procedure Take_Off (AC : Aircraft);
end X;

A client of this package could write code that accesses (for read
or write, who cares) Propeller_RPM. If I change the definition
of Aircraft so that this attribute no longer exists, the client
code breaks just like in the Eiffel case.

That's the issue I'm writing about: impact on client code. When
in Eiffel would you want to allow a client to be dependent on a
type's implementation in this way?

Now to Pat's question:

>What I'd like to know is, how do you decide in Ada whether
>to make an attribute public, and thus irrevocably commit to
>allowing clients to assign to it?

Personally, I only do this for types which cannot be put in an
invalid state by the client setting the attributes, e.g.

package Example is

   type Point is abstract tagged null record;
   function Distance (P1, P2 : Point) return Integer;

   type 1D_Point is new Point with
      record
         X: Integer;
      end record;
   function Distance(P1,P2 : 1D_Point) return Integer;

   type 2D_Point is new 1D_Point with
      record
         Y : Integer;
      end record;
   function Distance (P1, P2 : 2D_Point) return Integer;

end Example;

Here, the 1D_Point and 2D_Point classes cannot be put into
inconsistent/invalid states as a result of the client setting
the X and Y coordinates. The distance between them can always
be computed (within arithmetic range of machine, of course).

Now, if I had a type with more complex methods
and it was possible for a user-adjusted attribute to put the
object into an inconsistent state, then I would make its
attributes private and the attributes would be set only by
higher-level methods. That is, I wouldn't provide a low-level
accessor to set an attribute if doing so could allow the object
to be put into an invalid state.

- 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




^ permalink raw reply	[flat|nested] 23+ messages in thread
* Re: Visibility and access to "public" attributes
@ 1997-09-02  0:00 card
  0 siblings, 0 replies; 23+ messages in thread
From: card @ 1997-09-02  0:00 UTC (permalink / raw)
  To: card


I'd like to thank everyone who replied to my original
post RE: visibility of attributes.

I had wondered when/why one would make a class'
attributes read-only rather than hiding them altogether.
The answer to my question was provided by Peter Horan:

>One must export parameters and functions when they
>are needed to verify pre-conditions. This is an
>Eiffel validity requirement (VAPE). (I am reminded
>of keeping one's fingers crossed when making
>promises :-)). This does not apply to features in
>post-conditions and invariants, which may be more
>restrictive than the client needs without harm.

This makes sense. Since everything in a precondition
must be visible to a class's client(s), then the
client must be able to read the class's attributes
(features) if they are used in the precondition(s).

Thanks, Pete!

- 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




^ permalink raw reply	[flat|nested] 23+ messages in thread
* Visibility and access to "public" attributes
@ 1997-08-29  0:00 card
  1997-08-29  0:00 ` Patrick Doyle
                   ` (2 more replies)
  0 siblings, 3 replies; 23+ messages in thread
From: card @ 1997-08-29  0:00 UTC (permalink / raw)
  To: card


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




^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~1997-09-05  0:00 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-08-29  0:00 Visibility and access to "public" attributes card
1997-08-29  0:00 ` Ted Velkoff
1997-08-30  0:00 ` Patrick Doyle
1997-08-30  0:00 ` Darren New
  -- strict thread matches above, loose matches on Subject: below --
1997-09-02  0:00 card
1997-08-29  0:00 card
1997-08-29  0:00 ` 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     ` 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
1997-09-02  0:00     ` Jon S Anthony

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