comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <matthewjheaney@earthlink.net>
Subject: Re: Constructing an object
Date: Fri, 23 Sep 2005 05:40:43 GMT
Date: 2005-09-23T05:40:43+00:00	[thread overview]
Message-ID: <uhdcctk85.fsf@earthlink.net> (raw)
In-Reply-To: dgr6lb$f2k$1@sunnews.cern.ch

Maciej Sobczak <no.spam@no.spam.com> writes:

> Let's say I have the following:
> 
>     type Color is (Black, Red, Green, Blue, White);
> 
>     type Shape is tagged record
>        C : Color;
>     end record;
> 
>     type Rectangle is new Shape with record
>        A, B : Positive;
>     end record;


If you want greater control of object creation and initialization, then
you could make the types private:

package Shapes is
   type Color is ...;
   type Shape is tagged private;
   type Rectangle is new Shape with private;

   function New_Shape (C : Color) return Shape;
   function New_Rectangle (C : Color) return Rectangle;
   -- other ctors with dif't params as desired
private
   type Shape is tagged record ...;
   type Rectangle is new Shape with record ...;
end Shapes;


> 1. By procedure, which is a primitive operation for each type in the
> hierarchy, for example:

Use a function.  In Ada 2005, you can use a function even for limited types.


> 2. By function:
> 
>     function New_Shape(C : in Color) return Shape is
>        S : Shape;
>     begin
>        S.C := C;
>        return S;
>     end;

Yes, use a function.  However (and I'm really thinking of controlled
types here) you might want to use aggregate syntax:

   function New_Shape (C : Color) return Shape is
   begin
      return Shape'(C => C);
   end;

In Ada 2005, you'll be able say (I don't know if my syntax is quite
right):

   function New_Shape (C : Color) return Shape is
   begin
     return S : Shape do
        S.C := C;
     end return;
   end;


>   function New_Rectangle(A, B : in Positive; C : in Color) return
> Rectangle is
>        R : Rectangle;
>     begin
>        Shape(R) := New_Shape(C);   -- is this OK?

I think this is legal; it's called a "view conversion."


>        R.A := A;
>        R.B := B;
>        return R;
>     end;
> 
> This does not please me for the same reason - no enforcement of proper
> initialization, if a given type makes no sense uninitialized.

But that's because you didn't declare the type in such a way that
initialization is enforced.  You have a few possibilities, including
declaring the type as private, declaring it as private and with an
unknown discriminant, declaring it with a known discriminant, or giving
the record components default values.  


> What approach do you actually use in the real (non-tutorial) code?
> Are there other techniques?
> (I'm aware of the controlled types, but I need to provide parameters for
> construction, which the special Initialize procedure does not have.)

Not quite.  You can combine a function as above, but make the type
controlled.  Something like:

package Shapes is
   type Shape is tagged private;
   function New_Shape (C : Color) return Shape;
private
   type Shape is new Controlled with record
      C : Color;
   end record;

   procedure Initialize (S : in out Shape);
end;


package body Shapes is
  function New_Shape (C : Color) return Shape is
  begin
     return Shape'(Controlled with C => C);
  end;




  parent reply	other threads:[~2005-09-23  5:40 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-21  8:46 Constructing an object Maciej Sobczak
2005-09-21 10:16 ` Georg Bauhaus
2005-09-22  7:21   ` Maciej Sobczak
2005-09-21 11:55 ` Dmitry A. Kazakov
2005-09-22  7:28   ` Maciej Sobczak
2005-09-22  7:45     ` Maciej Sobczak
2005-09-22 13:33       ` Dmitry A. Kazakov
2005-09-24  5:23         ` Randy Brukardt
2005-09-24  9:47           ` Dmitry A. Kazakov
2005-09-29  0:12             ` Randy Brukardt
2005-09-29  8:17               ` Dmitry A. Kazakov
2005-09-29 22:21                 ` Randy Brukardt
2005-09-30  8:14                   ` Jean-Pierre Rosen
2005-09-30 19:28                     ` Dmitry A. Kazakov
2005-09-30 17:49                   ` Robert A Duff
2005-10-01  0:44                     ` Randy Brukardt
2005-10-01 10:49                       ` Dmitry A. Kazakov
2005-10-01 11:06                       ` Tapio Kelloniemi
2005-10-01 14:13                         ` Robert A Duff
2005-10-02 11:52                           ` Tapio Kelloniemi
2005-10-01 15:19                       ` Georg Bauhaus
2005-09-23  5:40 ` Matthew Heaney [this message]
2005-09-23  7:18   ` tmoran
2005-09-23  8:23   ` Maciej Sobczak
2005-09-23 12:04     ` Dmitry A. Kazakov
2005-09-23 12:36       ` Matthew Heaney
2005-09-23 13:03         ` Hyman Rosen
2005-09-23 13:41           ` Maciej Sobczak
2005-09-23 14:23           ` Matthew Heaney
2006-01-17  6:28             ` [Offtopic] " James Dennett
2005-09-23 13:42         ` Dmitry A. Kazakov
2005-09-23 14:27           ` Matthew Heaney
2005-09-23 12:24     ` Matthew Heaney
2005-09-24  5:34       ` Randy Brukardt
replies disabled

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