From: Julien Moutinho <julien.moutinho@gmail.com>
To: caml-list@inria.fr
Subject: Re: [Caml-list] polymorphic lists, existential types and asorted other hattery
Date: Tue, 13 Nov 2007 19:29:55 +0100 [thread overview]
Message-ID: <20071113182955.GB14227@localhost> (raw)
In-Reply-To: <200711131227.18910.peng.zang@gmail.com>
On Tue, Nov 13, 2007 at 12:27:07PM -0500, Peng Zang wrote:
> Is there a way to create lists in which the elements may be of
> differing types but which all have some set of operations defined
> (eg. tostr) in common?
> [...]
Objects may be of interest to you:
% cat file.ml
class ['e] skel (e: 'e) =
object
val mutable e = e
method get = e
method set x = e <- x
end
class virtual ['e] xs
(to_string: 'e -> string) =
object (self)
method virtual get : 'e
method coerce = (self :> < show : string >)
method show = to_string self#get
end
class oint (e: 'e) =
object
inherit ['e] skel e
inherit ['e] xs string_of_int
end
class ofloat (e: 'e) =
object
inherit ['e] skel e
inherit ['e] xs string_of_float
end
let xs =
[ (new oint 1)#coerce
; (new ofloat 2.0)#coerce
; (new oint 3 :> < show : string >)
; (object method show = "soleil" end)
]
;;
List.iter (fun o -> print_endline o#show) xs
% ocaml file.ml
1
2.
3
soleil
% ocamlc -i file.ml
class ['a] skel :
'a -> object val mutable e : 'a method get : 'a method set : 'a -> unit end
class virtual ['a] xs :
('a -> string) ->
object
method coerce : < show : string >
method virtual get : 'a
method show : string
end
class oint :
int ->
object
val mutable e : int
method coerce : < show : string >
method get : int
method set : int -> unit
method show : string
end
class ofloat :
float ->
object
val mutable e : float
method coerce : < show : string >
method get : float
method set : float -> unit
method show : string
end
val xs : < show : string > list
HTH,
Julien.
next prev parent reply other threads:[~2007-11-13 18:29 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-11-13 17:27 Peng Zang
2007-11-13 18:02 ` [Caml-list] " Arnaud Spiwack
2007-11-13 18:29 ` Julien Moutinho [this message]
2007-11-13 18:35 ` Julien Moutinho
2007-11-13 21:14 ` Dmitri Boulytchev
2007-11-13 18:24 ` Peng Zang
2007-11-13 21:39 ` Dmitri Boulytchev
2007-11-13 19:13 ` Benjamin Canou
2007-11-14 4:48 ` Jacques Garrigue
2007-11-14 12:45 ` Peng Zang
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=20071113182955.GB14227@localhost \
--to=julien.moutinho@gmail.com \
--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