comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: limited types (Was: Records that could be arrays)
Date: Tue, 28 Feb 2006 10:38:31 +0100
Date: 2006-02-28T10:38:35+01:00	[thread overview]
Message-ID: <6pmoa2lmyfty.i9e0ddc3no7p$.dlg@40tude.net> (raw)
In-Reply-To: 44037195$0$13798$9b4e6d93@newsread4.arcor-online.net

On Mon, 27 Feb 2006 22:40:18 +0100, Georg Bauhaus wrote:

> Dmitry A. Kazakov wrote:
> 
>> The question Georg asked was: where typed containers of elements of related
>> types might themselves appear related. The number of cases is huge.
>> Generics fundamentally cannot help here, they aren't dynamically
>> polymorphic.
> 
> But then wouldn't you again be mixing apples and herrings?
> 
>   type APPLE_CONTAINER is new CONTAINER with null record;
>     -- specializes in apples, only
> 
>   type HERRING_CONTAINER is new CONTAINER with null record;
>     -- specializes in herrings, only
> 
>   x: access CONTAINER'class := ...;
> 
>   x.insert(an_apple); -- right or wrong? compile time?

You cannot tell without the contract of Container. The above could be
compile error, run-time error or correct.

There are three [all useful] variants:

1. Containers of related types are unrelated
2. Containers of related types are same
3. Containers of subtypes are subtypes (polymorphic containers)

[there is also an axis for the container index/cursor types]

The variants 1 and 2 are relatively easy to get. The variant 3 is much more
challenging.

Consider [imaginary syntax]:

type Food is ...
type Food_Container is ..; -- of Food'Class
type Apple is new Food with ...;
type Herring is new Food with ...;

type Apple_Box is new Food_Container (Element'Tag => Apple'Class);
type Herring_Barrel is new Food_Container (Element'Tag => Herring'Class);

X : Food_Container'Class := Some_Herring_Barrel;
Y : Herring_Barrel := Some_Herring_Barrel;

X.Insert (An_Apple); -- Constraint_Error at run-time
Y.Insert (An_Apple); -- Constraint_Error at run-time + compiler warning

With full ADT one could also do this

   type Apple_Only_Box is new Food_Container some syntax sugar;
private
   type Apple_Only_Box is array (...) of Apple;
      -- The implementation of the container is completely overridden
      -- and replaced by a more efficient

> In fact, herrings with little pieces of apple aren't
> that unusual, add onions and mayonnaise and you are
> almost set -

rather pickled red beets and cucumbers ...

> not everyone's taste, maybe. :-)

Not in a class of apple juice! (:-))

> Seriously,
> wouldn't type-forcing containers be in the way of
> composition?

It will, when that is what's needed. It should be programmer's choice. And
it is much more flexible that the choices we have now: either same type or
different types. Ada was always better than that. Consider Integer and
Positive as an example.

Now what about an array of Integers vs. an array of Positives. Then a slice
of an array of Positives. Presently, constraints cannot propagate between
types. This is a weakness of the type system. You cannot get an array
subtype by constraining its elements. You cannot get an access subtype by
constraining the target type. This is more general and important question
than just container types.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  reply	other threads:[~2006-02-28  9:38 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-02-24 16:51 limited types (Was: Records that could be arrays) Thierry Bernier
2006-02-24 21:57 ` Randy Brukardt
2006-02-25  2:21   ` Matthew Heaney
2006-02-25  3:38     ` Matthew Heaney
2006-02-25 11:06   ` Dmitry A. Kazakov
2006-02-25 15:05     ` Matthew Heaney
2006-02-26  1:01       ` Randy Brukardt
2006-02-26  9:00       ` Dmitry A. Kazakov
2006-02-26 18:20         ` Matthew Heaney
2006-02-26 20:52           ` Dmitry A. Kazakov
2006-02-26 22:07             ` Matthew Heaney
2006-02-27  9:11               ` Dmitry A. Kazakov
2006-02-27 14:34                 ` Georg Bauhaus
2006-02-27 16:05                   ` Dmitry A. Kazakov
2006-02-27 16:52                     ` Matthew Heaney
2006-02-27 20:21                       ` Dmitry A. Kazakov
2006-02-27 21:40                         ` Georg Bauhaus
2006-02-28  9:38                           ` Dmitry A. Kazakov [this message]
2006-02-27 23:00                         ` Matthew Heaney
2006-02-28  9:39                           ` Dmitry A. Kazakov
2006-02-28 17:24                             ` Matthew Heaney
2006-02-28 19:06                               ` Dmitry A. Kazakov
2006-02-28 19:58                                 ` Matthew Heaney
2006-02-28 21:03                                   ` Dmitry A. Kazakov
2006-02-28 21:51                                   ` limited types Simon Wright
2006-03-01  1:59                                     ` Matthew Heaney
replies disabled

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