Anand
Anand

Reputation: 1122

Why doesn't compiler automatically brace expr?

I was reading through Donal Fellows' excellent explanation on why you should brace your expr.

It makes me wonder why doesn't the tcl compiler automatically brace {} the expr? What are conditions under which this automatic bracing will fail?

Upvotes: 1

Views: 79

Answers (2)

Donal Fellows
Donal Fellows

Reputation: 137637

It makes me wonder why doesn't the tcl compiler automatically brace {} the expr?

It actually did so during some of the alphas for 8.0. It was removed because the wider community absolutely hated it. The core Tcl language — the 12 rules on the Tcl(n) manual page — is kept extremely small and simple, and it's applied uniformly to everything.

No special cases. That is what most Tcl programmers want.

If I remember right, the auto-bracing was particularly considered for if and while. With if, it was because omitting the braces for a test of the result of a command was a particularly common practice with Tcl 7 because it was faster (the expression engine was rather embarrassingly slow). With while, it was because omitting the braces was a common user bug.

What are conditions under which this automatic bracing will fail?

Well, it would be noticeable in something like this:

set a 1
set b 2
set op +
set c [expr $a$op$b]

Right now, that sets c to 3. With auto-bracing, it would be a syntax error (since the expression grammar doesn't have anything it can do with three variables in a row). A work-around was proposed:

set c [eval expr $a$op$b]

But frankly, getting everyone to brace their expressions except when they really wanted double-substitution (and runtime expression compilation) was considered to be better.

Double-substitution is almost always an indication of a security bug, and it's always an indication of a performance problem; there's virtually always a better (faster, safer) way to do it. Brace your expressions. It's safer and faster and really easy to do: what's not to like?

Upvotes: 3

Peter Lewerin
Peter Lewerin

Reputation: 13252

Some random thoughts on this...

  • One of the strengths of the language is that evaluation is governed by a small number of rules which are applied uniformly. To have the arguments to expr automatically braced would mean that there would have to be an exception in the rules for this command.
  • It would also mean that the interpreter would have to be rewritten, since it evaluates the arguments before it determines which command it is dealing with. The expr command could be renamed or aliased, making it harder for the interpreter to figure out when to autobrace.
  • It would mean that if someone wanted double evaluation of the arguments, or wanted to construct the argument in a way that does not directly match the expression tokens, they would have to work around the automatic bracing.
  • It would mean possible incompatibilities with a lot of old code.

To have the interpreter as a "back seat driver" isn't really the Tcl Way.

Upvotes: 4

Related Questions