lappend puts some args in braces if they start list
Closed / Invalidbd45a0e32a1ce6cb1b4d0629e538da8fe94c1e65Created 8 years agoUpdated 8 years ago
Descriptionplain2018-08-13 16:49:37
if you lappend a string starting with # to an empty list or new variable, it gets wrapped in braces, but not if you lappend it to an existing list. e.g.,
% lappend new #1234
{#1234}
% lappend new #1234
{#1234} #1234
%
Expected behaviour is the element should be put in braces only if they are needed to make it a single list element.
sebreswiki2018-08-13 17:12:31
It's not a bug, it's a feature.
This way tcl allows to use canonical list as the command, for example compare:
% set l {#0 #1 #2}
#0 #1 #2
% if 1 $l
% set l {{#0} #1 #2}
{#0} #1 #2
% if 1 $l
invalid command name "#0"
You see in the first case tcl invokes the command "#0"... in second case it ignores the content of list because it is a comment in this case.
In case of the list representation, it working as expected, so each element is the same:
% lindex $new 0
#1234
% lindex $new 1
#1234
% set new
{#1234} #1234
Thus closed as invalid.
jal_frezieplain2018-08-13 18:36:19
In that case I would expect an element starting with # to be braced wherever it is added in the list, as is the case for elements starting with $. That also would not interfere with the list representation, and would be more consistent.
sebresplain2018-08-13 19:15:18
String representation of list-type is not constructed for "consistent" reasons as you meant it, but primary for example to follow the EIAS magic (see https://wiki.tcl.tk/3018), resp. secondary to avoid the lost of the list representation in case of interim type switch (e. g. list -> unicode, -> dict, etc. and then back to list by usage of lindex for example). Additionally Tcl follows "the shorter the better" strategy in the canonical form, so basically if possible the shortest will be preferred (even with some exceptions, like for the first argument for already explained reasons). The $, some constellations with backslashes, non-balanced quotes and another special characters could cause a string representation that are totally impossible without usage of braces, therefore it will be escaped this way. If you try to work with the string representation of list using something other as "list" primitives and API functions, you should be ready to experience many other surprises...
Log in (anonymous is fine) to add a comment.