Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Recovering masked methods
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Daniel de Rauglaudre <daniel.de_rauglaudre@i...>
Subject: Re: [Caml-list] Recovering masked methods (with CamlP4?)
Hi,

On Tue, Jul 16, 2002 at 02:54:45PM +0200, Alessandro Baretta wrote:

> Great! Don't you think this would be a cool addition to the 
> language? I like it, at least.

I cannot judge if it is a good idea or not: I never program with
objects and I don't know what is useful or not. I just answer the
syntactic point of view. For semantics, please go to the counter
"Semantics".

      -----

For your change:

  class a =
  object
     method m = <some stuff>
  end

into:

  class var_b =
  object
     inherit a as super_a through b as super_b
     method m = super_a # m
  end

You can do it with this file:

------------------------------------------------ foo.ml
#load "pa_extend.cmo";;
#load "q_MLast.cmo";;

open Pcaml

EXTEND
  GLOBAL: class_str_item;
  class_str_item:
    [ [ "method"; l = LIDENT; fb = fun_binding ->
          let class_a = "__class_a__" ^ l in
          <:class_str_item<
            declare
              method private $lid:class_a$ = $fb$;
              method m = self # $lid:class_a$;
            end >> ] ]
  ;
  fun_binding:
    [ RIGHTA
      [ p = patt LEVEL "simple"; e = SELF -> <:expr< fun $p$ -> $e$ >>
      | "="; e = expr -> <:expr< $e$ >>
      | ":"; t = ctyp; "="; e = expr -> <:expr< ($e$ : $t$) >> ] ]
  ;
END
------------------------------------------------

Compile it with:
   ocamlc -pp camlp4o -I +camlp4 -c foo.ml

Then you can compile your example "bar.ml" with the command:
   ocamlc -pp "camlp4o ./foo.cmo" bar.ml

You can also see what the syntax extension does with the command:
   camlp4o ./foo.cmo pr_o.cmo bar.ml

      -----

For the case:

  class var_b =
  object
     inherit a as super_a through b as super_b
     method m = super_a # m
  end

into:

  class var_b =
  object
     inherit b as super_b
     method m = __class_a__m
  end

It could be done by Camlp4 the same way but there is a small difficulty:
it is not really "syntactic sugar" since it supposes, when reaching
the item "method m = super_a # m" that you know that "super_a" has
been previously defined as "inherit a as super_a through b as
super_b".

This is mainly "semantics". I mean that the parser should record
the information that an "inherit" statement has been defined.

It is possible, of course, and I can give you the code which
does that, but is it not supposed to be Camlp4 work, if you want
to be "pure". The code risks to be a "hack". You have probably to
use references to record this information to be possibly used
afterwards.

-- 
Daniel de RAUGLAUDRE
daniel.de_rauglaudre@inria.fr
http://cristal.inria.fr/~ddr/
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners