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

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] operator overloading
From: Johan Georg Granström <georg.g@home.se>

> I had a way of working around lack of overloading, which is
> actually pretty nice, when working with LISP a couple of years
> ago. You simply define a macro which creates local bindings
> for the arithmetic functions: then you can write for example
[...]
> let a = make_van_der_monde 3
> and b = make_matrix [|...|] in
>   matrix_arithmetic
>     (B + A) * (B - A)
> 
> By defining the proper macro (using camlp4 I guess), this would
> expand to something like:
> 
> let a = make_van_der_monde 3
> and b = make_matrix [|...|] in
>   let (+) = (Matrix.+)
>   and (-) = (Matrix.-)
>   and (*) = (Matrix.*) in
>     (B + A) * (B - A)

Note that if we had a let open ... in ... construct, you wouldn't even
need camlp4 for that:

  let a = make_van_der_monde 3
  and b = make_matrix [|...|] in
  let open Matrix in
     (b + a) * (b - a)

By the way, this construct can already be simulated with camlp4.

Note of course that this is not as powerful as overloading, since you
often want to mix different instance of arithmetic operations in the
same formula.

Cheers,

        Jacques-------------------
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