modern Tcl and correct quoting

Gustaf Neumann neumann at wu.ac.at
Wed Jun 12 14:46:26 PDT 2013


Am 12.06.13 18:39, schrieb Joshua Root:
> On 2013-6-13 01:24 , Poor Yorick wrote:
>> On Wed, Jun 12, 2013 at 10:58:50PM +1000, Joshua Root wrote:
>>> On 2013-6-12 22:00 , Rainer Müller wrote:
>>>> On 2013-06-12 08:05, Poor Yorick wrote:
>>>>> 	use "eq" instead of "==" where appropriate
>>>> Fine as well.
>>> What is the benefit of this?
Here are a few examples, where the numeric "==" semantics differ from "eq".
Guess what the following statement do:

if { 0x01 == 1.00} {puts yes} {puts no}
if { 014 == 0xc} {puts yes} {puts no}
if {"nan" == "nan"} {puts yes} {puts no}

With "eq" all complexity is gone. Putting the operands in
quotes does not change anything.

Note, that "==" can lead to subtle problems/asymmetries,
when the variables are passed in:

set x nan
if {$x == $x}  {puts yes} {puts no}
set x man
if {$x == $x}  {puts yes} {puts no}

set n "07"
if {$n == 7} {puts yes} {puts no}
set n "08"
if {$n == 8} {puts yes} {puts no}

In a related but different area, the expression "?:" can be dangerous:

set x nan
expr {1 ? $x : 0}

These are a good reason to stay away from numeric expressions
unless necessary.

However, in all cases, the semantics are the same for Tcl 8.4, 8.5, and 8.6

-gustaf neumann



More information about the macports-dev mailing list