From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: How do you specialize a generic function from a nested function? Date: Sun, 26 Jul 2015 11:09:26 +0200 Organization: cbb software GmbH Message-ID: <2hnkone4ma0h.of3odv73sflv$.dlg@40tude.net> References: Reply-To: mailbox@dmitry-kazakov.de NNTP-Posting-Host: 5CNUwuQ0q1vZOSDFkeKrYQ.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: 40tude_Dialog/2.0.15.1 X-Notice: Filtered by postfilter v. 0.8.2 Xref: news.eternal-september.org comp.lang.ada:27011 Date: 2015-07-26T11:09:26+02:00 List-Id: 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