From: Benjamin Geer <ben@socialtools.net>
To: caml-list@inria.fr
Subject: [Caml-list] polymorphic function not recognised as such
Date: Sun, 14 Dec 2003 19:14:12 +0000 [thread overview]
Message-ID: <3FDCB684.5000203@socialtools.net> (raw)
Can anyone explain to me why the following doesn't compile:
----------
type thing = Int of int | String of string ;;
let int_of_thing t =
match t with
Int i -> i
| String s -> int_of_string s ;;
let string_of_thing t =
match t with
Int i -> string_of_int i
| String s -> s ;;
let compare_things t1 t2 comparison_fun =
match (t1, t2) with
(Int i, _) -> comparison_fun i (int_of_thing t2)
| (String s, _) -> comparison_fun s (string_of_thing t2) ;;
let is_less_than t1 t2 =
compare_things t1 t2 (<) ;;
let is_greater_than t1 t2 =
compare_things t1 t2 (>) ;;
----------
The compiler says:
File "test.ml", line 16, characters 38-39:
This expression has type string but is here used with type int
It seems to have decided that in the function compare_things,
comparison_fun is int -> int -> 'a because that's how it's used in the
first pattern, not realising that I mean to pass a polymorphic function,
'a -> 'a -> bool.
Is there a way to make this work? (Specifying the type of
comparison_fun as 'a -> 'a -> bool makes no difference.)
Ben
-------------------
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
next reply other threads:[~2003-12-14 19:14 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-12-14 19:14 Benjamin Geer [this message]
2003-12-14 20:06 ` Jean-Baptiste Rouquier
[not found] ` <3FDCC2A8.2090901@ens-lyon.org>
2003-12-14 21:38 ` Benjamin Geer
2003-12-14 22:06 ` Olivier Andrieu
2003-12-15 12:07 ` Michal Moskal
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=3FDCB684.5000203@socialtools.net \
--to=ben@socialtools.net \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox