Tcl Source Code

View Ticket
Login
Ticket UUID: 3304643
Title: User object info lost on more aggressive numeric conversion
Type: Bug Created on: 2011-05-19 15:04:21
Submitter: sehmann Assigned to: msofer
Subsystem: 10. Objects Severity:
Priority: 6 Last modified: 2011-06-30 03:20:07
Status: Open Closed by:
Resolution: None Closed on:
Version: obsolete: 8.5.10
Description:
Tcl8.4 would convert an object to a numeric type during execution of an operator (such as ==) when the other argument was of a numeric type.  It did not attempt to do this conversion if the other argument was "", a string, a user object type.

Tcl8.5 now converts both arguments blindly when certain operators (such as ==) are executed.  This sometimes results in our objects (our own types) being converted and our internalRep being freed.  This causes a large portion of our TCL codebase to have a regression when one of our objects happens to have a string rep that is convertible to a int (eg. "123") or to a double (eg. "INF", "NAN", "0.5").  For example comparing "INF" == "" should not attempt convert both to numeric values.

The fundamental problem is that there is no way to retain our object type information or avoid these conversions.  We key off the type to cast the internalRep pointer to our internal C++ object.  If this is not the best way to do cast to our internal object, please let us know the robustly supported way of doing that.

Some solutions we see are:
1) don't do the aggressive conversion (ie. go back to tcl8.4 style)
2) only attempt numeric conversion if the type is one of the standard internal TCL types and not some user-specified type
3) add a boolean to the TCL_Object which controls the immutability of an object (ie. don't do conversion if the flag is set)
4) add a userdata field where we can store what we need (NULL or failure to recognize type stored in there means not our object)

We need to be able to move forward with tcl8.5 and eventually tcl8.6 but this is holding us back.
User Comments:
cjmcdonald added on 2011-05-19 22:40:26:
Or always use the "eq" operator to do string comparisons on your objects, instead
of "==".

For better or worse I use a similar scheme, storing a pointer to a C++ object in
the Tcl object internalRep.  The only difference is that my object string reps
never look like a numeric value, but I don't think that will be an issue anyway if
you use "eq".  I haven't had many problems with Tcl 8.5, and the scheme has
proved reliable.  The only issues have been:

Losing internal rep converting between a list and a dict, which also affected
array get/set.  This have been fixed in later 8.5.x releases.

Overly aggressive conversions using %s in [format], which unfortunately
also affects parray, so that listing the contents of an array loses any
internal reps.  I think that this has been fixed in the Tcl 8.6 head.

Otherwise all OK, although it will never be an officially approved or supported
method, as using the internalRep in that way breaks the Tcl "Everything
is a string" contract.  These object representations consist of a string AND
some hidden state (the C++ object), which cannot be inferred just from the string.

sehmann added on 2011-06-28 21:10:22:
Moving Group to a "current" one rather than an "obsolete" one in the hopes of getting more response.  Also bumping priority to 6 FWIW.

Colin, always using "eq" is a good solution but is not going to work for us as we have lots of existing code and lots of developers who are never going to be able to remember these subtleties.

Does anyone else have a comment?

Generally not having a reliable model for our internal data causes us lost development time (not just on this issue) as we track down hard to understand Tcl internal workings.  It would be good to have a mode (such as my suggestion 3 or 4) so that we could be guaranteed data stability.  Basically there is one conversion policy and the data object provider cannot choose another one.

Do we just have to live with the current conversion schemes?  Have any of my suggested solutions been discussed previously or in response to this?

dgp added on 2011-06-30 03:20:07:
My inclination is to say you are asking Tcl to
do non-Tcl things.   But perhaps with a more
detailed example I might find some middle
ground of accommodation.  What's an example
expression that illustrates the problem?  

More examples and less description generally
would be a better way for more people to understand
the details of your troubles and either find a Tcl change
to make you happy, or suggest changes to your code
with the maximum pracitcality.

When you create your own Tcl_ObjTypes, part of
the contract with Tcl is that it can "re-type" values
as it sees fit.  It will not change the value (assuming
proper copy on write by everyone), but it might
change the type.  The robust supported way to
deal with that is for your Tcl_ObjType to supply a
Tcl_SetFromAnyProc() that can take any Tcl string
value and generate the corresponding internal
rep your Tcl_ObjType uses.  That implies either
encoding into the string rep all that needed to
reconstruct your intrep, or using some kind of
handle scheme with an auxilliary lookup (for
example a hash table).

That said, people have made other less robust
schemes work, so please give a few examples
so I can take a look at the details to see what
solutions might be feasible.