Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] [ANNOUNCE] ECaml 0.3 : a simple object system
[ 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] [ANNOUNCE] ECaml 0.3 : a simple object system
From: "Issac Trotts" <ijtrotts@ucdavis.edu>

> ECaml is a simple object system for OCaml, based on polymorphic variants
> and a Camlp4 syntax extension.
> 
> ECaml can
>   o create objects (entities) of anonymous class
>   o create new classes within functions or other classes.
>   o very easily define methods having polymorphic arguments

Interesting.
I shall have a look at your encoding.

You may also be interested by having a look at the current CVS version
of objective caml, because it actually has all the features you
describe. (This doesn't reduce your merit.)
Just a one line example:
        Objective Caml version 3.07+13 (2004-01-04)

# let o = object method id x = x end;;
val o : < id : 'a -> 'a > = <obj>

And if you like experiments, you may even try the objvariants branch.
Just get the CVS version, and then do:
  cvs update -r objvariants typing
It allows you to use unions of object types.
This is a quick hack (2 hours coding), and there may be bugs, but it
can be funny. Here is an example session.

# let f (x : [> ]) = x#m 3;;
val f : [>  as < m : int -> 'a; .. > ] -> 'a = <fun>
# let o = object method m x = x+2 end;;
val o : < m : int -> int > = <obj>
# f (`A o);;
- : int = 5
# let l = [`A o; `B(object method m x = x -2 method y = 3 end)];;
val l :
  [> `A of < m : int -> int > | `B of < m : int -> int; y : int > ] list =
  [`A <obj>; `B <obj>]
# List.map f l;;
- : int list = [5; 1]
# let g = function `A x -> x#m 3 | `B x -> x#y;;
val g : [< `A of < m : int -> 'a; .. > | `B of < y : 'a; .. > ] -> 'a = <fun>
# List.map g l;;
- : int list = [5; 3]

The type annotation (x : [> ]) is necessary: # is actually overloaded
on objects (the default) and variants of objects. You can view the
variant tag here a bit like a runtime type: you can pattern-match on
it when needed, but you can also call a method common to all cases
without looking at the tag.

I got the idea in a paper (in Japanese) by Hideshi Nagira and Atsushi
Igarashi, but actually this trick was first suggested to me by Koji
Kagawa about 5 years ago.

(Disclaimer: this kind of experiment is for fun, don't expect it in
the main branch anytime soon or ever.)

Jacques Garrigue

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