comp.lang.ada
 help / color / mirror / Atom feed
From: dennison@telepath.com (Ted Dennison)
Subject: Re: Novice help with types and de-allocation.
Date: 18 Nov 2002 07:29:53 -0800
Date: 2002-11-18T15:29:53+00:00	[thread overview]
Message-ID: <4519e058.0211180729.10b630dc@posting.google.com> (raw)
In-Reply-To: sX%B9.65239$NH2.4291@sccrnsc01

Stapler <spam.magnet@yahoo.com> wrote in message news:<sX%B9.65239$NH2.4291@sccrnsc01>...
> Alright, I have a package which more or less works but I'm having trouble
> in a couple spots. I've placed comments where I'm having trouble.

> 	type Buff_Out is array(Positive range <>) of Integer;
...
> 	type Int_Buffer is array(1..Size) of Integer;
...
> 	function Read_Buff(Y : in Int_Buffer; Read_Length : in Natural; Loc : in Natural) return Buff_Out is
> 	
> 		Ret_Buff : Buff_Out(1..Read_Length);
> 	begin
> 		-- For some reason, the compiler expects Y to be of type Buff_Out.
> 		-- Do I need to use Unchecked_Conversion here?
> 		Ret_Buff := Y(Loc .. Read_Length - 1);

The "some reason" is that the object you are assigning Y into is of
that type. Unlike some other languages, in Ada you can't assign
objects of one type into objects of a another type, just because both
types have similar contents. You defined Int_Buffer and Buff_Out to be
different types, so they are truly considered unrelated types.

Probably the best way out would be to make Int_Buffer a *subtype* of
Buff_Out, instead of a type in its own right. If you don't like that
solution, then the next best thing would be to make the private
definition of "Int_Buffer" a "new Buff_Out". That would allow you to
use a simple type conversion.

Do *not* used Unchecked_Conversion for this. Unchecked_Conversion is
not for bailing yourself out of a badly-designed type system. Use it
for when you truly have multiple different ways of looking at the data
(eg: A complicated record that requires a CRC at the end, so it must
also be an array of unsigned bytes).



  parent reply	other threads:[~2002-11-18 15:29 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-11-18  6:40 Novice help with types and de-allocation Stapler
2002-11-18 11:05 ` Colin Paul Gloster
2002-11-18 15:29 ` Ted Dennison [this message]
2002-11-18 21:33   ` Stapler
2002-11-18 23:34   ` Stapler
2002-11-18 19:06 ` Jeffrey Carter
replies disabled

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