comp.lang.ada
 help / color / mirror / Atom feed
* Construction/Destruction and copy semantics
@ 2001-07-22 16:33 Bertrand Augereau
  2001-07-22 17:42 ` Ehud Lamm
  2001-07-22 17:52 ` James Rogers
  0 siblings, 2 replies; 4+ messages in thread
From: Bertrand Augereau @ 2001-07-22 16:33 UTC (permalink / raw)


One of the (many) features I like in C++ is constructors/destructors
semantics.
I find it better than calling a construct procedure and destroy procedure
for non-trivial data types (those that have pointers to other ressources, or
sockets, or whetever, for instance).
You can't forget it this way.
Is the problem that I want to stick a C++ pattern in Ada? How do you Ada
programmers manage this problem?

Second thing, I tried to write some basic containers, and they rely on the
:= 'operator'. It seems to me you can't overload it in Ada.
So if you have a type which needs lazy copying for instance, you have to
define a 'copy' procedure for this type.
And then your container relies on this procedure. It is not that generic any
more at this point.
It seems bad to me as you can't instantiate your generic containers with the
types of somebody else who uses 'clone' instead.
Is it right? How do you adress this problem?

Thanks.
Bertrand

Btw, this is just for getting informed, not to start a language holy war
(some people tend to be paranoid)






^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Construction/Destruction and copy semantics
  2001-07-22 16:33 Construction/Destruction and copy semantics Bertrand Augereau
@ 2001-07-22 17:42 ` Ehud Lamm
  2001-07-22 17:52 ` James Rogers
  1 sibling, 0 replies; 4+ messages in thread
From: Ehud Lamm @ 2001-07-22 17:42 UTC (permalink / raw)


I am not sure I understand you. Do you have a critique of Ada's controlled
types, or don't you know they even exists?
You don't overload :=, you derive from Ada.Finalization.Controlled, and
define an Adjust procedure.

Ehud Lamm





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Construction/Destruction and copy semantics
  2001-07-22 16:33 Construction/Destruction and copy semantics Bertrand Augereau
  2001-07-22 17:42 ` Ehud Lamm
@ 2001-07-22 17:52 ` James Rogers
  2001-07-22 18:16   ` Bertrand Augereau
  1 sibling, 1 reply; 4+ messages in thread
From: James Rogers @ 2001-07-22 17:52 UTC (permalink / raw)


Bertrand Augereau wrote:
> 
> One of the (many) features I like in C++ is constructors/destructors
> semantics.
> I find it better than calling a construct procedure and destroy procedure
> for non-trivial data types (those that have pointers to other ressources, or
> sockets, or whetever, for instance).
> You can't forget it this way.
> Is the problem that I want to stick a C++ pattern in Ada? How do you Ada
> programmers manage this problem?
> Second thing, I tried to write some basic containers, and they rely on the
> := 'operator'. It seems to me you can't overload it in Ada.
> So if you have a type which needs lazy copying for instance, you have to
> define a 'copy' procedure for this type.
> And then your container relies on this procedure. It is not that generic any
> more at this point.
> It seems bad to me as you can't instantiate your generic containers with the
> types of somebody else who uses 'clone' instead.
> Is it right? How do you adress this problem?

The Ada approach to both of these questions is to create your tagged
types as extensions of either Ada.Finalization.Controlled or
Ada.Finalization.Limited_Controlled for the first question, and
Ada.Finalization.Controled for the second question.

The Ada.Finalization package defines two abstract tagged types.
Ada.Finalization.Controlled is for use when you want to create
non-limited types. Ada.Finalization.Limited_Controlled is used when
you want to create limited types.

The Ada.Finalization.Controlled type has three primitive procedures:
Initialze, Adjust, and Finalize. Initialize is similar to a C++
default constructor. Initialize is invoked immediately after the
normal initialization of a controlled object. Finalize is invoked
immediately before finalization of any of the components of a
controlled object. Adjust is invoked as the last step of an
assignment to a non-limited controlled object.

The Ada.Finalization package is described in section 7.6 of the
Ada Reference Manual.

I am sure you have noticed that the Initialization procedure does
not correspond to all forms of C++ constructors, but only the
default constructor. Ada allows you to handle this problem in
several ways. The manner which is IMHO most similar to a non-default
C++ constructor is to provide the tagged type with a discriminant.
This allows you to parameterize the creation of an object of 
that type. Combining type discriminants with the Initialize 
procedure will give you all the flexibility of a C++ constructor.

The adjust procedure can be used to customize the assignment of
a type. Note that Ada, by default, performs deep copies. Of course
one reason for that is that Ada code generally uses fewer access
types than C++ (compared with C++ pointers and references).
Shallow copying only makes sense for access types. If you
define an Ada record (tagged or not) containing fields which are
instances of access types, then copying an object of the record
will automatically result in shallow copies of the accessed
values. The "deep" copy operation for an access type is to copy
the value from the corresponding access type.

Jim Rogers
Colorado Springs, Colorado USA



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Construction/Destruction and copy semantics
  2001-07-22 17:52 ` James Rogers
@ 2001-07-22 18:16   ` Bertrand Augereau
  0 siblings, 0 replies; 4+ messages in thread
From: Bertrand Augereau @ 2001-07-22 18:16 UTC (permalink / raw)


Thanks, I had forgotten this (it's been 3 years since I last looked at Ada
code, at school, and I'm getting back to it for writing a program which
relies heavily on concurrency).
It solves the problems in an elegant way, except you have to work around the
different construction schemes with a discriminant
All this (controlled types) seems a bit syntactically "out of the language",
but I guess it is because it was an addition of Ada95, isn't it?

> The Ada approach to both of these questions is to create your tagged
> types as extensions of either Ada.Finalization.Controlled or
> Ada.Finalization.Limited_Controlled for the first question, and
> Ada.Finalization.Controled for the second question.
>
> The Ada.Finalization package defines two abstract tagged types.
> Ada.Finalization.Controlled is for use when you want to create
> non-limited types. Ada.Finalization.Limited_Controlled is used when
> you want to create limited types.
>
> The Ada.Finalization.Controlled type has three primitive procedures:
> Initialze, Adjust, and Finalize. Initialize is similar to a C++
> default constructor. Initialize is invoked immediately after the
> normal initialization of a controlled object. Finalize is invoked
> immediately before finalization of any of the components of a
> controlled object. Adjust is invoked as the last step of an
> assignment to a non-limited controlled object.
>
> The Ada.Finalization package is described in section 7.6 of the
> Ada Reference Manual.
>
> I am sure you have noticed that the Initialization procedure does
> not correspond to all forms of C++ constructors, but only the
> default constructor. Ada allows you to handle this problem in
> several ways. The manner which is IMHO most similar to a non-default
> C++ constructor is to provide the tagged type with a discriminant.
> This allows you to parameterize the creation of an object of
> that type. Combining type discriminants with the Initialize
> procedure will give you all the flexibility of a C++ constructor.
>
> The adjust procedure can be used to customize the assignment of
> a type. Note that Ada, by default, performs deep copies. Of course
> one reason for that is that Ada code generally uses fewer access
> types than C++ (compared with C++ pointers and references).
> Shallow copying only makes sense for access types. If you
> define an Ada record (tagged or not) containing fields which are
> instances of access types, then copying an object of the record
> will automatically result in shallow copies of the accessed
> values. The "deep" copy operation for an access type is to copy
> the value from the corresponding access type.
>
> Jim Rogers
> Colorado Springs, Colorado USA





^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2001-07-22 18:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-07-22 16:33 Construction/Destruction and copy semantics Bertrand Augereau
2001-07-22 17:42 ` Ehud Lamm
2001-07-22 17:52 ` James Rogers
2001-07-22 18:16   ` Bertrand Augereau

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