Version française
Home     About     Download     Resources     Contact us    
Browse thread
[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: -- (:)
From: Tomasz Zielonka <zielony@c...>
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
  ?add:int -> int -> int
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