Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: caml-list@inria.fr
Subject: [Caml-list] GADT and optional arguments: Can they work?
Date: Mon, 26 Mar 2012 01:02:24 +0200	[thread overview]
Message-ID: <87y5qouvwf.fsf@frosties.localnet> (raw)

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

                 reply	other threads:[~2012-03-25 23:02 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=87y5qouvwf.fsf@frosties.localnet \
    --to=goswin-v-b@web.de \
    --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