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=-0.4 required=5.0 tests=AC_FROM_MANY_DOTS,BAYES_00, XPRIO autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,6cdf06eb7605332d,start X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!news.glorb.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!cyclone1.gnilink.net!spamkiller2.gnilink.net!gnilink.net!trndny07.POSTED!0f19ed38!not-for-mail From: "Frank J. Lhota" Newsgroups: comp.lang.ada Subject: Smarter Generics X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1437 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441 Message-ID: <%6uTc.592$de4.1@trndny07> Date: Sat, 14 Aug 2004 19:55:39 GMT NNTP-Posting-Host: 141.154.229.139 X-Complaints-To: abuse@verizon.net X-Trace: trndny07 1092513339 141.154.229.139 (Sat, 14 Aug 2004 15:55:39 EDT) NNTP-Posting-Date: Sat, 14 Aug 2004 15:55:39 EDT Xref: g2news1.google.com comp.lang.ada:2734 Date: 2004-08-14T19:55:39+00:00 List-Id: The recent thread on "A Simple Ada Problem" got me thinking about a limitation of Ada generics. Generic are a great way of generalizing an implementation strategy for a particular problem, but generics cannot (or cannot easily) select one of several implementation strategies based on the generic parameters. Take the following simple example: Assume that I want to implement stacks of limited length, e.g. a stack that I can push or pop elements of a given type, but with a limit on how many elements will be on the stack at one time: generic type Stack_Element is private; Max_Length : in Positive; package Limited_Stacks is type Stack_Type is limited private; Stack_Overflow, Stack_Underflow : exception; procedure Push ( Item : in Stack_Element; Onto : in out Stack_Type ); procedure Pop ( Item : out Stack_Element; Onto : in out Stack_Type ); private ... end Limited_Stacks; Two possible implementation strategies: 1. We could implement the stack as an array of Max_Length elements, along with a component indicating the current length. 2. We could maintain an adjustable list of pointers to elements allocated from the heap. Obviously, implementation 1 would be faster and more convenient if Max_Length * Stack_Element'Size is relatively small. Implementation 2 involves additional overhead, but can handle cases where stack elements can vary quite a bit in size. What would be great is if there were some way to write Limited_Stacks so that the implementation strategy could be chosen based on the generic parameters, so that we could do something like this: if Max_Length * Stack_Element'Size <= Some_Threshold then -- Static memory allocation should be fine Implement Limited_Stacks using strategy 1; else -- Size could be a problem, better go with pointers. Implement Limited_Stacks using strategy 2; end if; AFAIK neither Ada generics nor C++ templates have this level of intelligence. Would smarter generics be a worthwhile addition to Ada, and if so, what form should it take?