0

Polyglot Programs strikes me as pretty confusing and error prone. Right away I don't see any use to it besides "showing-off". Wouldn't that be a bad programming pattern, since it's not modular?

Is there something that can be implemented with it that's cleaner than designing software applications as suites of independently deployable modules/libraries/services?

T. Sar
  • 2,166
Pierre B
  • 205

2 Answers2

4

There are a couple of rare edge cases where polyglot programming comes in handy. With polyglot programming, the source code can be understood by multiple languages. Usually, this is done for some kind of backwards-compatibility.

For example, we can create a Perl script that can also be invoked as a shell script, which has some uses if the system doesn't support the shebang line properly:

#!/bin/sh
#! -*-perl-*-
eval 'exec perl -x -wS $0 ${1+"$@"}'
    if 0;

(from perldoc perlrun)

It is also somewhat common for C++ libraries to express the externally-visible interface in a C-compatible manner. This allows the library to be used more easily. While C and C++ have similar syntax (and C++ was based on C), they differ in a couple of subtle points that can make it difficult to use C++ code from C.

Within C and C++, there's also a lot of clever use of the preprocessor to use compiler-specific features portably. If we argue that GCC's C is a different language from Visual Studio's C, a program that compiles under both could be considered to be polyglot.

Other than that, polyglot programming in production is pointless. You know what compiler or interpreter you'll be using, so you only have to target that.

amon
  • 135,795
-1

Polyglot Programming is a very useful skill, even essential in nowadays computing environment. It is pretty much impossible to develop any non-toy software system in just one language.

For example, in a typical web application, you might use HTML, CSS, ECMAScript, TypeScript, Liquid, SQL, Java, sh, and Ant.

Polyglot Programming is about recursively breaking down a large problem into atomic sub-problems, and choosing the best language to solve each subproblem. The next step on the ladder would then be Language-Oriented Programming, which is about recursively breaking down a large problem into atomic sub-problems, and designing (and implementing) the ideal language to solve each subproblem.

Jörg W Mittag
  • 104,619