comp.lang.ada
 help / color / mirror / Atom feed
From: Georg Bauhaus <rm.plus-bug.tsoh@maps.futureapps.de>
Subject: Re: User-defined type attributes (replacing genericity)
Date: Fri, 14 Mar 2008 15:31:40 +0100
Date: 2008-03-14T15:31:40+01:00	[thread overview]
Message-ID: <1205505100.6445.60.camel@K72> (raw)
In-Reply-To: <pqup5kf1u34t$.19kwms4bw12hs.dlg@40tude.net> <d9065e94-8586-4889-b5c7-a8ca9e8f2248@d21g2000prf.googlegroups.com> <p0joauyj6p7$.z2nuhn0t1c78.dlg@40tude.net> <30f629fd-69d3-4d40-995f-9933f52b8dad@s37g2000prg.googlegroups.com> <965suhjl0bxt$.74se2ylyksin.dlg@40tude.net> <frcppv$de8$1@jacob-sparre.dk> <pqup5kf1u34t$.19kwms4bw12hs.dlg@40tude.net>


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, that range should be
and interface type, etc. ;-) 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. 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.)

(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. 

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.






  parent reply	other threads:[~2008-03-14 14:31 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-12 18:58 User-defined type attributes Eric Hughes
2008-03-12 21:23 ` Dmitry A. Kazakov
2008-03-13 18:32   ` Eric Hughes
2008-03-13 19:58     ` Dmitry A. Kazakov
2008-03-14  1:46       ` Randy Brukardt
2008-03-14  1:46       ` Randy Brukardt
2008-03-14  1:46       ` Randy Brukardt
2008-03-14  9:00         ` Dmitry A. Kazakov
2008-03-14 14:36           ` User-defined type attributes (replacing genericity) Georg Bauhaus
2008-03-15  3:04           ` User-defined type attributes Randy Brukardt
2008-03-15  9:33             ` Dmitry A. Kazakov
2008-03-14 14:31         ` Georg Bauhaus [this message]
2008-03-14 14:48           ` User-defined type attributes (replacing genericity) Dmitry A. Kazakov
2008-03-14 17:51             ` Eric Hughes
2008-03-14 18:58               ` Dmitry A. Kazakov
2008-03-14 20:19                 ` Eric Hughes
2008-03-15  4:01               ` Randy Brukardt
2008-03-14 16:58           ` Georg Bauhaus
2008-03-14 18:39             ` Dmitry A. Kazakov
2008-03-15  9:39               ` Dmitry A. Kazakov
2008-03-14  3:55       ` User-defined type attributes Eric Hughes
2008-03-14  9:01         ` Dmitry A. Kazakov
2008-03-14 18:04           ` Eric Hughes
2008-03-14  1:46 ` Randy Brukardt
2008-03-14  4:41   ` Eric Hughes
2008-03-15  3:20     ` Randy Brukardt
2008-03-17  4:38       ` Eric Hughes
2008-03-17 21:03         ` Randy Brukardt
2008-03-17 21:58           ` Eric Hughes
replies disabled

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