Introspection: Difference between revisions

Content deleted Content added
Added error at compile time and exit at runtime.
Petelomax (talk | contribs)
→‎{{header|Phix}}: requires() added
Line 1,548: Line 1,548:


=={{header|Phix}}==
=={{header|Phix}}==
The requires procedure behaves like a compiler directive, although it is in fact just a normal executable routine.
Phix has a version() routine which returns a string such as "0.8.0":
<lang Phix>requires("0.8.2") -- crashes on 0.8.1 and earlier
<lang Phix>?version()
requires(WINDOWS) -- crashes on Linux
?scanf(version(),"%d.%d.%d")[1]</lang>
requires(64) -- crashes on 32-bit</lang>
{{out}}
If passed a string it compares it (intelligently) against the interpreter/compiler version and terminates in error with a suitable message should it be too old to cope. Otherwise the parameter must be an integer: <32 checks the platform, >=32 checks the word size. In the latter case when (interpreting and) it needs to, it hunts for a suitable alternative runtime and offers to re-run with that, maybe you need bigger ints, or maybe you only ship a 32-bit libcurl.dll.
<pre>

"0.8.0"
The version() routine used by the above can also be called directly and returns a string:
{0,8,0}
<lang Phix>?version() -- eg "0.8.0"</lang>
</pre>
Normally only really useful for display, but you can of course break that down into an integer triplet with scanf(), as requires does, maybe something works on 0.7.7 and 0.7.9 but not 0.7.8.
The scanf() result is probably easier to test against.


Phix has a builtin abs() routine, which will be auto-included if referenced.
Phix has a builtin abs() routine, which will be auto-included if referenced.
<lang Phix>include pmaths.e -- (needed pre-0.8.1 to work around a compiler bug [oops])
<lang Phix>--include pmaths.e -- (an auto-include, ok but not needed)
--include complex.e -- (not an auto-include, needed in all versions)
--include complex.e -- (not an auto-include, needed if used)
integer r_abs = routine_id("abs")
integer r_abs = routine_id("abs")
--integer r_abs = routine_id("complex_abs")
--integer r_abs = routine_id("complex_abs")
Line 1,567: Line 1,567:
end if</lang>
end if</lang>
Using complex_abs() is probably closer to the task requirement in that if complex.e is not included it will not be found/called.<br>
Using complex_abs() is probably closer to the task requirement in that if complex.e is not included it will not be found/called.<br>
In this case it happens to give exactly the same result, however under the hood it is actually returning sqrt((-42)*(-42)+(0)*(0)).
In this case it happens to give exactly the same result, however under the hood it is first promoting the -42 to -42+0i before returning sqrt((-42)*(-42)+(0)*(0)).


There is (as yet) no var_id() builtin, the following is a quick cobbling-together of code from builtins\VM\prtnidN.e (routine_id)
There is (as yet) no var_id() builtin, the following is a quick cobbling-together of code from builtins\VM\prtnidN.e (routine_id)