[Mono-list] Mixed programming languages

Jonathan Pryor jonpryor at vt.edu
Wed Feb 28 06:03:13 EST 2007


On Wed, 2007-02-28 at 09:03 +0000, tom potts wrote:
> I've always felt slightly robbed when its said that a
> project has to be all in one language in .NET of all
> environments having mixed and matched languages in
> projects before - when we had .o in the build path.

It's not necessarily true that a project needs to be within a single
language.  That's an IDE limitation, not necessarily a compiler
limitation.

If all of your compilers support -target:module (which creates
a .netmodule file), you could compile different parts of the assembly in
different languages, then create the final assembly by "merging" the
different .netmodule files by providing -addmodule:filename.

(This of course requires that the compiler support -target:module, and
you can't mix languages within a single class, but this does allow
intermixing languages within an assembly.)

This mixing can only be done a class at a time, so you can't write part
of a class in C# and another part in VB.NET, but you can use inheritance
for similar functionality, i.e. write the base type in C# and the
derived type in VB.NET.

If you wanted to get _really_ tricky, you could mark to-be-merged types
with an attribute -- [WriteType("MergedType")] class CSharpMergedType
{...}, and a VB.NET VbMergedType -- and then write a Cecil program which
will take the two separate assemblies/.netmodule files, look for the
[WriteType] attribute, and merge all types with the specified attribute
into a new type.

(Why you'd want to work that hard is another matter, but it _could_ be
done...)

> Would it not be possible (with partial classes?) to
> actually have a project or a library built using
> different languages. 

No.  Partial classes are a compiler feature, not a CIL  feature.  The
compiler merges all "partial" classes into a single CIL type.

> Granted it might impose further requirements in the
> code but to have a VB.NET programmer extend something
> written in C# without having to convert the code could
> be a massive plus for mono.

This can already be done, though if you use an IDE this is done in
separate assemblies -- a VB.NET programmer writing assembly A.dll can
extend (inherit from) a C# class in assembly B.dll.

(Which is how ASP.NET works -- most of the class library is C#, while
ASP.NET can use VB.NET, Chrome, C#, Nemerle... any language with a
CodeDom provider.)

 - Jon




More information about the Mono-list mailing list