comp.lang.ada
 help / color / mirror / Atom feed
* How to hide type internals
@ 2006-06-30 10:50 Gerd
  2006-06-30 11:19 ` claude.simon
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Gerd @ 2006-06-30 10:50 UTC (permalink / raw)


Hi,

I would like to write a package that exports functions using a special
type, without showing the details in the spec (even not in the private
part).
In Modula-2 one can declare opaque types (which mostly are pointers but
also can be any type of word size).

Like this:

package keys is
  type key;
  function GetKey return key;
  procedure FetchElement (k : key);
-- implementation of key not visible here
end keys;

package body keys
 ...
  type Data is record
                     ...
                    end record;
  type key is access Data;
...
end keys;




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

* Re: How to hide type internals
  2006-06-30 10:50 How to hide type internals Gerd
@ 2006-06-30 11:19 ` claude.simon
  2006-07-04 11:13   ` Gerd
  2006-06-30 11:26 ` Alex R. Mosteo
  2006-07-02 15:23 ` Stephen Leake
  2 siblings, 1 reply; 6+ messages in thread
From: claude.simon @ 2006-06-30 11:19 UTC (permalink / raw)



Gerd a écrit :

> Hi,
>
> I would like to write a package that exports functions using a special
> type, without showing the details in the spec (even not in the private
> part).
> In Modula-2 one can declare opaque types (which mostly are pointers but
> also can be any type of word size).

> Like this:
>
> package keys is
>   type key;
>   function GetKey return key;
>   procedure FetchElement (k : key);
> -- implementation of key not visible here
> end keys;
>
> package body keys
>  ...
>   type Data is record
>                      ...
>                     end record;
>   type key is access Data;
> ...
> end keys;


package Keys is
   type Key is private;
   function Getkey return Key;
-- ...
private
   type Imp_Key; -- deferred type to the body
   type Key is access Imp_Key;
end Keys;

package body Keys is
   type Imp_Key is new Integer; -- whatever type
   function Getkey return Key
   is
      The_Key : Key := new Imp_Key;
   begin
      return The_Key;
   end GetKey;
end Keys;




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

* Re: How to hide type internals
  2006-06-30 10:50 How to hide type internals Gerd
  2006-06-30 11:19 ` claude.simon
@ 2006-06-30 11:26 ` Alex R. Mosteo
  2006-07-02 15:23 ` Stephen Leake
  2 siblings, 0 replies; 6+ messages in thread
From: Alex R. Mosteo @ 2006-06-30 11:26 UTC (permalink / raw)


Gerd wrote:

> Hi,
> 
> I would like to write a package that exports functions using a special
> type, without showing the details in the spec (even not in the private
> part).
> In Modula-2 one can declare opaque types (which mostly are pointers but
> also can be any type of word size).

I've done this in occasions where I want to have a multi-platform package,
so switching bodies is enough, keeping the same spec file. Like this:

package Blah is

   type External is private;

   -- Operations on external

private

   type Internal; -- forward declaration

   type Internal_Acess is access all Internal;

   type External is record
      Ptr : Internal_Access;
   end record;

end;

And then you define Internal in the body.

With Ada05 you can even get rid of Internal_Access and simply have

type External is record 
   Ptr : access Internal;
end record;

Of course you can have reference counting, deep copying or whatever making
External a Controlled type, so you can have a proper abstraction (This will
be probably a requisite for initialization anyways...)

Hope this helps.



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

* Re: How to hide type internals
  2006-06-30 10:50 How to hide type internals Gerd
  2006-06-30 11:19 ` claude.simon
  2006-06-30 11:26 ` Alex R. Mosteo
@ 2006-07-02 15:23 ` Stephen Leake
  2006-07-04 11:11   ` Gerd
  2 siblings, 1 reply; 6+ messages in thread
From: Stephen Leake @ 2006-07-02 15:23 UTC (permalink / raw)


"Gerd" <GerdM.O@t-online.de> writes:

> I would like to write a package that exports functions using a special
> type, without showing the details in the spec (even not in the private
> part).

Others have shown how to do this.

But I'd like to understand _why_ you want to do this.

If it is just to explore the capabilities of the language, that's
fine. 

But you should also understand the compiler issues involved. The
reason Ada provides a private part in package specs is to provide
information to the compiler to allow it to generate more efficient
code (not using pointers, for example). 

One downside of providing information in the spec is more
recompilation when the private part changes.

-- 
-- Stephe



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

* Re: How to hide type internals
  2006-07-02 15:23 ` Stephen Leake
@ 2006-07-04 11:11   ` Gerd
  0 siblings, 0 replies; 6+ messages in thread
From: Gerd @ 2006-07-04 11:11 UTC (permalink / raw)



Stephen Leake schrieb:

> "Gerd" <GerdM.O@t-online.de> writes:
>
> > I would like to write a package that exports functions using a special
> > type, without showing the details in the spec (even not in the private
> > part).
>
> Others have shown how to do this.
>
> But I'd like to understand _why_ you want to do this.

There are at least two reasons:

If the definition is left to the body, a change of the definition needs
only a recompilation of the body. If the full type is declared within
the spec, you would have to recompile .all. packages that "with" this
spec. Otherwise it would be enough to provide a compiled object-file
with the spec.

First this is a question of compile time (large system), second this
would give the contractor a look into your internals. The first is a
simple thing of money, the second is a security decision (e.g. if the
"user" of this package has no clearance).




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

* Re: How to hide type internals
  2006-06-30 11:19 ` claude.simon
@ 2006-07-04 11:13   ` Gerd
  0 siblings, 0 replies; 6+ messages in thread
From: Gerd @ 2006-07-04 11:13 UTC (permalink / raw)



claude.simon@equipement.gouv.fr schrieb:

> Gerd a écrit :
>
> > Hi,
> >
>
> package Keys is
>    type Key is private;
>    function Getkey return Key;
> -- ...
> private
>    type Imp_Key; -- deferred type to the body
>    type Key is access Imp_Key;
> end Keys;
 
Thanks, this is exacly what I was looking for.




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

end of thread, other threads:[~2006-07-04 11:13 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-06-30 10:50 How to hide type internals Gerd
2006-06-30 11:19 ` claude.simon
2006-07-04 11:13   ` Gerd
2006-06-30 11:26 ` Alex R. Mosteo
2006-07-02 15:23 ` Stephen Leake
2006-07-04 11:11   ` Gerd

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