0
votes

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?

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.Jerry
The "mostly" not necessary scares me slightly, but this gives me something to work with.ClickRick
In this case, it's a variable you've quoted, not a string. So I'd say definitely not necessary.TrojanName
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.Peter Lewerin
That info exists will return false, as you've done a set map [something] immediately above, guaranteeing that map is not an array…Donal Fellows

1 Answers

2
votes

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)