Logo

Programming-Idioms

New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating resource.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
import std.stdio;
static if (is(typeof(x = x.init)))
    writeln(x);
int x = 42;

void printIfDefined(alias name)()
{
    import std.stdio: writeln;
    static if( __traits(compiles, writeln(mixin(name))))
        writeln(mixin(name));
}

void main(string[] args)
{
    printIfDefined!"x";
    printIfDefined!"Foo.bar";
}
implicit none

print *,x
if (typeof x !== 'undefined') {
    console.log(x);
}
try {
	console.log(x);
} catch (e) {
	if (!e instanceof ReferenceError) {
		throw e;
	}
}
if x then print(x) end
$x = 'foo';

if (isset($x)) {
    echo $x;
}
{$if DECLARED(x)}
writeln(x);
{$endif}
use 5.012;
print $x if $x; # won't compile, see explanation
if 'x' in locals():
	print x
puts x if defined?(x)