* Re: generic parameter Copy for primitifs types.
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
` (3 subsequent siblings)
4 siblings, 0 replies; 17+ messages in thread
From: Marc A. Criley @ 2005-06-23 17:47 UTC (permalink / raw)
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;
> ------
>
> At instanciation, for T_Item = Integer for example, is there a methode
> to take for Copy => ":=". Must I redefined Copy ? Can I say that, by
> default, take ":=" ?
No, you can't. The author of Toto presumably required the provision of
a Copy procedure because what it's going to be doing may not be
compatible with the default ":=". (Though perhaps T_Item should then
have been declared as "limited private".) If a special Copy procedure
wasn't needed, the implementation could've just used ":=", since that is
allowed for non-limited private types.
Even if the type you're supplying, e.g. Integer, would be fine with the
built-in assignment, the package author would want to ensure that you
had thought about it, and by writing a simple little function to do the
copying, he ensures that. (Though I also think the "Left" parameter
should probably be mode "out" rather than "in out".)
Marc A. Criley
www.mckae.com
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: generic parameter Copy for primitifs types.
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 18:30 ` Björn Persson
` (2 subsequent siblings)
4 siblings, 1 reply; 17+ messages in thread
From: Dmitry A. Kazakov @ 2005-06-23 17:54 UTC (permalink / raw)
On 23 Jun 2005 09:41:51 -0700, nblanpain@hotmail.com wrote:
> this is my problem :
>
> -----
> generic
> type T_Item is private;
> with procedure Copy (Left : in out T_Item; Right : in T_Item);
> package Toto is
> ...
> end Toto;
> ------
>
> At instanciation, for T_Item = Integer for example, is there a methode
> to take for Copy => ":=". Must I redefined Copy ? Can I say that, by
> default, take ":=" ?
No, for good or bad, but ":=" is not a procedure in Ada.
One point, the formal generic parameter T_Item is declared as "private".
This means that its actual counterpart must have assignment. So you can
write:
generic
type T_Item is private;
package Toto is
...
end Toto;
and use assignment within the body of Toto:
package body Toto is
...
X, Y : T_Item;
begin
...
X := Y;
...
end Toto;
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: generic parameter Copy for primitifs types.
2005-06-23 17:54 ` Dmitry A. Kazakov
@ 2005-06-23 19:41 ` nblanpain
2005-06-23 23:44 ` Marius Amado Alves
2005-06-24 7:28 ` Dmitry A. Kazakov
0 siblings, 2 replies; 17+ messages in thread
From: nblanpain @ 2005-06-23 19:41 UTC (permalink / raw)
But I want to use Copy for other types other than primitifs (integer,
float, boolean...). The best solution is to set ":=" by default but it
is not possible...
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: generic parameter Copy for primitifs types.
2005-06-23 19:41 ` nblanpain
@ 2005-06-23 23:44 ` Marius Amado Alves
2005-06-28 10:30 ` Matthew Heaney
2005-06-24 7:28 ` Dmitry A. Kazakov
1 sibling, 1 reply; 17+ messages in thread
From: Marius Amado Alves @ 2005-06-23 23:44 UTC (permalink / raw)
To: comp.lang.ada
> But I want to use Copy for other types other than primitifs (integer,
> float, boolean...). The best solution is to set ":=" by default but it
> is not possible...
If you really want a default you can do it with 2 levels of generics
and an access-to-subprogram type, as below (not tested). In Ada 2006 I
think you can do without the access type.
generic
type T is private;
package Level_1 is
type Copy_Ptr is access procedure (From : in T; To : out T);
procedure Default_Copy (From : in T; To : out T);
generic
Copy : Copy_Ptr := Default_Copy'Access;
package Level_2 is
...
end;
end;
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: generic parameter Copy for primitifs types.
2005-06-23 23:44 ` Marius Amado Alves
@ 2005-06-28 10:30 ` Matthew Heaney
2005-06-28 11:21 ` Marius Amado Alves
0 siblings, 1 reply; 17+ messages in thread
From: Matthew Heaney @ 2005-06-28 10:30 UTC (permalink / raw)
Marius Amado Alves <amado.alves@netcabo.pt> writes:
> generic
> type T is private;
> package Level_1 is
> type Copy_Ptr is access procedure (From : in T; To : out T);
> procedure Default_Copy (From : in T; To : out T);
> generic
> Copy : Copy_Ptr := Default_Copy'Access;
> package Level_2 is
> ...
> end;
> end;
The access type is unnecessary. The feature you want here has been
available since Ada83:
generic
type T is private;
package P is
procedure Default_Copy (From : in T; To : out T);
generic
with procedure Copy (From : in T; To : out T) is Default_Copy;
package Q is ...;
end P;
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: generic parameter Copy for primitifs types.
2005-06-28 10:30 ` Matthew Heaney
@ 2005-06-28 11:21 ` Marius Amado Alves
0 siblings, 0 replies; 17+ messages in thread
From: Marius Amado Alves @ 2005-06-28 11:21 UTC (permalink / raw)
To: comp.lang.ada
> The access type is unnecessary. The feature you want here has been
> available since Ada83:
>
> generic
> type T is private;
> package P is
> procedure Default_Copy (From : in T; To : out T);
>
> generic
> with procedure Copy (From : in T; To : out T) is Default_Copy;
> package Q is ...;
> end P;
Of course. What was I thinking? It's right there on RM95 12.6(2). I
hope the original poster is still listening, as surely this is the best
idiom.
I know what I was thinking. I was under the influence of a recent case
of (non-generic) subprograms with access-to-subprogram parameters with
defaults. Here you do need access types in Ada 95. Will the anonymous
access-to-subprogram types of Ada 2006 allow defaults?
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: generic parameter Copy for primitifs types.
2005-06-23 19:41 ` nblanpain
2005-06-23 23:44 ` Marius Amado Alves
@ 2005-06-24 7:28 ` Dmitry A. Kazakov
2005-06-24 11:45 ` Marius Amado Alves
1 sibling, 1 reply; 17+ messages in thread
From: Dmitry A. Kazakov @ 2005-06-24 7:28 UTC (permalink / raw)
On 23 Jun 2005 12:41:44 -0700, nblanpain@hotmail.com wrote:
> But I want to use Copy for other types other than primitifs (integer,
> float, boolean...).
Though other types have assignment too. So the cleanest way would be either
to consistently use it or to make the formal parameter T_Item limited and
provide Copy procedure as you did.
> The best solution is to set ":=" by default but it
> is not possible...
True. You can try generic "specialization" like this:
------------
generic
type T_Item is limited private;
with procedure Copy (Left : out T_Item; Right : in T_Item);
package Toto is
...
end Toto;
with Toto;
generic
type T_Item is private;
package Specialized_Toto is
procedure Copy (Left : out T_Item; Right : T_Item);
package Toto_Instance is new Toto (T_Item, Copy);
end Specialized_Toto;
package body Specialized_Toto is
procedure Copy (Left : out T_Item; Right : T_Item) is
begin
Left := Right;
end Copy;
end Specialized_Toto;
-------------
So instantiation with Integer might look like:
package Integer_Toto_Implementation is new Specialized_Toto (Integer);
package Integer_Toto renames Integer_Toto_Implementation.Toto_Instance;
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: generic parameter Copy for primitifs types.
2005-06-24 7:28 ` Dmitry A. Kazakov
@ 2005-06-24 11:45 ` Marius Amado Alves
2005-06-24 14:53 ` Dmitry A. Kazakov
0 siblings, 1 reply; 17+ messages in thread
From: Marius Amado Alves @ 2005-06-24 11:45 UTC (permalink / raw)
To: comp.lang.ada
On 24 Jun 2005, at 08:28, Dmitry A. Kazakov wrote:
>> The best solution is to set ":=" by default but it
>> is not possible...
>
> True. You can try generic "specialization" like this:
> ...
> generic
> type T_Item is private;
> package Specialized_Toto is
> procedure Copy (Left : out T_Item; Right : T_Item);
> package Toto_Instance is new Toto (T_Item, Copy);
> end Specialized_Toto;
> ...
True. But then the use is not uniform. A different generic package must
be selected for each case:
package Default_Pkg is new Specialized_Toto (T);
package Special_Pkg is new Toto (T, Special_Copy);
The idiom I have posted earlier does not have this problem. But,
granted, may have others instead, e.g. requiring two instantiations
instead of one for each case.
All this would go away if packages were first class citizens of Ada,
i.e. objects. This is one thing I'm trying to do with Ocpotus
(softdevelcoop.org/ocpotus).
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: generic parameter Copy for primitifs types.
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
0 siblings, 2 replies; 17+ messages in thread
From: Dmitry A. Kazakov @ 2005-06-24 14:53 UTC (permalink / raw)
On Fri, 24 Jun 2005 12:45:19 +0100, Marius Amado Alves wrote:
> On 24 Jun 2005, at 08:28, Dmitry A. Kazakov wrote:
>>> The best solution is to set ":=" by default but it
>>> is not possible...
>>
>> True. You can try generic "specialization" like this:
>> ...
>> generic
>> type T_Item is private;
>> package Specialized_Toto is
>> procedure Copy (Left : out T_Item; Right : T_Item);
>> package Toto_Instance is new Toto (T_Item, Copy);
>> end Specialized_Toto;
>> ...
>
> True. But then the use is not uniform. A different generic package must
> be selected for each case:
>
> package Default_Pkg is new Specialized_Toto (T);
> package Special_Pkg is new Toto (T, Special_Copy);
>
> The idiom I have posted earlier does not have this problem. But,
> granted, may have others instead, e.g. requiring two instantiations
> instead of one for each case.
>
> All this would go away if packages were first class citizens of Ada,
> i.e. objects. This is one thing I'm trying to do with Ocpotus
> (softdevelcoop.org/ocpotus).
Or
- if true package specialization were available;
- if generic types were ADT (you could have generic formal types of your
own);
- if assignment were a primitive operation (discriminants/constraints
problem is solvable, IMO);
- etc.
Packages as objects is a fundamental question I believe. A potential danger
is that when packages become objects any difference between "type" and
"package" might disappear. The result would be a type system close to C++.
But we know that C++ has immense problems in the elaboration model, or
better to say with absence of any model. Is it curable? I can't tell. If
Robert Dewar were here, he would probably shed more light on the mechanics.
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: generic parameter Copy for primitifs types.
2005-06-24 14:53 ` Dmitry A. Kazakov
@ 2005-06-24 15:11 ` Marius Amado Alves
2005-06-25 15:05 ` Martin Krischik
1 sibling, 0 replies; 17+ messages in thread
From: Marius Amado Alves @ 2005-06-24 15:11 UTC (permalink / raw)
To: comp.lang.ada
>> All this would go away if packages were first class citizens of Ada,
>> i.e. objects. This is one thing I'm trying to do with Ocpotus
>> (softdevelcoop.org/ocpotus).
>
> Or
>
> - if true package specialization were available;
>
> - if generic types were ADT (you could have generic formal types of
> your
> own);
>
> - if assignment were a primitive operation (discriminants/constraints
> problem is solvable, IMO);
>
> - etc.
>
Yes.
> Packages as objects is a fundamental question I believe. A potential
> danger
> is that when packages become objects any difference between "type" and
> "package" might disappear.
I don't see it as a danger, but as something I want.
> The result would be a type system close to C++.
I don't see this. C++ has the same schizophrenia between OO and
generics that Ada has.
> But we know that C++ has immense problems in the elaboration model, or
> better to say with absence of any model. Is it curable? I can't tell.
> If
> Robert Dewar were here, he would probably shed more light on the
> mechanics.
I think it's curable. However it may require relaxing the notion of
static typing. Namely generalizing the notion of type checking w.r.t.
bind times. End another aporia, that of static vs. dynamic, or compile
time vs. run time. Again, Ocpotus. But we drift away from Ada. Better
discuss this on the Ocpotus list, or privately.
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: generic parameter Copy for primitifs types.
2005-06-24 14:53 ` Dmitry A. Kazakov
2005-06-24 15:11 ` Marius Amado Alves
@ 2005-06-25 15:05 ` Martin Krischik
1 sibling, 0 replies; 17+ messages in thread
From: Martin Krischik @ 2005-06-25 15:05 UTC (permalink / raw)
Hyman Rosen wrote:
> Dmitry A. Kazakov wrote:
>> But we know that C++ has immense problems in the elaboration model, or
>> better to say with absence of any model.
>
> All C++ types are static - no type depends on a runtime value.
> So elaboration doesn't really mean anything for C++.
> What are these "immense problems" to which you refer?
Try:
class X
{
virutal inline F ();
}
inline X::F()
{
return;
}
Then use X from more then one object and or try to export/import it from a
DLL and or compile it with various C++ compilers.
Have fun.
Martin
--
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: generic parameter Copy for primitifs types.
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 18:30 ` Björn Persson
2005-06-23 19:39 ` nblanpain
2005-06-24 3:10 ` Jeffrey Carter
2005-06-27 4:28 ` Christoph Grein
4 siblings, 1 reply; 17+ messages in thread
From: Björn Persson @ 2005-06-23 18:30 UTC (permalink / raw)
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;
> ------
>
> At instanciation, for T_Item = Integer for example, is there a methode
> to take for Copy => ":=". Must I redefined Copy ? Can I say that, by
> default, take ":=" ?
The normal way when you need to do special things during assignment is
to use a controlled type. That is, you derive from
Ada.Finalization.Controlled and override Adjust:
type Special_Type is new Ada.Finalization.Controlled with record ...
procedure Adjust (Object : in out Special_Type) is ...
Then, instead of a Copy procedure you just use ":=", and Adjust will be
called. (See RM 7.6.)
Your generic package would then look like this:
generic
type T_Item is private;
package Toto is
...
end Toto;
package Integer_Toto is new Toto (Integer);
package Special_Toto is new Toto (Special_Type);
Or is there a reason why controlled types wouldn't work in your case?
--
Bj�rn Persson PGP key A88682FD
omb jor ers @sv ge.
r o.b n.p son eri nu
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: generic parameter Copy for primitifs types.
2005-06-23 16:41 generic parameter Copy for primitifs types nblanpain
` (2 preceding siblings ...)
2005-06-23 18:30 ` Björn Persson
@ 2005-06-24 3:10 ` Jeffrey Carter
2005-06-27 4:28 ` Christoph Grein
4 siblings, 0 replies; 17+ messages in thread
From: Jeffrey Carter @ 2005-06-24 3:10 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: generic parameter Copy for primitifs types.
2005-06-23 16:41 generic parameter Copy for primitifs types nblanpain
` (3 preceding siblings ...)
2005-06-24 3:10 ` Jeffrey Carter
@ 2005-06-27 4:28 ` Christoph Grein
4 siblings, 0 replies; 17+ messages in thread
From: Christoph Grein @ 2005-06-27 4:28 UTC (permalink / raw)
Cc: comp.lang.ada
nblanpain@hotmail.com wrote:
>Hello,
>
>this is my problem :
>
>-----
>generic
>type T_Item is private;
>with procedure Copy (Left : in out T_Item; Right : in T_Item);
>package Toto is
>...
>end Toto;
>------
>
>At instanciation, for T_Item = Integer for example, is there a methode
>to take for Copy => ":=". Must I redefined Copy ? Can I say that, by
>default, take ":=" ?
>
As you should know by now from all the answers there is no way to avoid
defining Copy.
As also others have pointed out, T_Item should probably be limited
private. As it stands,
Toto has := and Copy:
package body Toto is
...
X := Y; -- Do you really want both?
Copy (X, Y); -- which to use?
If T_Item is limited private, := is no longer available in Toto.
What you perhaps don't know is that the actual type for a limited formal
type need not be limited. If you say
generic
type T_Item is limited private;
then you can take any definite type as actual.
^ permalink raw reply [flat|nested] 17+ messages in thread