comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adambeneschan@gmail.com>
Subject: Re: What is the difference of "with null record" and "with private"?
Date: Fri, 23 May 2014 12:42:06 -0700 (PDT)
Date: 2014-05-23T12:42:06-07:00	[thread overview]
Message-ID: <70638024-2a8b-45e1-aea4-5964f4416abf@googlegroups.com> (raw)
In-Reply-To: <29a09fe5-72c0-40e0-bf72-632c4a13232e@googlegroups.com>

On Friday, May 23, 2014 11:55:09 AM UTC-7, mockturtle wrote:
> On Friday, May 23, 2014 8:21:21 PM UTC+2, Victor Porton wrote:

> One could say that "with private" is used to hide the internal details of your type and that if the extension is done with a "null record" there is not much to hide. Actually, I cannot imagine a case where the fact that it is public that the extension is null can cause loss of maintainability the day some new fields are added.  Nevertheless, usually I prefer the "with private" form for uniformity.

I can easily imagine a case.

    package Pack0 is
        type Base_Record is tagged record
            F1 : Integer;
        end record;
    end Pack0;

    package Pack1 is
        type An_Extension is new Base_Record with null record;
        type A_Private_Extension is new Base_Record with private;
    private
        type A_Private_Extension is new Base_Record with null record;
    end Pack1;

...

    procedure Proc is        -- in some other package
        A1 : An_Extension := (F1 => 3);         -- legal 
        A2 : A_Private_Extension := (F1 => 3);  -- illegal
    begin 
        ...

The point here is that when declaring an aggregate of type An_Extension, the program can count on the fact that the extension has no additional fields, while for A_Private_Extension, it can't.  **The fact that A_Private_Extension has no additional fields is an implementation detail that is hidden from the rest of the program.**  It's not true that there is "nothing to hide" ... the fact that there are no details is definitely something that may need to be hidden.  Suppose that you later change the implementation of Pack1 and decide to add some fields to A_Private_Extension.  Since the implementation details are hidden, you can change them without worrying that you will break something else in some other package far, far away.  (This wouldn't be true if the declaration of A2 were legal.)  And that's the whole purpose of "private" and the whole purpose of encapsulation in general--so that hidden implementation details can be changed without worrying about having to search every user of the package to see if it needs to change.

                               -- Adam


  reply	other threads:[~2014-05-23 19:42 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-23 18:19 What is the difference of "with null record" and "with private"? Victor Porton
2014-05-23 18:21 ` Victor Porton
2014-05-23 18:55   ` mockturtle
2014-05-23 19:42     ` Adam Beneschan [this message]
2014-05-23 22:05       ` Adam Beneschan
2014-05-24  8:19       ` mockturtle
2014-05-23 20:45     ` Georg Bauhaus
2014-05-23 18:59 ` Dan'l Miller
2014-05-23 21:45 ` Shark8
2014-05-24 10:49   ` Victor Porton
2014-05-24 17:33     ` Shark8
2014-05-23 21:55 ` Randy Brukardt
replies disabled

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