Re: The "never use string commands with lists" mantra - Why?

From: Bryan Oakley (bryan_at_bitmover.com)
Date: 01/02/04


Date: Fri, 02 Jan 2004 19:07:17 GMT

Phil Powell wrote:

> why does TCL
> continually allow you to use string commands on lists and list
> commands on strings? We're told not to do so and yet your language
> completely permits it to be so!

The reason is because Tcl isn't like other languages. Tcl isn't a
language nearly as much as it is a philosophy or way or programming.

The fundamental role of the tcl parser is to take a string (eg: a line
of text from a script), separate it into words, and pass each word as a
distinct argument to a command. Tcl has no idea what the command will do
with the data or what types of data it requires. In that sense, then, it
is absolutely impossible for tcl to prevent you from passing a list to a
string command or a string to a list command. Or a string to a math
command or an image to a widget command or a widget to an array command
or...

For example, one of the strengths of Tcl is that you can rewrite any
command to work any way you like. Maybe you are exposing part of the
interpreter to the end user, say for writing config files or macros or
whatever. Maybe you want to give them a command named "string" that
looks like this "string from $list to varname". How can the parser know
that in your version of the string command a list is a perfectly valid
argument?

Now, you may be saying "that's fine, but why does the lappend command,
for example, automatically try to convert a string to a list instead of
just complaining that it was given a string?". The answer to that is
mostly historyical -- that's just the way it was first implemented back
when lists truly were strings.

Is this good or bad? It doesn't really matter -- it's the way it is.

It's good that you are trying to understand why the language is the way
it is, but don't get too hung up on it. I find that most of the people
that have a hard time learning Tcl are the ones who are always saying
"why isn't Tcl like language foo" or "it's stupid that Tcl works this
way...". Learn the language for what it can do rather than fret over how
it doesn't work the way some other language works.



Relevant Pages

  • TIP #185: Null Handling
    ... nulls, and command modifications for manipulating them. ... Tcl deals with strings, the universal medium for representing data. ... is know and it is an empty string, but if a respondent forgets to give ...
    (comp.lang.tcl)
  • All commands are equal, but some are more equal than others.
    ... just finished implementing a HTTP webcrawler using the http package ... be able to scramble a string. ... Very surprised AH starts searching the Tcl wiki to find out what might ... % <command to add subcommands to namespace ensemble> string scramble ...
    (comp.lang.tcl)
  • Re: Import in sqlite
    ... TCL list as opposed to a file. ... would be a list of lists and the copy command would have to know that. ... The 'sqlite3' command line executable has a '.import' command, ...
    (comp.lang.tcl)
  • Re: How do I filter Tcl code as it is being executed?
    ... Tcl doesn't execute the contents of variables like that. ... The problem is when you have a command that takes N arguments, and you want to pass it two scalars and the contents of a list as arguments, and you write ... Then the contents of $remainder get interpreted twice because you asked for it, and $first and $second get interpreted twice because you asked for that too. ... The solution is to "quote" the other arguments with the command, which builds lists, with the side effect of quoting all the characters that are special to eval: ...
    (comp.lang.tcl)
  • TIP #225: Arithmetic Series with Optimized Space Complexity
    ... This TIP proposes to add a new command to generate arithmetic sequences ... The only change from the point of view of the Tcl ... initialized to the first element of the sequence, ... Infinite series and series resulting in lists bigger than the maximum ...
    (comp.lang.tcl)