comp.lang.ada
 help / color / mirror / Atom feed
From: Gautier write-only <gautier_niouzes@hotmail.com>
Subject: Re: 'private' and Privacy
Date: Tue, 7 Jul 2009 23:51:09 -0700 (PDT)
Date: 2009-07-07T23:51:09-07:00	[thread overview]
Message-ID: <b166c75a-d448-4a23-8031-ac061441b03f@h2g2000yqg.googlegroups.com> (raw)
In-Reply-To: 843a36b0-041d-4826-98b4-0fbcb1a4d287@d9g2000prh.googlegroups.com

On 8 Jul., 04:48, Rick <rickdu...@gmail.com> wrote:
> I have never fathomed out why we declare entities in packages to be
> 'private' and then have to go and tell the world what is in them.

I also wondered why, until I began using it. I elaborate on your
example:

package Test_private_pkg is

   type Key_Type is private;
   type Keys_Type is private;

   procedure Define_Key(k: Key_Type; val: Integer);
   --
   function Get_Key(k: Keys_Type; i,j: Integer) return Key_Type;
   procedure Set_Key(k: Keys_Type; i,j: Integer; new_val: Key_Type);

private

   type Key_Type is new Integer;

   KEYPAD_ROWS_COUNT : constant Positive := 2;
   -- The number of rows on a keypad.
   KEYPAD_COLUMNS_COUNT : constant Positive := 2;
   -- The number of columns on a keypad.
   type Keys_Type is array
     (1 .. KEYPAD_ROWS_COUNT, 1 .. KEYPAD_COLUMNS_COUNT)
   of Key_Type;

end;

> I am trying to find a way to ensure that the user only addresses items
> in the array in the manner I provide rather than making use of the
> information clearly visible about the range of the array.

Now, with the private definitions as above, we have exactly what you
need:

with Test_private_pkg;
use Test_private_pkg;

procedure Test_private is

  key: Key_Type;
  keys: Keys_Type;

  i: Integer;

begin
  Define_key(key, 123);
  Set_Key(keys, 1,2, key);

  i:= KEYPAD_ROWS_COUNT; -- You don't want the user to do that

>>> test_private.adb:15:07: "KEYPAD_ROWS_COUNT" is not visible
>>> test_private.adb:15:07: non-visible (private) declaration at test_private_pkg.ads:15

The reason why the private things are not hidden is that the compiler
needs to know about type sizes and perhaps other details when using
the private types.
If you have to really hide information (passwords, software
registration ID's,... ), you need to use functions, provide the
specification and not the body's source code, and in the body you may
even need to encrypt your stuff. But it is another topic...
_________________________________________________________
Gautier's Ada programming -- http://sf.net/users/gdemont/

NB: For a direct answer, e-mail address on the Web site!



  parent reply	other threads:[~2009-07-08  6:51 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-08  2:48 'private' and Privacy Rick
2009-07-08  2:48 ` stefan-lucks
2009-07-08  6:51 ` Gautier write-only [this message]
2009-07-08 12:47 ` Ludovic Brenta
2009-07-08 15:25 ` (see below)
replies disabled

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