0

I have this proc in some Tcl

proc AddType {type} {
    set map (...code to create the map here...)
    if {[info exists map("$type")]} {    
        set frame $map("$type")
    } else {
        set frame [MakeFrame]
    }
}

which works correctly. Unfortunately, something in the middle of it "breaks" Notepad++'s syntax highlighting, such that it shows like this:

screenshot showing syntax highlighting error

If I change $map("$type") to $map( "$type") then all is happy in terms of the syntax highlighting:

screenshot showing correct syntax highlighting

however the Tcl then fails to load. This is some legacy Tcl, and this proc is near the start of a pretty large file and all of the syntax highlighting is wrong from this point to the end of the file, so it's pretty irritating.

Is there another way to rework that array access such that it still works and such that Notepad++'s syntax highlighting will be happy?

10
  • 1
    Quoting the wiki: "In Tcl, everything is a string. Quoting strings is mostly not necessary - and can even be harmful, as in this example..." You can read the article to see how it can be harmful. Commented May 23, 2014 at 12:02
  • The "mostly" not necessary scares me slightly, but this gives me something to work with. Commented May 23, 2014 at 12:45
  • In this case, it's a variable you've quoted, not a string. So I'd say definitely not necessary. Commented May 23, 2014 at 13:10
  • The "mostly" does not indicate inconsistencies, but rather that a full discussion of the implications may be longish (there are, for instance, eight basic ways to dereference a variable name using combinations of dqoutes and braces, and one of them will fail because of a subtle interaction). The issue here is that Notepad++ treats double quotes as indicating a string literal, while in Tcl double quotes are used to "soft quote" strings (removing the syntactic effects of e.g. spaces and braces). So, yes, test it to see if it works, but if it does work you can be assured that it will always work. Commented May 23, 2014 at 13:44
  • 3
    That info exists will return false, as you've done a set map [something] immediately above, guaranteeing that map is not an array… Commented May 23, 2014 at 17:38

1 Answer 1

2

I use Textpad and your proc looks good to me when I put in a TCL file.

What you could do is remove the double quotes around $type. They're not needed i.e.

change

if {[info exists map("$type")]} {    
        set frame $map("$type")

to

if {[info exists map($type)]} {    
        set frame $map($type)
Sign up to request clarification or add additional context in comments.

3 Comments

"They're not needed" will need to be tested, specifically against other modules, legacy code elsewhere, customisations, persisted values, and the like, but I'm encouraged enough to try it. If it works out then I'll accept the answer, but if it doesn't then I'll hopefully have a refinement which I can add to the question.
Testing is always wise :)
@ClickRick: in this case, quotes are absolutely, unequivocally not needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.