Re: The "never use string commands with lists" mantra - Why?
From: Bryan Oakley (bryan_at_bitmover.com)
Date: 01/02/04
- Next message: Howard Jess: "Re: TCL equivalent of PHP "phpinfo()"?"
- Previous message: Derk Gwen: "Re: regexp substitution"
- In reply to: Phil Powell: "The "never use string commands with lists" mantra - Why?"
- Next in thread: Donal K. Fellows: "Re: The "never use string commands with lists" mantra - Why?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Howard Jess: "Re: TCL equivalent of PHP "phpinfo()"?"
- Previous message: Derk Gwen: "Re: regexp substitution"
- In reply to: Phil Powell: "The "never use string commands with lists" mantra - Why?"
- Next in thread: Donal K. Fellows: "Re: The "never use string commands with lists" mantra - Why?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|