comp.lang.ada
 help / color / mirror / Atom feed
* Protected types and visibility of their internals
@ 2008-07-08  9:57 Maciej Sobczak
  2008-07-08 12:52 ` Georg Bauhaus
                   ` (5 more replies)
  0 siblings, 6 replies; 14+ messages in thread
From: Maciej Sobczak @ 2008-07-08  9:57 UTC (permalink / raw)


Consider a protected type in some package:

package P is
   protected type PT is
      procedure Foo;
   private
      X : Some_Type;
   end PT;
end P;

The protected type has a component X of Some_Type, which is entirely
the private business of the protected type.

Where this type should be declared?

My first natural attempt was to put the declaration of Some_Type in
the private part of PT, but this seems to be impossible even at the
grammar level.
My second, although less natural attempt was to put it to the private
part of P, but then it is not visible to PT.

The only place that seems to be allowed is the visible part of P. I
don't find it very comfortable, because I do not intend the users of P
to see Some_Type.
What is the rationale for this?
Can you recommend some other solution that enforces strict coding
practice?

--
Maciej Sobczak * www.msobczak.com * www.inspirel.com



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

* Re: Protected types and visibility of their internals
  2008-07-08  9:57 Protected types and visibility of their internals Maciej Sobczak
@ 2008-07-08 12:52 ` Georg Bauhaus
  2008-07-08 16:13 ` Robert A Duff
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 14+ messages in thread
From: Georg Bauhaus @ 2008-07-08 12:52 UTC (permalink / raw)


Maciej Sobczak schrieb:
> Consider a protected type in some package:
> 
> package P is
>    protected type PT is
>       procedure Foo;
>    private
>       X : Some_Type;
>    end PT;
> end P;
> ...
> Can you recommend some other solution that enforces strict coding
> practice?

I'm assuming you want clients to consider PT  some synchronized
abstract data type? Using Ada 83, a passive task might have
worked :-) Using Ada 95, I'd consider moving the protected
type to the body, and provide procedural wrappers.
Using current Ada, the following might work, though
I have probably made a few grammatical mistakes in a rush;
all GNATs crash on this is some way or other:

package News21 is

    type Only_Ops is synchronized interface;
    procedure Foo(It: in out Only_Ops) is abstract;

    type PT is limited new Only_Ops with private;

private

    type Some_Type is null record;

    protected type PT is  new Only_Ops with
       overriding
       procedure Foo;
    private
       X : Some_Type;
    end PT;

end News21;


--
Georg Bauhaus
Y A Time Drain  http://www.9toX.de



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

* Re: Protected types and visibility of their internals
  2008-07-08  9:57 Protected types and visibility of their internals Maciej Sobczak
  2008-07-08 12:52 ` Georg Bauhaus
@ 2008-07-08 16:13 ` Robert A Duff
  2008-07-09  7:53   ` christoph.grein
  2008-07-08 21:03 ` Randy Brukardt
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 14+ messages in thread
From: Robert A Duff @ 2008-07-08 16:13 UTC (permalink / raw)


Maciej Sobczak <see.my.homepage@gmail.com> writes:

> Consider a protected type in some package:
>
> package P is
>    protected type PT is
>       procedure Foo;
>    private
>       X : Some_Type;
>    end PT;
> end P;
>
> The protected type has a component X of Some_Type, which is entirely
> the private business of the protected type.
>
> Where this type should be declared?

I don't think there's any good solution to this.

You could put it in a nested package called Private_Stuff,
with a comment saying clients shouldn't mess with it.

> What is the rationale for this?

I don't remember exactly, but I think there are anomalies if you start
allowing types to be declared inside types.

- Bob



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

* Re: Protected types and visibility of their internals
  2008-07-08  9:57 Protected types and visibility of their internals Maciej Sobczak
  2008-07-08 12:52 ` Georg Bauhaus
  2008-07-08 16:13 ` Robert A Duff
@ 2008-07-08 21:03 ` Randy Brukardt
  2008-07-10 21:49   ` Maciej Sobczak
  2008-07-09  5:04 ` christoph.grein
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 14+ messages in thread
From: Randy Brukardt @ 2008-07-08 21:03 UTC (permalink / raw)


"Maciej Sobczak" <see.my.homepage@gmail.com> wrote in message 
news:b1f2203b-ed02-4031-bcba-40c0e1b1be07@j22g2000hsf.googlegroups.com...
> Consider a protected type in some package:
>
> package P is
>   protected type PT is
>      procedure Foo;
>   private
>      X : Some_Type;
>   end PT;
> end P;
>
> The protected type has a component X of Some_Type, which is entirely
> the private business of the protected type.
>
> Where this type should be declared?

I think you are asking the wrong question. The question is "Where should the 
protected type be declared?". Since there are no operations that are 
specific to protected types, there is very little reason to make those a 
visible type.

Thus, I would generally structure the package with a private type and the 
protected type (if any) declared in the private part. Something like:

 package P2 is
     type Priv is tagged limited private;
     procedure Foo (Obj : in out Priv);
 private
    type Some_Type is ...
    protected type PT is
        procedure Foo;
   private
      X : Some_Type;
   end PT;
   type Priv is tagged limited record
        Lock : PT;
   end record;
end P2;

This structure works well because it is always important to make the 
protected portion of the operation as short as possible, and this layout 
allows a place to do things that don't need mutual exclusion.

There is an alternative if you really need access to the protected object 
directly, which is to declare a protected interface in the visible part of 
the package and then derive the protected type from it in the private part.

Either way, you will find that you put most of your declarations in the 
private part. It's not unusual for the majority of the code of a package 
specification to be in the private part.

                                Randy.








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

* Re: Protected types and visibility of their internals
  2008-07-08  9:57 Protected types and visibility of their internals Maciej Sobczak
                   ` (2 preceding siblings ...)
  2008-07-08 21:03 ` Randy Brukardt
@ 2008-07-09  5:04 ` christoph.grein
  2008-07-09 13:44   ` Maciej Sobczak
  2008-07-09 15:12   ` Adam Beneschan
  2008-07-09 22:19 ` Stephen Leake
  2008-07-09 23:22 ` jimmaureenrogers
  5 siblings, 2 replies; 14+ messages in thread
From: christoph.grein @ 2008-07-09  5:04 UTC (permalink / raw)


If you want the protected type to be visible, alas there is no other
place to declare this type than in the visible part of the package.
This is inconvenient. The rationale for this?

You can however hide the PO in the body and export the operations via
renaming.



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

* Re: Protected types and visibility of their internals
  2008-07-08 16:13 ` Robert A Duff
@ 2008-07-09  7:53   ` christoph.grein
  0 siblings, 0 replies; 14+ messages in thread
From: christoph.grein @ 2008-07-09  7:53 UTC (permalink / raw)


> What is the rationale for this?

http://www.adaic.com/standards/95rat/RAThtml/rat95-p2-9.html

Quote from the end of 9.1.2:

A minor point is that the type Item_Array has to be declared outside
the protected type. This is because type declarations are not allowed
inside a protected type which generally follows the same rules as
records. Allowing types within types would have introduced additional
complexity with little benefit.



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

* Re: Protected types and visibility of their internals
  2008-07-09  5:04 ` christoph.grein
@ 2008-07-09 13:44   ` Maciej Sobczak
  2008-07-09 15:12   ` Adam Beneschan
  1 sibling, 0 replies; 14+ messages in thread
From: Maciej Sobczak @ 2008-07-09 13:44 UTC (permalink / raw)


On 9 Lip, 07:04, christoph.gr...@eurocopter.com wrote:

> You can however hide the PO in the body and export the operations via
> renaming.

I want the protected type to be a type so that users of the package
can make instances of it. In other words, it is not a singleton (this
would justify moving the state to the package body).

The "real" solution is synchronized interface + derived hidden
implementation + factory function, but this solution happens to be
also very heavy-handed and... fragile with regard to the ability of
today compilers to properly digest it. Sigh.

--
Maciej Sobczak * www.msobczak.com * www.inspirel.com



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

* Re: Protected types and visibility of their internals
  2008-07-09  5:04 ` christoph.grein
  2008-07-09 13:44   ` Maciej Sobczak
@ 2008-07-09 15:12   ` Adam Beneschan
  1 sibling, 0 replies; 14+ messages in thread
From: Adam Beneschan @ 2008-07-09 15:12 UTC (permalink / raw)


On Jul 8, 10:04 pm, christoph.gr...@eurocopter.com wrote:
> If you want the protected type to be visible, alas there is no other
> place to declare this type than in the visible part of the package.
> This is inconvenient. The rationale for this?
>
> You can however hide the PO in the body and export the operations via
> renaming.

Except that if he's trying to declare a protected *type*, that
probably means he wants to declare more than one protected object of
that type, so renaming isn't going to work.  You'll probably have to
use a wrapper or follow Georg's suggestion.

                              -- Adam






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

* Re: Protected types and visibility of their internals
  2008-07-08  9:57 Protected types and visibility of their internals Maciej Sobczak
                   ` (3 preceding siblings ...)
  2008-07-09  5:04 ` christoph.grein
@ 2008-07-09 22:19 ` Stephen Leake
  2008-07-09 22:38   ` Adam Beneschan
  2008-07-09 23:22 ` jimmaureenrogers
  5 siblings, 1 reply; 14+ messages in thread
From: Stephen Leake @ 2008-07-09 22:19 UTC (permalink / raw)


Maciej Sobczak <see.my.homepage@gmail.com> writes:

> Consider a protected type in some package:
>
> package P is
>    protected type PT is
>       procedure Foo;
>    private
>       X : Some_Type;
>    end PT;
> end P;
>
> The protected type has a component X of Some_Type, which is entirely
> the private business of the protected type.
>
> Where this type should be declared?

In a sibling private package:

private
package P.stuff is

    type Some_Type is ...;
end P.Stuff;

private with P.stuff;
package P is
   protected type PT is
      procedure Foo;
   private
      X : P.Stuff.Some_Type;
   end PT;
end P;

Hmm. Maybe that's not right, since P.Stuff is used in the private part
of a public type, not strictly in the private part of P. But it
_should_ be ok!

-- 
-- Stephe



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

* Re: Protected types and visibility of their internals
  2008-07-09 22:19 ` Stephen Leake
@ 2008-07-09 22:38   ` Adam Beneschan
  2008-07-10  0:18     ` Randy Brukardt
  2008-07-10  0:18     ` Randy Brukardt
  0 siblings, 2 replies; 14+ messages in thread
From: Adam Beneschan @ 2008-07-09 22:38 UTC (permalink / raw)


On Jul 9, 3:19 pm, Stephen Leake <Stephe.Le...@nasa.gov> wrote:
> Maciej Sobczak <see.my.homep...@gmail.com> writes:
> > Consider a protected type in some package:
>
> > package P is
> >    protected type PT is
> >       procedure Foo;
> >    private
> >       X : Some_Type;
> >    end PT;
> > end P;
>
> > The protected type has a component X of Some_Type, which is entirely
> > the private business of the protected type.
>
> > Where this type should be declared?
>
> In a sibling private package:
>
> private
> package P.stuff is
>
>     type Some_Type is ...;
> end P.Stuff;
>
> private with P.stuff;
> package P is
>    protected type PT is
>       procedure Foo;
>    private
>       X : P.Stuff.Some_Type;
>    end PT;
> end P;
>
> Hmm. Maybe that's not right, since P.Stuff is used in the private part
> of a public type, not strictly in the private part of P. But it
> _should_ be ok!

It appears that private parts of protected types *can* use things
declared in packages mentioned with "private with".  However, your
example won't work because you can't have the specification of P
"with" a child of P; this creates a circular dependency.  You *could*
do this, however:

private with P.Stuff;
package P.Stuff2 is
   protected type PT is
      procedure Foo;
   private
      X : P.Stuff.Some_Type;
   end PT;
end P.Stuff2;

This still won't solve Maciej's problem if the definition of Some_Type
requires some other types declared earlier in Stuff2.  If that's the
case, he'll have to reorganize things yet further and move some of
those types to yet another package, Stuff3.  It does seem like there
ought to be a better way to accomplish all this---how about letting
private and public parts of a package spec be interspersed?

package P is
private
    type Some_Type is ...
not private  -- heh, heh, heh!
    protected type PT is
       procedure Foo;
    private
       X : Some_Type;
    end PT;
private
    .. other private stuff can go here if needed
end P;

But any solution is likely to be a huge burden on both the language
designers and implementors, at this point.  I think we're stuck.

                             -- Adam




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

* Re: Protected types and visibility of their internals
  2008-07-08  9:57 Protected types and visibility of their internals Maciej Sobczak
                   ` (4 preceding siblings ...)
  2008-07-09 22:19 ` Stephen Leake
@ 2008-07-09 23:22 ` jimmaureenrogers
  5 siblings, 0 replies; 14+ messages in thread
From: jimmaureenrogers @ 2008-07-09 23:22 UTC (permalink / raw)


The way that works, even if it is not quite what is desired, is:

package Stuff is
   type Some_Type is private;
   protected type Foo is
      procedure Register;
   private
      Item : Some_Type;
   end Foo;
private
   type Some_Type is new Integer;
end Stuff;

In this case only protected type Foo uses Some_Type. The composition
of
Some_Type is completely private to the enclosing package.

Jim Rogers



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

* Re: Protected types and visibility of their internals
  2008-07-09 22:38   ` Adam Beneschan
@ 2008-07-10  0:18     ` Randy Brukardt
  2008-07-10  0:18     ` Randy Brukardt
  1 sibling, 0 replies; 14+ messages in thread
From: Randy Brukardt @ 2008-07-10  0:18 UTC (permalink / raw)


"Adam Beneschan" <adam@irvine.com> wrote in message 
news:efab0add-0f40-4302-8057-85bee50639d6@34g2000hsf.googlegroups.com...
...
> It does seem like there
> ought to be a better way to accomplish all this---how about letting
> private and public parts of a package spec be interspersed?
>
> package P is
> private
>    type Some_Type is ...
> not private  -- heh, heh, heh!
>    protected type PT is
>       procedure Foo;
>    private
>       X : Some_Type;
>    end PT;
> private
>    .. other private stuff can go here if needed
> end P;
>
> But any solution is likely to be a huge burden on both the language
> designers and implementors, at this point.  I think we're stuck.

Tucker has seriously proposed such an idea (see AI05-0074-2), but he's never 
written up the real details so it is impossible to say what pitfalls there 
are in the idea.

                                    Randy.





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

* Re: Protected types and visibility of their internals
  2008-07-09 22:38   ` Adam Beneschan
  2008-07-10  0:18     ` Randy Brukardt
@ 2008-07-10  0:18     ` Randy Brukardt
  1 sibling, 0 replies; 14+ messages in thread
From: Randy Brukardt @ 2008-07-10  0:18 UTC (permalink / raw)


"Adam Beneschan" <adam@irvine.com> wrote in message 
news:efab0add-0f40-4302-8057-85bee50639d6@34g2000hsf.googlegroups.com...
...
> It does seem like there
> ought to be a better way to accomplish all this---how about letting
> private and public parts of a package spec be interspersed?
>
> package P is
> private
>    type Some_Type is ...
> not private  -- heh, heh, heh!
>    protected type PT is
>       procedure Foo;
>    private
>       X : Some_Type;
>    end PT;
> private
>    .. other private stuff can go here if needed
> end P;
>
> But any solution is likely to be a huge burden on both the language
> designers and implementors, at this point.  I think we're stuck.

Tucker has seriously proposed such an idea (see AI05-0074-2), but he's never 
written up the real details so it is impossible to say what pitfalls there 
are in the idea.

                                    Randy.





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

* Re: Protected types and visibility of their internals
  2008-07-08 21:03 ` Randy Brukardt
@ 2008-07-10 21:49   ` Maciej Sobczak
  0 siblings, 0 replies; 14+ messages in thread
From: Maciej Sobczak @ 2008-07-10 21:49 UTC (permalink / raw)


On 8 Lip, 23:03, "Randy Brukardt" <ra...@rrsoftware.com> wrote:

> I think you are asking the wrong question. The question is "Where should the
> protected type be declared?". Since there are no operations that are
> specific to protected types, there is very little reason to make those a
> visible type.
>
> Thus, I would generally structure the package with a private type and the
> protected type (if any) declared in the private part. Something like:
>
>  package P2 is
>      type Priv is tagged limited private;
>      procedure Foo (Obj : in out Priv);
>  private
>     type Some_Type is ...
>     protected type PT is
>         procedure Foo;
>    private
>       X : Some_Type;
>    end PT;
>    type Priv is tagged limited record
>         Lock : PT;
>    end record;
> end P2;

Yes, it works for me. In addition, this allowed me to hide some
additional operations of the protected type, which are not supposed to
be used by public, but are necessary in interactions with other
components. Putting the whole protected type in private part of the
package is a clean way to do this.

"Every computing problem can be solved with additional level of
indirection." :-)

--
Maciej Sobczak * www.msobczak.com * www.inspirel.com



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

end of thread, other threads:[~2008-07-10 21:49 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-07-08  9:57 Protected types and visibility of their internals Maciej Sobczak
2008-07-08 12:52 ` Georg Bauhaus
2008-07-08 16:13 ` Robert A Duff
2008-07-09  7:53   ` christoph.grein
2008-07-08 21:03 ` Randy Brukardt
2008-07-10 21:49   ` Maciej Sobczak
2008-07-09  5:04 ` christoph.grein
2008-07-09 13:44   ` Maciej Sobczak
2008-07-09 15:12   ` Adam Beneschan
2008-07-09 22:19 ` Stephen Leake
2008-07-09 22:38   ` Adam Beneschan
2008-07-10  0:18     ` Randy Brukardt
2008-07-10  0:18     ` Randy Brukardt
2008-07-09 23:22 ` jimmaureenrogers

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