comp.lang.ada
 help / color / mirror / Atom feed
From: Preben Randhol <randhol+abuse@pvv.org>
Subject: Re: A question and a request
Date: Wed, 7 Nov 2001 11:00:49 +0000 (UTC)
Date: 2001-11-07T11:00:49+00:00	[thread overview]
Message-ID: <slrn9ui901.em.randhol+abuse@kiuk0156.chembio.ntnu.no> (raw)
In-Reply-To: mailman.1005116423.21054.comp.lang.ada@ada.eu.org

On Tue, 6 Nov 2001 20:44:19 -0800 (PST), Eric Merritt wrote:
> As some of you may know I am fairly new to Ada and I
> just found out something that surprised me. I declared
> two subtypes of an integer. I was then able to add
> these two types together and assign the result to an
> instance of one of the above subtypes. or
> 
>  subtype type_1 is Integer;
>  subtype type_2 is Integer;
>   
>  type_1_instance   : type_1;
>  type_2_instance   : type_2;
>  type_1_instance_2 : type_1;
> 
>  type_1_instance   := 1;
>  type_2_instance   := 2;
>  type_1_instance_2 := type_1_instance +
> type_2_instance;
> 
> The addition operator is taking two disparate but
> related types and adding them. This is fine,
> up-casting is a normal thing. However, when the result
> is returned it is not (should not be?) an instance of
> the subtype. It seems that the compiler is
> down-casting automatically. This bothers me and it
> seams that is violates Ada's strong typing. What am I
> missing here?

Use type not subtype if you do not want the above behavior. Read
below (taken from:
http://goanna.cs.rmit.edu.au/~dale/ada/aln/4_basic_types.htmli )

Subtypes

We can restrict the range of values a variable can take by declaring a
subtype with a restricted range of values (this corresponds to Pascal's
user defined types). Any attempt to place an out-of-range value into a
variable of a subtype results in an exception (program error). In this
way program errors can be discovered. The syntax for a subtype
declaration is

subtype Name is Base_Type;
subtype Name is Base_Type range lowerbound . . upperbound;

Examples of declaring subtypes are given below.

type Processors is (M68000,  i8086, i80386, M68030, Pentium, PowerPC);
subtype Old_Processors is Processors range M68000..i8086;
subtype New_Processors is Processors range Pentium..PowerPC;

subtype Data is Integer;
subtype Age is Data range 0 . . 140;
subtype Temperatures is Float range -50.0 .. 200.0;
subtype Upper_Chars is Character range 'A' .. 'Z';

Subtypes are compatable with their base types . They can be placed in
the same place as any variable of the base type can. Also variables of
different subtypes that are derived from the same base type are
compatable.

My_Age	: Age;
Height	: Integer;

Height := My_Age;	-- silly, but never causes a problem.

My_Age := Height;	-- will cause a problem if height's
			-- value is outside the range of 
			-- my_age (0..140), but still 
			-- compilable.


Preben
-- 
Please, stop bombing civilians in Afghanistan. One cannot write off
killing innocent children and other civilians as "collateral damage".
A civilian is a civilian whether he or she is American or from another
country in the world.           http://web.amnesty.org/11september.htm



  reply	other threads:[~2001-11-07 11:00 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-11-06 16:45 List Container Straw Man Nick Roberts
2001-11-06 17:29 ` Stephen Leake
2001-11-06 18:25   ` Marin David Condic
2001-11-06 23:02     ` Nick Roberts
2001-11-07 19:49       ` Stephen Leake
2001-11-07 20:30         ` Marin David Condic
2001-11-07 23:58           ` Nick Roberts
2001-11-08  4:34             ` Jeffrey Carter
2001-11-08 10:45             ` Ehud Lamm
2001-11-08 19:09               ` Nick Roberts
2001-11-09 16:32                 ` Ted Dennison
2001-11-10  2:20                   ` Nick Roberts
2001-11-10 19:50                     ` Ehud Lamm
2001-11-11  3:48                       ` Nick Roberts
2001-11-08 10:41           ` Ehud Lamm
2001-11-08 19:20             ` Marin David Condic
2001-11-08 20:35               ` Ehud Lamm
2001-11-09 16:39             ` Ted Dennison
2001-11-08  0:06         ` Nick Roberts
2001-11-09 16:16           ` Stephen Leake
2001-11-09 16:24             ` Ehud Lamm
2001-11-09 16:52               ` Brian Rogoff
2001-11-09 18:04             ` Darren New
2001-11-09 20:05               ` Stephen Leake
2001-11-10  3:24             ` Nick Roberts
2001-11-06 23:52   ` Nick Roberts
2001-11-07  4:44     ` A question and a request Eric Merritt
2001-11-07 11:00       ` Preben Randhol [this message]
2001-11-07 12:54         ` David C. Hoos, Sr.
2001-11-08  2:35           ` dale
2001-11-07 13:24         ` Eric Merritt
2001-11-07 13:58       ` James Rogers
2001-11-07 16:32       ` Jeffrey Carter
2001-11-09 23:32       ` Matthew Heaney
     [not found] <3BE93E12.7BB33908@worldnet.att.net>
2001-11-07 17:38 ` Eric Merritt
replies disabled

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