caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Narrowing coercions for functions with optional parameters
@ 2002-02-24 14:01 Tomasz Zielonka
  2002-02-24 18:33 ` Stefano Zacchiroli
  0 siblings, 1 reply; 5+ messages in thread
From: Tomasz Zielonka @ 2002-02-24 14:01 UTC (permalink / raw)
  To: CAML list

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2002-02-25  9:44 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-02-24 14:01 [Caml-list] Narrowing coercions for functions with optional parameters Tomasz Zielonka
2002-02-24 18:33 ` Stefano Zacchiroli
2002-02-24 19:50   ` Tomasz Zielonka
2002-02-25  1:29     ` Jacques Garrigue
2002-02-25  9:44       ` Tomasz Zielonka

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).