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,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,745ecf6266b2fdbf X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-02-12 10:23:01 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!skynet.be!skynet.be!213.51.129.3.MISMATCH!newshub1.home.nl!home.nl!news2.euro.net!ash.uu.net!spool0900.news.uu.net!reader0902.news.uu.net!not-for-mail Date: Wed, 12 Feb 2003 13:22:57 -0500 From: Hyman Rosen User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.3b) Gecko/20030130 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: [ANN] an EBNF parser and coding pattern tool (LGPL) References: In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Organization: KBC Financial Products Message-ID: <1045074177.729142@master.nyc.kbcfp.com> Cache-Post-Path: master.nyc.kbcfp.com!unknown@nightcrawler.nyc.kbcfp.com X-Cache: nntpcache 3.0.1 (see http://www.nntpcache.org/) NNTP-Posting-Host: 204.253.250.10 X-Trace: 1045074178 reader2.ash.ops.us.uu.net 5646 204.253.250.10 Xref: archiver1.google.com comp.lang.ada:34029 Date: 2003-02-12T13:22:57-05:00 List-Id: Stephen Leake wrote: > Is there any language whose generics do this? Not that I know of, except maybe for the lisp-like ones. But Java is certainly able to do it at runtime. > You've lost me here. If I need to navigate a tree, I first have to > build the tree, presumably using a tree class. Then the tree class > must provide a "walker" (aka "iterator"). Where do "generics" or > "templates" come into it? When things are known at compile time, they can be generated at compile time. You need to read up on what's being done in the world of C++ templates. Here's a cursory synopsis: struct Nil { }; template struct TList { typedef T1 first; typedef T2 rest; }; Now, given an arbitrary class, like struct A { int a; double c; char *p; }; I could reasonably ask the compiler to provide me with Introspect::FieldTypes which would be a typedef for TList > > and then I could write "recursive" templates which would work on this typelist. If I had a similar list of integers that gave the offset of each field within A, I could then use the pair of lists to build a serialization mechanism as a generic function that would wotk for arbitrary classes. (I'd need something similar for the base classes, but you get the idea.) The actual code that would wind up being generated by an instantiation for a particular type would be a straightforward set of operations on each field, which is why it's so desirable for this to be in templates. So you see, this answers your question. The "tree" is built by the compiler, and is represented by the TList<...> type. The walker/iterator is done through template partial specializations (which I realize I haven't shown, so here's an example). // Get the Nth type of a typelist template struct Select; template struct Select<0, TList > { typedef T1 Type; }; template struct Select > { typedef typename Select::Type Type; };