comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Belmont <sbelmont700@gmail.com>
Subject: Re: Building limited types through nested creator functions
Date: Mon, 6 Feb 2012 14:33:39 -0800 (PST)
Date: 2012-02-06T14:33:39-08:00	[thread overview]
Message-ID: <beff6ac3-d7d5-4d23-a34b-d7e5a642959b@k6g2000vbz.googlegroups.com> (raw)
In-Reply-To: 1f36269b-78fa-4a8a-90c8-104c932e8658@d15g2000yqg.googlegroups.com

On Feb 6, 2:11 pm, Shark8 <onewingedsh...@gmail.com> wrote:
> On Feb 5, 4:03 pm, Simon Belmont <sbelmont...@gmail.com> wrote:
>
>
>
>
>
> > Hi,
>
> > Consider two limited record types, inner and outer, with the former
> > nested inside the latter.  If the records are public, then code can
> > initialize the outer by specifying an aggregate for the inner, as in:
>
> > type Inner is limited
> >   record
> >     e : Integer;
> >   end record;
>
> > type Outer is limited
> >   record
> >     i : Inner;
> >   end record;
>
> > o : Outer := Outer'(i => Inner'(e => 42));
>
> > However, if types are made private, suitable functions must be
> > provided to make the appropriate objects.  If just Inner is private,
> > then this can be done (assuming simple creator functions that just
> > create the objects with the given values):
>
> > o : Outer := Outer'(i => Make_Inner(arg => 42));
>
> > but if both are private, then the following:
>
> > o : Outer := Make_Outer (arg => Make_Inner(arg => 42));
>
> > ends up being illegal, because in the code:
>
> > function Make_Outer (arg : Inner) return Outer is
> > begin
> >   return Outer'(i => arg);
> > end Make_Outer
>
> > would end up trying to copy a limited type.  For the cases in which an
> > existing object is passed in, this would be the appropriate action,
> > but for cases where the object is built-in-place into the argument,
> > clearly the intended behavior is to build it in place to the resultant
> > object.  It's easy enough to use an access value, but as unnecessary
> > use of access values is generally discouraged, I was just curious if
> > there was an alternative mechanism to achieve the desired effect.
>
> > -sb
>
> Try this:
>
> Function Make_Inner( I: Integer ) Return Inner is
> begin
>     Return Result : Inner:= Inner'( E => I );
> end Make_Inner;
>
> Function Make_Outer( I: Integer ) Return Outer is
> begin
>     Return Result : Outer:= Outer'( I => Make_Inner(I) );
> end Make_Outer;
>
> The 'trick' is to use all the parameters used in make_inner plus
> whatever is needed in the Outer record for the parameter-list of
> Make_Outer; thus you avoid all copying restrictions as you are really
> building-in-place.- Hide quoted text -
>
> - Show quoted text -

This was my original plan, but the worry is that this doesn't reflect
the appropriate responsibilities; If A creates B and gives it to C,
then C should have no say in how (or if) it's created.  For instance,
the Make_Outer code could easily alter the supplied arguments and
Make_Inner in any way it chooses.  I suppose using an access value has
essentially the same problem, as it could simply eschew the supplied
value and allocate whatever it wants, but such is life I guess

Thank you

-sb



  reply	other threads:[~2012-02-06 22:40 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-05 22:03 Building limited types through nested creator functions Simon Belmont
2012-02-06 12:44 ` Julian Leyh
2012-02-06 19:11 ` Shark8
2012-02-06 22:33   ` Simon Belmont [this message]
2012-02-07  0:03 ` Adam Beneschan
2012-02-07  0:27   ` Simon Belmont
2012-02-07  0:53     ` Adam Beneschan
2012-02-07  2:19       ` Simon Belmont
2012-02-07  9:10         ` Dmitry A. Kazakov
2012-02-07 10:58           ` Georg Bauhaus
2012-02-07 13:25             ` Dmitry A. Kazakov
2012-02-07 14:43               ` Yannick Duchêne (Hibou57)
2012-02-07 15:08                 ` Dmitry A. Kazakov
2012-02-07 17:04 ` Robert A Duff
replies disabled

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