comp.lang.ada
 help / color / mirror / Atom feed
From: eachus@spectre.mitre.org (Robert I. Eachus)
Subject: Re: Help - Constructors - ASAP.
Date: 1998/07/30
Date: 1998-07-30T00:00:00+00:00	[thread overview]
Message-ID: <EACHUS.98Jul30114523@spectre.mitre.org> (raw)
In-Reply-To: 6pi4jq$j73$1@nnrp1.dejanews.com

In article <6pi4jq$j73$1@nnrp1.dejanews.com> dennison@telepath.com writes:

  > Hmm. You have probably been through this more than I, but it would
  > think it would be useful in some circumstances to have the
  > function be inheritable. If you need a new version of the
  > "constructor" for a new derived type, you can override the old one
  > and the client won't be able to call the old one anymore.  If you
  > make the original "constructor" class-wide instead, clients will
  > always be able to call it with any derived type.

    There are two reasons for following the advice.  First is that Ada
has a rule that inherited functions with a controlling result are
abstract, so they must be overridden.  Yes, there are cases where
constructor functions can be safely inherited from the parent, but
this rule makes you think it through. Second is that when you do want
a constructor which can return any type in the class, the right thing
to do usually is to make it a classwide type.

     So you normally finding yourself either defining a classwide
constructor or making the constructors non-primitive so they are not
derived.  For example:

    package Mixed_Lists is

      type Element_Kind is (Integer_Item, Float_Item, String_Item);
      type List is private;
      type List_Element is tagged private;
      type Integer_Element is new List_Element with private;
      type Float_Element is new List_Element with private;
      type String_Element is new List_Element with private;

      function New_Element(Kind: in Element_Kind) 
                                     return List_Element'Class;
      ...
    private
      ...
    end Mixed_Lists;

    package Mixed_List.Constructors is

      function New_Element(Value: Integer := 0) return Integer_Element;
      function New_Element(Value: Float := 0.0) return Float_Element;
      function New_Element(Value: String := "") return String_Element;

    end Mixed_List.Constructors;

    This example is contrived, becuase it uses both models and you
usually want one or the other.  (And for a type like this, you would
probably want direct append operations rather than explicit
constructors for the list elements.)



--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




  reply	other threads:[~1998-07-30  0:00 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-07-23  0:00 Help - Constructors - ASAP Maxim Senin
1998-07-26  0:00 ` Matthew Heaney
1998-07-27  0:00   ` dennison
1998-07-30  0:00     ` Robert I. Eachus [this message]
1998-07-30  0:00       ` tedennison
1998-07-31  0:00         ` Matthew Heaney
1998-07-31  0:00         ` Mats Weber
1998-07-31  0:00           ` dennison
1998-08-01  0:00             ` Robert Dewar
1998-08-01  0:00               ` Matthew Heaney
1998-08-01  0:00             ` Matthew Heaney
1998-08-03  0:00             ` Mats Weber
1998-07-31  0:00           ` Robert I. Eachus
1998-08-01  0:00             ` Matthew Heaney
1998-08-01  0:00       ` Robert Dewar
1998-08-01  0:00         ` Matthew Heaney
1998-08-03  0:00           ` Mats Weber
1998-08-03  0:00             ` Matthew Heaney
1998-08-03  0:00               ` Mats Weber
1998-08-04  0:00                 ` Matthew Heaney
1998-08-04  0:00                   ` Mats Weber
1998-08-05  0:00                     ` Matthew Heaney
1998-08-10  0:00           ` Robert I. Eachus
1998-08-03  0:00         ` tedennison
  -- strict thread matches above, loose matches on Subject: below --
1998-07-15  0:00 Maxim Senin
1998-07-16  0:00 ` Stephen Leake
replies disabled

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