comp.lang.ada
 help / color / mirror / Atom feed
From: ka@sorry.no.email (Kenneth Almquist)
Subject: Re: The "()" operator revisited.
Date: 16 Jan 2004 19:15:40 -0500
Date: 2004-01-16T19:15:40-05:00	[thread overview]
Message-ID: <bu9urc$k4t$1@shell.monmouth.com> (raw)
In-Reply-To: 820a00puecpmodp3vg43gpd7063rr5lj8q@4ax.com

Dmitry A. Kazakov wrote:
> The problem is that without notion of abstract array as a class-wide
> or generic parameter type, there will be no way to write
> class-wide/generic subroutines taking advantage of indexing. Let I
> have all containers of different kind having () indexing. This will
> not help me. Because I will still be unable as little as just to write
> a universal program searching max element in *a* container.

You can write it.  The problem is that it's awkward to instantiate.

    generic
        type Collection is limited private;
        type Index is (<>);
        type Element is private;
        with function "()"(C : Collection; I : Index) return Element is <>;
        with function First(C : Collection) return Index is <>;
        with function Last(C : Collection) return Index is <>;
        with function ">"(Left, Right : Element) return Boolean is <>;
        Element_First : Element;   -- Value of Element'first
    function Max(C : Collection) return Element;

    function Max(C : Collection) return Element is
        Result : Element := First;
    begin
        for I in First(C) .. Last(C) loop
            if C(I) > Result then
                Result := C(I);
            end if;
        end loop;
        return Result;
    end Max;

>> To get something that could match a generic array parameter would require
>> a lot of restrictions on abstract arrays, in particular that the indice
>> types would have to be abstract discrete types.
>
> No. We should simply go on and introduce abstract index type as a
> counterpart of the formal discrete type:
>
> type X is (<>);
>
> One should be consequent. We need abstract discrete, integer, index,
> numeric, string, array, access types. Have I forgot something? (:-))

The problem with extending the Ada class system to cover user
defined types is that the language specification is never going
to include all the classes that user defined types might fall
into.  Languages like Haskell get around this by allowing the
programmer to define new classes.

Ada already has a work-around for the lack of user-defined classes:
default values for generic subprogram parameters.  I that that the
way to support user defined types which act like arrays is to
(1) make array indexing be an operator, as was proposed at the start
of this thread, (2) allow user defined types to have attributes such
as 'first and 'last, and (3) allow the generic to specify that it's
parameters must support specific attributes.  I'd also define
attributes for array types which give the index and component types,
and allow generic type parameters to have defaults.  With these
changes, the Max generic becomes:

    generic
        type Collection is limited private;
        type Index is (<>) := Collection'Index_Type;
        type Element is private := Collection'Element_Type;
        with function "()"(C : Collection; I : Index) return Element is <>;
        with Collection'Object'First;
        with Collection'Object'Last;
        with function ">"(Left, Right : Element) return Boolean is <>;
        with Element'First;
    function Max(C : Collection) return Element;

The directive "wtih Element'First;" means that type Element provides
the 'First attribute.  The directive "with Collection'Object'First;"
means that objects of type Collection provide the 'First attribute.
With these changes, the function Max can be sensibly instantiated
by providing a single parameter specifying an array type.
				Kenneth Almquist



  reply	other threads:[~2004-01-17  0:15 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-01-12 17:53 The "()" operator revisited Frank J. Lhota
2004-01-12 18:38 ` Frank J. Lhota
2004-01-12 22:26 ` Robert A Duff
2004-01-13 16:29   ` Frank J. Lhota
2004-01-13  9:24 ` Dmitry A. Kazakov
2004-01-13 16:44   ` Frank J. Lhota
2004-01-13 17:13     ` Hyman Rosen
2004-01-13 22:27     ` Randy Brukardt
2004-01-14  2:30     ` Stephen Leake
2004-01-14  9:04     ` Dmitry A. Kazakov
2004-01-17  0:15       ` Kenneth Almquist [this message]
2004-01-17 21:15         ` Robert A Duff
2004-01-19 10:25         ` Dmitry A. Kazakov
2004-01-13 13:13 ` Marin David Condic
2004-01-13 17:38   ` Warren W. Gay VE3WWG
2004-01-13 19:09     ` Robert A Duff
2004-01-15 17:30       ` Warren W. Gay VE3WWG
2004-01-15 18:11         ` Robert A Duff
2004-01-15 19:36           ` tmoran
2004-01-15 20:35             ` Robert A Duff
2004-01-17  5:48               ` Robert I. Eachus
2004-01-16  1:52           ` Redefining := (was: The "()" operator revisited.) Jeffrey Carter
2004-01-16 21:37             ` Randy Brukardt
2004-01-19 11:33               ` Dmitry A. Kazakov
2004-01-16  3:11           ` The "()" operator revisited Mark A. Biggar
2004-01-16 13:28             ` Hyman Rosen
2004-01-16 16:19             ` Robert A Duff
2004-01-16 18:09             ` Warren W. Gay VE3WWG
2004-01-16 13:56           ` Frank J. Lhota
2004-01-16 16:14             ` Robert A Duff
2004-01-16 21:29               ` Frank J. Lhota
  -- strict thread matches above, loose matches on Subject: below --
2004-01-13 17:46 amado.alves
2004-01-13 22:21 ` Randy Brukardt
2004-01-13 17:53 amado.alves
2004-01-14  9:09 ` Dmitry A. Kazakov
2004-01-14 12:55   ` Georg Bauhaus
2004-01-14 15:05     ` Dmitry A. Kazakov
2004-01-15  1:21       ` Georg Bauhaus
2004-01-15  8:50         ` Dmitry A. Kazakov
2004-01-15 11:09           ` Georg Bauhaus
2004-01-15 13:23             ` Dmitry A. Kazakov
2004-01-17  6:26               ` Robert I. Eachus
2004-01-14 13:04   ` Hyman Rosen
2004-01-14 15:22 amado.alves
2004-01-14 16:16 ` Dmitry A. Kazakov
replies disabled

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