comp.lang.ada
 help / color / mirror / Atom feed
From: ncohen@watson.ibm.com (Norman H. Cohen)
Subject: Re: ADA Objects Help!
Date: 26 Jan 1995 15:31:27 GMT
Date: 1995-01-26T15:31:27+00:00	[thread overview]
Message-ID: <3g8f8g$15l2@watnews1.watson.ibm.com> (raw)
In-Reply-To: 3g49bu$7fv@nps.navy.mil

In article <3g49bu$7fv@nps.navy.mil>, swdecato@nps.navy.mil writes: 

|> I am a new Ada student, so correct me if I make any glaring mistakes.  Your
|> sample code attempts to mimic a C++ class using a package.  The problem with
|> the package implementation is that I can't instantiate instances of the object
|> at run time.

Of course you can: 

   X: C_Type;
   Y: C_Type;
   type C_Pointer_Type is access C_Type;
   Z: C_Pointer_Type := new C_Type;

(C++ confuses things enough by using the same construct, the class, both
as a type definition and a module.  Let's not make it worse by using the
term "object" as a synonym for "class".  An object is an instance of a
class.  In a language in which classes are not runtime entities--in
contrast to Smalltalk, for example--an "instance of an object" is sloppy
and misleading terminology.)

X, Y, and Z.all each contain their own copies of all data that differ
from one object to another.  All the entities that are really the same
for all objects--functions and what C++ calls static data--are declared
in the package, clearly indicating that they are per-type (or
per-package, or per-class) entities rather than per-object entities.

|>               In Ada, a pointer to a package is created at compile time since
|> the declaration using the "new" operation occurs prior to the firt BEGIN.  From
|> a syntax standpoint, Ada packages are as close to C++ classes as you can get,
|> minus the dynamic allocation.

Ada packages are not run-time entities, any more than a C header file is.
They are a mechanism for grouping logically related entities.  They are
not assigned, pointed to, or passed to subprograms.  They are not
components of arrays or records.  A package IS analogous to a class in
that both can be used as modules.  A record-type declaration is analogous
to a class in that both can contain components and have instances of
them, each of which has its own copies of each of the components.

|> Many Ada folks have demonstrated how Ada objects are created and deleted.
|> My argument against the Ada style was that I felt that the C++ syntax more
|> accurately modelled the englist language.  I have no doubt that the same effect
|> can be achieved with either language.  I still say that the following code is
|> easier to understand when dealing with objects...
|>
|>              MY_TYPE *objectPtr = new MY_TYPE;
|>                      objectPtr->run();
|>                      objectPtr->sleep();
|>              objectPtr->move();
|>              objectPtr->Set(A,B);
|>
|>              etc
|>
|> In Ada the same program would be: 
|>
|>              Run(objectPtr);
|>              Sleep(objectPtr);
|>              Move(objectPtr);
|>              Set(objectPtr, A, B);

There is a style of Ada programming that reminds you which package is the
source of the operations you are calling: 

   package Widgets is
      type Widget_Type is ...;
      type Widget_Pointer_Type is access Widget_Type;
      procedure Run (X: access Widget_Type);
      procedure Sleep (X: access Widget_Type);
      procedure Move (X: access Widget_Type);
      procedure Set (X: access Widget_Type; Y, Z: in out Who_Knows_What);
      ...
   end Widgets;

   Object_Pointer : Widgets.Widget_Pointer_Type := new Widget_Type;
   A, B           : Who_Knows_What;
   ...
   Widgets.Run (Object_Pointer);
   Widgets.Sleep (Object_Pointer);
   Widgets.Move (Object_Pointer);
   Widgets.Set (Object_Pointer, A, B);

Some Ada programmers feel that the code is easier to understand with the
dotted notation and some do not.  This is the "use clause controversy": 
Without a use clause, you are forced to use the dotted notation.  Some
Ada programmers feel that there is no one approach that is always best,
but that it depends on a number of factors, such as whether Widgets is a
well-known package whose facilities will be instantly recognized by all
programmers, whether lots of different facilities from lots of different
packages are all being used in the same place, whether the facilities
provided by the package have descriptive names, whether there are
name clashes among several packages being used in the same place, and
what kind of tools, from cross-reference listings to hypertext links, are
available to tell the program reader where any undotted names come from.

|> I don't see how passing "structs" to static functions differs from any other
|> function which takes a parameter.  In my mind, when I create a pointer to
|> an object of some user defined class, that one pointer takes any public
|> data and functions associated with the class.

You are certainly not alone in that respect, but I do not find it helpful
to create the fiction that a one-per-package subprogram or variable is
somehow "included" in all objects of a type declared in a package, and
pointed to by all pointers to objects of that type (especially since, on
occasion, it is most appropriate for the same package to provide more
than one type).

--
Norman H. Cohen    ncohen@watson.ibm.com



  parent reply	other threads:[~1995-01-26 15:31 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <3f9g1u$j4m@nps.navy.mil>
     [not found] ` <D2H5un.FEr@nntpa.cb.att.com>
     [not found]   ` <3fcs59$70s@nps.navy.mil>
     [not found]     ` <3ff186$c19@gnat.cs.nyu.edu>
1995-01-17 17:57       ` ADA Objects Help! Mats Weber
1995-01-18 17:47         ` Robert Dewar
1995-01-20 16:04           ` Mats Weber
1995-01-21 18:59             ` Robert Dewar
1995-01-23 12:03               ` Robb Nebbe
1995-01-25 20:44                 ` Mats Weber
1995-01-25 20:44               ` Mats Weber
1995-01-27  4:03                 ` Robert Dewar
1995-01-26  3:36           ` swdecato
     [not found]         ` <3fhggr$11dp@watnews1.watson.ibm.com>
     [not found]           ` <Mats.Weber-1901951739360001@mlma11.matrix.ch>
1995-01-20 17:22             ` Norman H. Cohen
1995-01-23 16:37               ` Mats Weber
1995-01-25 20:44               ` Mats Weber
1995-01-27  4:05                 ` Robert Dewar
1995-01-19 11:57   ` Robert M. Wilkinson
1995-01-22 18:06     ` Robert Dewar
1995-01-24 22:18       ` Norman H. Cohen
1995-01-25  1:26         ` swdecato
1995-01-25 18:18           ` Bob Kitzberger
1995-01-25 20:11             ` Bob Kitzberger
1995-01-26 15:31           ` Norman H. Cohen [this message]
     [not found]           ` <D330pK.M1@nntpa.cb.att.com>
1995-01-28 21:46             ` John DiCamillo
1995-01-30 14:13               ` David Emery
1995-01-30 22:50               ` Subject/Object Confusion Syndrome [was: Ada Objects Help] John Volan
1995-02-01 14:33                 ` Norman H. Cohen
     [not found]                   ` <D3DpJu.4nK@swlvx2.msd.ray.com>
     [not found]                     ` <D3H7J3.B2x@inmet.camb.inmet.com>
1995-02-06 10:32                       ` Robb Nebbe
     [not found]                     ` <3gu21g$ch@portal.gmu.edu>
1995-02-06 14:01                       ` John Volan
1995-02-01 22:37                 ` Maarten Landzaat
     [not found]                   ` <3h1ahp$gf5@gnat.cs.nyu.edu>
     [not found]                     ` <3h3jmp$1h1@Starbase.NeoSoft.COM>
1995-02-07 14:39                       ` John Volan
1995-02-09  2:25                         ` David Weller
1995-01-29 18:19             ` ADA Objects Help! mat
     [not found]               ` <1995Feb5.180601@hobbit>
1995-02-07 23:04                 ` Subject/Object Confusion Syndrome [was: Ada Objects Help] John Volan
1995-01-25  9:48       ` ADA Objects Help! mat
1995-01-23 10:01     ` calling syntax (was Re: Ada Objects) Robb Nebbe
1995-01-23 18:08       ` John DiCamillo
1995-01-23 23:47     ` ADA Objects Help! Ed Osinski
1995-01-25  6:19       ` David O'Brien
     [not found] ` <1995Jan16.132400@lglsun.epfl.ch>
     [not found]   ` <131279@cup.portal.com>
1995-01-20 16:52     ` Ada " Robert Dewar
1995-01-22 18:30       ` Tucker Taft
1995-01-24 22:09         ` Jacob Sparre Andersen
1995-01-26 16:20           ` Robert A Duff
1995-01-27 17:04             ` Robert A Duff
1995-01-27 19:58             ` Tucker Taft
1995-01-20 17:41   ` Mark S. Hathaway
1995-01-23 10:41     ` Robb Nebbe
1995-01-23 11:53     ` Stephane Barbey
replies disabled

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