Accueil     À propos     Téléchargement     Ressources     Contactez-nous

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

[Caml-list] Narrowing coercions for functions with optional parameters
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2002-02-24 (14:01) From: Tomasz Zielonka Subject: [Caml-list] Narrowing coercions for functions with optional parameters
```Hi

I'm just curious - would it be possible to implement narrowing coercions
for functions with optional parameters?

It seems that such coercion is performed automatically in some situations.

Example:

# type basefun = int -> int ;;
type basefun = int -> int

# let f x = x ;;
val f : 'a -> 'a = <fun>

# (f :> basefun) ;;
- : basefun = <fun>

# let g ?(add = 0) x = x + add ;;
val g : ?add:int -> int -> int = <fun>

# (g :> basefun) ;;
This expression cannot be coerced to type basefun = int -> int; it has type
but is here used with type int -> int

(* Of course I can do: *)

# (fun x -> g x : basefun) ;;
- : basefun = <fun>

(* Hmmm... now it gets strange... *)

# let coerce f x = f x
val coerce : ('a -> 'b) -> 'a -> 'b = <fun>

# coerce g ;;
- : int -> int = <fun>
(* The above works, so g was used with type: int -> int *)
(* But... *)

# (g : int -> int) ;;
This expression has type ?add:int -> int -> int but is here used with type
int -> int

(* ??? *)

I hope someone can explain me this...

best regards,
tom

--
.-.   Tomasz Zielonka                           CYBER SERVICE
oo|   programista                        http://www.cs.net.pl
/`'\   zielony@cs.net.pl
(\_;/)  tel: [48] (22) 723-06-79 | tel/fax: [48] (22) 723-01-75
-------------------
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

```