From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,e859f774bbb3dfb3 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!w4g2000prd.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: another way to shoot yourself in the foot? Date: Fri, 20 Jun 2008 08:48:00 -0700 (PDT) Organization: http://groups.google.com Message-ID: <4acc9475-249a-4a72-9453-000c38739fbb@w4g2000prd.googlegroups.com> References: <54157920-377a-441b-9b0b-f0c4f9ddffec@f36g2000hsa.googlegroups.com> <54435596-5e7f-4686-a2b7-1e22d7c4b186@p25g2000hsf.googlegroups.com> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1213976880 24641 127.0.0.1 (20 Jun 2008 15:48:00 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 20 Jun 2008 15:48:00 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: w4g2000prd.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:784 Date: 2008-06-20T08:48:00-07:00 List-Id: 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