Menu

#192 Null expressions

obsolete: 8.0p2
closed-invalid
nobody
2
2001-04-13
2000-10-26
Anonymous
No

OriginalBugID: 639 Bug
Version: 8.0
SubmitDate: '1998-05-25'
LastModified: '1999-08-14'
Severity: MED
Status: Closed
Submitter: hershey
ChangedBy: hobbs
OS: All
OSVersion: NA
Machine: NA
FixedDate: '2000-10-25'
FixedInVersion: NA
ClosedDate: '1999-08-14'

Consider the following commands in TCL 8.0p2:

% expr 8 + ""
syntax error in expression "8 + "
% expr "" + 8
8
%
% expr "" > 10
syntax error in expression " > 10"
% if { "" > 10 } { puts true } else { puts false }
false

Granted, the above code is somewhat strange, but realize that the ""
could be the return result of any other command such as:

if { [ myCustomCmd arg1 arg2 ] > 10 } { puts true }

In general, the fact that TCL does not handle NULL values in its
expression evaluation causes tremendous headaches.

TCL handles NULL STRINGS such as:

string index "" 9

But returns errors whenever a NULL value is encountered in an
expression.

Many languages will simply return NULL if an expression
contains any type of NULL value in it. It would be very nice if
TCL would do the same.

I have created a GUI builder that uses TCL as its scripting
language. Each field on my screen can have TCL methods associated
with it. We have extended TCL to provide a "getfld" and a
"setfld" command that retrieves/sets the value contained in the field
on the screen. We would like to simply write code as appears below:

setfld Field1 [ expr [ getfld Field2 ] + [ getfld Field3 ] ]

Unfortunately, we had to create another command called "checkfld"
that first makes sure the field is not NULL before attempting to
evaluate the expression:

if { [checkfld Field2 Field3] } {
setfld Field1 [ expr [ getfld Field2 ] + [ getfld Field3 ] ]
}
else {
setfld Field1 ""
}

This might not seem too bad but its alot of work to simply add
2 numbers together and it means that every single time we
refer to a field on our GUI we need to first make sure its not
NULL.

We could modify our code to not allow NULL values on our screen
and force all numeric fields to zero but this would not make a
very friendly GUI - you need to allow blank numeric fields on
any GUI.

We have also noticed that the expr "" + 8 command behaves differently
between TCL 7.3 and TCL 8.0.

So my question is:

Why not change the expr command to return a NULL value when any
argument in its expression list is NULL? Other commands that
take expressions such as "if { expression } " could easily
be written to make NULL expressions behave the same as FALSE
expressions.

Thank you for reading this far. Please let me know if there is
any hope for this request.

Chris Handorf
602-914-8116
Motorola SPS
Phoenix, AZ
r13335@email.sps.mot.com

These are programming misunderstandings. The changes to expr
are well defined in the porting docs, and expr expressions should
now always be {}ed for proper evaluation (and speed).
The error reporting is correct. For each case:

% expr 8 + ""
syntax error in expression "8 + "

Here the user receives an error because the binary op + is unsatisfied

% expr "" + 8
8

Here the user isn't using braces, so the Tcl level of eval converts
this to just "+ 8", and then expr uses the + as a unary op.

% expr "" > 10
syntax error in expression " > 10"

> is never a unary op...

% if { "" > 10 } { puts true } else { puts false }
false

This works, because the initial use of a string is kicking this
into the string comparison mode. A command should not be used
in this way if it isn't guaranteed what type of arg would be
received. Use string compare/equal or fix the command.
-- 08/14/1999 hobbs

Discussion

  • Brent B. Welch

    Brent B. Welch - 2000-10-26
    • priority: 5 --> 2
    • status: open --> closed-fixed
     
  • Don Porter

    Don Porter - 2001-04-13
    • labels: 104246 --> 16. Commands A-H
    • milestone: 102414 --> obsolete: 8.0p2
    • status: closed-fixed --> closed-invalid