English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
A (Silly?) Question About Universal Type Quantification
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-09-10 (15:51)
From: Alan Schmitt <alan.schmitt@p...>
Subject: Re: [Caml-list] A (Silly?) Question About Universal Type Quantification
On Thu, Sep 10, 2009 at 5:00 PM, Will M Farr <farr@mit.edu> wrote:

> Hello,
> I recently encountered a situation where I had (effectively) the following
> polymorphic type:
> type 'a record = { id : int; data : 'a }
> and the following compare function
> let compare {id = id1} {id = id2} = Pervasives.compare id1 id2
> and wanted to put such records into a set.  However, I could not figure out
> how to make the polymorphic 'a in the type definition "disappear" in the
> module argument to the Set.Make functor.

Interestingly, I had the same problem recently where I wanted to create a
list of "tests" that could access some extra information  when run (and
would also return a list of tests to run later). I found that using a class
type worked well:

class type test =
    method name : string
    method run : (test * float) list

To define a test, I simply do:

let test_download : Timed_events.test =
  object (self)
    val mutable cache = 0 (* anything I want *)
    method name = "Download"
    method run =
      (* From here I can access the cache and plan to run the test again in
10 seconds *)
      [self, 10.0]