Re: limited types (Was: Records that could be arrays)
- From: "Dmitry A. Kazakov" <mailbox@xxxxxxxxxxxxxxxxx>
- Date: Tue, 28 Feb 2006 10:38:31 +0100
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
.
- References:
- limited types (Was: Records that could be arrays)
- From: Thierry Bernier
- Re: limited types (Was: Records that could be arrays)
- From: Randy Brukardt
- Re: limited types (Was: Records that could be arrays)
- From: Dmitry A. Kazakov
- Re: limited types (Was: Records that could be arrays)
- From: Matthew Heaney
- Re: limited types (Was: Records that could be arrays)
- From: Dmitry A. Kazakov
- Re: limited types (Was: Records that could be arrays)
- From: Matthew Heaney
- Re: limited types (Was: Records that could be arrays)
- From: Dmitry A. Kazakov
- Re: limited types (Was: Records that could be arrays)
- From: Matthew Heaney
- Re: limited types (Was: Records that could be arrays)
- From: Dmitry A. Kazakov
- Re: limited types (Was: Records that could be arrays)
- From: Georg Bauhaus
- Re: limited types (Was: Records that could be arrays)
- From: Dmitry A. Kazakov
- Re: limited types (Was: Records that could be arrays)
- From: Matthew Heaney
- Re: limited types (Was: Records that could be arrays)
- From: Dmitry A. Kazakov
- Re: limited types (Was: Records that could be arrays)
- From: Georg Bauhaus
- limited types (Was: Records that could be arrays)
- Prev by Date: Re: Programming in Ada 2005 book
- Next by Date: Re: limited types (Was: Records that could be arrays)
- Previous by thread: Re: limited types (Was: Records that could be arrays)
- Next by thread: Re: limited types (Was: Records that could be arrays)
- Index(es):
Relevant Pages
|
|