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 X-Google-Thread: 103376,c1fe4bc1dd51fc87 X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!feeder.erje.net!news-fra1.dfn.de!storethat.news.telefonica.de!telefonica.de!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: User-defined type attributes (replacing genericity) Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <30f629fd-69d3-4d40-995f-9933f52b8dad@s37g2000prg.googlegroups.com> <965suhjl0bxt$.74se2ylyksin.dlg@40tude.net> <1205505100.6445.60.camel@K72> Date: Fri, 14 Mar 2008 15:48:59 +0100 Message-ID: NNTP-Posting-Date: 14 Mar 2008 15:48:59 CET NNTP-Posting-Host: a52e769e.newsspool4.arcor-online.net X-Trace: DXC=m7X5\n_d9WeEB;5>eE0T7m4IUKoCdRESJ_NVOh X-Complaints-To: usenet-abuse@arcor.de Xref: g2news1.google.com comp.lang.ada:20378 Date: 2008-03-14T15:48:59+01:00 List-Id: On Fri, 14 Mar 2008 15:31:40 +0100, Georg Bauhaus wrote: > On Fri, 2008-03-14 at 10:00 +0100, Dmitry A. Kazakov wrote: > >> Right, but this also implies that there is no need to have generics at all. >> A class-wide object does all what needed: >> >> generic >> type S is new T with private; >> procedure Foo (X : in out S); >> >> can be replaced by: >> >> procedure Foo (X : in out T'Class); > > I assume your answer to what follows will be that Ada semantics > should be put under programmer control, What's wrong with having control over the semantics? > that range should be and interface type, etc. ;-) Shouldn't it? > But anyway, even that doesn't change things. > > I just looked at the effects. Not promising on two accounts, > but more rattling regularity. Which might have its place. > > Replacing genericity means every generic instance is replaced > with an invocation of just one procedure. No, only calls to instances of generic bodies. > However, for objects of > specially crafted types. One type needs to be available for > mimicking each generic. > > I had two types > > type Simple is range 1 .. 100; > -- predefined "<=" > > type Data is record > amount: Natural; > wpi: Float; > end record; > > function "<="(a, b: Data) return Boolean; > > According to the suggestion to abandon generics I have had instead, > basically, > > procedure Foo (X, Y, Z : in out T'Class) is > function Is_Highest(V: T) return Boolean is > begin > return X <= V and then Y <= V and then Z <= V; > end Is_Highest; > begin > ... > > The comparison function "<=" needs to be one of the primitive > operations of all types in T'Class. (Hence a wrapper type becomes > necessary for range type Simple.) No, what you need is an interface implemented by both. You need to declare Simple implementing Ordered. That will tell the compiler that the predefined "<=" is an implementation of Ordered's "<=". > (I'll leave out the problem of local variables of type T'Class.) > (I'll leave out required variance programming work, too.) > > With generics, I got two nicely specialized instances of Foo, > one for the range type, the other for the composite type. > The effect is that function Is_Highest will use "<=" as I > would expect for a range type. Small code, obvious, and > efficient. Similarly, the "<=" of the Data instance's > Is_Highest function corresponds directly to both the definition > and to the expectation. > > Without generics, there is only one Is_Highest, as might be > expected. Why there have to be two? The whole idea of generics was to have *one* generic body. This is why it is called generic [programming]. > Inlining did not help. In fact, I wouldn't expect inlining to be > readily available with 'Class---unless of course > replacing generics should also entail compilers that look at > the entire program. Then, maybe, a few more thing can be > "optimized" at compile time. You mixing two different problems. BTW, I answered both in my earlier posts. So I repeat it short. If you want to constrain the dispatching table to the diagonal you do it procedure Foo (X, Y, Z : in out T) This requires X, Y, Z to have exactly same type. The language of constraints is a different theme. Do not sell a limitation of generics to a certain type of constraint as genuine advantage. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de