Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCaml equivalent types

Tags:

types

ocaml

I am building two libraries in OCaml which contain the same variant type. The detail of the variant is not too important, other than that it is really large, and it would be annoying to manually write conversion functions for it. (It's actually the bfd_architecture enumeration converted from C using ocamlidl).

I'm now writing a program using the two libraries. In particular, I'm calling A.f which returns a value of A.variant_type, and I need to use that value in a call to B.g, which takes a value of B.variant_type as an input.

Is there any way to tell OCaml that A.variant_type and B.variant_type are really the same type, and thus it's okay to convert a value from one to the other? The libraries are independent, so they should not reference each other. Right now I'm using Obj.magic to do the conversion, but this is a hack.

like image 448
Ed McMan Avatar asked Feb 15 '12 14:02

Ed McMan


People also ask

What is == in OCaml?

OCaml distinguishes between structural equality and physical equality (essentially equality of the address of an object). = is structural equality and == is physical equality.

What does |> do in OCaml?

The |> operator represents reverse function application. It sounds complicated but it just means you can put the function (and maybe a few extra parameters) after the value you want to apply it to.


1 Answers

I believe there is no clean way unless these libraries have some common dependency which defines this type (i.e. the same module referenced from both libraries at build time).

like image 127
ygrek Avatar answered Oct 02 '22 03:10

ygrek