comp.lang.ada
 help / color / mirror / Atom feed
* Re: [newbie] Need som feedback on code snip
  1999-10-22  0:00 [newbie] Need som feedback on code snip Preben Randhol
@ 1999-10-22  0:00 ` Ted Dennison
  1999-10-22  0:00   ` Ted Dennison
  1999-10-22  0:00 ` Robert I. Eachus
  1999-10-22  0:00 ` [newbie] Need some " Matthew Heaney
  2 siblings, 1 reply; 6+ messages in thread
From: Ted Dennison @ 1999-10-22  0:00 UTC (permalink / raw)


In article <m31zan4l1n.fsf@kiuk0156.chembio.ntnu.no>,
  Preben Randhol <randhol@pvv.org> wrote:
>
> comment on it. It works, but is is the Right Way to do it or is there
> a smarter way. I'm learning Ada95 on my own and could need some
> feedback from somebody experienced with Ada.
>
> with Text_IO; use Text_IO;
> with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
>

If it were me, I'd get rid of the "aliased", String_Ptr, and
Text_Pointer (and the silly hungarian routine name). All you really need
is:

 procedure My_Test is
    Text : Unbounded_String := To_Unbounded_String("Hello world!");
 begin
    Put_Line(To_String(Text));
    Text := To_Unbounded_String("Hello yourself!");
    Put_Line(To_String(Text));
 end My_Test;

There's no point in aliasing things when you don't have to, and it can
cause serious maintanence headaches in larger programs.

--
T.E.D.


Sent via Deja.com http://www.deja.com/
Before you buy.




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

* Re: [newbie] Need som feedback on code snip
  1999-10-22  0:00 ` Ted Dennison
@ 1999-10-22  0:00   ` Ted Dennison
  0 siblings, 0 replies; 6+ messages in thread
From: Ted Dennison @ 1999-10-22  0:00 UTC (permalink / raw)


In article <7uqilp$g1p$1@nnrp1.deja.com>,
  Ted Dennison <dennison@telepath.com> wrote:
> In article <m31zan4l1n.fsf@kiuk0156.chembio.ntnu.no>,
>   Preben Randhol <randhol@pvv.org> wrote:
> >
> > comment on it. It works, but is is the Right Way to do it or is

>  procedure My_Test is

Btw: if my formatting looks wierd, *don't* emulate it. Deja-news likes
to mundge whitespace in postings. (sigh).

--
T.E.D.


Sent via Deja.com http://www.deja.com/
Before you buy.




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

* Re: [newbie] Need some feedback on code snip
  1999-10-22  0:00 [newbie] Need som feedback on code snip Preben Randhol
  1999-10-22  0:00 ` Ted Dennison
  1999-10-22  0:00 ` Robert I. Eachus
@ 1999-10-22  0:00 ` Matthew Heaney
  2 siblings, 0 replies; 6+ messages in thread
From: Matthew Heaney @ 1999-10-22  0:00 UTC (permalink / raw)


In article <m31zan4l1n.fsf@kiuk0156.chembio.ntnu.no> , Preben Randhol 
<randhol@pvv.org>  wrote:

> -- Just a small test to see how I can change the content of a string
> -- variable when I do not know the length of the new or old text.
>
> with Text_IO; use Text_IO;
> with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
>
> procedure myTest is
>    Orig_Text : aliased Unbounded_String := To_Unbounded_String("Hello
world!");
>    type String_ptr is access all Unbounded_String;
>    Text_pointer : String_ptr;
>
> begin
>    Put_Line(To_String(Orig_Text));
>    Text_Pointer := Orig_Text'Access;
>    Text_Pointer.all := To_Unbounded_String("Hello yourself!");
>    Put_Line(To_String(Orig_Text));
> end myTest;

Don't use heap or pointers.  All of that is hidden behind
Unbounded_String.

  procedure MyTest is
    Text : Unbounded_String := To_Unbounded_String ("hello world");
  begin
    Put_Line (To_String (Text));
    Text := To_Unbounded_String ("Hello yourself");
    Put_Line (To_String (Text));
  end;






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

* [newbie] Need som feedback on code snip
@ 1999-10-22  0:00 Preben Randhol
  1999-10-22  0:00 ` Ted Dennison
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Preben Randhol @ 1999-10-22  0:00 UTC (permalink / raw)



I wondered if somebody could take a peep at the code snip below and
comment on it. It works, but is is the Right Way to do it or is there
a smarter way. I'm learning Ada95 on my own and could need some feedback
from somebody experienced with Ada.



-- Just a small test to see how I can change the content of a string
-- variable when I do not know the length of the new or old text.

with Text_IO; use Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;

procedure myTest is
   Orig_Text : aliased Unbounded_String := To_Unbounded_String("Hello world!");
   type String_ptr is access all Unbounded_String;
   Text_pointer : String_ptr;

begin
   Put_Line(To_String(Orig_Text));
   Text_Pointer := Orig_Text'Access;
   Text_Pointer.all := To_Unbounded_String("Hello yourself!");
   Put_Line(To_String(Orig_Text));
end myTest;

Thanks in advance...

-- 
Preben Randhol                 Affliction is enamoured of thy parts, 
[randhol@pvv.org]              And thou art wedded to calamity. 
[http://www.pvv.org/~randhol/]                    -- W. Shakespeare 




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

* Re: [newbie] Need som feedback on code snip
  1999-10-22  0:00 [newbie] Need som feedback on code snip Preben Randhol
  1999-10-22  0:00 ` Ted Dennison
@ 1999-10-22  0:00 ` Robert I. Eachus
  1999-10-24  0:00   ` Preben Randhol
  1999-10-22  0:00 ` [newbie] Need some " Matthew Heaney
  2 siblings, 1 reply; 6+ messages in thread
From: Robert I. Eachus @ 1999-10-22  0:00 UTC (permalink / raw)


  Can I take this one?  Can I?  Can I please... ;-)

Preben Randhol wrote:
  
> I wondered if somebody could take a peep at the code snip below and
> comment on it.

  It works way too hard to do something which is very simple...

> It works, but is is the Right Way to do it or is there
> a smarter way.

-- Just a small test to see how I can change the content of a string
-- variable when I do not know the length of the new or old text.

with Text_IO; use Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
 
procedure My_Test is
   Text :  Unbounded_String := To_Unbounded_String("Hello world!");
begin
   Put_Line(To_String(Text));
   Text := To_Unbounded_String("Hello yourself!");
   Put_Line(To_String(Text));
end My_Test;

   Unbounded strings do for you all that ugly pointer and memory
allocation and reclamation stuff that you have to worry about in C. 
And,
if you are using GNAT, there is a package Ada.Strings.Unbounded.Text_IO,
that makes it still easier:

with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Strings.Unbounded.Text_IO; use Ada.Strings.Unbounded.Text_IO;
procedure Test2 is
   Text: Unbounded_String := To_Unbounded_String("Hello world!");
begin
   Put_Line(Text);
   Text := To_Unbounded_String("Hello yourself!");
   Put_Line(Text);
end Test2;
 
-- 

                                        Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




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

* Re: [newbie] Need som feedback on code snip
  1999-10-22  0:00 ` Robert I. Eachus
@ 1999-10-24  0:00   ` Preben Randhol
  0 siblings, 0 replies; 6+ messages in thread
From: Preben Randhol @ 1999-10-24  0:00 UTC (permalink / raw)


"Robert I. Eachus" <eachus@mitre.org> writes:

|   Can I take this one?  Can I?  Can I please... ;-)

Take what? Sorry I don't understand.
 
|    Unbounded strings do for you all that ugly pointer and memory
| allocation and reclamation stuff that you have to worry about in C. 
| And,
| if you are using GNAT, there is a package Ada.Strings.Unbounded.Text_IO,
| that makes it still easier:

Ah thanks.

Thanks to everybody for replying!

The reason that I used aliased was that I wanted to see how it
worked. I agree that one should not use it for this simple example
though. Sorry for not commenting it.
-- 
Preben Randhol                 Affliction is enamoured of thy parts, 
[randhol@pvv.org]              And thou art wedded to calamity. 
[http://www.pvv.org/~randhol/]                    -- W. Shakespeare 




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

end of thread, other threads:[~1999-10-24  0:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-10-22  0:00 [newbie] Need som feedback on code snip Preben Randhol
1999-10-22  0:00 ` Ted Dennison
1999-10-22  0:00   ` Ted Dennison
1999-10-22  0:00 ` Robert I. Eachus
1999-10-24  0:00   ` Preben Randhol
1999-10-22  0:00 ` [newbie] Need some " Matthew Heaney

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