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=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!feeder.eternal-september.org!news.glorb.com!peer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post02.iad.highwinds-media.com!fx11.iad.POSTED!not-for-mail From: Brad Moore User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: A few questions References: In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Message-ID: NNTP-Posting-Host: 68.145.219.148 X-Complaints-To: internet.abuse@sjrb.ca X-Trace: 1423347285 68.145.219.148 (Sat, 07 Feb 2015 22:14:45 UTC) NNTP-Posting-Date: Sat, 07 Feb 2015 22:14:45 UTC Date: Sat, 07 Feb 2015 15:15:04 -0700 X-Received-Bytes: 6700 X-Received-Body-CRC: 1526734306 Xref: news.eternal-september.org comp.lang.ada:24920 Date: 2015-02-07T15:15:04-07:00 List-Id: On 15-02-07 10:43 AM, Laurent wrote: > Hi > > 1) Trying to implement a search function in my linked list. Doesn't compile because the return type :List_Ptr is not visible in the ads file? The compiler is telling you that List_Ptr can't be used in the visible part of a package (the part before the word private), if it hasn't been defined yet. (ie the definition is not seen until further down in the file inside the private part of the package. Your abstraction in this case involves the use of something called List_Ptr, and users of your abstraction should be able to use your package without having to look inside the private part of the package. One way to get around this, is to define List_Ptr as a private type in the visible part of the package. eg. type List_Ptr is private; The full definition of List_Ptr still occurs in the private part of the package as you have it, but the private type declaration in the visible part of the package exposes the name of the type in the visible part so that it can be used there. > > spec: > > function Search (L : in List; Element : in Element_Type) return List_Ptr; > -- Pre: L is defined; L may be empty; Element is a scalar type > -- Post: returns access to found node > > body > > function Search (L : in List; Element : in Element_Type) return List_Ptr is > Temp : List_Ptr; > Current : List; > > begin > Current := L.Head; > while Current.Head /= null loop > if Equals(Current.Head.Element, Element) then > Temp:= Current.Head; > return Temp; > end if; > Current.Head := Current.Head.Next; > end loop; > end Search; > > The List_Ptr is defined as: > > private > > ---------- > -- List -- > ---------- > > type List_Node; > type List_Ptr is access List_Node; > type List_Node is record > Element : Element_Type; > Next : List_Ptr; > Previous:List_Ptr; > end record; > > type List is record > Head : List_Ptr; > Tail : List_Ptr; > end record; > > I have put the search function in a child package. A child package isn't necessary for this purpose. > > 2) Tried the same thing but this time using an iterator but fails with > > 65:20 (Ada 2005) cannot copy object of a limited type (RM-2005 6.5(5.5/2)) The compiler is complaining because you have declared It as a local object inside your Search function, and you are trying to return that object as the result of a function call, which involves copying the object to wherever the result of the function call is to be placed. Limited types are not copyable. Is there a reason why you declared your Iterator type to be a limited type, (ie. with the keyword limited)? If you do want it to be a limited type you can return an object of that type in a function call in certain contexts. You have to use an extended return statement though. See RM 6.5 (2.1/2) and 6.5 (28/2) for an example. That would allow the return object to be built in place where the function result is to be placed, and thus avoids copying any object. Such a function that returns a limited type is usable only to initialize a declaration of a limited type. You cannot use it to change the value of the limited type once it has been initialized. This may be too restrictive for your needs, so perhaps you should consider not making this type limited. > 65:20 return by reference not permitted in Ada 2005 > on > > return It; > > spec > > function Search (L : access List; Element : Element_Type) return Iterator; > -- Pre: L is defined; L may be empty; Element is a scalar type > -- Post: returns access to found node > > body > > function Search (L : access List; Element : Element_Type) return Iterator is > It : Iterator (L); > > begin > Start (It); > while not Done (It) loop > if Equals (It.This.Element, Element) then > return It; > end if; > Next(It); > end loop; > end Search; > > The Iterator is defined as: > > private > > -------------- > -- Iterator -- > -------------- > > type Iterator (L : access List) is limited > record > This : List_Ptr; > end record; > > I have tried to understand RM-2005 6.5(5.5/2) but don't know what > > "If the result subtype of the function is defined by a subtype_mark, the return_subtype_indication shall be a subtype_indication. The type of the subtype_indication shall be the result type of the function" > > is supposed to look like. I believe you are referring to 6.5(5.2/2) not 6.5(5.5/2) This applies to an extended return statement. In your example, you are using a simple return statement, if you want to use an extended return, you need to use that syntax. The result subtype of your function is defined by a subtype_mark (Iterator), so 6.5(5.2/2) is saying that the declaration of the result object in the extended return must also be an Iterator, but you may optionally add further constraints on the declaration. In your case, you need to add a discriminant constraint for the access to the List discriminant, since you want the result to have that value. eg. return Result (L) : Iterator do null; end return; Brad > > If someone could enlighten me on both problems. > > https://github.com/Chutulu/Chapter-15.git > > 3) How can I take a look at the assembler code of both versions? Just curious to see if there is actually a difference between both versions. I have no idea of assembler but that doesn't keep me from trying. > > Thanks > > Laurent >