comp.lang.ada
 help / color / mirror / Atom feed
From: Hyman Rosen <hyrosen@mail.com>
Subject: Re: [ANN] an EBNF parser and coding pattern tool (LGPL)
Date: Wed, 12 Feb 2003 13:22:57 -0500
Date: 2003-02-12T13:22:57-05:00	[thread overview]
Message-ID: <1045074177.729142@master.nyc.kbcfp.com> (raw)
In-Reply-To: <clcm-20030211-0012@plethora.net>

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 <typename T1, typename T2>
     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<A>::FieldTypes

which would be a typedef for

     TList<int, TList<double, TList<char *, Nil> > >

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 <int N, typename T>
     struct Select;

     template <typename T1, typename T2>
     struct Select<0, TList<T1, T2> >
     { typedef T1 Type; };

     template <int N, typename T1, typename T2>
     struct Select<N, TList<T1, T2> >
     { typedef typename Select<N - 1, T2>::Type Type; };




  reply	other threads:[~2003-02-12 18:22 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-01-24  3:36 [ANN] an EBNF parser and coding pattern tool (LGPL) Cedric LEMAIRE
2003-01-24 16:40 ` Victor Porton
2003-01-25  0:33   ` llewelly
2003-01-29  2:55   ` Cedric LEMAIRE
2003-01-24 16:40 ` apm
2003-01-29  2:56   ` Cedric LEMAIRE
2003-01-31  2:25     ` Cedric LEMAIRE
2003-01-31  2:25     ` Anthony Williams
2003-02-01  4:10       ` Cedric LEMAIRE
2003-02-04 16:53     ` apm
2003-02-06 20:35       ` Cedric LEMAIRE
2003-02-06 20:36       ` Cedric LEMAIRE
2003-02-01  4:09   ` White Wolf
2003-02-03  1:28     ` Martin Ambuhl
2003-02-03  1:28     ` Hillel Y. Sims
2003-02-03 16:31       ` Tars_Tarkas
2003-02-03  1:29     ` Mark McIntyre
2003-02-03 16:31       ` CBFalconer
2003-02-04 16:53         ` Georg Bauhaus
2003-02-06 20:36           ` Cedric LEMAIRE
2003-02-07 17:33             ` Hyman Rosen
2003-02-08 18:22               ` Thant Tessman
2003-02-07 17:33             ` Anthony Williams
2003-02-11  6:15               ` Cedric LEMAIRE
2003-02-12  1:30                 ` Hyman Rosen
2003-02-13 20:03                   ` Brian Inglis
2003-02-15 20:13                     ` Hyman Rosen
2003-02-15 20:14                   ` Cedric LEMAIRE
2003-02-12  1:30                 ` Julián Albo
2003-02-15 20:14                   ` Cedric LEMAIRE
2003-02-19  6:10                     ` Alan Balmer
2003-02-12  1:30                 ` Stephen Leake
2003-02-12 18:22                   ` Hyman Rosen [this message]
2003-02-12 19:39                     ` Stephen Leake
2003-02-12 21:16                       ` Hyman Rosen
2003-02-14 20:34                         ` Stephen Leake
2003-02-15 20:14                   ` Cedric LEMAIRE
2003-02-07 17:33             ` Hyman Rosen
2003-02-03 16:31       ` ozbear
2003-02-03 16:31     ` Cedric LEMAIRE
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox