This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2001-04-02 (11:06) From: Sven LUTHER Subject: Re: Overloading again (Was Re: [Caml-list] Interfacing C++ and Ocaml)
On Sun, Apr 01, 2001 at 10:15:02PM +0200, Sven LUTHER wrote:
> let f1 = function
>   | 'A x -> xxx
>   | 'B x,y -> xx
>
>
> and later :
>
> let f1 = function
>   | 'C x,y,z -> xxx
>   | x -> f1 x
>

I was trold that this would not work, which after a bit of experimenting is
true :

Objective Caml version 3.01
# let f1 = function
| `A x -> 0
| `B (x,y) -> 1;;
val f1 : [< `A of 'a | `B of 'b * 'c] -> int = <fun>
#  let f1 = function
| `C (x,y,z) -> 3
| x -> f1 x;;
This expression has type [? `C of 'a * 'b * 'c] but is here used with type
[< `A of 'd | `B of 'e * 'f]

which is not cool, but the following works :

# exception Ea of int;;
exception Ea of int
# exception Eb of int * int ;;
exception Eb of int * int
# exception Ec of int * int * int ;;
exception Ec of int * int * int
# let f1 f = try f () with
| Ea x -> 0
| Eb (x,y) -> 1 ;;
val f1 : (unit -> int) -> int = <fun>
# let f2 f = try f () with
| Ec (x,y,z) -> 2
| exn -> let f () = raise exn in f1 f;;
val f2 : (unit -> int) -> int = <fun>
# let fa () = raise (Ea 1) ;;
val fa : unit -> 'a = <fun>
# let fb () = raise (Eb (1,2)) ;;
val fb : unit -> 'a = <fun>
# let fc () = raise (Ec (1,2,3)) ;;
val fc : unit -> 'a = <fun>
# f1 fa;;
- : int = 0
# f1 fb;;
- : int = 1
# f2 fc ;;
- : int = 2
# f1 fc;;
Uncaught exception: Ec (1, 2, 3).
# f2 fa;;
- : int = 0
# f2 fb;;
- : int = 1

So there may be some use for the introduction of extensible types (like the
exceptions) after all.

Friendly,

Sven Luther
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr