comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@world.std.com>
Subject: Re: Type Conversion in an Assignment Statement
Date: Mon, 18 Jun 2001 20:08:26 GMT
Date: 2001-06-18T20:08:26+00:00	[thread overview]
Message-ID: <wcc3d8xtuth.fsf@world.std.com> (raw)
In-Reply-To: 3B2DB8B9.1A26F90F@informatik.uni-jena.de

Carsten Freining <freining@informatik.uni-jena.de> writes:

>    type IntegerAccessType is access Integer;
>    subtype IntegerSubType is Integer range 1..100;
>    Pointer: IntegerAccessType := new IntegerSubType;
> begin
>    Pointer.all :=105;
> end;

> This is a correct program. I thought it would raise a constraint_error,

No, it will not raise C_E.  The designated subtype of IntegerAccessType
is Integer.  When you allocate with "new IntegerSubType", it does *not*
remember the bounds of IntegerSubType.  In the assignment "Pointer.all
:= 105;", it's checking that 105 is within the bounds of Integer,
not within the bounds of IntegerSubType.

This is how elementary types work.  Composite types work differently: if
you allocate an array or record on the heap, it *does* remember the
bounds or discriminants of what you allocated, and it will check that
they don't change, and raise C_E if you try.

Anyway, it's probably better to write "new Integer" above, to avoid this
confusion.

I'm too lazy to quote chapter and verse here.  If you really want to
understand the RM wording, remember that the name in a type_declaration
is the name of a subtype (the "first subtype").  A type_declaration
also creates a type, but types in Ada do not have names.  Integer and
IntegerSubType are both subtypes (of the same underlying type).  This
terminology is admittedly a bit confusing, so most people are happy to
say "the type Integer", when strictly speaking they really mean "the
type of subtype Integer".

- Bob

P.S. You ought to use the generally accepted Ada style, and call it
Integer_Access_Type, rather than IntegerAccessType.



  parent reply	other threads:[~2001-06-18 20:08 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-06-18  8:15 Type Conversion in an Assignment Statement Carsten Freining
2001-06-18  9:27 ` [comp.lang.ada] " David C. Hoos, Sr.
2001-06-18 10:18   ` Carsten Freining
2001-06-18 10:18   ` Carsten Freining
2001-06-18 13:28 ` Ted Dennison
2001-06-19  5:23   ` Carsten Freining
2001-06-18 20:08 ` Robert A Duff [this message]
2001-06-19  5:41   ` Carsten Freining
2001-06-19 12:35     ` Underscore Usage (was: Type Conversion in an Assignment Statement) Wilhelm Spickermann
replies disabled

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