comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: another way to shoot yourself in the foot?
Date: Fri, 20 Jun 2008 08:48:00 -0700 (PDT)
Date: 2008-06-20T08:48:00-07:00	[thread overview]
Message-ID: <4acc9475-249a-4a72-9453-000c38739fbb@w4g2000prd.googlegroups.com> (raw)
In-Reply-To: 54435596-5e7f-4686-a2b7-1e22d7c4b186@p25g2000hsf.googlegroups.com

On Jun 20, 3:05 am, christoph.gr...@eurocopter.com wrote:
> > --- Factory ads ---
> > with Named;
> > with Some;
>
> > package Factory is
> >    function Create(class: String; name: String) return
> > Named.Object'Class;
> > end Factory;
>
> > --- Factory.adb ---
>
> > with Ada.Strings.Fixed; use Ada.Strings.Fixed;
> > with Some;
>
> > package body Factory is
> >    --- this one is ugly, but in sake of simplicity ...
> >    function Create(class: String; name: String) return
> > Named.Object'Class is
> >    begin
> >       if Index(class, "Some") /= 0 then
> >          return Some.Create(name);
> >       end if;
> >       raise Program_Error with "Unknown class " & class;
> >    end;
> > end Factory;
>
> Since Some.Object is limited, Factory must construct in place. The
> normal return statement produces a copy

Not true.  If a function result is a limited type, a "return"
statement can be used with a function call.  The idea is that the
build-in-place would propagate.  For example, say you initialize an
object of a limited type:

  Obj : Limited_Type := Func3(Arg);

where Func3 has an extended return statement:

  return Result : Limited_Type do
    Result.Field1 := ... code to set up Result, etc.
  end return;

When this happens, I believe the effect is the same as if the code in
the extended return that sets up Result's components actually works
directly with Obj, at least semantically.  To implement this, the
address of Obj has to be passed to Func3 somehow so that Func3 can
work directly with Obj.

If, instead, you say

  Obj : Limited_Type := Func1(Something);

and Func1 does this:

  return Func2(Something_Else);

and Func2 does this:

  return Func3(Arg);

and Func3 works as above, then in effect the address of Obj gets
passed to Func1, which then propagates it to Func2, which propagates
it to Func3.  No copy (in Ada semantics) is involved.

Dmitry is right that if the language allowed this "return" to copy an
object of a limited type like this, it would be a serious language
design error, since that goes against the whole point of limited
types.  But, in fact, there is no such error.  The language is fine.

                            -- Adam



  parent reply	other threads:[~2008-06-20 15:48 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-20  9:03 another way to shoot yourself in the foot? fedya_fedyakoff
2008-06-20  9:34 ` Dmitry A. Kazakov
2008-06-20  9:48   ` fedya_fedyakoff
2008-06-20 10:01     ` Ludovic Brenta
2008-06-20 10:05 ` christoph.grein
2008-06-20 10:26   ` Dmitry A. Kazakov
2008-06-20 16:12     ` Adam Beneschan
2008-06-20 15:48   ` Adam Beneschan [this message]
2008-06-20 19:27   ` Robert A Duff
2008-06-20 23:37     ` Jeffrey R. Carter
2008-06-21  8:56       ` Dmitry A. Kazakov
2008-06-22 20:44         ` Robert A Duff
2008-06-23  7:49           ` Dmitry A. Kazakov
2008-06-24  4:02             ` george.priv
2008-06-24  7:30               ` Dmitry A. Kazakov
2008-06-24 17:16                 ` Robert A Duff
2008-06-24 19:15                   ` Jeffrey R. Carter
2008-06-24 20:31                     ` Robert A Duff
2008-06-24 20:50                       ` Ludovic Brenta
2008-06-24 23:02                         ` Robert A Duff
2008-06-24 23:42                         ` Georg Bauhaus
2008-06-24 21:24                       ` Jeffrey R. Carter
2008-06-24 23:24                         ` Robert A Duff
2008-06-25 15:07                       ` Adam Beneschan
2008-06-24 14:59             ` Adam Beneschan
2008-06-24 16:41               ` Dmitry A. Kazakov
2008-06-24 17:20                 ` Robert A Duff
2008-06-24 17:52                   ` Dmitry A. Kazakov
2008-06-24 23:35                     ` Georg Bauhaus
2008-06-25  8:09                       ` Dmitry A. Kazakov
2008-06-25 10:32                         ` Georg Bauhaus
2008-06-25 12:06                           ` Dmitry A. Kazakov
2008-06-22 20:37       ` Robert A Duff
2008-06-22 21:25         ` Jeffrey R. Carter
2008-07-04 20:52           ` Colin Paul Gloster
2008-07-04 22:15             ` (see below)
2008-07-05 16:06               ` Colin Paul Gloster
2008-07-05 13:38             ` Gary Scott
2008-07-05 16:42               ` Colin Paul Gloster
2008-07-05 19:00                 ` Gary Scott
2008-07-09 19:39                   ` Colin Paul Gloster
2008-07-09 20:35                     ` Richard Maine
2008-07-09 22:49                       ` Terence
2008-07-10  1:07                         ` Gary Scott
2008-07-10 14:10                       ` Colin Paul Gloster
2008-07-10 14:57                         ` fj
2008-07-10 16:47                           ` Richard Maine
2008-07-10 17:03                         ` Dick Hendrickson
2008-07-10 17:26                           ` Craig Powers
2008-07-10 19:55                             ` James Giles
2008-07-10 20:45                               ` Dick Hendrickson
2008-07-10 21:22                                 ` Richard Maine
2008-07-10 21:29                                   ` Craig Powers
2008-07-10 20:45                               ` Craig Powers
2008-07-10 19:51                           ` James Giles
2008-07-11 15:02                             ` Colin Paul Gloster
replies disabled

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