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=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI autolearn=unavailable autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,87077e61d6b3095b X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-12-17 18:18:55 PST Path: archiver1.google.com!news2.google.com!newsfeed2.dallas1.level3.net!news.level3.com!news-out.visi.com!petbe.visi.com!news2.telebyte.nl!news.completel.fr!fr.ip.ndsoftware.net!teaser.fr!enst.fr!melchior!cuivre.fr.eu.org!melchior.frmug.org!not-for-mail From: Stephen Leake Newsgroups: comp.lang.ada Subject: Re: how do i implement double-dispatching? Date: 17 Dec 2003 21:17:29 -0500 Organization: Cuivre, Argent, Or Message-ID: References: NNTP-Posting-Host: lovelace.ada-france.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: melchior.cuivre.fr.eu.org 1071713860 65995 80.67.180.195 (18 Dec 2003 02:17:40 GMT) X-Complaints-To: usenet@melchior.cuivre.fr.eu.org NNTP-Posting-Date: Thu, 18 Dec 2003 02:17:40 +0000 (UTC) Cc: comp.lang.ada@ada-france.org To: cl1motorsports Return-Path: In-Reply-To: User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 X-Virus-Scanned: by amavisd-new-20030616-p5 (Debian) at ada-france.org X-BeenThere: comp.lang.ada@ada-france.org X-Mailman-Version: 2.1.3 Precedence: list List-Id: Gateway to the comp.lang.ada Usenet newsgroup List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Xref: archiver1.google.com comp.lang.ada:3523 Date: 2003-12-17T21:17:29-05:00 cl1motorsports writes: > I'm trying to implement double dispatching with a visitor pattern in (of > course) Ada. Ok, good choice :). > I have a parse tree package that has all the node types of the tree, > and i have a visitor package with an abstract visitor type that has > a Visit operation for all the types in the parse tree. It doesn't > want to compile the way i have it written. To be dispatching, an operation must be declared in the same package as the tagged type. In general, double dispatching must be done in two steps; first dispatch on one parameter, then, within that routine, dispatch on the other parameter. This allows both parameters to control what goes on. There is no way in general to dispatch on two parameters at once; how can the compiler know that all combinations have been provided? For your particular problem, I think the best idea is to abandon the idea of dispatching on the concrete parse_tree_node type. Instead, define one output routine that might be needed for each concrete node type, but have it dispatch on the Visitor type (HTML, Ada pretty-print, etc). Then in the tree traversal subprogram, use a case statement to call the appropriate output routine, dispatching on Vistor. A case statement is appropriate here, because the syntax of the input language is known at compile time, and therefore you can have an enumeration of node types. Hmm. I would start this project by using OpenToken (http://www.telepath.com/~dennison/Ted/OpenToken/OpenToken.html). That would lead to a set of packages defining the input grammar, with bodies that need to call output routines. The token types could take a discriminant for Visitor, so the output calls can be dispatching. That will have an enumeration of nodes, but not an explicit case statement. You should look into OpenToken; it's a great system. Feel free to email me with support questions (Ted's pretty busy these days). In your structure, that would be something like: type Visitor_Record is abstract tagged null record; type Parse_Tree_Node_Record (Visitor : access visitor_Record) is abstract tagged record ... end record; Note that it's ok to define Vistor_Record and Parse_Tree_node_Record in the same package; there's not much point to having a parse tree without a way to visit it. Hope this helps. > oh and this is my first post ever to a newsgroup so be gentle :) Congratulations :). Nice to have a Real Ada Question :). -- -- Stephe