* [Caml-list] GADT and optional arguments: Can they work?
@ 2012-03-25 23:02 Goswin von Brederlow
0 siblings, 0 replies; only message in thread
From: Goswin von Brederlow @ 2012-03-25 23:02 UTC (permalink / raw)
To: caml-list
Hi,
after having just written a ton of functions that just differ in the
argument type I started to think that this might be a good use acase for
GADT types.
So instead of seperate get_int, get_int32, get_int64, ... functions
there would only be one get function with an extra GADT argument to
specifiy the type to be used.
For example Unix.lseek and Unix.LargeFile.lseek could be written as:
(* GADT for LargeFile *)
type _ size = Int : int size | Int64 : int64 size
let lseek : type a . a size -> Unix.file_descr -> a -> Unix.seek_command -> a =
fun size fd off cmd ->
match size with
| Int -> Unix.lseek fd off cmd
| Int64 -> Unix.LargeFile.lseek fd off cmd
val lseek : 'a size -> Unix.file_descr -> 'a -> Unix.seek_command -> 'a = <fun>
So far so good. But now one has to specify the argument size on every
lseek call. To make it nicer I would like the size to be optional and
specify a default size for the most common use:
let lseek : type a . ?size:a size -> Unix.file_descr -> a -> Unix.seek_command -> a =
fun ?(size=Int) fd off cmd ->
match size with
| Int -> Unix.lseek fd off cmd
| Int64 -> Unix.LargeFile.lseek fd off cmd
;;
(*
* fun ?(size=Int) fd off cmd ->
* ^^^
* Error: This expression has type int size
* but an expression was expected of type a size
*)
Is there a way to write this so it types correctly? I guess I would need
to somehow specify a default type for "a" for the "a size" argument to
be optional.
MfG
Goswin
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2012-03-25 23:02 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-03-25 23:02 [Caml-list] GADT and optional arguments: Can they work? Goswin von Brederlow
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox