comp.lang.ada
 help / color / mirror / Atom feed
* What is the difference?
@ 2019-08-10 13:59 Gilbert Gosseyn
  2019-08-10 15:24 ` Jeffrey R. Carter
  2019-08-12 13:44 ` Shark8
  0 siblings, 2 replies; 4+ messages in thread
From: Gilbert Gosseyn @ 2019-08-10 13:59 UTC (permalink / raw)


package G is

generic

with function f return Integer is <>;

-- alternativ: with function f return Integer;

procedure gg;

end G;


package body G is

procedure gg is
begin
null;
end gg;

end G;


procedure test is

function f1 return Integer is
begin
null;
end f1;

procedure gg1 is new gg(f => f1);

begin
null;
end test;

It works in both versions.

What is here the meaning of: is <>?


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

* Re: What is the difference?
  2019-08-10 13:59 What is the difference? Gilbert Gosseyn
@ 2019-08-10 15:24 ` Jeffrey R. Carter
  2019-08-10 16:04   ` Gilbert Gosseyn
  2019-08-12 13:44 ` Shark8
  1 sibling, 1 reply; 4+ messages in thread
From: Jeffrey R. Carter @ 2019-08-10 15:24 UTC (permalink / raw)


On 8/10/19 3:59 PM, Gilbert Gosseyn wrote:
> 
> It works in both versions.

It doesn't work at all:

$ cat gilbert_gosseyn.adb
procedure Gilbert_Gosseyn is
    function F1 return Integer is (23);

    procedure Gg1 is new Gg (F => F1);
begin
    Gg1;
end Gilbert_Gosseyn;
$ gnatmake gilbert_gosseyn.adb
x86_64-linux-gnu-gcc-8 -c gilbert_gosseyn.adb
gilbert_gosseyn.adb:4:25: "Gg" is undefined
gnatmake: "gilbert_gosseyn.adb" compilation error

If I clean it up, it compiles (which I presume is what you mean by "work"):

$ cat gilbert_gosseyn.adb
with G;

procedure Gilbert_Gosseyn is
    function F1 return Integer is (23);

    procedure Gg1 is new G.Gg (F => F1);
begin
    Gg1;
end Gilbert_Gosseyn;
$ gnatmake gilbert_gosseyn.adb
x86_64-linux-gnu-gcc-8 -c gilbert_gosseyn.adb
x86_64-linux-gnu-gcc-8 -c g.adb
x86_64-linux-gnu-gnatbind-8 -x gilbert_gosseyn.ali
x86_64-linux-gnu-gnatlink-8 gilbert_gosseyn.ali

> What is here the meaning of: is <>?

It means that if there is function named F visible at the point of instantiation 
with the same parameter-and-return-type profile as the formal function F, and no 
actual parameter is supplied for the formal function F, then the visible 
function F will be used by default:

$ cat gilbert_gosseyn.adb
with G;

procedure Gilbert_Gosseyn is
    function F return Integer is (23);

    procedure Gg1 is new G.Gg;
begin
    Gg1;
end Gilbert_Gosseyn;
$ gnatmake gilbert_gosseyn.adb
x86_64-linux-gnu-gcc-8 -c gilbert_gosseyn.adb
x86_64-linux-gnu-gnatbind-8 -x gilbert_gosseyn.ali
x86_64-linux-gnu-gnatlink-8 gilbert_gosseyn.ali

If Gg actually uses F:

$ cat g.adb
with Ada.Text_IO;

package body G is
    procedure Gg is
    begin
       Ada.Text_IO.Put_Line (Item => F'Image);
    end Gg;
end G;

then we can see that it calls the implicitly provided function F:

$ ./gilbert_gosseyn
  23

-- 
Jeff Carter
"I did not rob a bank. If I'd robbed a bank, everything
would be great. I tried to rob a bank, is what happened,
and they got me."
Take the Money and Run
139


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

* Re: What is the difference?
  2019-08-10 15:24 ` Jeffrey R. Carter
@ 2019-08-10 16:04   ` Gilbert Gosseyn
  0 siblings, 0 replies; 4+ messages in thread
From: Gilbert Gosseyn @ 2019-08-10 16:04 UTC (permalink / raw)


On Saturday, August 10, 2019 at 5:24:37 PM UTC+2, Jeffrey R. Carter wrote:
> On 8/10/19 3:59 PM, Gilbert Gosseyn wrote:
> > 
> > It works in both versions.
> 
> It doesn't work at all:
> 
> $ cat gilbert_gosseyn.adb
> procedure Gilbert_Gosseyn is
>     function F1 return Integer is (23);
> 
>     procedure Gg1 is new Gg (F => F1);
> begin
>     Gg1;
> end Gilbert_Gosseyn;
> $ gnatmake gilbert_gosseyn.adb
> x86_64-linux-gnu-gcc-8 -c gilbert_gosseyn.adb
> gilbert_gosseyn.adb:4:25: "Gg" is undefined
> gnatmake: "gilbert_gosseyn.adb" compilation error
> 
> If I clean it up, it compiles (which I presume is what you mean by "work"):
> 
> $ cat gilbert_gosseyn.adb
> with G;
> 
> procedure Gilbert_Gosseyn is
>     function F1 return Integer is (23);
> 
>     procedure Gg1 is new G.Gg (F => F1);
> begin
>     Gg1;
> end Gilbert_Gosseyn;
> $ gnatmake gilbert_gosseyn.adb
> x86_64-linux-gnu-gcc-8 -c gilbert_gosseyn.adb
> x86_64-linux-gnu-gcc-8 -c g.adb
> x86_64-linux-gnu-gnatbind-8 -x gilbert_gosseyn.ali
> x86_64-linux-gnu-gnatlink-8 gilbert_gosseyn.ali
> 
> > What is here the meaning of: is <>?
> 
> It means that if there is function named F visible at the point of instantiation 
> with the same parameter-and-return-type profile as the formal function F, and no 
> actual parameter is supplied for the formal function F, then the visible 
> function F will be used by default:
> 
> $ cat gilbert_gosseyn.adb
> with G;
> 
> procedure Gilbert_Gosseyn is
>     function F return Integer is (23);
> 
>     procedure Gg1 is new G.Gg;
> begin
>     Gg1;
> end Gilbert_Gosseyn;
> $ gnatmake gilbert_gosseyn.adb
> x86_64-linux-gnu-gcc-8 -c gilbert_gosseyn.adb
> x86_64-linux-gnu-gnatbind-8 -x gilbert_gosseyn.ali
> x86_64-linux-gnu-gnatlink-8 gilbert_gosseyn.ali
> 
> If Gg actually uses F:
> 
> $ cat g.adb
> with Ada.Text_IO;
> 
> package body G is
>     procedure Gg is
>     begin
>        Ada.Text_IO.Put_Line (Item => F'Image);
>     end Gg;
> end G;
> 
> then we can see that it calls the implicitly provided function F:
> 
> $ ./gilbert_gosseyn
>   23
> 
> -- 
> Jeff Carter
> "I did not rob a bank. If I'd robbed a bank, everything
> would be great. I tried to rob a bank, is what happened,
> and they got me."
> Take the Money and Run
> 139

please add: with G; use G; before procedure test is


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

* Re: What is the difference?
  2019-08-10 13:59 What is the difference? Gilbert Gosseyn
  2019-08-10 15:24 ` Jeffrey R. Carter
@ 2019-08-12 13:44 ` Shark8
  1 sibling, 0 replies; 4+ messages in thread
From: Shark8 @ 2019-08-12 13:44 UTC (permalink / raw)


> What is here the meaning of: is <>?

That is a default; if there is a matching subprogram (name, parameters) then it can use that without being specified. Example:

Generic
  Type Element(<>) is private;
  Unity : Element;
  with Function "*"(Left, Right : Element) return Element is <>;
Function Generic_Exponent( Left : Element; Right : Natural ) return Element;
--...
Function Generic_Exponent( Left : Element; Right : Natural ) return Element is
(case Right is
  when 0 => Unity,
  when 1 => Left,
  when 2 => Left * Left,
  when others =>
   (if Right mod 2 = 0 
    then  (Left ** (Right/2) ** 2
    else ((Left ** (Right/2) ** 2) * Left
   )
);

Now you can say something like
Procedure Test is
  Type X is 1..8;
  Function Exp is new Generic_Exponent(X, 1, others => <>);
  Function "**"(Left : X; Right : Natural) Return X renames Exp;
Begin
  --Testing procedures
End Test;

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

end of thread, other threads:[~2019-08-12 13:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-10 13:59 What is the difference? Gilbert Gosseyn
2019-08-10 15:24 ` Jeffrey R. Carter
2019-08-10 16:04   ` Gilbert Gosseyn
2019-08-12 13:44 ` Shark8

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