From: "Robert I. Eachus" <rieachus@attbi.com>
Subject: Re: problem with abstract types
Date: Thu, 31 Oct 2002 17:18:50 GMT
Date: 2002-10-31T17:18:50+00:00 [thread overview]
Message-ID: <3DC165F9.9000000@attbi.com> (raw)
In-Reply-To: ohfrpa-ua1.ln@uli.uli-eckhardt.de
Ulrich Eckhardt wrote:
> No. openRegistry is first defined abstract and then implemented
> as openRegistry(reg : in out file_registry; ..) and
> openRegistry(reg : in out win_registry; ..) .
Error, C++ thinking detected. In Ada, unless you really have use them
for some reason, avoid access types. Access types IMHO, should be
buried in packages which implement container abstractions and never show
up in package specifications or user code.
If you change your specification to:
package registry is
type registry is abstract tagged limited private;
function openRegistry(Priviledge: in Sys_Access
Registry_Name: String)
return registry is abstract;
[..]
exception Registry_Error; -- if you must.
end Registry;
In the two (or more!) child packages:
function openRegistry (a : in sys_access;
p_name : String)
return file_registry is --change as necessary.
Reg: file_registry;
begin
-- any non-default actions here...
return Reg;
end fileRegistry;
Then your test program becomes:
with registry; use registry;
procedure main is
declare
reg := openRegistry(CLASS_USER,"registrytest");
begin
null; -- for now.
exception
when Registry_Error => -- do something useful like printing
-- location.
when others => -- be prepared for anything in a test program...
end;
exception
when others => -- catch finalization bugs here.
end main;
Now you might think that Reg is declared inside a function and that it
will either need to be declared on the heap, or copied during the
return. LET THE COMPILER WORRY ABOUT ALL THAT. In practice the
compiler will probably build the return value in place if it can inline
openRegistry. But it is not your problem, and a lot of unnecessary code
goes away.
Also you will note that I got rid of any need for Unbounded_String. It
may be that some of the individual implementations of openRegistry will
need to use Unbounded_String, but if the name never changes, you
probably won't. In any case, make things easy for the user of the
abstraction. In this case it also makes it easier for the implementor.
next prev parent reply other threads:[~2002-10-31 17:18 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-10-31 12:06 problem with abstract types Ulrich Eckhardt
2002-10-31 13:15 ` John English
2002-10-31 14:43 ` Ulrich Eckhardt
2002-10-31 17:18 ` Robert I. Eachus [this message]
2002-10-31 18:20 ` Jeffrey Carter
2002-10-31 20:58 ` Robert I. Eachus
2002-11-01 10:57 ` Ulrich Eckhardt
2002-11-01 12:05 ` Simon Wright
2002-11-01 17:03 ` Robert A Duff
2002-11-04 15:18 ` Robert I. Eachus
2002-11-04 16:14 ` Robert A Duff
2002-10-31 13:32 ` David C. Hoos
2002-10-31 13:47 ` problem with posters Peter Hermann
2002-10-31 14:15 ` Preben Randhol
2002-10-31 14:35 ` problem with abstract types Ulrich Eckhardt
2002-11-01 8:31 ` Dmitry A.Kazakov
2002-11-01 11:32 ` Ulrich Eckhardt
2002-10-31 15:31 ` Simon Wright
2002-10-31 17:22 ` Stephen Leake
2002-11-01 10:25 ` Ulrich Eckhardt
2002-11-04 14:30 ` Ted Dennison
2002-11-04 15:08 ` Ulrich Eckhardt
2002-11-04 15:32 ` Stephen Leake
2002-11-04 17:12 ` Ulrich Eckhardt
2002-11-04 17:43 ` David C. Hoos
2002-11-04 19:34 ` Ulrich Eckhardt
2002-11-04 19:54 ` Stephen Leake
2002-11-04 20:08 ` Robert A Duff
2002-11-01 11:15 ` Preben Randhol
2002-11-01 17:21 ` Stephen Leake
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox