Wednesday, April 8, 2009

Extra Characters After Close-Brace

As noted previously, Tcl forces you to put open-braces "{" on the same line as the if or proc that they belong to, and forces you to use the atrocious "} else {" style.

That's appalling, but get this: it also requires a space to the right of the close-brace "}". That's just plain stupid.

Compounding the problem, the error message isn't something like "a space is required after a close-brace". Oh no. It says "extra characters after close-brace". And if the body of your if is very long, like this:
if {$tcl_is_stupid} {
  # ... many lines of code ...
  # Next line is the error
}else{
  i_eat_my_hat
}

then the error message is truncated so that you only see the part of the command that isn't broken, along with the line number of the "if", not the offending "else". As icing on the cake, in the case I was trying to help someone figure out, the error was at the top of one of Tcl's Icelandic Saga stacktraces, and the line number reported to be in error was with respect to the proc it was in, not the file.

Thankfully, Google led me to the answer, because Tcl sure didn't. I love the subtitle of that Tcl wiki page: Purpose: to discuss one of the few 'gotchas' in Tcl. Ha! What a crock!

13 comments:

Unknown said...

Thank you for this post, saved me from eating my hat =)

Anonymous said...

saved my day. I don't know who's idea it was, but this is ridiculous

Anonymous said...

Thanks for the explanation. Helped me a lot

Anonymous said...

Thanks for the help. Tcl is nice for prototyping GUIs but WOW! That community really needs to fix its documentation!

Anonymous said...

Thanks for the help...

Viplove said...

My hat is intact too. Thanks to you!

goho said...

This is how I want all blogs to be :-) Short piece of information that saved my day. Thanks.

Anonymous said...

Thank you
Tcl is a nightmare. You are the bedside light.

Anonymous said...

Have you run into the "braces in comments are syntactically significant" SNAFU yet?

This doesn't compile:

proc hello {
# I'd like to put an unmatched { in my comment
puts "Hello!"
}

Bill Night said...

Ha ha! Thanks for pointing that one out. I may have to make a new post just for that one.

What I love is, if you change your open brace for a close brace, you get a message that doesn't begin to tell you the problem:

wrong # args: should be "proc name args body"

At least with the open brace I get:

missing close-brace: possible unbalanced brace in comment

Anonymous said...

They're also syntactically significant in strings.

proc goodbye {} {
puts "Sod off, { tcl!"
}

PS. Sorry, there was a syntax error in my previous post. Should have been an empty parameter list {} after between "hello" and the opening brace. But anyway.

I'm an ASIC verification engineer, and having to use Tcl makes me pine for the relative sanity of SystemVerilog. And that's bloody scary.

romeroc24 said...

amazing!!!!

Unknown said...

well I still haven't found the "extra" characters after my braces in my very long ifelse chain and I am about to kill myself