comp.lang.ada
 help / color / mirror / Atom feed
From: adam@irvine.com (Adam Beneschan)
Subject: Re: How do I avoid 'use' in this case?
Date: Thu, 18 Mar 1993 03:02:23 GMT
Date: 1993-03-18T03:02:23+00:00	[thread overview]
Message-ID: <C42Drz.HF7@irvine.com> (raw)
In-Reply-To: eriks@lin.foa.se's message of Thu, 11 Mar 1993 18:20:21 GMT

In article <1993Mar11.182021.1129@lin.foa.se> eriks@lin.foa.se 
(Erik Svensson FOA2) writes:

>   Use rename instead.
>   Like this:
>
>           function "/="(A,B:Foo) return boolean renames field_def."/=";
>
>   You could do field_def. "/="from the beginning, but that`s not so 
>   convinient.

Wrong.  You cannot rename the "/=" operator.  What you need to do is
rename the "=" operator:

       function "="(A,B:Foo) return boolean renames field_def."=";

and then "/=" will be defined automatically.  /= is the only operator
that cannot be renamed explicitly.

By the way, the original post asked how to use operators such as /=
without using the USE clause.  Several responses have suggested
putting a RENAMES in the using package to make the operator visible.
While this is correct, if a common package P is with'ed by lots of
other packages Q1, Q2, etc., putting a RENAMES clause for every
operator in every using package Q1, Q2, ..., is quite tedious.
There's a better way around this, in my opinion.  Put the RENAMES in a
subpackage of P, like this:

    package P is

        type some_type_1 is record ... end record;
        type some_type_2 is (val1, val2, val3, ...);

        package OPERATORS is
            function "=" (left, right : some_type_1) return boolean
                renames P."=";
            function "=" (left, right : some_type_2) return boolean
                renames P."=";
            function "<" (left, right : some_type_2) return boolean
                renames P."<";
            function "<=" (left, right : some_type_2) return boolean
                renames P."<=";
            ... same for ">", ">="
        end OPERATORS;

    end P;

Now, in packages Q1, Q2, etc., you can make all the operators visible
very simply:

with P;
package Q1 is
    use P.OPERATORS;

    procedure PR (x, y : P.some_type_2) is
    begin
        if x = y then 
. . . . . . .


This does use the "use" statement, so if your Ada compiler has an
automatic preprocessor that deletes your source file if it sees the
word "use" :-), this solution won't work.  However, in environments
where rules like this aren't carved in stone, I think this is a great
solution.  Although it uses "use", it uses it in a restricted way that
doesn't make the whole world visible, and so IMHO it doesn't have the
problems that are usually associated with "use".

                                -- Adam



  parent reply	other threads:[~1993-03-18  3:02 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1993-03-11 16:39 How do I avoid 'use' in this case? Val Kartchner
1993-03-11 18:17 ` MILLS,JOHN M.
1993-03-11 18:20 ` Erik Svensson FOA2
1993-03-16 13:12   ` Sandy Wise
1993-03-18  3:02   ` Adam Beneschan [this message]
1993-03-11 18:47 ` Arthur Evans
1993-03-12 17:26 ` Gary Morris @ignite
  -- strict thread matches above, loose matches on Subject: below --
1993-03-18 15:58 howland.reston.ans.net!zaphod.mps.ohio-state.edu!uwm.edu!caen!nic.umass.e
replies disabled

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