comp.lang.ada
 help / color / mirror / Atom feed
From: "Nick Roberts" <nick.roberts@acm.org>
Subject: Re: Renaming of enumeration constant
Date: Thu, 10 Jun 2004 18:38:49 +0100
Date: 2004-06-10T18:38:49+01:00	[thread overview]
Message-ID: <2irkl9Fqi7qpU1@uni-berlin.de> (raw)
In-Reply-To: ca9v8l$5gb8@cui1.lmms.lmco.com

"Xenos" <dont.spam.me@spamhate.com> wrote in message
news:ca9v8l$5gb8@cui1.lmms.lmco.com...

> Just a curiosity question:
>
> What is the difference (advantages, etc.) of defining a function rename
for
> an enumeration constant over just creating a constant.  Consider:
>
> package X is
>   type E is (A, B, C);
> end X;
>
> with X;
> package Y is
>    function A return X.E renames X.A;
> end Y;
>
> with X;
> package Z is
>   A : constant X.E := X.A;
> end Z;
>
> Is the "A" in Y treated any different than the "A" in Z by the compiler,
or
> will both achieve the same results.  Is one considered "better" (whatever
> that means) than the other?

I think the answer to your question is actually about inheritance.

Suppose we have the following packages:

   package Farm is
      type Leggedness is (Four_Legs, Two_Legs);
      Bipedal: constant Leggedness := Two_Legs;
      function Quadrupedal return Leggedness renames Four_Legs;
      ...
   end;

   with Farm;
   package Zoo is
      type Podality is new Farm.Leggedness;
      ...
   end;

In this situation, there will be an inherited operation:

   function Quadrupedal return Podality renames Four_Legs;

in which the Four_Legs value it returns is of the type Podality. This
operation is inherited, because it is a primitive operation of the type
Leggedness (it is declared directly in the same package specification). In
fact, this is why we get the names Four_Legs and Two_Legs for values of the
type Podality: they are functions, and they are primitive operations, so
they are inherited.

The constant Bipedal, however, is not inherited.

My own opinion is that constants are silly in Ada, but they were introduced
in Ada 83 (and its predecessors), long before the question of inheritance
was understood to be important. The advantage that constants have is that
their declaration is simpler and perhaps more obvious, and it doesn't matter
if they do not need to be inherited.

I sometimes wish to derive a type from
Ada.Strings.Unbounded.Unbounded_String, and I am annoyed that I have to
redeclare the constant Null_Unbounded_String (because it is not inherited).
It gives me the opportunity to play with names:

   type Teacher_Name is new Ada.Strings.Unbounded.Unbounded_String;

   Null_Teacher_Name: constant Teacher_Name :=
      Teacher_Name(Ada.Strings.Unbounded.Null_Unbounded_String);

but inheritance doesn't usually prevent the introduction of new names (as
renamings of functions or procedures).

For interest, compare the following possible alternative package
specifications for Zoo:

   with Farm;
   package Zoo is
      subtype Podality is Farm.Leggedness;
      ...
   end;

In this example, Podality is declared simply as a subtype of Farm.Leggedness
(without reducing the set of values). This is really just a way of renaming
a (sub)type. Since Zoo.Podality and Farm.Leggedness are of the same type, no
conversion is required between values of either subtype.

   with Farm;
   package Zoo is
      type Podality is range 0..100;
      function To_Leggedness (B: Podality) return Farm.Leggedness;
      function To_Podality (L: Farm.Leggedness) return Podality;
      ...
      Invalid_Legs: exception;
   end;

Here we declare a new type Podality altogether (which can cope with
centipedes and fish), and provide functions to convert to and from
Farm.Leggedness. The function To_Leggedness raises the exception
Invalid_Legs if B is neither 2 nor 4.

HTH

-- 
Nick Roberts





  reply	other threads:[~2004-06-10 17:38 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-06-10 15:37 Renaming of enumeration constant Xenos
2004-06-10 17:38 ` Nick Roberts [this message]
2004-06-10 20:40   ` Xenos
2004-06-11  9:25   ` Jean-Pierre Rosen
2004-06-11 12:00     ` Nick Roberts
2004-06-11 12:49       ` Dmitry A. Kazakov
2004-06-12  4:34     ` Robert I. Eachus
2004-06-12  4:01 ` Robert I. Eachus
replies disabled

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