comp.lang.ada
 help / color / mirror / Atom feed
From: "Hibou57 (Yannick Duchêne)" <yannick_duchene@yahoo.fr>
Subject: Re: Package's private parts and protected types
Date: Tue, 9 Feb 2010 21:12:04 -0800 (PST)
Date: 2010-02-09T21:12:04-08:00	[thread overview]
Message-ID: <af983fa7-fb94-4bc0-b923-2ed327011857@d37g2000yqa.googlegroups.com> (raw)
In-Reply-To: hkt69e$6fs$1@munin.nbi.dk

On 10 fév, 00:29, Robert A Duff <bobd...@shell01.TheWorld.com> wrote:
> I don't understand the problem.  The following should work.
> Doesn't it do what you want?  Type T exports Public,
> but hides Hidden.  No heap allocation.
>
>  package P is
>     type T is synchronized interface;
>     function Create return T'Class;
>
>     procedure Public (X : in out T) is abstract;
>
>  private
>
>     protected type T2 is new T with
>        overriding entry Public;
>        entry Hidden;
>     end T2;
>
>  end P;
>
>  package body P is
>     function Create return T'Class is
>     begin
>        return Result : T2 do
>           ...
>        end return;
>     end Create;
>
>     protected body T2 is
>        entry Public when ... is
>        begin
>           ...
>        end Public;
>
>        entry Hidden when ... is
>        begin
>           ...
>        end Hidden;
>     end T2;
>
>  end P;
>
>  with P; use P;
>  procedure Main is
>
>     X : T'Class := Create;
>
>  begin
>     Public (X);
>  end Main;
>
> - Bob

This was exactly what I've tried, except that I used something like “
return Result : T'Class := ... do ” instead of “ return Result : T2 do
”, which is not legal, as stated by the ARM :

[ARM 6.5(5.2/2)]
If the result subtype of the function is defined by a subtype_mark,
the return_subtype_indication shall be a subtype_indication.

Ok for this one, but later it's going wrong :

Remainder of [ARM 6.5(5.2/2)]
The type of the subtype_indication shall be the result type of the
function.

But T2, in the return statement, is not T'Class, in the function
return type indication. And indeed, when I've tried your way, GNAT
complained “ wrong type for return_subtype_indication ”

So it must be T'Class in the return statement, and as by both

[ARM 6.5(5.d/2)]
Discussion: We know that if the result type is class wide, then there
must be an expression of the return statement.

and

[ARM 6.5(5.5/2)]
If the result subtype of the function is limited, then the expression
of the return statement (if any) shall be an aggregate, a function
call (or equivalent use of an operator), or a qualified_expression or
parenthesized expression whose operand is one of these.

an initialization is required (otherwise the concret type of the
object would be unknown).

I was failing there, as there is no kind of extention aggregate for a
protected type (it's not a record, although tagged).

But I've finally solved the trick : see next reply to Randy.
On 10 fév, 00:29, Robert A Duff <bobd...@shell01.TheWorld.com> wrote:
> I don't understand the problem.  The following should work.
> Doesn't it do what you want?  Type T exports Public,
> but hides Hidden.  No heap allocation.
>
>  package P is
>     type T is synchronized interface;
>     function Create return T'Class;
>
>     procedure Public (X : in out T) is abstract;
>
>  private
>
>     protected type T2 is new T with
>        overriding entry Public;
>        entry Hidden;
>     end T2;
>
>  end P;
>
>  package body P is
>     function Create return T'Class is
>     begin
>        return Result : T2 do
>           ...
>        end return;
>     end Create;
>
>     protected body T2 is
>        entry Public when ... is
>        begin
>           ...
>        end Public;
>
>        entry Hidden when ... is
>        begin
>           ...
>        end Hidden;
>     end T2;
>
>  end P;
>
>  with P; use P;
>  procedure Main is
>
>     X : T'Class := Create;
>
>  begin
>     Public (X);
>  end Main;
>
> - Bob

This was exactly what I've tried, except that I used something like “
return Result : T'Class := ... do ” instead of “ return Result : T2 do
”, which is not legal, as stated by the ARM :

[ARM 6.5(5.2/2)]
If the result subtype of the function is defined by a subtype_mark,
the return_subtype_indication shall be a subtype_indication.

Ok for this one, but later it's going wrong :

Remainder of [ARM 6.5(5.2/2)]
The type of the subtype_indication shall be the result type of the
function.

But T2, in the return statement, is not T'Class, in the function
return type indication. And indeed, when I've tried your way, GNAT
complained “ wrong type for return_subtype_indication ”

So it must be T'Class in the return statement, and as by both

[ARM 6.5(5.d/2)]
Discussion: We know that if the result type is class wide, then there
must be an expression of the return statement.

and

[ARM 6.5(5.5/2)]
If the result subtype of the function is limited, then the expression
of the return statement (if any) shall be an aggregate, a function
call (or equivalent use of an operator), or a qualified_expression or
parenthesized expression whose operand is one of these.

an initialization is required (otherwise the concret type of the
object would be unknown).

I was failing there, as there is no kind of extention aggregate for a
protected type (it's not a record, although tagged).

But I've finally solved the trick : see next, in the reply to Randy

On 10 fév, 03:39, "Randy Brukardt" <ra...@rrsoftware.com> wrote:
> I don't buy this at all. The exact types to create can be selected by many
> means (presumably by the parameters). You can use a dispatching constructor
> function in the concrete types, using the factory pattern
> (Generic_Dispatching_Constructor), and roughly organized as Bob noted.
What confused me, was I meet with initialization of a class wide type
whose initializer was to of a protected type, as explained in the
reply to Robert/Bob.

I could finally solve it, using a function, instead of an aggregate.

> Note that you have this problem with *any* constructor of any tagged type if
> you have any interest at all in allowing future extensions.
While tagged record and tagged protected is not fully the same, as
this example shows.

Well, finally the previous assertion is canceled, and there is indeed
a nice and clean way to do things with a synchronized interface type.

Thanks to the one who suggested this path.



  reply	other threads:[~2010-02-10  5:12 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-02-08  4:30 Package's private parts and protected types Hibou57 (Yannick Duchêne)
2010-02-08  8:30 ` Martin
2010-02-08  9:11   ` Hibou57 (Yannick Duchêne)
2010-02-08 10:10     ` Alex R. Mosteo
2010-02-08 10:46       ` Hibou57 (Yannick Duchêne)
2010-02-09 14:55         ` Alex R. Mosteo
2010-02-08 10:20   ` Dmitry A. Kazakov
2010-02-08 10:54     ` Hibou57 (Yannick Duchêne)
2010-02-08 10:58       ` Hibou57 (Yannick Duchêne)
2010-02-08 11:01       ` Dmitry A. Kazakov
2010-02-08 13:19         ` Georg Bauhaus
2010-02-08 15:17         ` Robert A Duff
2010-02-08 16:15           ` (see below)
2010-02-08 20:44             ` Robert A Duff
2010-02-08 22:00               ` Hibou57 (Yannick Duchêne)
2010-02-09  5:48               ` AdaMagica
2010-02-09 14:56                 ` Robert A Duff
2010-02-10  2:29                   ` Randy Brukardt
2010-02-11 23:46                     ` Robert A Duff
2010-02-12  1:29                       ` Randy Brukardt
2010-02-11 23:53                     ` Robert A Duff
2010-02-12  1:10                       ` Randy Brukardt
2010-02-10 16:05                   ` Adam Beneschan
2010-02-10 20:17                     ` sjw
2010-02-12  0:05                     ` Robert A Duff
2010-02-12 11:07                       ` Stephen Leake
2010-02-12 15:01                         ` Robert A Duff
2010-02-13  8:00                           ` Stephen Leake
2010-02-09  9:04               ` stefan-lucks
2010-02-08 17:11           ` Jeffrey R. Carter
2010-02-08 14:56       ` Robert A Duff
2010-02-08 15:36         ` Dmitry A. Kazakov
2010-02-08 16:06           ` Robert A Duff
2010-02-08 17:46             ` Jean-Pierre Rosen
2010-02-08 20:39               ` Robert A Duff
2010-02-08 21:54                 ` Hibou57 (Yannick Duchêne)
2010-02-08 21:50               ` Hibou57 (Yannick Duchêne)
2010-02-08 22:04         ` Hibou57 (Yannick Duchêne)
2010-02-09 10:58         ` Hibou57 (Yannick Duchêne)
2010-02-09 14:47           ` Robert A Duff
2010-02-09 19:34             ` Hibou57 (Yannick Duchêne)
2010-02-09 20:19               ` Hibou57 (Yannick Duchêne)
2010-02-09 23:29               ` Robert A Duff
2010-02-10  2:39               ` Randy Brukardt
2010-02-10  5:12                 ` Hibou57 (Yannick Duchêne) [this message]
2010-02-10  7:17                   ` Hibou57 (Yannick Duchêne)
2010-02-10 16:09                   ` Robert A Duff
2010-02-10 22:21                     ` Hibou57 (Yannick Duchêne)
2010-02-11  0:48                       ` Robert A Duff
2010-02-09  0:48     ` Randy Brukardt
2010-02-09 12:43     ` Hibou57 (Yannick Duchêne)
replies disabled

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