On Sat, May 30, 2015 at 5:28 PM, Jonathan S. Shapiro <[email protected]> wrote:
> Let me show the part I don't know how to do in any current language.
>
> Assume we have a templated structure ASTNode<T>. It has a children field.
> The intent is that the child field will be some structured type (a record or
> a product). Depending on the type T, we want the type of the "child" field
> to be different.
>
> I suppose we could do this with a family of AST structures, e.g. where T is
> the record type itself. So we might have
>
> struct Expr {
>   ASTNode<Expr> * e1;
>   ASTNode<Expr> *e2;
> }
>
> struct ASTNode<T> {
>   ... common fields ...
>   T children;
> }

Where did all the other union legs go? I guess this is C++ now, and
those can be defined separately.

> though this doesn't seem to handle any of the "subunion" cases in a way I
> can obviously see.

Right. The Node trick just factors out the common fields. It doesn't
give you your subunions. I wasn't aware you'd need that from the
problem description from about a week ago.

> Except I'm not sure that we can forward-reference a
> template in this way.

Right. ASTNode should've gone before Expr.

Anyway, why does this problem keep switching languages? At first I
thought you were doing something in F#, then I showed it in OCaml, now
today for a while I thought you wanted to be able to do it in BitC,
and now you're talking about templates from C++.

While I am saying that you should be able to do this (factoring with
Node, not subunion) in any of those, I don't know if it'd be a good
way to do it in any of those except OCaml.

> Even if we can, it begs the question of how to specialize the member
> functions of ASTNode, which need to do different things depending on how T
> was instantiated. We really don't want do this by using a distinct function
> for each type T.

Back when I recommended it, Node was just an alias for a tuple type.
No member functions. If the AST were to be a variant datatype, it
wouldn't technically have member functions either. I showed how you'd
define a function by pattern matching on Node and then the children
field (which didn't have a name at the time).

If you're doing this in a class-oriented way, I wouldn't recommend Node at all.
_______________________________________________
bitc-dev mailing list
[email protected]
http://www.coyotos.org/mailman/listinfo/bitc-dev

Reply via email to