comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <stephen_leake@acm.org>
To: cl1motorsports <cwlambert76@yahoo.com>
Cc: comp.lang.ada@ada-france.org
Subject: Re: how do i implement double-dispatching?
Date: 17 Dec 2003 21:17:29 -0500
Date: 2003-12-17T21:17:29-05:00	[thread overview]
Message-ID: <mailman.134.1071713860.31149.comp.lang.ada@ada-france.org> (raw)
In-Reply-To: <pan.2003.12.17.18.08.34.122050@yahoo.com>

cl1motorsports <cwlambert76@yahoo.com> 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




  reply	other threads:[~2003-12-18  2:17 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-12-17 18:08 how do i implement double-dispatching? cl1motorsports
2003-12-18  2:17 ` Stephen Leake [this message]
2003-12-18  9:48 ` Dmitry A. Kazakov
2003-12-18 15:13 ` Martin Krischik
2003-12-20  6:27 ` cl1motorsports
2003-12-20  9:56   ` Martin Krischik
2003-12-20 17:00     ` cl1motorsports
2003-12-21 13:39       ` Stephen Leake
2003-12-21 17:40         ` cl1motorsports
2003-12-22 22:47           ` Stephen Leake
2003-12-22 21:41   ` cl1motorsports
2003-12-22 21:47     ` cl1motorsports
2003-12-23 11:03       ` Dmitry A. Kazakov
2003-12-22 22:49     ` Stephen Leake
2003-12-22 23:04       ` cl1motorsports
replies disabled

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