comp.lang.ada
 help / color / mirror / Atom feed
* Attempting to modify a function result
@ 1999-07-22  0:00 Raymond C. Rawa
  1999-07-22  0:00 ` Pascal MALAISE
  1999-07-22  0:00 ` Robert I. Eachus
  0 siblings, 2 replies; 8+ messages in thread
From: Raymond C. Rawa @ 1999-07-22  0:00 UTC (permalink / raw)


I've encountered a "feature" of Ada83 or the VADS compiler that I don't
understand.

I discovered this little feature while unit testing a package that
contained a typo.

   package Foo is

      Display_Type is record
         a : integer;
         b : float;
      end record;

      function Display return Display_Type;

      procedure Huh;

   end Foo;

   package body Foo is

      The_Display : Display_Type; 
      --% internal object

      function Display return Display_Type is
      begin
         return The_Display;
      end Display;

      procedure Huh is
      begin
         Display.a := 5; -- I mean't to type: The_Display.a := 5;
      end Huh;
   end Foo;

The procedure Foo.Huh compiles and executes without error.  As a side
note, The_Display is unchanged by this procedure.

Can someone please explain what's going on here?




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

* Re: Attempting to modify a function result
  1999-07-22  0:00 Attempting to modify a function result Raymond C. Rawa
  1999-07-22  0:00 ` Pascal MALAISE
@ 1999-07-22  0:00 ` Robert I. Eachus
  1999-07-22  0:00   ` Keith Thompson
  1999-07-26  0:00   ` Tucker Taft
  1 sibling, 2 replies; 8+ messages in thread
From: Robert I. Eachus @ 1999-07-22  0:00 UTC (permalink / raw)




"Raymond C. Rawa" wrote:
  
> I've encountered a "feature" of Ada83 or the VADS compiler that I don't
> understand.

   It is a feature of Ada, not just VADS.

>    ...Display.a := 5; -- I mean't to type: The_Display.a := 5;
 
   Display is a function that returns an object of type Display_Type. 
Display.a is a compontent of that object of type Integer, which gets
assigned the value 5.  What is hard about that?

> The procedure Foo.Huh compiles and executes without error.  As a side
> note, The_Display is unchanged by this procedure.

   That probably means that the function is returning a copy of
The_Display.  Some other implementation might return The_Display by
reference, in which case The_Display would be changed.
 
> Can someone please explain what's going on here?

   A function call can be the prefix of an indexed or selected
component, or even a slice.  In this case since Display has no
parameters, it surprised you.
(Actually I often use the construct of slicing the string returned by an
attribute: Put(Integer'Image(Foo)(2..Integer'Image(Foo)'Last); if I want
to drop the leading space.)
-- 

                                        Robert I. Eachus

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




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

* Re: Attempting to modify a function result
  1999-07-22  0:00 Attempting to modify a function result Raymond C. Rawa
@ 1999-07-22  0:00 ` Pascal MALAISE
  1999-07-23  0:00   ` Raymond C. Rawa
  1999-07-22  0:00 ` Robert I. Eachus
  1 sibling, 1 reply; 8+ messages in thread
From: Pascal MALAISE @ 1999-07-22  0:00 UTC (permalink / raw)


"Raymond C. Rawa" wrote:
>       Display_Type is record
>          a : integer;
>          b : float;
>       end record;
You mean "type Display_Type" :-)

 
>          Display.a := 5; -- I mean't to type: The_Display.a := 5;
> The procedure Foo.Huh compiles and executes without error.  As a side
> note, The_Display is unchanged by this procedure.

Well, you call the function Display which returns a object of type
Display_Type,
you change the field "a" of this object to 5.
After this instruction the returned object does not exist any longer.

-- 
Pascal MALAISE
(priv) mailto:malaise@magic.fr
(prof) mailto:malaise@fr.airsysatm.thomson-csf.com




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

* Re: Attempting to modify a function result
  1999-07-22  0:00 ` Robert I. Eachus
@ 1999-07-22  0:00   ` Keith Thompson
  1999-07-23  0:00     ` John Herro
  1999-07-26  0:00   ` Tucker Taft
  1 sibling, 1 reply; 8+ messages in thread
From: Keith Thompson @ 1999-07-22  0:00 UTC (permalink / raw)


"Robert I. Eachus" <eachus@mitre.org> writes:
> "Raymond C. Rawa" wrote:
> > I've encountered a "feature" of Ada83 or the VADS compiler that I don't
> > understand.
> 
>    It is a feature of Ada, not just VADS.
> 
> >    ...Display.a := 5; -- I mean't to type: The_Display.a := 5;
>  
>    Display is a function that returns an object of type Display_Type. 
> Display.a is a compontent of that object of type Integer, which gets
> assigned the value 5.  What is hard about that?

That doesn't mean you can legally assign to it.  RM95-6.4(12) says

    A function_call denotes a constant, as defined in 6.5; the nominal
    subtype of the constant is given by the result subtype of the
    function.

I couldn't find quite as clear a statement in RM83, but RM83-6.5 says

    A function is a subprogram that returns a value (the result of the
    function call).

As I understand it, a value is not an object.

Now, if the Display function returned an access value,
"Display.a := 5" could implicitly dereference the result and assign a
value to the component "a", but that's not what's happening here.

Looks like a bug in VADS to me.

-- 
Keith Thompson (The_Other_Keith) kst@cts.com  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center           <*>  <http://www.sdsc.edu/~kst>
One of the great tragedies of ancient history is that Helen of Troy
lived before the invention of the champagne bottle.




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

* Re: Attempting to modify a function result
  1999-07-22  0:00   ` Keith Thompson
@ 1999-07-23  0:00     ` John Herro
  0 siblings, 0 replies; 8+ messages in thread
From: John Herro @ 1999-07-23  0:00 UTC (permalink / raw)


Keith Thompson <kst@cts.com> writes:
>Looks like a bug in VADS to me.
     I tend to agree.  I tried the code (adding the missing word TYPE, of
course) on a once-validated Ada 83 compiler (Open Ada) and on GNAT (ver. 3.05,
admittedly not the latest version), and both compilers gave error messages.
     Now I admit that trying code on a compiler, even a validated compiler, is
NOT the right way to see if something is legal; the RIGHT way is to study the
LRM.  But I thought these data would be interesting, and they tend to confirm
what Keith said.
- John Herro
You can download a shareware AdaTutor program at
http://members.aol.com/AdaTutor






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

* Re: Attempting to modify a function result
  1999-07-22  0:00 ` Pascal MALAISE
@ 1999-07-23  0:00   ` Raymond C. Rawa
  0 siblings, 0 replies; 8+ messages in thread
From: Raymond C. Rawa @ 1999-07-23  0:00 UTC (permalink / raw)


Pascal MALAISE wrote:
> Well, you call the function Display which returns a object of type
> Display_Type,
> you change the field "a" of this object to 5.
> After this instruction the returned object does not exist any longer.

This is exactly my point.  I thought that the record returned by the
function would be a constant and therefore the compiler wouldn't let me
make an assignment to it.  At the very least, a runtime error would have
been nice.

---------------
Raymond C. Rawa




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

* Re: Attempting to modify a function result
  1999-07-22  0:00 ` Robert I. Eachus
  1999-07-22  0:00   ` Keith Thompson
@ 1999-07-26  0:00   ` Tucker Taft
  1999-07-27  0:00     ` Ehud Lamm
  1 sibling, 1 reply; 8+ messages in thread
From: Tucker Taft @ 1999-07-26  0:00 UTC (permalink / raw)


Robert I. Eachus wrote:
> 
> "Raymond C. Rawa" wrote:
> 
> > I've encountered a "feature" of Ada83 or the VADS compiler that I don't
> > understand.
> 
>    It is a feature of Ada, not just VADS.

Huh?  This is definitely a VADS bug.  Unless the function is returning
an access value, you can't use it on the left-hand-side of an
assignment.  If it returns an access value, then Display.A is
equivalent to Display.all.A, and again the value returned by the
function call is not being changed, but rather the record it
points at.
 
>                                         Robert I. Eachus
> 
> with Standard_Disclaimer;

Presumably the Standard_Disclaimer covers this "typo"? ;-)

-Tuck
-- 
-Tucker Taft   stt@averstar.com   http://www.averstar.com/~stt/
Technical Director, Distributed IT Solutions  (www.averstar.com/tools)
AverStar (formerly Intermetrics, Inc.)   Burlington, MA  USA




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

* Re: Attempting to modify a function result
  1999-07-26  0:00   ` Tucker Taft
@ 1999-07-27  0:00     ` Ehud Lamm
  0 siblings, 0 replies; 8+ messages in thread
From: Ehud Lamm @ 1999-07-27  0:00 UTC (permalink / raw)


On Mon, 26 Jul 1999, Tucker Taft wrote:

|Robert I. Eachus wrote:
|> 
|> with Standard_Disclaimer;
|
|Presumably the Standard_Disclaimer covers this "typo"? ;-)
|
|-Tuck

Only is you USE it... :-)

Ehud Lamm     mslamm@pluto.mscc.huji.ac.il
http://purl.oclc.org/NET/ehudlamm  <== My home on the web






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

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

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-07-22  0:00 Attempting to modify a function result Raymond C. Rawa
1999-07-22  0:00 ` Pascal MALAISE
1999-07-23  0:00   ` Raymond C. Rawa
1999-07-22  0:00 ` Robert I. Eachus
1999-07-22  0:00   ` Keith Thompson
1999-07-23  0:00     ` John Herro
1999-07-26  0:00   ` Tucker Taft
1999-07-27  0:00     ` Ehud Lamm

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