From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: ** X-Spam-Status: No, score=2.9 required=5.0 tests=AWL,DNS_FROM_RFC_POST, HTML_MESSAGE,SPF_NEUTRAL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id A0001BBAF for ; Thu, 10 Sep 2009 17:51:01 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: An4BAAi9qErRVdzdkGdsb2JhbACCJjCYKT8BAQEBCQkMBxMDswSQQAEDAgSEFAWId4Fl X-IronPort-AV: E=Sophos;i="4.44,365,1249250400"; d="scan'208";a="35827553" Received: from mail-fx0-f221.google.com ([209.85.220.221]) by mail1-smtp-roc.national.inria.fr with ESMTP; 10 Sep 2009 17:51:01 +0200 Received: by fxm21 with SMTP id 21so189551fxm.9 for ; Thu, 10 Sep 2009 08:51:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:received:in-reply-to :references:date:x-google-sender-auth:message-id:subject:from:to:cc :content-type; bh=90gIJ2JSPPQrQnMh8NqRaf9FFo+i/zmJJjQr4UXRszE=; b=mhlCy5VQr08y3KNsX7s8MFismbqZHhNEvIDodLHCyLl8hC13GST3Pdbql7OOouubRo dD5iL09ZDa8DhIuWzqp+7CTFYzMBNkGtTLESBivKsrb9wUME9yPEYB8P/c4M5RCLYAkG 9LpJFqKEMG8j2fQNAFu1oCekmbyiJM+7oCeIk= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; b=mdKCJgB7M7I2HSZjOzkuR/qpJphKP1diVR9xZju0JnhCGd06QJYuCQOTea02FfRCwt zkIfVtBY9M5eVYnBn1lJWlXVZPYiy+LUYwTJ4m9u6JcLCffwKJYeAgo/X+B33kFg1z9V 2DE/yj7YALVr9Dc6V6dYuCHSGUMNOeJ9RsAi0= MIME-Version: 1.0 Sender: brab.alan@gmail.com Received: by 10.86.10.36 with SMTP id 36mr1301634fgj.61.1252597860915; Thu, 10 Sep 2009 08:51:00 -0700 (PDT) In-Reply-To: References: Date: Thu, 10 Sep 2009 17:51:00 +0200 X-Google-Sender-Auth: cac9831531dfa699 Message-ID: <25ec8ca60909100851o4495ec29t101970ef00b0294@mail.gmail.com> Subject: Re: [Caml-list] A (Silly?) Question About Universal Type Quantification From: Alan Schmitt To: Will M Farr Cc: caml-list@inria.fr Content-Type: multipart/alternative; boundary=0014852e19408bf95204733b2a33 X-Spam: no; 0.00; schmitt:01 schmitt:01 pervasives:01 functor:01 val:01 mutable:01 pervasives:01 functor:01 val:01 mutable:01 10,:98 2009:98 10.0:98 10,:98 2009:98 --0014852e19408bf95204733b2a33 Content-Type: text/plain; charset=ISO-8859-1 On Thu, Sep 10, 2009 at 5:00 PM, Will M Farr 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 = object method name : string method run : (test * float) list end 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] end Alan --0014852e19408bf95204733b2a33 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Thu, Sep 10, 2009 at 5:00 PM, Will M Farr <farr@mit.edu> wrot= e:
Hello,

I recently encountered a situation where I had (effectively) the following = polymorphic type:

type 'a record =3D { id : int; data : 'a }

and the following compare function

let compare {id =3D id1} {id =3D id2} =3D Pervasives.compare id1 id2

and wanted to put such records into a set. =A0However, I could not figure o= ut how to make the polymorphic 'a in the type definition "disappea= r" in the module argument to the Set.Make functor.

Interestingly, I had the same problem recently where I wante= d to create a list of "tests" that could access some extra inform= ation =A0when run (and would also return a list of tests to run later). I f= ound that using a class type worked well:

class type test =3D
=A0=A0object
=A0=A0 =A0method name : string
=A0=A0 =A0method run : (test * = float) list
=A0=A0end

To define a test, = I simply do:

let test_download : Timed_events.test =3D
=A0=A0object (self)
=A0=A0 =A0val mutable cache =3D 0 (* anythi= ng I want *)
=A0=A0 =A0method name =3D "Download"
=
=A0=A0 =A0method run =3D
=A0=A0 =A0 =A0(* From here I can access the cache and plan to run the = test again in 10 seconds *)
=A0=A0 =A0 =A0[self, 10.0]
= =A0=A0 =A0end

Alan
--0014852e19408bf95204733b2a33--