comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam@spam.com>
Subject: Re: generic parameter Copy for primitifs types.
Date: Fri, 24 Jun 2005 03:10:46 GMT
Date: 2005-06-24T03:10:46+00:00	[thread overview]
Message-ID: <WQKue.8495$NX4.756@newsread1.news.pas.earthlink.net> (raw)
In-Reply-To: <1119544911.159343.288010@g43g2000cwa.googlegroups.com>

nblanpain@hotmail.com wrote:
> 
> -----
> generic
> type T_Item is private;
> with procedure Copy (Left : in out T_Item; Right : in T_Item);
> package Toto is
> ...
> end Toto;

Not that both ":=" and Copy are available to Toto for type T_Item. That 
seems odd; Toto is probably incorrectly specified.

Making Left (in Copy) mode in out can cause a problem with elementary 
types. Such types are checked for subtype conformance on copy-in to 
subprograms, and again on copy-out (elementary types are passed by 
copy). Consider a type

type T is range 1 .. 10;

and a variable

V : T;

and a procedure Copy such as yours for T. If I write

Copy (Left => V, Right => T'First);

the value of V will be checked against the range of T on copy-in to the 
procedure. Objects of type T will probably take 8 bits (or more) on most 
processors; thus, it's likely that the initial value of V will not be in 
T, and the call to Copy will raise Constraint_Error.

While you cannot pass default assignment to the generic, you can create 
a helper generic to make this case easier:

generic -- Assignment
    type Item is private;
procedure Assignment (To : out Item; From : in Item);

procedure Assignment (To : out Item; From : in Item) is
    -- null;
begin -- Assignment
    To := From;
end Assignment;

Then you can write

procedure Assign is new Assignment (Item => Integer);
package Dunno is new Toto (T_Item => Integer, Copy => Assign);

If you change Copy to Assign and use a default parameter:

    with procedure Assign (To : out T_Item; From : in T_Item) is <>;

then the 2nd instantiation can become

package Dummo is new Toto (T_Item => Integer);

-- 
Jeff Carter
"Sheriff murdered, crops burned, stores looted,
people stampeded, and cattle raped."
Blazing Saddles
35



  parent reply	other threads:[~2005-06-24  3:10 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-06-23 16:41 generic parameter Copy for primitifs types nblanpain
2005-06-23 17:47 ` Marc A. Criley
2005-06-23 17:54 ` Dmitry A. Kazakov
2005-06-23 19:41   ` nblanpain
2005-06-23 23:44     ` Marius Amado Alves
2005-06-28 10:30       ` Matthew Heaney
2005-06-28 11:21         ` Marius Amado Alves
2005-06-24  7:28     ` Dmitry A. Kazakov
2005-06-24 11:45       ` Marius Amado Alves
2005-06-24 14:53         ` Dmitry A. Kazakov
2005-06-24 15:11           ` Marius Amado Alves
2005-06-25 15:05           ` Martin Krischik
2005-06-23 18:30 ` Björn Persson
2005-06-23 19:39   ` nblanpain
2005-06-23 19:52     ` Marc A. Criley
2005-06-24  3:10 ` Jeffrey Carter [this message]
2005-06-27  4:28 ` 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