Re: another way to shoot yourself in the foot?
- From: "Jeffrey R. Carter" <spam.jrcarter.not@xxxxxxxxxxxx>
- Date: Tue, 24 Jun 2008 21:24:50 GMT
Robert A Duff wrote:
Not quite the "only" -- you could rename it.
True. I forgot that one.
But in Ada 95, if you say:
P(F(...));
where F returns limited, you are not passing a limited object,
but an implicit reference to some object that already existed before the
call to F. You can do that more clearly by having F return
an access type. I really think return-by-ref was a mistake.
Too bad we didn't think of it in 1992. For that matter, too
bad JDI didn't think of it in 1980. ;-)
Of course you're passing a reference, since limited types are always passed by reference (since Ada 95). But I don't think that the object had to exist before the call to F:
procedure Task_Return_Test is
package Wrap is
task type T;
function F return T;
end Wrap;
package body Wrap is
type T_Ptr is access T;
function F return T is
R : T_Ptr := new T;
begin -- F
return R.all;
end F;
task body T is
-- null;
begin -- T
null;
end T;
end Wrap;
procedure P (V : in Wrap.T) is
-- null;
begin -- P
null;
end P;
begin -- Task_Return_Test
P (V => Wrap.F);
end Task_Return_Test;
This is valid Ada 95, according to a compiler.
Well, there are only two ways a function call can be used: to initialize
a new object, or to update an existing object (i.e. on the right-hand
side of an assignment statement). But don't forget there are a dozen or
so different ways of creating new objects (components, parameters,
parent parts of aggregates, etc). All of these are allowed for
build-in-place function calls. The only thing that's not allowed is the
assignment statement.
You can still say:
P(F(...));
where F returns a limited type. Yes, there's a new object being created
(the formal parameter of P), and F is being used to initialize that
object (in place!). In Ada 2005, functions always return newly-created
objects, which I think is appropriate.
I learned that functions return objects, and in this case the object is the actual parameter to P, not the formal parameter. The object is anonymous. Maybe that rule's changed for limited parameters in current Ada. But I take your point that F can be regarded as always building in place, wherever that place may be.
if F(...).Flag then ...
The last one is pretty silly -- it creates a new limited object,
grabs a boolean flag out of it, and then throws the whole thing
away.
It's also an example in which the object that F builds in is clearly anonymous, unlike the parameter of P above.
--
Jeff Carter
"So if I understand 'The Matrix Reloaded' correctly, the Matrix is
basically a Microsoft operating system--it runs for a while and
then crashes and reboots. By design, no less. Neo is just a
memory leak that's too hard to fix, so they left him in ... The
users don't complain because they're packed in slush and kept
sedated."
Marin D. Condic
65
.
- Follow-Ups:
- Re: another way to shoot yourself in the foot?
- From: Robert A Duff
- Re: another way to shoot yourself in the foot?
- References:
- another way to shoot yourself in the foot?
- From: fedya_fedyakoff
- Re: another way to shoot yourself in the foot?
- From: christoph . grein
- Re: another way to shoot yourself in the foot?
- From: Robert A Duff
- Re: another way to shoot yourself in the foot?
- From: Jeffrey R. Carter
- Re: another way to shoot yourself in the foot?
- From: Dmitry A. Kazakov
- Re: another way to shoot yourself in the foot?
- From: Robert A Duff
- Re: another way to shoot yourself in the foot?
- From: Dmitry A. Kazakov
- Re: another way to shoot yourself in the foot?
- From: george . priv
- Re: another way to shoot yourself in the foot?
- From: Dmitry A. Kazakov
- Re: another way to shoot yourself in the foot?
- From: Robert A Duff
- Re: another way to shoot yourself in the foot?
- From: Jeffrey R. Carter
- Re: another way to shoot yourself in the foot?
- From: Robert A Duff
- another way to shoot yourself in the foot?
- Prev by Date: Re: another way to shoot yourself in the foot?
- Next by Date: ridiculous question
- Previous by thread: Re: another way to shoot yourself in the foot?
- Next by thread: Re: another way to shoot yourself in the foot?
- Index(es):
Relevant Pages
|