comp.lang.ada
 help / color / mirror / Atom feed
From: "Marc A. Criley" <mcqada@earthlink.net>
Subject: Re: Types, subtypes and ranges
Date: Mon, 19 Mar 2001 13:22:02 GMT
Date: 2001-03-19T13:22:02+00:00	[thread overview]
Message-ID: <3AB5FA4A.1779F976@earthlink.net> (raw)
In-Reply-To: 3AB53681.2959BAF7@ix.netcom.com

Lao Xiao Hai wrote:
> 
> >
> > >WM <wwminirl@hotmail.com> wrote in message
> > >news:97pfmt$ll30@tech.port.ac.uk...
> > >> Hi, I am an ada beginner. Can anyone kindly tell me what's the difference
> > >> between TYPE and SUBTYPE?
> 

  <Nice, informal description of type/subtypes snipped>
> 
> A subtype [...]

> weakens the type system slightly, so many regard it as unsafe.
> 
> In addition, a subtype may have additional operations defined for it; and, in
> some cases, it may have a smaller set (range) of values.
> 
> The key difference between a type and subtype, for the practical programmer, is
> the absence of a wall between the subtype and its parent and sibling/cousin
> subtypes.

Hmmm, I've never really considered subtypes a "weakening" of the type
slightly, though obviously they could be ill-used to that end.  My use
of them has been in fact to tighten up the typing.

One useful use of subtypes is with indexing.  For example:

   type Item_Count is range 0 .. Max_Items;
   subtype Indexes is Item_Count range 1 .. Max_Items;

   type Item_List is array(Indexes) of Item_Type;
   
   Items : Item_List;
   Number_Of_Items : Item_Count := 0;

So now I can increment Number_Of_Items and use that as the index into
the Item_List array while ensuring that only valid indexes will be
available.

Another useful area where subtypes can increase type safety is when
you're working with discriminated records.

   type Operation is (Load, Store, Clear);

   type Command(Op : Operation) is record ... end record;

Now if you want to require that only data of a specific kind is allowed
you can subtype the record:

   subtype Store_Command is Command(Store);

In this case any variables of type Store_Command must conform to that
value of the discriminant.

An actual instance of this latter approach is used extensively in a
particular US Navy weapon control system.  Messages that came in over a
socket had their header read to determine what type of message was being
transmitted.  Then within a declare/begin/end block the Message variant
record type was subtyped with that type of message, and then the message
was read off the socket directly into the message.  Works slick, shoots
missiles! :-)

Marc A. Criley
Senior Staff Engineer
Quadrus Corporation
www.quadruscorp.com



  reply	other threads:[~2001-03-19 13:22 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-03-03  1:00 Questions: WM
2001-03-03  3:30 ` Questions: Robert Love
2001-03-03 11:09 ` Questions: David C. Hoos, Sr.
2001-03-07 23:05 ` Questions: Mark Lundquist
2001-03-08  1:14   ` Questions: Robert A Duff
2001-03-12  7:41   ` Types, subtypes and ranges Anders Wirzenius
2001-03-12 11:57     ` David C. Hoos, Sr.
2001-03-12 16:06       ` Tucker Taft
2001-03-13  6:40       ` Anders Wirzenius
2001-03-12 16:57     ` Scott Ingram
2001-03-18 22:28     ` Lao Xiao Hai
2001-03-19 13:22       ` Marc A. Criley [this message]
2001-03-20 16:57         ` Lao Xiao Hai
  -- strict thread matches above, loose matches on Subject: below --
2001-03-14 13:50 Christoph Grein
replies disabled

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