comp.lang.ada
 help / color / mirror / Atom feed
* Re: Ada generics and private type operations
       [not found] <mailman.0.1160709703.4389.comp.lang.ada@ada-france.org>
@ 2006-10-13  4:34 ` Jeffrey R. Carter
       [not found]   ` <mailman.1.1160744826.4389.comp.lang.ada@ada-france.org>
  2006-10-14  7:46 ` Stephen Leake
  1 sibling, 1 reply; 6+ messages in thread
From: Jeffrey R. Carter @ 2006-10-13  4:34 UTC (permalink / raw)


Robert Sinclair wrote:
> I've written a generic using Ada 95.  The generic has 5 private types.  
> I'm trying to perform ">=" and "<=" operations on the private types to 
> no avail.  I'm using 'With Function ">=" (l, r : private_type) return 
> boolean' in the declaration of the generic.  I can't seem to figure out 
> how to code the instantiating program to actually perform the 
> operation.  Can someone please provide a simple code example?

There's your problem. You're capitalizing reserved words.

Seriously, though, see ARM 12.2 and 12.6.

generic -- GP
    type PT is private;
    with function ">=" (L : PT; R : PT) return Boolean [is <>];
package GP is
    function F (L : PT; R : PT) return Boolean;
end GP;

package body GP is
    function F (L : PT; R : PT) return Boolean is
       -- null;
    begin -- F
       return R >= L;
    end F;
end GP;

with GP;
procedure GP_Test is
    package P is new GP (PT => Integer, ">=" => ">=");

    B : Boolean := P.F (Integer'First, Integer'Last);
begin -- GP_Test
    null;
end GP_Test;


-- 
Jeff Carter
"If you don't get the President of the United States on that
phone, ... you're going to have to answer to the Coca-Cola
Company."
Dr. Strangelove
32



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Ada generics and private type operations
       [not found]   ` <mailman.1.1160744826.4389.comp.lang.ada@ada-france.org>
@ 2006-10-13 20:39     ` Adam Beneschan
  2006-10-13 21:04       ` Adam Beneschan
  2006-10-13 21:08     ` Jeffrey R. Carter
  1 sibling, 1 reply; 6+ messages in thread
From: Adam Beneschan @ 2006-10-13 20:39 UTC (permalink / raw)


Robert Sinclair wrote:
> Jeff,
>
>   Thank you for the prompt reply.  That was excellent information  I
> was able to get your code to compile using one private type.  However,
> when I attempt a second, third, etc. type I get errors.  Can you assist?

You are declaring a "with function" on ">=" for each private type,
right?

I'm not clear on what you're trying to do and what errors it's giving
you.  But the following works for me:

generic
    type PT1 is private;
    type PT2 is private;
    type PT3 is private;
    with function ">=" (Left, Right : PT1) return Boolean is <>;
    with function ">=" (Left, Right : PT2) return Boolean is <>;
    with function ">=" (Left, Right : PT3) return Boolean is <>;
package GP is
    function F (L1, R1 : PT1;
                L2, R2 : PT2;
                L3, R3 : PT3) return Boolean;
end GP;

package body GP is
    function F (L1, R1 : PT1;
                L2, R2 : PT2;
                L3, R3 : PT3) return Boolean is
    begin
        return L1 >= R1 and then
               L2 >= R2 and then
               L3 >= R3;
    end F;
end GP;


with GP;
procedure GP_Test is
    package New_GP is new GP (PT1 => integer, PT2 => float,
                                  PT3 => character);
    B : Boolean := New_GP.F (1000, 5, 3.14159, 2.71828, 'Z', 'A');
begin
    null;
end GP_Test;

By the way, I do recommend using the "is <>" syntax when declaring the
generic formal functions, if for no other reason than having to type

   ">=" => ">="

just once in your instantiation is pretty awful, and having to type it
five times could produce a migraine.

                         -- Adam




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Ada generics and private type operations
  2006-10-13 20:39     ` Adam Beneschan
@ 2006-10-13 21:04       ` Adam Beneschan
  2006-10-14  9:48         ` Simon Wright
  0 siblings, 1 reply; 6+ messages in thread
From: Adam Beneschan @ 2006-10-13 21:04 UTC (permalink / raw)


In one of his not-quite-finest moments, Adam Beneschan wrote:

> By the way, I do recommend using the "is <>" syntax when declaring the
> generic formal functions, if for no other reason than having to type
>
>    ">=" => ">="
>
> just once in your instantiation is pretty awful, and having to type it
> five times could produce a migraine.

Silly me.  I got so distracted by the presence of bird tracks all over
my source that I forgot: you *can't* say ">=" => ">=" five times in
your instantiation, because the thing on the left of the => can't be
ambiguous.

In fact, if you have more than one "with function >=" in your generic
formal, then you can't say ">=" => anything, even once.  Either you
have to let all of them default, or you have to use positional notation
when you're instantiating (12.3(9)).  Actually, it's occurred to me
that maybe this was your problem?

Now that I think about this, it seems like a minor flaw in the
language.  You can avoid the named notation problem by calling the
generic formal subprograms something else, e.g.

   type PT1 is private;
   type PT2 is private;
   with function PT1_GE (Left, Right : PT1) return Boolean;
   with function PT2_GE (Left, Right : PT2) return Boolean;

Now you can used named notation any way you like to specify the actual
functions during instantiation.  But you can no longer use the "is <>"
syntax to let them default to the actual meanings of ">=".  And this
will not work:

   with function PT1_GE (Left, Right : PT1) return Boolean is ">=";

because that has a different meaning---it means the default is however
">=" is defined when the generic is *declared* (not when it's
instantiated), and ">=" has no meaning at that point.

Perhaps there should be a way to combine the two?

   with function PT1_GE (Left, Right : PT1) return Boolean is <> ">=";

OK, so I don't like this syntax.  Still looks too much like bird
tracks, for one thing.  But it would mean that you can use PT1_GE if
you want to specify the actual using named notation; but if you don't
specify anything, the default would be whatever ">=" means at the point
of the instantiation. 

                         -- Adam




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Ada generics and private type operations
       [not found]   ` <mailman.1.1160744826.4389.comp.lang.ada@ada-france.org>
  2006-10-13 20:39     ` Adam Beneschan
@ 2006-10-13 21:08     ` Jeffrey R. Carter
  1 sibling, 0 replies; 6+ messages in thread
From: Jeffrey R. Carter @ 2006-10-13 21:08 UTC (permalink / raw)


Robert Sinclair wrote:
>   
>   Thank you for the prompt reply.  That was excellent information  I
> was able to get your code to compile using one private type.  However,
> when I attempt a second, third, etc. type I get errors.  Can you assist?

Not without more information, such as the spec and body of your generic, 
the unit that instantiates it, and the actual error msgs you get.

Did Beneschan's msg help?

-- 
Jeff Carter
"If a sperm is wasted, God gets quite irate."
Monty Python's the Meaning of Life
56



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Ada generics and private type operations
       [not found] <mailman.0.1160709703.4389.comp.lang.ada@ada-france.org>
  2006-10-13  4:34 ` Ada generics and private type operations Jeffrey R. Carter
@ 2006-10-14  7:46 ` Stephen Leake
  1 sibling, 0 replies; 6+ messages in thread
From: Stephen Leake @ 2006-10-14  7:46 UTC (permalink / raw)


Robert Sinclair <bob1sinclair@yahoo.com> writes:

> I've written a generic using Ada 95. The generic has 5 private
> types. I'm trying to perform ">=" and "<=" operations on the private
> types to no avail. I'm using 'With Function ">=" (l, r :
> private_type) return boolean' in the declaration of the generic. I
> can't seem to figure out how to code the instantiating program to
> actually perform the operation. Can someone please provide a simple
> code example?

We need to see your code first.

One general rule I follow is to use names rather than operator symbols
for generic parameters;

generic
   with function Greater_Equal ...;
...

That makes it clearer what is going on. The overloading/inheritance
rules for operator symbols can be hard to understand.

After you get things working this way, you can try replacing
Greater_Equal with ">=", and see if it breaks.

-- 
-- Stephe



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Ada generics and private type operations
  2006-10-13 21:04       ` Adam Beneschan
@ 2006-10-14  9:48         ` Simon Wright
  0 siblings, 0 replies; 6+ messages in thread
From: Simon Wright @ 2006-10-14  9:48 UTC (permalink / raw)


"Adam Beneschan" <adam@irvine.com> writes:

> Silly me.  I got so distracted by the presence of bird tracks all over
> my source that I forgot: you *can't* say ">=" => ">=" five times in
> your instantiation, because the thing on the left of the => can't be
> ambiguous.
>
> In fact, if you have more than one "with function >=" in your generic
> formal, then you can't say ">=" => anything, even once.  Either you
> have to let all of them default, or you have to use positional notation
> when you're instantiating (12.3(9)).  Actually, it's occurred to me
> that maybe this was your problem?

This is possibly a case for generic signature packages.

   generic
      type T is private;
      with function ">=" (L, R : T) return Boolean is <>;
   package Type_Signature is end Type_Signature;

   with Type_Signature;
   generic
      with package A is new Type_Signature (<>);
      with package B is new Type_Signature (<>);
   package User is
      function "<" (L, R : A.T) return Boolean;
   end User;

   package body User is
      function "<" (L, R : A.T) return Boolean is
      begin
         return not A.">=" (L, R);
      end "<";
   end User;



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2006-10-14  9:48 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <mailman.0.1160709703.4389.comp.lang.ada@ada-france.org>
2006-10-13  4:34 ` Ada generics and private type operations Jeffrey R. Carter
     [not found]   ` <mailman.1.1160744826.4389.comp.lang.ada@ada-france.org>
2006-10-13 20:39     ` Adam Beneschan
2006-10-13 21:04       ` Adam Beneschan
2006-10-14  9:48         ` Simon Wright
2006-10-13 21:08     ` Jeffrey R. Carter
2006-10-14  7:46 ` Stephen Leake

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