comp.lang.ada
 help / color / mirror / Atom feed
* How do you specialize a generic function from a nested function?
@ 2015-07-26  8:29 EGarrulo
  2015-07-26  9:09 ` Dmitry A. Kazakov
  0 siblings, 1 reply; 2+ messages in thread
From: EGarrulo @ 2015-07-26  8:29 UTC (permalink / raw)


I am trying to write a generic predicate that negates a supplied predicate.  The generic predicate compiles, but the compilation of client code fails with "generic subprogram cannot be called" (because the client code supplies a nested predicate, I suppose).  Here is the code (see the comment in the last file).

------------- negate.ads ------------------

generic
   type Value_Type is private;
   with function Predicate (Value : Value_Type) return Boolean;
   -- I have also tried with:
   -- Predicate : access function (Value : Value_Type) return Boolean;
function Negate (Value : Value_Type) return Boolean;

------------- negate.adb ------------------

function Negate (Value : Value_Type) return Boolean is
begin
   return not Predicate (Value);
end Negate;

------------- test.adb ------------------

with Negate;

procedure test is
   function Is_Zero (Value: Integer) return Boolean is (Value = 0);

   Is_Not_Zero : access function (Value: Integer) return Boolean 
   := Negate(Integer, Is_Zero'Access); -- <<== ##### ERROR HERE #####
begin
   null;
end test;

--------------------------------------


What is wrong?  And -- in any case -- could the code be simplified?  Thank you.


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

* Re: How do you specialize a generic function from a nested function?
  2015-07-26  8:29 How do you specialize a generic function from a nested function? EGarrulo
@ 2015-07-26  9:09 ` Dmitry A. Kazakov
  0 siblings, 0 replies; 2+ messages in thread
From: Dmitry A. Kazakov @ 2015-07-26  9:09 UTC (permalink / raw)


On Sun, 26 Jul 2015 01:29:35 -0700 (PDT), EGarrulo wrote:

> ------------- negate.ads ------------------
> 
> generic
>    type Value_Type is private;
>    with function Predicate (Value : Value_Type) return Boolean;
>    -- I have also tried with:
>    -- Predicate : access function (Value : Value_Type) return Boolean;
> function Negate (Value : Value_Type) return Boolean;
> 
> ------------- negate.adb ------------------
> 
> function Negate (Value : Value_Type) return Boolean is
> begin
>    return not Predicate (Value);
> end Negate;
> 
> ------------- test.adb ------------------
> 
> with Negate;
> 
> procedure test is
>    function Is_Zero (Value: Integer) return Boolean is (Value = 0);
> 
>    Is_Not_Zero : access function (Value: Integer) return Boolean 
>    := Negate(Integer, Is_Zero'Access); -- <<== ##### ERROR HERE #####
> begin
>    null;
> end test;
> 
> --------------------------------------
> 
> 
> What is wrong?

You have to instantiate the generic:

   function Is_Not_Zero is new Negate (Integer, Is_Zero);

> And -- in any case -- could the code be simplified?

That depends on what are you going to achieve. Generics are never simple
and never handy. Avoid them if you can.

If you want to implement some algebra of functions use a tagged type to
represent a function (not an Ada's function, not an Ada's generic
function). Have a primitive operation Evaluate to execute the "function".
Implement operations on "functions" (like "get inverse function") as
operations of the type.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

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

end of thread, other threads:[~2015-07-26  9:09 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-26  8:29 How do you specialize a generic function from a nested function? EGarrulo
2015-07-26  9:09 ` Dmitry A. Kazakov

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