From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: Didier Cassirame <didier.cassirame@gmail.com>
Cc: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] parameterized classes, modules & polymorphic variants
Date: Sun, 11 Nov 2012 17:21:10 +0900 [thread overview]
Message-ID: <67E98D41-5721-4685-8AEF-806438A7E5B8@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <CA+Lkvyo_y7rV+7YcOmn8xKjzY7Khr58_voaouvn_aGsabHixsQ@mail.gmail.com>
On 2012/11/09, at 23:25, Didier Cassirame <didier.cassirame@gmail.com> wrote:
> Dear caml-list,
>
> I have been trying recently to combine classes, modules and variants
> in the following fashion:
>
> module A1 = struct
>
> class ['a] t = object
> constraint 'a = [>`a]
> method m : 'a -> string = function `a -> "a" | `a1 -> "a1" | _ -> "_"
> end
>
> end;;
>
> […]
>
> module type A = sig
>
> class ['a] t : object
> constraint 'a = [>`a]
> method m : 'a -> string
> end
>
> end;;
>
> type m = (module A);;
>
> let l: m list = [ (module A1); (module A2); (module A3)];;
>
> --------------------------------
>
> Unfortunately the list typecheck fails. However, making a list of
> class instances from A1.t, A2.t, A3.t succeed, with the type:
>
> [> `a | `a1 | `a2 | `a3 ] ct list
>
> ct being defined as equal to A.t.
>
> I thought that perhaps I should parameterize the type m from the type
> parameter 'a of A.t to solve my problem, but I am not sure of the
> syntax, or if it's the problem. Does anyone have an idea?
Actually the parameterization would not help here, since you want to put them all in the same list.
The idea of using first-class modules is to be explicit about types, so using an explicit type definition for a solves the problem.
Jacques Garrigue
module A1 = struct
type a = private [> `a | `a1]
class t = object
method m : a -> string = function `a -> "a" | `a1 -> "a1" | _ -> "_"
end
end;;
module A2 = struct
type a = private [> `a | `a2]
class t = object
method m : a -> string = function `a -> "a" | `a2 -> "a2" | _ -> "_"
end
end;;
module A3 = struct
type a = private [> `a | `a3]
class t = object
method m : a -> string = function `a -> "a" | `a3 -> "a3" | _ -> "_"
end
end;;
module type A = sig
type a = private [> `a]
class t : object
method m : a -> string
end
end;;
type m = (module A);;
let l: m list = [ (module A1); (module A2); (module A3)];;
next prev parent reply other threads:[~2012-11-11 8:21 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-11-09 14:25 Didier Cassirame
2012-11-11 8:21 ` Jacques Garrigue [this message]
2012-11-11 9:25 ` Didier Cassirame
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=67E98D41-5721-4685-8AEF-806438A7E5B8@math.nagoya-u.ac.jp \
--to=garrigue@math.nagoya-u.ac.jp \
--cc=caml-list@inria.fr \
--cc=didier.cassirame@gmail.com \
/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