From: Jeffrey Carter <jrcarter@acm.org>
Subject: Re: Newbie, returning strings in Ada
Date: Sun, 16 Feb 2003 19:23:48 GMT
Date: 2003-02-16T19:23:48+00:00 [thread overview]
Message-ID: <3E4FE52F.2060101@acm.org> (raw)
In-Reply-To: 87525c4f.0302160333.de26f1@posting.google.com
Santiago A. wrote:
> 2) This case works fine too:
>
> var U_Line:Unbounded_String;
> ....
> U_Line:=To_Unbounded_String(Get_Line(Sock));
>
>
> 3) This case fails
>
> var F_Line:string:="1234567890";
> .....
> F_Line:=Get_Line(Sock);
This is not Ada. Perhaps you are using Pascal; "var" is not part of
Ada's syntax.
If you were using Ada, you might write
declare
F_Line : String := "1234567890";
begin
F_Line := Get_Line (Sock);
...
end;
The assignment to F_Line will usually raise Constraint_Error. The only
time this will not raise an exception is when Get_Line returns a String
of length 10, or if you have run-time checks suppressed. I suggest you
not suppress run-time checking.
Using an Unbounded_String works because it's an abstraction that may
hold a string of any length. An object of type String is always the same
length.
> Is there any problem in a function that returns a string with lenght 2
> into a variable string(10)?
Yes.
>
> Must the returned string and the variable that will hold the result be
> of the same length?
Yes. However, you can store the result in an object created for that
purpose:
declare
F_Line : [constant] String := Get_Line (Sock);
begin
...
end;
If you leave off "constant", then you can modify the value in F_Line. If
you don't do that, though, it's best to mark the object as a constant.
--
Jeff Carter
"If I could find a sheriff who so offends the citizens of Rock
Ridge that his very appearance would drive them out of town ...
but where would I find such a man? Why am I asking you?"
Blazing Saddles
prev parent reply other threads:[~2003-02-16 19:23 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-02-16 11:33 Newbie, returning strings in Ada Santiago A.
2003-02-16 11:58 ` Simon Wright
2003-02-16 19:23 ` Jeffrey Carter [this message]
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox