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
module type...
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-11-12 (16:44)
From: Matt Gushee <mgushee@h...>
Subject: Re: [Caml-list] Specifying abstract type in a record
On Fri, Nov 12, 2004 at 10:11:51AM -0600, josh wrote:

> # type doer = { file_name:string ; actor: ('a -> unit) };;
> But when I do this, it tells me that I've got "Unbound type parameter 'a 
> ".

Your first impulse was on the right track, but see below.

> # type t
> # type doer = { file_name:string; actor (t -> unit) };;
> It works until I try to use a created record:
> # let b = {file_name = "one"; actor = (fun x -> () ) };;
> # b.actor 10;;
> The expression has type int but is used with type t

Right. You can't directly *use* an abstract type. Generally, you would
declare an abstract type in an interface--an .mli file, a module sig, or
a class type--then specify it ('type t = ...') in an implementation.
Abstract types are good for keeping implementation details hidden from
the user, but they don't, by themselves, give you polymorphism.

> it doesn't work.  So, how _can_ I specify a record with an abstract 
> field?

  type 'a doer = { file_name : string; actor : ('a -> unit) }

That's known as a parameterized record type.

You would also do the same thing for other data structures, and for
objects (though the syntax is a bit different in the latter case)

Matt Gushee                 When a nation follows the Way,
Haven Rock Press            Horses bear manure through
Englewood, Colorado, USA        its fields;   
books@havenrock.com         When a nation ignores the Way,
                            Horses bear soldiers through
                                its streets.
                            --Lao Tzu (Peter Merel, trans.)