comp.lang.ada
 help / color / mirror / Atom feed
* What is the point of Private?
@ 2005-04-28 23:22 Simon Smith
  2005-04-28 23:40 ` Ed Falis
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Simon Smith @ 2005-04-28 23:22 UTC (permalink / raw)


Hi,

I am a student at Glasgow uni in the UK and have been writing ada 
programs for my first two years of my course.

I have used and still do use private, eg

type Hash_Array is private;

blah...blah...

private

type Hash_Array is array (Integer range 0 .. 25) of Sc.Set;

end hash.ads;

  however I dont really understand the point of it.

Why is it there? I am sure I have heard talk that its to stop the client 
from seeing exactly how the data is stored/manipulated however if people 
really did want to know this wouldn't they just scroll down the ada spec 
file and find out for themselves or am I missing something here?

Simon



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

* Re: What is the point of Private?
  2005-04-28 23:22 What is the point of Private? Simon Smith
@ 2005-04-28 23:40 ` Ed Falis
  2005-04-29  3:14 ` Jeffrey Carter
  2005-04-30  0:49 ` chris
  2 siblings, 0 replies; 7+ messages in thread
From: Ed Falis @ 2005-04-28 23:40 UTC (permalink / raw)


On Thu, 28 Apr 2005 19:22:23 -0400, Simon Smith <Simon@skcs.co.uk> wrote:

>   however I dont really understand the point of it.
>
> Why is it there? I am sure I have heard talk that its to stop the client  
> from seeing exactly how the data is stored/manipulated however if people  
> really did want to know this wouldn't they just scroll down the ada spec  
> file and find out for themselves or am I missing something here?

The point is not to hide information from developers - it's to narrow the  
way that client code interacts with the services provided by a package, to  
provide interfaces that are straightforward to reason about.

An useful way to think about package specs is that the public part  
provides a logical interface representing the abstractions provided by the  
package, while the private part provides any additional relevant physical  
interface that the compiler might need.  (I believe this metaphor is from  
Tucker Taft).




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

* Re: What is the point of Private?
  2005-04-28 23:22 What is the point of Private? Simon Smith
  2005-04-28 23:40 ` Ed Falis
@ 2005-04-29  3:14 ` Jeffrey Carter
  2005-04-29  9:07   ` Peter Amey
  2005-04-30  0:49 ` chris
  2 siblings, 1 reply; 7+ messages in thread
From: Jeffrey Carter @ 2005-04-29  3:14 UTC (permalink / raw)


Simon Smith wrote:

> Why is it there? I am sure I have heard talk that its to stop the client 
> from seeing exactly how the data is stored/manipulated however if people 
> really did want to know this wouldn't they just scroll down the ada spec 
> file and find out for themselves or am I missing something here?

No, it was there originally to put stuff that the compiler needs to know 
to compile client code, but that the client code doesn't need to know to 
use the package. (Actually, the compiler probably doesn't need to know 
that stuff, and it could be put in the body, but in the late 1970's 
Ada's designers weren't willing to accept the extra work and overhead 
that would entail.)

Now, it also serves to put stuff that will be visible to the private 
parts and bodies of children, but not to anyone else.

-- 
Jeff Carter
"Beyond 100,000 lines of code you
should probably be coding in Ada."
P. J. Plauger
26



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

* Re: What is the point of Private?
  2005-04-29  3:14 ` Jeffrey Carter
@ 2005-04-29  9:07   ` Peter Amey
  2005-04-30  3:27     ` Jeffrey Carter
  0 siblings, 1 reply; 7+ messages in thread
From: Peter Amey @ 2005-04-29  9:07 UTC (permalink / raw)




Jeffrey Carter wrote:
> Simon Smith wrote:
> 
>> Why is it there? I am sure I have heard talk that its to stop the 
>> client from seeing exactly how the data is stored/manipulated however 
>> if people really did want to know this wouldn't they just scroll down 
>> the ada spec file and find out for themselves or am I missing 
>> something here?
> 
> 
> No, it was there originally to put stuff that the compiler needs to know 
> to compile client code, but that the client code doesn't need to know to 
> use the package. (Actually, the compiler probably doesn't need to know 
> that stuff, and it could be put in the body, but in the late 1970's 
> Ada's designers weren't willing to accept the extra work and overhead 
> that would entail.)

I think there are real compiler design issues here.  Modula 2 had 
"opaque types" which were conceptually similar to private types. 
However, Modula 2 had no equivalent of a package's private part which 
meant that the size of the opaque type wasn't known and that all 
implementations therefore required an opaque type to be implemented as a 
pointer.  Ada's scheme means the size is known to the compiler from the 
package spec alone.


> 
> Now, it also serves to put stuff that will be visible to the private 
> parts and bodies of children, but not to anyone else.
> 

Which is rather useful!

Peter




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

* Re: What is the point of Private?
  2005-04-28 23:22 What is the point of Private? Simon Smith
  2005-04-28 23:40 ` Ed Falis
  2005-04-29  3:14 ` Jeffrey Carter
@ 2005-04-30  0:49 ` chris
  2005-05-01 22:57   ` Simon Smith
  2 siblings, 1 reply; 7+ messages in thread
From: chris @ 2005-04-30  0:49 UTC (permalink / raw)


Simon Smith wrote:
> Hi,
> 
> I am a student at Glasgow uni in the UK and have been writing ada 
> programs for my first two years of my course.

Great uni :)  If you do third year CS, have fun on the OS course... C 
with pthreads.  I remember it well. :)

Actually it's not that bad, but I have to maintain the fear and dread 
surrounding it because I too had to endure the dread and Dr Dickman is 
my project supervisor. ;)

> I have used and still do use private, eg
> 
> type Hash_Array is private;
> 
> blah...blah...
> 
> private
> 
> type Hash_Array is array (Integer range 0 .. 25) of Sc.Set;
> 
> end hash.ads;
> 
>  however I dont really understand the point of it.

It's there for two reasons.  It hides the implementation from clients of 
the code and makes the compilers job easier.  The decision seems to be 
that the compiler should be able to determine how much space something 
will take at compile time*.  This means they had to put the 
representation in the spec, so the compiler knew what to allocate.

The alternatives are to leave everything public or to put the 
representation in the body.  Making everything public means someone can 
adversely manipulate the contents of your data structures.  They can 
break the rules, and mess up the software.  Completely hiding the 
representation by putting it in the body has a different trade off.  On 
the one hand, you can substitute any representation by changing the body 
alone, but on the other hand the compiler won't know how big the data 
structure is and it has to treat all things the same way (by wrapping or 
boxing them up - which has a cost).

It's a trade off.  The Ada people chose to put it in the spec because it 
simplified the compiler, and it best suited their over all goals.  Other 
languages allow you to completely hide the representation in the 
implementation.  e.g. SML, Ocaml.  Some make it public.

> Why is it there? I am sure I have heard talk that its to stop the client 
> from seeing exactly how the data is stored/manipulated however if people 
> really did want to know this wouldn't they just scroll down the ada spec 
> file and find out for themselves or am I missing something here?

Client code can't do that though.  This means someone who uses the 
compiled package can't see how it's implemented, but the maintainer can.



Chris

*There are probably other reasons too, I don't know what they are though.



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

* Re: What is the point of Private?
  2005-04-29  9:07   ` Peter Amey
@ 2005-04-30  3:27     ` Jeffrey Carter
  0 siblings, 0 replies; 7+ messages in thread
From: Jeffrey Carter @ 2005-04-30  3:27 UTC (permalink / raw)


Peter Amey wrote:
> 
> I think there are real compiler design issues here.  Modula 2 had 
> "opaque types" which were conceptually similar to private types. 
> However, Modula 2 had no equivalent of a package's private part which 
> meant that the size of the opaque type wasn't known and that all 
> implementations therefore required an opaque type to be implemented as a 
> pointer.  Ada's scheme means the size is known to the compiler from the 
> package spec alone.

Right. That's why it's not really needed by the compiler (which was the 
explanation I got in 1984), but it does make compilers a little simpler 
and eliminates the overhead of the indirection.

-- 
Jeff Carter
"Spam! Spam! Spam! Spam! Spam! Spam! Spam! Spam!"
Monty Python's Flying Circus
53



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

* Re: What is the point of Private?
  2005-04-30  0:49 ` chris
@ 2005-05-01 22:57   ` Simon Smith
  0 siblings, 0 replies; 7+ messages in thread
From: Simon Smith @ 2005-05-01 22:57 UTC (permalink / raw)


thanks for all the replies.

All beginning to make sense now :-)

chris wrote:

> It's there for two reasons.  It hides the implementation from clients of 
> the code and makes the compilers job easier.  The decision seems to be 
> that the compiler should be able to determine how much space something 
> will take at compile time*.  This means they had to put the 
> representation in the spec, so the compiler knew what to allocate.
> 
> The alternatives are to leave everything public or to put the 
> representation in the body.  Making everything public means someone can 
> adversely manipulate the contents of your data structures.  They can 
> break the rules, and mess up the software.  Completely hiding the 
> representation by putting it in the body has a different trade off.  On 
> the one hand, you can substitute any representation by changing the body 
> alone, but on the other hand the compiler won't know how big the data 
> structure is and it has to treat all things the same way (by wrapping or 
> boxing them up - which has a cost).
> 
> It's a trade off.  The Ada people chose to put it in the spec because it 
> simplified the compiler, and it best suited their over all goals.  Other 
> languages allow you to completely hide the representation in the 
> implementation.  e.g. SML, Ocaml.  Some make it public.
> 
>> Why is it there? I am sure I have heard talk that its to stop the 
>> client from seeing exactly how the data is stored/manipulated however 
>> if people really did want to know this wouldn't they just scroll down 
>> the ada spec file and find out for themselves or am I missing 
>> something here?
> 
> 
> Client code can't do that though.  This means someone who uses the 
> compiled package can't see how it's implemented, but the maintainer can.
> 
> 
> 
> Chris
> 
> *There are probably other reasons too, I don't know what they are though.



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

end of thread, other threads:[~2005-05-01 22:57 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-04-28 23:22 What is the point of Private? Simon Smith
2005-04-28 23:40 ` Ed Falis
2005-04-29  3:14 ` Jeffrey Carter
2005-04-29  9:07   ` Peter Amey
2005-04-30  3:27     ` Jeffrey Carter
2005-04-30  0:49 ` chris
2005-05-01 22:57   ` Simon Smith

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