Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Best way to dispatch on two arguments
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 1999-01-24 (14:43)
From: Juan Jose Garcia Ripoll <jjgarcia@i...>
Subject: Best way to dispatch on two arguments

I'm trying to code a library of mathematical operations among matrices,
vectors, polynoms, etc. I know OCaml does not have generic methods so
now the question is what is the best way to dispatch on two arguments?
I've thought of two possible implementations

1) Creating a single type of the form
	type = Matrix of ([int],float array) | Real of float |...
and doing a large sequence of pattern matching.

2) Creating classes and defining one method for each binary operation.

Now my questions are

3) When using (1) I would like to hide implementation issues without
adding additional indirections. Thus, is it possible in OCaml to sum
abstract types such as in
	type = matrix | real |...
where Matrix, Real, etc, would be abstract types defined in separate

4) Is (2) really possible? OCaml's methods are not polymorphic, so how
should I code that binary operation method? What type should it be so
that it admits any kind of mathemtical entity I define?