comp.lang.ada
 help / color / mirror / Atom feed
* warning: cannot return a local value by reference
@ 2004-06-02 22:11 k_leau
  2004-06-02 22:36 ` Georg Bauhaus
  2004-06-03  8:23 ` Martin Krischik
  0 siblings, 2 replies; 6+ messages in thread
From: k_leau @ 2004-06-02 22:11 UTC (permalink / raw)


Hello,

I am new to Ada programming and I have the following problem:

I am using the APQ binding for working with Postgresql.

I have created the following spec "item_dbm_cls.ads" with the following:

with item_cls, apq.postgresql.client;
use item_cls, apq, apq.postgresql, apq.postgresql.client;

package item_dbm_cls is
    type item_dbm is limited private;
    function new_item_dbm(it_dbm: item_dbm) return item_dbm;

private
    type item_dbm is record
        it: item;
        connection: connection_type;
    end record;

end item_dbm_cls;

---------------------------------------
And a body "item_dbm_cls.adb" with:

package body item_dbm_cls is
    function new_item_dbm(it_dbm: item_dbm) return item_dbm is
        i_dbm: item_dbm;
    begin
        set_host_name(i_dbm.connection, host_name(it_dbm.connection));
    return i_dbm;
    end new_item_dbm;

end item_dbm_cls;

Why do I have this warning?
What mistake have I done? 

Thanks for any help.




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

* Re: warning: cannot return a local value by reference
  2004-06-02 22:11 warning: cannot return a local value by reference k_leau
@ 2004-06-02 22:36 ` Georg Bauhaus
  2004-06-02 22:49   ` k_leau
  2004-06-03  8:23 ` Martin Krischik
  1 sibling, 1 reply; 6+ messages in thread
From: Georg Bauhaus @ 2004-06-02 22:36 UTC (permalink / raw)


k_leau <k_leau@yahoo.com> wrote:
: Hello,
: 
: I am new to Ada programming and I have the following problem:

The value of type item_dbm that your function should return is

a) local to the function
b) limited.

(b) makes item_dbm a return-by-reference type under current rules.
(The local value will be lost when the function is done, it can
no longer be refered to).

Instead you could declare a procedure,

  procedure new_from_old(old: item_dbm; another: out item_dbm);

and then create a new item_db object, `fresh', and initialise it with
values from a previous item_db object,

  declare
     fresh: item_dbm;
  begin
     new_from_old(..., fresh);
     ...use fresh...
  end;



-- Georg



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

* Re: warning: cannot return a local value by reference
  2004-06-02 22:36 ` Georg Bauhaus
@ 2004-06-02 22:49   ` k_leau
  0 siblings, 0 replies; 6+ messages in thread
From: k_leau @ 2004-06-02 22:49 UTC (permalink / raw)


Thanks

"Georg Bauhaus" <sb463ba@l1-hrz.uni-duisburg.de> wrote in message
news:c9lkp0$iqs$1@a1-hrz.uni-duisburg.de...
> k_leau <k_leau@yahoo.com> wrote:
> : Hello,
> :
> : I am new to Ada programming and I have the following problem:
>
> The value of type item_dbm that your function should return is
>
> a) local to the function
> b) limited.
>
> (b) makes item_dbm a return-by-reference type under current rules.
> (The local value will be lost when the function is done, it can
> no longer be refered to).
>
> Instead you could declare a procedure,
>
>   procedure new_from_old(old: item_dbm; another: out item_dbm);
>
> and then create a new item_db object, `fresh', and initialise it with
> values from a previous item_db object,
>
>   declare
>      fresh: item_dbm;
>   begin
>      new_from_old(..., fresh);
>      ...use fresh...
>   end;
>
>
>
> -- Georg




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

* Re: warning: cannot return a local value by reference
  2004-06-02 22:11 warning: cannot return a local value by reference k_leau
  2004-06-02 22:36 ` Georg Bauhaus
@ 2004-06-03  8:23 ` Martin Krischik
  2004-06-03 10:02   ` k_leau
  1 sibling, 1 reply; 6+ messages in thread
From: Martin Krischik @ 2004-06-03  8:23 UTC (permalink / raw)


k_leau wrote:

> Hello,
> 
> I am new to Ada programming and I have the following problem:
> 
> I am using the APQ binding for working with Postgresql.
> 
> I have created the following spec "item_dbm_cls.ads" with the following:
> 
> with item_cls, apq.postgresql.client;
> use item_cls, apq, apq.postgresql, apq.postgresql.client;
> 
> package item_dbm_cls is
>     type item_dbm is limited private;
>     function new_item_dbm(it_dbm: item_dbm) return item_dbm;
> 
> private
>     type item_dbm is record
>         it: item;
>         connection: connection_type;
>     end record;
> 
> end item_dbm_cls;

In addition to the other answer (the one suggesting a procedure) let me ask
you a question:

The full private view of item_dbm is non limited so why did you make the
partial public view limited?

With Regards

Martin
-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com




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

* Re: warning: cannot return a local value by reference
  2004-06-03  8:23 ` Martin Krischik
@ 2004-06-03 10:02   ` k_leau
  2004-06-03 10:37     ` Martin Krischik
  0 siblings, 1 reply; 6+ messages in thread
From: k_leau @ 2004-06-03 10:02 UTC (permalink / raw)


I think that's because Connection_type is a limited type and there was an
error message telling me "completion of non limited type cannot be
limited"...so I put limited in the public view and the error disappeared...
I am just an Ada newbie...and I am far for understanding all the subtleties
of the language yet.

"Martin Krischik" <krischik@users.sourceforge.net> wrote in message
news:3595768.JMpgFWyRs6@linux1.krischik.com...
> k_leau wrote:
>
> > Hello,
> >
> > I am new to Ada programming and I have the following problem:
> >
> > I am using the APQ binding for working with Postgresql.
> >
> > I have created the following spec "item_dbm_cls.ads" with the following:
> >
> > with item_cls, apq.postgresql.client;
> > use item_cls, apq, apq.postgresql, apq.postgresql.client;
> >
> > package item_dbm_cls is
> >     type item_dbm is limited private;
> >     function new_item_dbm(it_dbm: item_dbm) return item_dbm;
> >
> > private
> >     type item_dbm is record
> >         it: item;
> >         connection: connection_type;
> >     end record;
> >
> > end item_dbm_cls;
>
> In addition to the other answer (the one suggesting a procedure) let me
ask
> you a question:
>
> The full private view of item_dbm is non limited so why did you make the
> partial public view limited?
>
> With Regards
>
> Martin
> -- 
> mailto://krischik@users.sourceforge.net
> http://www.ada.krischik.com
>




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

* Re: warning: cannot return a local value by reference
  2004-06-03 10:02   ` k_leau
@ 2004-06-03 10:37     ` Martin Krischik
  0 siblings, 0 replies; 6+ messages in thread
From: Martin Krischik @ 2004-06-03 10:37 UTC (permalink / raw)


k_leau wrote:

> I think that's because Connection_type is a limited type and there was an
> error message telling me "completion of non limited type cannot be
> limited"...so I put limited in the public view and the error
> disappeared... I am just an Ada newbie...and I am far for understanding
> all the subtleties of the language yet.

Well that is in fact correct. If Connection_type is limited the record need
to be limited as well.

But than it should have been:

ᅵᅵᅵᅵᅵtypeᅵitem_dbmᅵis limitedᅵrecord
ᅵᅵᅵᅵᅵᅵᅵᅵᅵit:ᅵitem;
ᅵᅵᅵᅵᅵᅵᅵᅵᅵconnection:ᅵconnection_type;
ᅵᅵᅵᅵᅵendᅵrecord;

as well. Otherwise I would expect that the record is not limited when used
privatly. Privat use is allowed for the package and all it's child
packages.

One of the language lawers might explain why not.

With Regards

Martin
-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com




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

end of thread, other threads:[~2004-06-03 10:37 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-06-02 22:11 warning: cannot return a local value by reference k_leau
2004-06-02 22:36 ` Georg Bauhaus
2004-06-02 22:49   ` k_leau
2004-06-03  8:23 ` Martin Krischik
2004-06-03 10:02   ` k_leau
2004-06-03 10:37     ` Martin Krischik

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