Re: How to hide type internals




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;

.



Relevant Pages