Version française
Home     About     Download     Resources     Contact us    
Browse thread
C++/C# inheritance is bad?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Yoann Padioleau <padator@w...>
Subject: Re: Visitor in OCaml
"blue storm" <bluestorm.dylc@gmail.com> writes:

> On 1/19/09, Yoann Padioleau <padator@wanadoo.fr> wrote:
>> What we want is really give code like
>>
>> let my_analysis program =
>>  analyze_all_expressions program (fun expr ->
>>    match expr with
>>    | FunCall (e, es) -> do_something()
>>    | _ -> <find_a_way_to_recurse_for_all_the_other_cases>
>>  )
>>
>> The problem is how to write analyze_all_expressions
>> and find_a_way_to_recurse_for_all_the_other_cases.
>
> You should have a look at the Camlp4 "metaprogramming" facilities :
> http://brion.inria.fr/gallium/index.php/Camlp4MapGenerator

I've been there. I once implemented in camlp4 my own map-generator but
I now prefer not using fancy features or langage extensions. I prefer
to go back to basics. My solution does not use any fancy
stuff; just higher order functions, the continuation idiom,
and records. People reading your code, or downloading your
code then do not need to care about camlp4 (or objects).

Moreover, sometimes you need to do things slightly differently
from what is auto-generated and it becomes painful. For instance
it's possible to auto-generate the string_of_xxx for each
type xxx, but at some point you may want to do the printing
slightly differently, hide a few things, do some indentation
work (and use the Format library), etc, and at this point
you will have to write the tedious code. Camlp4
metaprogramming may save you some time at the beginning, but but do you really 
win that much time in the end ? 



>
> You would write something like :
> let my_analysis program =
>   let analysis = object (self)
>     inherit fold as super
>     method expr = function
>     | FunCall (e, es) -> do_something (); self
>     | other -> super#expr other
>   end in analysis#expr
>
> While sometimes a bit hairy, the object oriented layer makes it easy
> to use a refine mapper or folders without writing all the heavy
> plumbing yourself.

Thx, always interesting to see different ways to solve 
the same problem.