comp.lang.ada
 help / color / mirror / Atom feed
From: Robert Eachus <rieachus@comcast.net>
Subject: Re: how to do things like procedure foo(A: string) is bar: string := A; begin; ...;end;
Date: Sun, 17 Dec 2017 22:39:37 -0800 (PST)
Date: 2017-12-17T22:39:37-08:00	[thread overview]
Message-ID: <3c367d8d-351e-4c72-b259-c04a5285d025@googlegroups.com> (raw)
In-Reply-To: <6bedbdee-ca7f-4d2a-83f9-6cc9c53d21cb@googlegroups.com>

On Saturday, December 16, 2017 at 3:56:45 PM UTC-5, Mehdi Saada wrote:
> DATA being strings components of the records LEFT and RIGHT, is the expression (LEFT.DATA & RIGHT.DATA)'Range possible ?
> I try to implement something like
> procedure foo(A: string) is
>    bar: string := A;
> begin
> ...
> end;
> but A'Length isn't known at compilation. In fact I want to use bar only like a macro, to avoid puting things like LEFT.DATA & RIGHT.DATA everywhere and not being able to use attributes (Range, length & co).

There is no problem with your example.  There is a potential (mental) gotcha if you concatenate a character or a null string literal to the beginning of another string literal.  A bit of thinking should tell you that such strings begin at 1, not Integer'First, and the last is Left.Data'First + Right.Data'Length.

If your subprogram Foo has an unconstrained String parameter (yours does) then the bounds of A are the bounds of the actual argument, and can be different on every call.  If you have a constrained String (i.e. String(6..15) or whatever) then inside Foo the bounds are those of the formal parameter.  If the LENGTHS of (constrained) formal and actual don't match, Constraint_Error will be raised.

As I said, there is not a problem in Ada--but everyone will eventually run into a couple of bugs because they unthinkingly assumed that the actual for the unconstrained formal starts at 1.

So now if you see something like:

   function Foo(S: String) return String is
   begin
     if S'Length < 80
     then return "" & S;
     else...

You will have a clue what is going on. ;-)


  parent reply	other threads:[~2017-12-18  6:39 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-16 20:56 how to do things like procedure foo(A: string) is bar: string := A; begin; ...;end; Mehdi Saada
2017-12-16 21:49 ` Jeffrey R. Carter
2017-12-17 12:40   ` Mehdi Saada
2017-12-17 13:25     ` Jere
2017-12-17 13:49       ` Jere
2017-12-17 13:51     ` Jeffrey R. Carter
2017-12-18 22:22     ` Randy Brukardt
2017-12-18  6:39 ` Robert Eachus [this message]
2017-12-18 15:34   ` Mehdi Saada
2017-12-18 17:31     ` Anh Vo
2017-12-18 21:14     ` Robert Eachus
2017-12-19 22:52   ` Robert Eachus
replies disabled

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