comp.lang.ada
 help / color / mirror / Atom feed
* Q: how to copy contents and use ancestor's constraints at the same time ?
@ 2007-08-13 21:16 Gautier
  2007-08-13 21:33 ` Adam Beneschan
  2007-08-14  7:45 ` Dmitry A. Kazakov
  0 siblings, 2 replies; 6+ messages in thread
From: Gautier @ 2007-08-13 21:16 UTC (permalink / raw)


Hello!

In the procedure below, how to do [A] and [B] together, and is there a way not 
to have to write the constraints, like when constraining an object by 
initializing it with a value of the same type ?

Thanks in advance!
NB: I looked in a book, but maybe not hard enough ;-)
-----
procedure Test_Ancestor_copy is

   type Index_array_type is array(Positive range <>) of Natural;

   type PF_Data(
     n1: Natural;
     n2: Natural;
     n3: Natural
   )
   is
     tagged record
       p1 : Index_array_type(1..n1);
       p2 : Index_array_type(1..n2);
       p3 : Index_array_type(1..n3);
     end record;

   function Read return PF_Data is
   begin
     return
       (n1 => 1,
        n2 => 2,
        n3 => 3,
        p1 => (1=>1),
        p2 => (1,2),
        p3 => (1,2,3)
       );
   end Read;

   type PF is new PF_Data with record
     computed: Boolean:= False;
     result: Float;
   end record;

   initial_pf: constant PF_Data:= Read;

   work_pf: PF(
     n1 => initial_pf.n1,
     n2 => initial_pf.n2,
     n3 => initial_pf.n3
   );
   -- [A] work_pf sized with initial_pf constraints

begin
   PF_Data(work_pf):= initial_pf; -- [B] copy base data
end;
______________________________________________________________
Gautier         -- http://www.mysunrise.ch/users/gdm/index.htm
Ada programming -- http://www.mysunrise.ch/users/gdm/gsoft.htm

NB: For a direct answer, e-mail address on the Web site!



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

* Re: Q: how to copy contents and use ancestor's constraints at the same time ?
  2007-08-13 21:16 Q: how to copy contents and use ancestor's constraints at the same time ? Gautier
@ 2007-08-13 21:33 ` Adam Beneschan
  2007-08-14  0:48   ` Jeffrey R. Carter
  2007-08-14  7:45 ` Dmitry A. Kazakov
  1 sibling, 1 reply; 6+ messages in thread
From: Adam Beneschan @ 2007-08-13 21:33 UTC (permalink / raw)


On Aug 13, 2:16 pm, Gautier <gaut...@fakeaddress.nil> wrote:
> Hello!
>
> In the procedure below, how to do [A] and [B] together, and is there a way not
> to have to write the constraints, like when constraining an object by
> initializing it with a value of the same type ?
>
> Thanks in advance!
> NB: I looked in a book, but maybe not hard enough ;-)
> -----
> procedure Test_Ancestor_copy is
>
>    type Index_array_type is array(Positive range <>) of Natural;
>
>    type PF_Data(
>      n1: Natural;
>      n2: Natural;
>      n3: Natural
>    )
>    is
>      tagged record
>        p1 : Index_array_type(1..n1);
>        p2 : Index_array_type(1..n2);
>        p3 : Index_array_type(1..n3);
>      end record;
>
>    function Read return PF_Data is
>    begin
>      return
>        (n1 => 1,
>         n2 => 2,
>         n3 => 3,
>         p1 => (1=>1),
>         p2 => (1,2),
>         p3 => (1,2,3)
>        );
>    end Read;
>
>    type PF is new PF_Data with record
>      computed: Boolean:= False;
>      result: Float;
>    end record;
>
>    initial_pf: constant PF_Data:= Read;
>
>    work_pf: PF(
>      n1 => initial_pf.n1,
>      n2 => initial_pf.n2,
>      n3 => initial_pf.n3
>    );
>    -- [A] work_pf sized with initial_pf constraints
>
> begin
>    PF_Data(work_pf):= initial_pf; -- [B] copy base data
> end;

My first thought (using Ada 2005) is

   work_pf : PF := (initial_pf with others => <>);

But I haven't looked through all the language rules to make sure this
is legal.

                      -- Adam




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

* Re: Q: how to copy contents and use ancestor's constraints at the same time ?
  2007-08-13 21:33 ` Adam Beneschan
@ 2007-08-14  0:48   ` Jeffrey R. Carter
  2007-08-14 23:35     ` Adam Beneschan
  0 siblings, 1 reply; 6+ messages in thread
From: Jeffrey R. Carter @ 2007-08-14  0:48 UTC (permalink / raw)


Adam Beneschan wrote:
> 
>    work_pf : PF := (initial_pf with others => <>);

That's my suggestion, too. It seems legal to me.

-- 
Jeff Carter
"My brain hurts!"
Monty Python's Flying Circus
21



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

* Re: Q: how to copy contents and use ancestor's constraints at the same time ?
  2007-08-13 21:16 Q: how to copy contents and use ancestor's constraints at the same time ? Gautier
  2007-08-13 21:33 ` Adam Beneschan
@ 2007-08-14  7:45 ` Dmitry A. Kazakov
  2007-08-14  8:59   ` gautier_niouzes
  1 sibling, 1 reply; 6+ messages in thread
From: Dmitry A. Kazakov @ 2007-08-14  7:45 UTC (permalink / raw)


On Mon, 13 Aug 2007 23:16:05 +0200, Gautier wrote:

> In the procedure below, how to do [A] and [B] together, and is there a way not 
> to have to write the constraints,

What about:

   subtype Constrained_PF is PF (n1=>1, n2=>2, n3=>3);

> like when constraining an object by 
> initializing it with a value of the same type ?

  X : PF := Y; -- ?

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: Q: how to copy contents and use ancestor's constraints at the same time ?
  2007-08-14  7:45 ` Dmitry A. Kazakov
@ 2007-08-14  8:59   ` gautier_niouzes
  0 siblings, 0 replies; 6+ messages in thread
From: gautier_niouzes @ 2007-08-14  8:59 UTC (permalink / raw)


Dmitry:

> What about:
>    subtype Constrained_PF is PF (n1=>1, n2=>2, n3=>3);

> > like when constraining an object by
> > initializing it with a value of the same type ?

>   X : PF := Y; -- ?

Fine, but the contraints are again explicit (=> 1, => 2, => 3) and it
is in two pieces again:  subtype definition and declaration of X.
Indeed, Y being of type PF_Data and not PF, it would require three
lines.
Adam and Jeff's suggestion is exactly what I'm looking for.
Thanks for all the brainstorming and help!
______________________________________________________________
Gautier         -- http://www.mysunrise.ch/users/gdm/index.htm
Ada programming -- http://www.mysunrise.ch/users/gdm/gsoft.htm

NB: For a direct answer, e-mail address on the Web site!




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

* Re: Q: how to copy contents and use ancestor's constraints at the same time ?
  2007-08-14  0:48   ` Jeffrey R. Carter
@ 2007-08-14 23:35     ` Adam Beneschan
  0 siblings, 0 replies; 6+ messages in thread
From: Adam Beneschan @ 2007-08-14 23:35 UTC (permalink / raw)


On Aug 13, 5:48 pm, "Jeffrey R. Carter"
<spam.jrcarter....@acm.nospam.org> wrote:
> Adam Beneschan wrote:
>
> >    work_pf : PF := (initial_pf with others => <>);
>
> That's my suggestion, too. It seems legal to me.

I did check the rules, and it seems legal to me, too.  The thing I
can't find in the RM is an explicit statement that for an extension
aggregate, where the ancestor_part is an expression and the derived
type has discriminants inherited from the type of the ancestor_part,
the resulting extension aggregate has a constrained subtype with the
same discriminant values as the ancestor expression.    (The section
on extension aggregates does discuss the case where discriminants
aren't inherited, but there's no explicit mention of the inherited
discriminant case.)  It's probably obvious enough not to need mention;
and in any case, it probably follows from something else in the RM,
but if I tried to dig any further, I'd end up saying something like

> "My brain hurts!"

and then it would have to come out.  (The brain in my head, that is.)

                        -- Adam






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

end of thread, other threads:[~2007-08-14 23:35 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-08-13 21:16 Q: how to copy contents and use ancestor's constraints at the same time ? Gautier
2007-08-13 21:33 ` Adam Beneschan
2007-08-14  0:48   ` Jeffrey R. Carter
2007-08-14 23:35     ` Adam Beneschan
2007-08-14  7:45 ` Dmitry A. Kazakov
2007-08-14  8:59   ` gautier_niouzes

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