]>
Are structures allowed? :)
type t = {show : unit -> string}
let show l = List.map (fun x -> x.show ()) l
let integer x = {show = fun () -> string_of_int x}
let floating x = {show = fun () -> string_of_float x}
let boolean x = {show = fun () -> string_of_bool x}
let _ =
List.iter
(Printf.printf "%s\n")
(show
[
integer 10;
floating 3.14;
boolean true;
]
)
OCaml does not have Haskell-style existential types (I don't exactly
know why, but can
presume that they may interfere with objects, which considered to be
much more worthy).
I like modules and functors very much, too, but, first, modules are
not "first-class
citizens", and second, there may be no need to re-implement all your
stuff to
start using objects --- OCaml is fairy orthogonal language.
Best regards,
DB.
P.S. Objects are efficient :)
> Ahh, right! Sorry, I forgot to mention I'm looking for a possible
> solution
> without classes.
>
> I ask because most of my code base is modules and functor based and it
> would
> be a pain to convert over. Also because performance is typically
> better with
> just functions and data types.
>
> I feel like a solution without the OO side is possible through perhaps an
> analog of existential types?
>
> Peng
>
> On Tuesday 13 November 2007 04:14:06 pm Dmitri Boulytchev wrote:
>
> > Try using classes for this purpose:
>
> >let show l = List.map (fun x -> x#show) l
>
> >class integer x =
> > object
> > method show = string_of_int x
> > end
>
> >class floating x =
> > object
> > method show = string_of_float x
> > end
>
> >class boolean x =
> > object
> > method show = string_of_bool x
> > end
>
>
> >let _ =
> > List.iter
> > (Printf.printf "%s\n")
> > (show
> > [
> > new integer 10;
> > new floating 3.14;
> > new boolean true;
> > ]
> > )
>
> > Best regards,
> > Dmitri Boulytchev,
> > St.Petersburg State University.
>
> >>Hi,
> >>
> >>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? One can then imagine mapping over such lists
> >>with "generic" versions of those common operations. Here's a concrete
> >>example of what I mean:
> >>
> >> module Int = struct
> >> type t = int
> >> let show x = string_of_int x
> >> end
> >> module Float = struct
> >> type t = float
> >> let show x = string_of_float x
> >> end
> >> module Bool = struct
> >> type t = bool
> >> let show x = string_of_bool x
> >> end
> >>
> >> let xs = [`Int 1; `Float 2.0; `Bool false]
> >> let showany x = match x with
> >>
> >> | `Int x -> Int.show x
> >> | `Float x -> Float.show x
> >> | `Bool x -> Bool.show x
> >>
> >> ;;
> >> List.map showany xs;;
> >>
> >>Essentially we have ints, floats and bools. All these types can be
> >>shown. It would be nice to be able to create a list of them [1; 2.0;
> >>false] that you can then map a generalized show over. In the above
> >>example, I used polymorphic variants in order to get them into the
> >>same list and then had to define my own generalized show function,
> >>"showany". This is fine as there is only one shared operation but if
> >>there is a large set of these common operations, it becomes
> >>impractical to define a generalized version for each of them.
> >>
> >>I've come across a way to do this in haskell using what they call
> >>"existential types".
> >>
> >> http://www.haskell.org/haskellwiki/Existential_type
> >>
> >>I don't really understand existential types however and don't know if
> >>OCaml has them nor how to use them.
> >>
> >>So. How can one do this in OCaml? Is there perhaps a camlp4
> >>extension that can do this? Is there a possible functor trick that
> >>can take N modules as arguments and spit out a new module with a
> >>generalized type that can take on any of the types in the arguments
> >>and also make generalized versions of operations common to the N
> >>modules? Are there existential types or equivalents in OCaml? If so
> >>how does one go about using them?
> >>
> >>Thanks in advance to anyone who forays into this bundle of questions.
> >>
> >>Peng
>
> >_______________________________________________
> >Caml-list mailing list. Subscription management:
> >http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> >Archives: http://caml.inria.fr
> >Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> >Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>
>
_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs