comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam@spam.com>
Subject: Re: Integer-Types
Date: Tue, 05 Oct 2004 19:37:38 GMT
Date: 2004-10-05T19:37:38+00:00	[thread overview]
Message-ID: <6KC8d.3566$UP1.3327@newsread1.news.pas.earthlink.net> (raw)
In-Reply-To: <cjsse8$vds$04$1@news.t-online.com>

Rick Santa-Cruz wrote:


> So this means the main difference is, that without the "new" the
> compiler will choose the Base-Type (for example a short-int) and in
> the second case I explicitly decide which is the base-type. Is this
> correct?

That's right. You can also leave off the range information, in which 
case the new type is just like the parent type, but is a different type 
with a different name.

New, in a type declaration, declares a derived type. A derived type has 
the same representation as the parent (unless that is changed with 
representation clauses) and inherits all the primitive operations of the 
parent. Conversions between the two types are also defined.

Sometimes this is useful. Suppose you have a memory location that maps 
to a hardware output register. The register can have only a few valid 
values. Then you can write:

type Sensor_State is (One, Two, Three, Four);

Suppose the 4 values are represented by 1, 2, 4, 8, respectively. You 
can hide this external representation from your application using a 
derived type.

Define an operation to read the sensor and return a Sensor_State. The 
rest of the application doesn't need to know that the register uses a 
different encoding:

function Get return Sensor_State is
    type Hardware_Sensor_State is new Sensor_State;
    for Hardware_Sensor_State'Size use Hardware_Register_Size;
    for Hardware_Sensor_State use
    (One   => 2#0001#, Two  => 2#0010#,
     Three => 2#0100#, Four => 2#1000#);

    Register : Hardware_Sensor_State;
    for Register'Address use Hardware_Sensor_Address;
    pragma Import (Ada, Register);
    pragma Volatile (Register);
begin -- Get
    return Sensor_State (Register);
end Get;

This is called a change of representation.

> Is there somewhere a summing-up in which contexts I can use the
> keyword "new"?

Not that I know of. ARM Annex P contains a syntax summary.

> I know some:

> 1.) For declaring a new type

For declaring a derived type.

> 2.) For deriving from tagged-types

This is also a derived type. It's called a type extension, because the 
new type extends the parent.

> 3.) For using with pointers... that means for creating a
> pointer-object.

This is an allocator. It doesn't create a pointer object. It allocates 
an object of the designated type and returns an access value designating 
that object.

> 4.) In the context of the instantiation of generics

Annex P shows

derived type definition (including formal derived type definitions)

allocator

private extension definition

generic instantiation (including formal package definitions)

-- 
Jeff Carter
"We burst our pimples at you."
Monty Python & the Holy Grail
16




  reply	other threads:[~2004-10-05 19:37 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-10-04 23:54 Integer-Types Rick Santa-Cruz
2004-10-05  0:48 ` Integer-Types Jeffrey Carter
2004-10-05  1:12   ` Integer-Types Rick Santa-Cruz
2004-10-05 19:37     ` Jeffrey Carter [this message]
2004-10-05  1:23 ` Integer-Types Stephen Leake
2004-10-05  9:58   ` Integer-Types Rick Santa-Cruz
replies disabled

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