comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <matthew_heaney@acm.org>
Subject: Re: How can I qualify the predefined logical operations
Date: 1998/07/25
Date: 1998-07-25T00:00:00+00:00	[thread overview]
Message-ID: <m390limhw4.fsf@mheaney.ni.net> (raw)
In-Reply-To: 6okcnl$n93$1@nnrp1.dejanews.com

hoyngj@my-dejanews.com writes:

> I have the following problem, when trying to implement a function "and".
> 
>  package Some_Package is
>    type Private_Type is private;
>    function "And" (Left, Right: Private_Type) return Private_Type ;
>  private
>    type Private_Type is array (0 .. 1023) of Boolean;
>  end Some_Package;
> 
> 
> -- I want to implement Some_Package."And" using the predefined logical
> -- operation "And" for any one-dimensional array type whose components
> -- are of a boolean type.
>  package body Some_Package is
>    function "And" (Left, Right: Private_Type) return Private_Type   is
>    begin
>      return Left and Right;  -- ERROR: AND is here Some_Package."AND",
>                              -- endless recursive
>    end "and";
>  end Some_Package;

I like to do this:

private

   type Rep is array (0 .. 1023) of Boolean;

   type Private_Type is new Private_Type_Rep;

end Some_Package;

   function "and" (L, R : Private_Type) return Private_Type is
   begin
      return Rep (L) and Rep (R);
   end;


The problem you're having is that the public and private views of the
type share a namespace.  This is sometimes a pain, but you can get
around it simply by just declaring separate types for the public and
private views, and explicitly converting between them in the body.

Another technique is to implement the full view of the type as a record:

private

   type Boolean_Array is array (0 .. 1023) of Boolean;

   type Private_Type is
      record
         Values : Boolean_Array;
      end record;

end;

   function "and" (L, R : Private_Type) return Private_Type is
   begin
      return L.Values and R.Values;
   end;

This is another way to create separate namespaces for the public and
private views of a type.

> To fix the error in the implementation above, I need to qualify the
> predefined "And" operation:
> 
> 
>  package body Some_Package is
>    function "And" (Left, Right: Private_Type) return Private_Type is
>    begin
>      return Standard."And" (Left, Right);  -- ERROR: not possible
>    end "and";
>  end Some_Package;
> 
> How is it possible to qualify the predefined logical operations to
> distinguish between Some_Package."and" and the predefined "and"?

No.  The operator "and" is implicitly declared immediately after the
declaration of the full view of the type.  As if you did this:

private

   type Private_Type is array (0 .. 1023) of Boolean;

   function "and" (L, R : Private_Type) return Boolean;

end;


In times like this (operations for the public type have the same
signiture as predefined operations of the private type), it's best to
use either type derivation or a record type to implement the full
view of the type.





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

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-07-16  0:00 How can I qualify the predefined logical operations hoyngj
1998-07-16  0:00 ` hoyngj
1998-07-17  0:00   ` Robert Dewar
1998-07-16  0:00 ` Anonymous
1998-07-16  0:00 ` dennison
1998-07-16  0:00 ` dennison
1998-07-16  0:00 ` Tucker Taft
1998-07-17  0:00 ` Andreas Kalla
1998-07-19  0:00   ` Mats Weber
1998-07-25  0:00 ` Matthew Heaney [this message]
replies disabled

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