comp.lang.ada
 help / color / mirror / Atom feed
From: "Adam Beneschan" <adam@irvine.com>
Subject: Re: Initialize with aggregate?
Date: 22 Nov 2005 08:57:23 -0800
Date: 2005-11-22T08:57:23-08:00	[thread overview]
Message-ID: <1132678643.906071.122770@z14g2000cwz.googlegroups.com> (raw)
In-Reply-To: wccd5kt4p6g.fsf@shell01.TheWorld.com

Robert A Duff wrote:
> ejijott@gmail.com writes:
>
> > Hi there!
> >
> > Regarding the code below, what syntax do I use to initialize tempstore
> > with an aggregate?
> > [code]
> > .
> > .
> > .
> >     type node;
> >     type storage is access node;
> >     type node is
> >         record
> >             next:  storage;       -- Pekare till nästa nod.
> >             item:  string(1..50); -- En textsträng
> >             len:   integer :=-1;  -- Längden på strängen.
> >             count: integer := 0;  -- Antalet förekomster av strängen
> >         end record;
> > .
> > .
> > .
> >         tempstore:=new Node;
> >         tempstore.item(1 .. Val'length):=Val;
> >         tempstore.len:=Val'length;
> >         tempstore.count:=1;
> >         if S /= null then
> >             tempstore.next:=S;
> >         end if;
> >         S:=tempstore;
> > [/code]
>
> How about something like this:
>
>     type node;
>     type storage is access node;
>     type node(len: natural) is
>         record
>             next:  storage;       -- Pekare till nästa nod.
>             item:  string(1..len); -- En textsträng
>             count: integer := 0;  -- Antalet förekomster av strängen
>         end record;
>
>     tempstore:=new Node'(
>             len => val'length,
>             next => S,
>             item => val,
>             count => 1);
>     S:=tempstore;
>
> Hardwiring Item'Length to 50 isn't a good idea, and it makes the
> programming harder, because you have to carefully avoid the junk
> at the end.

I don't see how you can make a blanket statement like this.  If you
code it the way you've shown, you can't change "len" in a node once
you've allocated it.  But sometimes changing "item" and "len" in an
existing node to change the length of the string, without changing the
other fields in the node, is what you want to do, so there are cases
where the original poster's implementation is more appropriate than
using a discriminant record.  (Using Ada.Strings.Bounded may make
programming easier still, but you still have to hardwire a maximum
length.)

                                    -- Adam




  reply	other threads:[~2005-11-22 16:57 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-21 19:40 Initialize with aggregate? ejijott
2005-11-21 21:38 ` Gautier Write-only
2005-11-21 21:49 ` Simon Wright
2005-11-22 17:10   ` Adam Beneschan
2005-11-22 20:00     ` Simon Wright
2005-11-21 22:21 ` Robert A Duff
2005-11-22 16:57   ` Adam Beneschan [this message]
2005-11-22 20:07     ` 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