<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE TIP SYSTEM "http://www.tcl.tk/cgi-bin/tct/tip/tipxml.dtd">
<!-- Converted at Tue May 21 10:35:20 GMT 2013 -->
<!-- TIP AutoGenerator - written by Donal K. Fellows -->

<TIP number='266'>
<header><title>Numbers are Commands</title><author address="mailto:setok@fishpool.com">Kristoffer Lawson</author><author address="mailto:ethouris@gmail.com">Michal Malecki</author><author address="mailto:wolf-dieter.busch@gmx.de">Wolf-Dieter Busch</author><author address="mailto:webscool@ihug.co.nz">Paul Nash</author><status type='project' state='rejected' tclversion="8.5" vote='after'>$Revision: 1.8 $</status><history></history><created day='11' month='apr' year='2006' /><keyword>Tcl unknown expression</keyword></header>
<abstract>This TIP describes a change to Tcl&apos;s command dispatch which would allow every number to act as a command.</abstract>
<body><section title="Rationale">
<para>Maths in Tcl are continuously a stumbling block. The <emph style="bold">expr</emph> command is ugly and cumbersome. It adds a new C-like syntax within Tcl and easily makes lines less readable, especially with the most common use of simple arithmetic. In addition it is confusing to the programmer to have to remember there is a completely separate set of commands, or functions, which are only available to the <emph style="bold">expr</emph> command.</para>
<para>This proposal offers a clean solution which could eventually totally replace <emph style="bold">expr</emph>, yet offers few or no backward compatibility problems and fits in with the Tcl philosophy of &quot;every piece of executable code is a command&quot;.</para>
<para>This addition is easy to build as a separate extension using the mechanisms provided by Tcl. However, this will not be widely used unless it is stamped as part of the language and part of its unique philosophy. In addition, implementations in the core can gain from many forms of optimisation that would not be possible with a normal extension.</para>
<para>There is an existing proposal for making operands into commands. That proposal is still a valid one, but the description in this TIP leads to syntax which will be more familiar to most programmers. It does not have operator precedence, which is a difference from normal mathematics, but one that should be easier to grasp than the reversed notation that operator commands offer. In fact, there are many claims which state operator precedence is a mistake anyway.</para>
</section>
<section title="Specification">
<para>Every number that can be handled by Tcl is a command.</para>
<para>The format for number commands is:</para>
<quote><emph style="italic">number</emph> <emph style="italic">operation</emph> ?<emph style="italic">arg1 arg2 ...</emph>?</quote>
<para>For example:</para>
<verbatim><vline encoding='base64'>IDggKiA1ICsgMg==</vline></verbatim>
<para>The first argument must be an operator, and can be one of any specified for the <emph style="bold">expr</emph> command (+, -, *, /, ...)</para>
<para>Subsequent arguments can be either numbers or operators.</para>
<para>The command evaluates its arguments from left-to-right (no precedence) with the result of the left used as the operand for the next operation. <emph style="italic">e.g.</emph> to get 242 a simple addition can be made:</para>
<verbatim><vline encoding='base64'>IDIwMCArIDQy</vline></verbatim>
<para>If multiple numbers are given without operators between them, the last operator is used. <emph style="italic">e.g.</emph> for the result &quot;42&quot; the following can be used:</para>
<verbatim><vline encoding='base64'>IDIwICsgMTAgMTAgMg==</vline></verbatim>
<para>To get &quot;23&quot;, the following can be used:</para>
<verbatim><vline encoding='base64'>IDIwICsgMTAgMTAgMiAvIDIgKyAy</vline></verbatim>
<para>Normal Tcl brackets can be used for grouping purposes. <emph style="italic">e.g.</emph> the result &quot;31337&quot; can be reached with the following:</para>
<verbatim><vline encoding='base64'>IDYyNjc0IC8gWzEgKyAxXQ==</vline></verbatim>
<para>Number commands act just like any other commands in Tcl. They can be nested with brackets, aliased, replaced and even removed. The only difference is that they do not appear in the list returned by <emph style="bold">info commands</emph>. However, any replacements made on them do.</para>
<subsection title="Compatibility">
<para>Because of the ability to replace number-commands, there are no real backwards compatibility issues. There are very few, if any, extensions that make numbers into commands and any that do will still work with the only exception being software that would mix extensions that would depend on the functionality in this TIP and extensions that create their own commands which are only numbers. In any case, the author is not aware of any extensions that create commands in that manner.</para>
<para>The only other area where incompatibilities would arise would be if someone is using an <emph style="bold">unknown</emph> mechanism which creates similar functionality to the one presented here. These extensions do exist, but are mostly presented as brain-twisters instead of for serious use.</para>
<para>Additionally, Tcl does not and cannot guarantee that new commands will not appear in the global namespace. Indeed, every new release does this.</para>
<para>For these reasons, this proposal can be implemented with a minor version bump.</para>
</subsection>
</section>
<section title="Implementation Discussion">
<para>The first natural step is to add a detection phase before the normal unknown mechanism. In fact, a prototype implementation can easily and trivially be built with Tcl&apos;s <emph style="bold">unknown</emph> command. The detection should work by checking if the unknown command is a number and if so, it will run the number command functionality. Otherwise, the normal Tcl chain of events is followed.</para>
<para>If the command is replaced, this number mechanism is never reached, as a normal command exists to handle the situation.</para>
<para>If the command is removed, a special flag should be set to specify that the number in question cannot be used as a command.</para>
<para>At a later date, further integration with the bytecode engine can be made. The details of such optimizations are outside the scope of this TIP.</para>
<para>Some may argue that it is not good to disable defining commands with numbers by users. For example, someone would like to &quot;overload&quot; 0 and 1 to use [!file exists $f] &amp;&amp; { puts &quot;File not found&quot;; exit }. This also does not look like a good solution for &apos;expr&apos; command; this will be still not the same as expressions in other languages (required spaces between operators etc.)</para>
</section>
<section title="Alternative Approaches">
<para>Instead of allowing multiple number arguments to use the same operator, a requirement could be made that every other argument must be an operator and every other one an operand. This might make the specification somewhat clearer, but would mean a mathematical operation on a list would not be as easy.</para>
</section>
<section title="Comments">
<para>This is a change to the &quot;eleven rules&quot;, so please do not do it. It takes the possibility that I can do it myself by changing the procedure unknown.</para>
</section>
<section title="Copyright">
<para>This document has been placed in the public domain.</para>
</section>
</body></TIP>
