Follow

Hey why the heck are programming languages so cleanly separated from one another

Why don't we have a standardized protocol that lets languages call libraries written in other languages

I'm sure this is a hard problem but purely from intuition it seems like the kind of problem that should be solved by now

@socks are you talking about libraries like dlls or so files, or like included code? because i know you can call dlls from whatever language you want, since they're already all compiled code
-F

@Hearth I guess DLLs count? To some extent? Pretty limited though

@socks how are they limited? it's just a difference of whether the library in question is compiled or not--dlls and sos are compiled
-F

@Hearth I dunno, can you call them from, say, Java? It runs in its own VM so I wouldn't assume so

@socks I don't know but i expect you can, the jvm probably has support for that

this is all a fair bit outside our area of expertise though
-F

calling into different programming languages 

@socks we do have protocols like that, they're called remote procedure call (rpc) protocols. they make it possible to call functions in other processes, possibly running on other machines, and get return values, the same as any other function. those processes can be written in any programming language.

usually you don't want to start another process just for language interop, though, and getting languages to coexist in a process is complicated

calling into different programming languages 

@socks different languages expect different things of their environments in order to achieve certain properties they want (usually high efficiency). for instance, java code expects to run in a vm with a compacting generational garbage collector, which conflicts with c code's expectation that its environment won't rewrite pointers under its nose. rust and c both run on bare metal, but rust expects unwind information that c can't provide!

calling into different programming languages 

@socks people find ways to reconcile these incompatibilities, but it's not possible to make a standardized solution because each environment is bespoke. people seem to have decided this is worth it.

there are two main things i can think of that people do to make it easier, though: running many programming languages in the same vm, and letting every programming language switch to the c abi for interop.

calling into different programming languages 

@socks the main VMs that are famous for running many languages are the clr (the dotnet vm) and the jvm. the jvm in particular has many languages designed for it, like java, kotlin, clojure, and scala, plus languages ported to it, like python and ruby. this works as long as the vm can provide a good enough level of efficiency in all the languages you need, but some languages are just too different from java and c#, like c and erlang.

calling into different programming languages 

@socks the c abi is essentially a standardized protocol for passing certain types of data and making function calls between languages outside of a vm. it's only capable of expressing what can be expressed in c, which makes things complicated when things like garbage collection, exceptions, dynamic typing, and lazy evaluation get involved, but it provides a starting point for interop between any two languages that support it and is often good enough.

calling into different programming languages 

@socks that's my explanation after spending a fair amount of time thinking about this problem (one of my projects is a vm designed to run many programming languages). and of course i've left out anything to do with higher-level problems like type parameters and other things that are normally evaluated by the compiler, which would be a big topic unto itself!

calling into different programming languages 

@autumnontape That makes sense! Thank you for the explanation!

Sign in to participate in the conversation
social.emisocks.com

Emil Socks' personal instance!