Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2011-10-01
14:50 • Closed ticket [3169002fff]: conversion from "dict" to "list"... artifact: a19386de6f user: dkf
2011-02-04
03:11 • Open ticket [3169002fff]. artifact: a6cf3032c4 user: sehmann
2011-02-03
04:15 • Closed ticket [3169002fff]. artifact: d24f858e4a user: dkf
02:51 • Ticket [3169002fff]: 4 changes artifact: ec7b57e00b user: sehmann
2011-02-01
19:14 • Ticket [3169002fff]: 1 change artifact: 0202c6ba4c user: msofer
05:30 • Ticket [3169002fff]: 4 changes artifact: b297d2badf user: sehmann
05:16 • Ticket [3169002fff]: 4 changes artifact: 21d5aa2ec8 user: andreas_kupries
2011-01-31
22:10 • New ticket [3169002fff]. artifact: bf41ebf1a2 user: sehmann

conversion from "dict" to "list" loses object types

Closed / Works For Me3169002fffffffffffffffffffffffffffffffffCreated 16 years agoUpdated 15 years ago
Descriptionplain2011-01-31 22:10:25
When we use "array get" a dict object is returned.  If this is used directly in a foreach all types are preserved.  The problem arises when the result is copied.  It seems like the fix is to simply copy the objects as they are when converting from dict to list (tcl8.4.9 just appends the array object into the result list) rather than stringify the objects.

we are trying to upgrade our system from 8.4.9 to 8.5.9 and this is a blocker.

% array set a ""
% set a(b) "1"
1
% set a(c) [list 1 2]
1 2
% otype [array get a]
dict
% set l [array get a]
b 1 c {1 2}
% otype $l
dict
% otype $l
dict
% foreach key [array get a] { puts "[otype $key]" }
string
int
string
list
% otype $l
dict
% foreach key $l { puts "[otype $key]" }
string
string
string
string
% otype $l
list
% puts $l
b 1 c {1 2}
%
andreas_kupriesplain2011-02-01 05:16:32
I am not sure that it is the 'set l ...' which looses the type information of the elements. You are working interactively, and it may be the code printing the value to the terminal which does it.

What is the output for

% set l [array get a] ; foreach key $l { puts "[otype $key]" }

This avoids the printing of 'l'.
sehmannplain2011-02-01 05:30:37
It prints the right thing... Hmm...
% array set a ""
% set a(b) "1"
1
% set a(c) [list 1 2]
1 2
% set l [array get a] ; foreach key $l { puts "[otype $key]" }
string
int
string
list

But separating them does not:
% array set a ""
% set a(b) "1"
1
% set a(c) [list 1 2]
1 2
% set l [array get a]
b 1 c {1 2}
% foreach key $l { puts "[otype $key]" }
string
string
string
string

Why would list operations behave ok in this regard?
% set l [list 1 [list 1 2]]
1 {1 2}
% otype $l
list
% otype [lindex $l 0]
int
% otype [lindex $l 1]
list
%


I forgot to add that otype is a C function we wrote to print the type of the object and is implemented like this:
    if ( obj->typePtr ) {
        char* typeName = obj->typePtr->name;
        Tcl_SetResult(interp, typeName, TCL_VOLATILE);

    } else if ( obj->bytes ) {
        Tcl_SetResult(interp, (char*)"string", TCL_STATIC);

    } else {
        Tcl_SetResult(interp, (char*)"unknown", TCL_STATIC);
    }
msofer Ticket [3169002fff]: 1 change [History diff]2011-02-01 19:14:29
sehmannplain2011-02-03 02:51:00
puts $l
also makes the issue appear.
dkfplain2011-02-03 04:15:00
Seems to be working exactly as designed. We do not guarantee (and never have guaranteed) that the "type" of values will be preserved; it does not represent type information in the same way that a Python or Ruby object's type does.
sehmannplain2011-02-04 03:11:08
Thanks, we need to rethink parts of our system that were relying on this...  You can close this issue.
dkfplain2011-10-01 14:50:47
allow_comments - 1
Log in (anonymous is fine) to add a comment.