comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: How do you specialize a generic function from a nested function?
Date: Sun, 26 Jul 2015 11:09:26 +0200
Date: 2015-07-26T11:09:26+02:00	[thread overview]
Message-ID: <2hnkone4ma0h.of3odv73sflv$.dlg@40tude.net> (raw)
In-Reply-To: b18f6aba-3311-4497-a3fc-72dc3cef94be@googlegroups.com

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

      reply	other threads:[~2015-07-26  9:09 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 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