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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 107e1d,c8f58d04ea55331 X-Google-Attributes: gid107e1d,public X-Google-Thread: 103376,c8f58d04ea55331 X-Google-Attributes: gid103376,public From: "Samuel T. Harris" Subject: Re: Generic Packages in Ada Date: 2000/08/10 Message-ID: <3992B839.6918FE77@Raytheon.com>#1/1 X-Deja-AN: 656771345 Content-Transfer-Encoding: 7bit References: <39926569@newsserver1.picknowl.com.au> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii Organization: Raytheon Aerospace Engineering Services Mime-Version: 1.0 Newsgroups: comp.lang.ada,fr.comp.lang.ada Date: 2000-08-10T00:00:00+00:00 List-Id: Ashley Manos wrote: > > Hi - can anyone help me with this: > > I'm attempting to write a generic package for linked lists in Ada. > The data structure I'm looking to make generic is the element > ie each element in the list is composed of 2 parts: the elemtn and a pointer > to the next element. > > So my package spec looks something like this: > > generic > type Node is private; -- here's the list element > type List is access Node; -- here's the pointer to the next element Here you declare a generic formal type which is an access to a generic private type. You generic package has no knowledge of the structure of the Node type! > in the list > > package Lists is > function CreateList(N:in Node) return List; > procedure AddToRear(N:in Node; L:in out List); > procedure RemoveFirst(N:in Node; L:in out List); > end Lists; As a side note, I personally abhore mixed-case squishing-words-together style of code. Just what is wrong with Create_List, Add_to_Rear, and Remove_First. Are underscores such a problem. Add to the fact that most code pretty-printers will drop the succeeding upper case letter to lowe case and you will actually get Createlist, Addtorear, and Removefirst. I find this to be completely unreadable! > > However, the compiler is complaining (and rightly so!) about my package > body, the start of which looks like this: > > package body Lists is > > function CreateList(N:in Node) return List is > L:List; > begin > L:= new Node; > L.Next:= Null; *** Since L.all is of type Node, a generic formal private type, your code here has absolutely no knowledge of anything about type Node. Node could be an integer type, an array, a fixed point type, anything! There is nothing to imply to the generic that L points to a record of which Next is some component! > return L; > end CreateList; > > ............... etc .................. > > The compiler is saying "invalid prefix in selected component L" about the > line ***. > I can see why the compiler is raising this error, but I need my function to > refer to the "Next" component of the element, as it will be of type "List" > ie the pointer to the next element. > > If this makes absolutely any sense to anybody that might be able to help, > could you PLEASE reply. I suggest you get a copy of Grady Booch's tome on software components in Ada. He discusses in great detail the reasoning behind his design choices and provides extensive discussion on List packages. > > Thanks > Ashley (atm@picknowl.com.au) -- Samuel T. Harris, Principal Engineer Raytheon, Aerospace Engineering Services "If you can make it, We can fake it!"