comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Constructors for Ada (was: Converting C++ class to Ada)
Date: 1996/12/12
Date: 1996-12-12T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023280001212962345240001@news.ni.net> (raw)
In-Reply-To: 58npck$iku$1@goanna.cs.rmit.edu.au


In article <58npck$iku$1@goanna.cs.rmit.edu.au>, Dale Stanbrough
<dale@goanna.cs.rmit.edu.au> wrote:

   (1) What do I do about the constructor? As I understand C++,
>       I need to force the user to supply values for the components of
>       Object_Type when something of that type is declared. How do I do
> this?"
>
>The only way to force this seems to be the use of private types with
>discriminants.
>
>        type Object_Type(<>) is private;
>        
>which says this _may_ have discriminants, but that's private.
>
>The only way to initialize and object, and you are forced to, is 
>via a function.
>
>        Item : Object_Type; -- illegal, must be initialized
>        
>        Item : Object_Type := Init(....); -- Ok.
>        
>The full type definition does not have to have the discriminants.
>IMHO, it would have been nicer to be able to say "this object must
>be initialized when declared" by using a different mechanism to this 
>(rather obscure) one.

Your solution is correct: call a function that returns a object of the
type.  Different functions can perform different kinds of initialization. 
But this only works if the type is non-limited (which is the normal case
anyway).

I'm not sure this technique - making the type indefinate to force
initialization - qualifies as "obscure," though.  Rather, it's just that we
haven't all been programming in Ada 95 long enough for this to be an
obvious idiom.

However, you have touched on an area where I feel there is an omission in
the language.

The issue I have with the language is that there is an asymmetry between
how stack objects are initialized and how heap objects are initialized.

For example, to initialize an object on the heap:

type Integer_Access is access Integer;
O : Integer_Access := new Integer'(4);

Why shouldn't I be able to do that for stack objects too?

O : Integer'(4);

What I'd like (and Dale too, it seems) is a way to initialize objects (on
the stack or heap) by invoking a subprogram explicitly identified as a
constructor, and be able to do so irrespective of whether the type is
limited or non-limited.

The problem is that in Ada initialization of an object on the stack
requires assignment, but this not need be so.  It's just a syntactic quirk
that prevents me from calling an initialization subprogram during
elaboration of a limited object.

For example, suppose I had a persistant queue.  I'd like to read its state
from disk by doing this:

Q : Queue'(Name => "my_queue.dat");

Though I suppose I could do that now if my queue were non-limited by using
a function:

Q : Queue := Create (Name => "my_queue.dat");


But suppose my type were limited.  For  example, I'd like to open a file
during elaboration of the file object:

F : File_Type'(Name => "my_file.dat", Mode => In_File);

That's something I can't do now, because type File_Type is limited private. 
So I have to do this

   F : File_Type;
begin
   Open (F, Name => "my_file.dat", Mode => In_File);


I'd like to see a subprogram explicitly identified as a constructor:

   type T is limited private;

   constructor T (Name : in String; Mode : in File_Mode);

or maybe 

   constructor (Name : in String; Mode : in File_Mode) initializes T;

But we can do better.  Ian Joyner made the point in his C++ critique that
it's often not obvious what each constructor does, because you only get the
argument part of the signiture - there is no meaningful name.  He points
out that in Eiffel each constructor has a name, too, unlike C++.

So maybe my hypothetical syntax is better if I can do this:

   constructor T Open (Name : in String; Mode : in File_Mode);

or maybe

   constructor Open (Name : in String; Mode : in File_Mode) initializes T;

Then we could use the name during initialization:

F : File_Type'Open (Name => "my_file.dat", Mode => In_File);

This feature is my current favorite for inclusion in a future version of
the language.

What do you think?

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
mheaney@ni.net
(818) 985-1271




  reply	other threads:[~1996-12-12  0:00 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-12-10  0:00 Converting C++ class to Ada Ken Garlington
1996-12-11  0:00 ` Jon S Anthony
1996-12-12  0:00   ` Ken Garlington
1996-12-18  0:00   ` Jon S Anthony
1996-12-18  0:00     ` Matthew Heaney
1996-12-19  0:00     ` Robert A Duff
1996-12-20  0:00       ` Stephen Leake
1996-12-20  0:00         ` Robert A Duff
1996-12-20  0:00   ` Norman H. Cohen
1996-12-20  0:00   ` Jon S Anthony
1996-12-20  0:00     ` Mitch Gart
1996-12-21  0:00   ` Jon S Anthony
1996-12-11  0:00 ` Norman H. Cohen
1996-12-12  0:00   ` Jon S Anthony
1996-12-11  0:00 ` Larry Kilgallen
1996-12-11  0:00 ` Stephen Leake
1996-12-13  0:00   ` Stephen Leake
1996-12-12  0:00 ` Dale Stanbrough
1996-12-12  0:00   ` Matthew Heaney [this message]
1996-12-13  0:00     ` Constructors for Ada (was: Converting C++ class to Ada) Norman H. Cohen
1996-12-13  0:00       ` Matthew Heaney
1996-12-17  0:00   ` Robert I. Eachus
replies disabled

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