Introspection: Difference between revisions

Content added Content deleted
(Added C++)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 88: Line 88:
There are 3 integer variables in the global scope
There are 3 integer variables in the global scope
Their sum is 1936
Their sum is 1936




=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Line 227: Line 225:
if IsFunc("abs")
if IsFunc("abs")
MsgBox % abs(bloop)
MsgBox % abs(bloop)
return</lang>
return</lang>


=={{header|AWK}}==
=={{header|AWK}}==
Line 296: Line 294:
doing this kind of check in a shell script and defining symbols
doing this kind of check in a shell script and defining symbols
such as HAVE_ABS which ''can'' be checked by the preprocessor.
such as HAVE_ABS which ''can'' be checked by the preprocessor.
=={{header|C++}}==
Identifying the version of the C++ standard used by the compiler in C++ is syntactically very similar to the way of checking the C standard version (also seen on this page).


<lang cpp>#if !defined(__cplusplus) || __cplusplus < 201103L
#pragma error("The following code requires at least C++11.")
#else
// ...
#endif</lang>

As in C, the introspective capabilities of C++ are very limited.
=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
There has to be some caveats made with C#. There are no truly "global" variables - just publicly exported ones from individual classes/types. I chose to make a couple of public static variables in my program's class. Also, the "version" of the compiler is difficult to impossible to get at. There are no predefined compiler constants that can be compared against as in C/C++ but then again, it's hardly the thing that counts in C#. What really counts is the version of .NET and the framework you're working with since that determines what C++ features you can use and the various calls that can be made. Consequently, I check the .NET version to make sure it's past 4.0 and exit if not.
There has to be some caveats made with C#. There are no truly "global" variables - just publicly exported ones from individual classes/types. I chose to make a couple of public static variables in my program's class. Also, the "version" of the compiler is difficult to impossible to get at. There are no predefined compiler constants that can be compared against as in C/C++ but then again, it's hardly the thing that counts in C#. What really counts is the version of .NET and the framework you're working with since that determines what C++ features you can use and the various calls that can be made. Consequently, I check the .NET version to make sure it's past 4.0 and exit if not.
Line 394: Line 383:
<pre>bloop's abs value = 10
<pre>bloop's abs value = 10
2 exported ints which total to -30</pre>
2 exported ints which total to -30</pre>

=={{header|C++}}==
Identifying the version of the C++ standard used by the compiler in C++ is syntactically very similar to the way of checking the C standard version (also seen on this page).

<lang cpp>#if !defined(__cplusplus) || __cplusplus < 201103L
#pragma error("The following code requires at least C++11.")
#else
// ...
#endif</lang>

As in C, the introspective capabilities of C++ are very limited.


=={{header|Clojure}}==
=={{header|Clojure}}==
Line 410: Line 410:
(throw (Error. "Bad version"))))
(throw (Error. "Bad version"))))
</lang>
</lang>



=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Line 1,543: Line 1,542:
4 integers, sum = 74717
4 integers, sum = 74717
</pre>
</pre>

=={{header|Perl 6}}==
<lang perl6>use v6; # require Perl 6

my $bloop = -123;

if MY::{'$bloop'}.defined and CORE::{'&abs'}.defined { say abs $bloop }

my @ints = ($_ when Int for PROCESS::.values);
say "Number of PROCESS vars of type Int: ", +@ints;
say "PROCESS vars of type Int add up to ", [+] @ints;</lang>
{{out}}
<pre>123
Number of PROCESS vars of type Int: 1
PROCESS vars of type Int add up to 28785</pre>
Obviously Perl 6 doesn't maintain a lot of global integer variables... <tt>:-)</tt>

Nevertheless, you can use similar code to access all the variables in any package you like,
such as the GLOBAL package, which typically has absolutely nothing in it. Since the PROCESS package is even more global than GLOBAL, we used that instead. Go figure...


=={{header|Phix}}==
=={{header|Phix}}==
Line 1,939: Line 1,919:
(unless (version<=? "5.3" (version)) (error "ancient version"))
(unless (version<=? "5.3" (version)) (error "ancient version"))
</lang>
</lang>

=={{header|Raku}}==
(formerly Perl 6)
<lang perl6>use v6; # require Perl 6

my $bloop = -123;

if MY::{'$bloop'}.defined and CORE::{'&abs'}.defined { say abs $bloop }

my @ints = ($_ when Int for PROCESS::.values);
say "Number of PROCESS vars of type Int: ", +@ints;
say "PROCESS vars of type Int add up to ", [+] @ints;</lang>
{{out}}
<pre>123
Number of PROCESS vars of type Int: 1
PROCESS vars of type Int add up to 28785</pre>
Obviously Perl 6 doesn't maintain a lot of global integer variables... <tt>:-)</tt>

Nevertheless, you can use similar code to access all the variables in any package you like,
such as the GLOBAL package, which typically has absolutely nothing in it. Since the PROCESS package is even more global than GLOBAL, we used that instead. Go figure...


=={{header|Raven}}==
=={{header|Raven}}==
Line 1,946: Line 1,946:
'bloop' GLOBAL keys in && 'abs' CORE keys in
'bloop' GLOBAL keys in && 'abs' CORE keys in
if bloop abs print</lang>
if bloop abs print</lang>



=={{header|Retro}}==
=={{header|Retro}}==
Line 2,107: Line 2,106:
if (bloop.isDefined) bloop.get.abs
if (bloop.isDefined) bloop.get.abs
}</lang>
}</lang>

=={{header|Slate}}==
=={{header|Slate}}==
No version string included inside the system presently.
No version string included inside the system presently.