caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] What am I reinventing here?
@ 2012-02-13 23:33 Andre Nathan
  2012-02-14  7:03 ` Arnaud Spiwack
  0 siblings, 1 reply; 7+ messages in thread
From: Andre Nathan @ 2012-02-13 23:33 UTC (permalink / raw)
  To: caml-list

Hello

I have some code that behaves like the following.

module M = struct
   type ('a, 'b) ops = {
     bar_of_foo : 'a -> 'b;
     foo_of_bar : 'b -> 'a
   }

   let foo ops x = ops.bar_of_foo x
   let bar ops x = ops.foo_of_bar x
end

type foo = A | B
type bar = C | D

let ops = {
   M.bar_of_foo = (function A -> C | B -> D);
   M.foo_of_bar = (function C -> A | D -> B)
}

let () =
   match M.foo ops A with
   | C -> ()
   | D -> ()

The idea is to parametrize the behavior of some module M on two types 
and a set of operations involving these two types, with the catch that I 
want to keep the ability to pattern-match on the possible values of 
these types in the code that handles the return values from the 
functions exported by M.

With the scheme above I can do this, but passing a table of functions 
around doesn't look very nice. Is there any way I can use functors to do 
what this code does, while still being able to use pattern-matching? 
Would I have to resort to objects to be able to do this?

Thanks in advance,
Andre

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2012-02-14 11:05 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-02-13 23:33 [Caml-list] What am I reinventing here? Andre Nathan
2012-02-14  7:03 ` Arnaud Spiwack
2012-02-14  9:59   ` Andre Nathan
2012-02-14 10:09     ` Gabriel Scherer
2012-02-14 10:24       ` Andre Nathan
2012-02-14 10:39         ` Gabriel Scherer
2012-02-14 11:05           ` Andre Nathan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).