Dan Doel wrote:

> MikkelFJ wrote:
> 
>>You cannot avoid C when intergrating languages unless you go to a

> This always confuses me a bit.  Is the reason for needing to use C that
> both OCaml and Ruby are
> written in C?

Not at all. The reason is that any other language (almost) that somehow link
with object modules do so in a restricted way. That is, Ruby requires
special datatypes when calling C. So does OCaml. But Ruby and OCaml
datatypes are different. Only C is flexible enough to mediate between the
two datatypes. You could use some other language than C but there are very
few offering the required features and headers for interfacing C to Ruby
and OCaml are provided in C.

>  For example, I can understand that you can't interface
> Java and Ruby without either
> writing Ruby in something that compiles to JVM bytecode, or using JNI to
> link to native code.

Right - but you can move up on the abstraction layer. Instead of using
object module linkage (dll, or static), you can use marshalling. This is
where SOAP, XML-RPC, DCOM, Corba etc. comes into place. So it really
depends on what you mean by interfacing.

You can frequently call DLL's from other languages. For instance C# and
Visual Basic 6.0 allow you to call DLL's and I think someone recently made
this possible in Ruby. This is actually some sort of lightweight
marshalling as there is a thin interpreter layer translating datatypes
before calling into the DLL. Exposing things as DLL's is a bit more tricky
I think, but this has also been done for OCaml more or less automatically.

There are also two variants of marshalling: those the generate a lot of
bulky code and those that has a lot of runtime interpration - and some
lands in between.
 
> But OCaml compiles to machine code, doesn't it?  Would it only be
> possible to interface with
> OCaml directly if you wrote Ruby in OCaml (I assume that's the main
> problem)?

No because there are langauge specific macroes and functions that are needed
to interpret input and return arguments. These are not compatible. To some
extend the Ruby and OCaml call interfaces are very similar and you could
almost write C libraries that could be called from both languages - but not
quite. Datatypes dealing with gargage collection complicates matters
greatly.

It is good to pivot around C or a marshalling layer - otherwise each
language would specifically have to interface all other languages. This
quickly becomes very complex.

> I guess that's a good reason for keeping Ruby written in C (that is, if
> we somehow got a
> compiler from Ruby to machine code, and were able to write Ruby in
> Ruby), because if all
> languages are implemented in C, or can interface with C easily, they can
> all be linked together.

You could write Ruby in another language and still expose its API in a DLL
much as it is now.
Indeed I have many times though of implementing Ruby or a subset of Ruby in
OCaml - it is the perfect language for the job.
To create the DLL with API you would link OCaml with some C code to create
the proper interface functions.

Mikkel