<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE TIP SYSTEM "http://tcl.activestate.com/cgi-bin/tct/tip/tipxml.dtd">
<!-- Converted at Thu Feb 09 11:22:49 GMT 2012 -->
<!-- TIP AutoGenerator - written by Donal K. Fellows -->

<TIP number='15'>
<header><title>Functions to List and Detail Math Functions</title><author address="mailto:fellowsd@cs.man.ac.uk">Donal K. Fellows</author><status type='project' state='final' tclversion="8.4.0" vote='after'>$Revision: 1.8 $</status><history></history><created day='22' month='nov' year='2000' /><keyword>Tcl expr function introspection</keyword></header>
<abstract>Provides a way for the list of all math functions defined in the current interpreter to be discovered, and for discovering what arguments might be passed to an existing math function. This may be useful in tests as well as more general use.</abstract>
<body><section title="Rationale">
<para>Although it is quite easy to define a new function for use in expressions, there is no public way of performing introspection on this information. Having a way to extract the arguments from an existing math function was requested by <url ref="http://sourceforge.net/bugs/?func=detailbug&amp;bug_id=119304&amp;group_id=10894"/> and once you have one, it becomes trivial to also ask for a second function to list what functions are defined.</para>
<para>I propose the creation of two functions that fulfil this rôle; <emph style="italic">Tcl_GetMathFuncInfo</emph> and <emph style="italic">Tcl_ListMathFuncs</emph>. These functions will be documented on the same manual page as <emph style="italic">Tcl_CreateMathFunc</emph> and implemented in the same file.</para>
<para>Furthermore, I also propose that the <emph style="italic">info</emph> command in the Tcl interpreter be extended to include a new subcommand, <emph style="italic">functions</emph>, which will allow Tcl scripts to discover the list of installed functions (by acting as a thin veneer over <emph style="italic">Tcl_ListMathFuncs</emph>.) Note that this is an extension of the <emph style="italic">info</emph> command because it allows for introspection of a system that affects the behaviour of several commands that form the core part of the command-set: <emph style="italic">expr</emph>, <emph style="italic">for</emph>, <emph style="italic">if</emph> and <emph style="italic">while</emph>.</para>
</section>
<section title="Tcl_GetMathFuncInfo">
<para>This function will take an interpreter reference, a function name (as a string) and pointers to variables capable of taking each of the last four arguments to <emph style="italic">Tcl_CreateMathFunc</emph>, and will return a standard Tcl result (either <emph style="italic">TCL_OK</emph> or <emph style="italic">TCL_ERROR</emph>, depending on whether a function with the given name exists within the given interpreter, with an error message being left in the interpreter&apos;s result in the <emph style="italic">TCL_ERROR</emph> case.) The array of argument types whose reference is placed into the variable pointed to by <emph style="italic">argTypesPtr</emph> will be allocated by Tcl, and should be freed with <emph style="italic">Tcl_Free</emph>.</para>
<verbatim><vline encoding='base64'>aW50IFRjbF9HZXRNYXRoRnVuY0luZm8oVGNsX0ludGVycCAqaW50ZXJwLCBDT05TVCBjaGFyICpuYW1lLA==</vline><vline encoding='base64'>ICAgICAgICAgICAgICAgICAgICAgICAgaW50ICpudW1BcmdzUHRyLCBUY2xfVmFsdWVUeXBlICoqYXJnVHlwZXNQdHIs</vline><vline encoding='base64'>ICAgICAgICAgICAgICAgICAgICAgICAgVGNsX01hdGhQcm9jICoqcHJvY1B0ciw=</vline><vline encoding='base64'>ICAgICAgICAgICAgICAgICAgICAgICAgQ2xpZW50RGF0YSAqY2xpZW50RGF0YVB0cik7</vline></verbatim>
<para>The parameter names are chosen by analogy with <emph style="italic">Tcl_CreateMathFunc</emph>.</para>
<para>In the case where a math function is defined internally by the bytecode engine and has no standard implementation (all the builtin functions in 8.4a2 are like this) the value placed in the variable indicated by the <emph style="italic">procPtr</emph> argument will be NULL.</para>
</section>
<section title="Tcl_ListMathFuncs">
<para>This function will take an interpreter reference and an optional string that describes a glob-like pattern that restricts the set of math functions that the caller is interested in receiving (with a <emph style="italic">NULL</emph> indicating that no filtering is desired.) The function will return a pointer to a newly-allocated <emph style="italic">Tcl_Obj</emph> list of the names of all the math functions defined within that interpreter, or <emph style="italic">NULL</emph> in the case of an error (in which case a suitable message will be left in the interpreter.) The list will not be required to be sorted.</para>
<verbatim><vline encoding='base64'>VGNsX09iaiAqVGNsX0xpc3RNYXRoRnVuY3MoVGNsX0ludGVycCAqaW50ZXJwLCBDT05TVCBjaGFyICpwYXR0ZXJuKTs=</vline></verbatim>
<para>The alternative is to pass in the addresses of variables that will be updated to contain the number of functions and an array of function names. But I prefer the <emph style="italic">Tcl_Obj</emph> approach as it is expressing the fact that the list of function names is really a single thing being returned (albeit one that is not a simple value.) It is not anticipated that the performance of this function will need to be crucial to too many applications.</para>
</section>
<section title="info functions">
<para>This new subcommand will provide access from Tcl scripts to the functionality of <emph style="italic">Tcl_ListMathFuncs</emph>. It will take a single optional argument consisting of a pattern to pass on as the <emph style="italic">pattern</emph> argument (with the absence of the argument indicating that NULL is to be passed instead.)</para>
</section>
<section title="Copyright">
<para>This document is placed in the public domain.</para>
</section>
</body></TIP>

