Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!mips!apple!netcomsv!jls From: jls@netcom.COM (Jim Showalter) Newsgroups: comp.object Subject: Re: generic sort orders Message-ID: <1991May22.220606.4601@netcom.COM> Date: 22 May 91 22:06:06 GMT References: <1991May21.064913.16149@netcom.COM> <1991May22.012821.12048@tkou02.enet.dec.com> <1991May22.183044.5634@Think.COM> <1991May22.203058.15452@ccs.carleton.ca> Distribution: comp Organization: Netcom - Online Communication Services UNIX System {408 241-9760 guest} Lines: 47 >In article <1991May22.183044.5634@Think.COM> barmar@think.com writes: >>In article <1991May22.012821.12048@tkou02.enet.dec.com> diamond@jit533.enet@tkou02.enet.dec.com (Norman Diamond) writes: >>>Responsibility for specifying a "<" operation belongs to the call site. >> >>Which then requires the call site to know about all the possible types of >>things it will compare, which is contrary to object orientation. Not at all true. The generic sort takes in two arguments: the specific type being acted upon, and the "<" operation for that particular type. Both are supplied by the client of the generic. In Ada, it looks like this: generic type Item is private; -- Formal metatype for any item. with function "<" (Left_Item : in Item; Right_Item : in Item) return Boolean; package Sort_Generic is type Stuff_To_Sort is array (<>) of Item; procedure Sort (This_Stuff : in out Stuff_To_Sort); -- This procedure calls the supplied "<" operation to do the sorting. end Sort_Generic; package Integer_Sorting is new Sort_Generic (Item => Integer, "<" => "<" -- For integers.); Then one declares an array of integers and passes them to Integer_Sorting.Sort. Note the clean separation of concerns: the sort generic has no clue as to what it might be instantiated with nor to how to compare what it is instantiated with, but it doesn't matter because the client, of course, knows how to do both. The client, on the other hand, has no idea how to sort things. >>One possible way around this would be for the generic comparison function >>to take a third argument, which would be an object that represents the >>style of comparison being done. Sorry, I haven't really thought this >>through, but it could contain flags such as case-sensitivity, relative >>priority of fields, etc. Making it truly generic could be tough, though; >>it would probably work best in a language with good relexive capabilities >>(e.g. the field priorities could be passed in as a list of slot names if >>the language permits accessing slots using computed names, a la CLOS's >>SLOT-VALUE function). None of this is necessary, as per above. -- **************** JIM SHOWALTER, jls@netcom.com, (408) 243-0630 **************** *Proven solutions to software problems. Consulting and training on all aspects* *of software development. Management/process/methodology. Architecture/design/* *reuse. Quality/productivity. Risk reduction. EFFECTIVE OO usage. Ada/C++. *