Polyglots are programs that are valid in multiple programming languages simultaneously. Most such polyglots are written in such a way that certain constructs of one language are interpreted as comments of another language (e.g.
#define in C being interpreted as a comment in several scripting languages).
I am curious to see if it is possible to make a non-trivial polyglot which contains no comments, but also immediately changes when you remove any non-whitespace character, I therefore challenge you to come up with such a program.
The concrete rules are as follows:
- (Output). Your program must produce some output on the console under each of your languages. That is, your program is not permitted to simply exit without printing anything.
- (Variance). As a relaxation of the standard polyglot definition, the program's output may vary between languages.
- (Errors). Your program must not produce any errors (broadly defined) under any of your languages. For most languages, this is defined as returning a non-zero exit code from the compiler and/or interpreter.
- (Restriction). The removal of any single non-whitespace character from your code should cause your program to change its behaviour under every one of your languages. The program may "change" by becoming invalid for that language, or by changing the output that is produced.
- This is a code challenge. Winner is the program which is valid in the most programming languages. Ties will be broken in favor of shorter program length.
The restriction rule doesn't apply to the removal of several characters. That is, it is fine if removing several characters simultaneously results in no change for one of your languages.
Observe that the restriction rule implies that you cannot use Whitespace as one of your languages, as removing any non-whitespace character won't change the behaviour of the Whitespace program.
Here's a simple example of a program that fulfills all the above restrictions, for the languages Python 2 and Python 3:
Removing any character in
NameError; removing any bracket or quote will throw a
SyntaxError, and removing any of the string characters will change the output in both languages. (Note that
print("hello", "world") is a more subtle, but still valid program under the above rules).
This example is a bit lame because Python 2 and Python 3 are very similar, so I won't accept any other solutions that only use different versions of the same language (especially Python 2 and Python 3).
print("Hello World!")is also valid Ruby code \$\endgroup\$