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.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Received: by 2002:a6b:b548:: with SMTP id e69-v6mr1933923iof.70.1528311361445; Wed, 06 Jun 2018 11:56:01 -0700 (PDT) X-Received: by 2002:aca:abc6:: with SMTP id u189-v6mr121379oie.2.1528311361312; Wed, 06 Jun 2018 11:56:01 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!feeder.eternal-september.org!news.linkpendium.com!news.linkpendium.com!news.snarked.org!border2.nntp.dca1.giganews.com!nntp.giganews.com!u74-v6no2134962itb.0!news-out.google.com!z3-v6ni1741iti.0!nntp.google.com!v8-v6no2143425itc.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Wed, 6 Jun 2018 11:56:01 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=76.113.16.86; posting-account=lJ3JNwoAAAAQfH3VV9vttJLkThaxtTfC NNTP-Posting-Host: 76.113.16.86 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <0dd44c4f-99b5-426d-ba08-831cbb14a795@googlegroups.com> Subject: Re: Generic formal type with 'Image From: Shark8 Injection-Date: Wed, 06 Jun 2018 18:56:01 +0000 Content-Type: text/plain; charset="UTF-8" Xref: reader02.eternal-september.org comp.lang.ada:52961 Date: 2018-06-06T11:56:01-07:00 List-Id: On Wednesday, June 6, 2018 at 7:03:24 AM UTC-6, Alejandro R. Mosteo wrote: > I'm pretty sure the answer is "no", but just in case: > > Is there a formal for a generic that serves for any type that has a > predefined 'Image? Yes, there is: GENERIC Type K is (<>); PACKAGE Example IS ... Will do it, as all parameters of K are discrete, and therefore K has the 'Image attribute. (As of Ada 2012 *only* discrete types have the 'Image attribute.) > > The purpose is to avoid: > > generic > type Printable is ... -- What should go here? > with function Image (P : Printable) return String is <>; > package Extended_Generic is ... > > and then have to pass the 'Image attribute as the Image function in all > instantiations. This is the more general construct ans will work on something like: TYPE Vector IS ARRAY(Positive RANGE <>) of Integer; Function Image( Input : Vector ) Return String is Function List( Data : Vector := Input ) Return String is (case Data'Length is when 0 => '', when 1 => Integer'Image(Data'First), when others => Image(Data(Data'First..Natural'Pred(Data'Last))) & Integer'Image(Data'Last) ); Begin Return '(' & List & ')'; End Image; Package X is new Extended_Generic( Vector ); this allows more complex items for your Printable parameter; you could in fact make the parameter "Type Printable(<>) is limited private" for near-maximum coverage. (You wouldn't be able to use it on incomplete types.) > > The closest thing I can think of is (<>) but that won't do for floating > point types. This is because floats aren't discrete. > > I understand this is an unusually narrow case (I need a generic for many > numeric types, both discrete and floating, and this would save me some > typing -- that I have already spent here anyway.) > > With these issues I feel a kind of overlap/missed connection between > attributes and interfaces. This is true. What you might do is make a sort of generic signature package and work from there. GENERIC Type Numeric is private; Function Image( Input Numeric ) Return String is <>; Function "+" ( Left, Right : Numeric ) Return Numeric is <>; Function "-" ( Left, Right : Numeric ) Return Numeric is <>; Function "/" ( Left, Right : Numeric ) Return Numeric is <>; Function "*" ( Left, Right : Numeric ) Return Numeric is <>; Function "**"( Left : Numeric; Right Natural ) Return Numeric is <>; -- Other operations. PACKAGE General_Numeric IS ... Then for your particular case use either a child of this, or a generic accepting it as a parameter.