Question

So, I used the thread of a similar name to get this working using the list command. Here is my working code:

proc E364xA::Connect [list [list VisaAlias ${E364xA::DefaultAlias}]] {

    ::VISA::Connect $VisaAlias
}

Now this is working currently by using the value stored in DefaultAlias within the namespace eval. I was wondering if there is a more intuitive way of doing this like:

proc E364xA::Connect {{VisaAlias ${E364xA::DefaultAlias}}} {

    ::VISA::Connect $VisaAlias
}

This way you wouldn't have two list commands muddling the waters. Any Ideas?

Thanks for the help in advance!

Was it helpful?

Solution 2

Maybe this? (Removed previous edits.)

proc E364xA::Connect "{VisaAlias {$E364xA::DefaultAlias}}" {

    ::VISA::Connect $VisaAlias
}

Test

Here's a simplified test:

set def "hello, world!"

proc test "{var {$def}}" {
    puts $var
}

test

Outputting:

hello, world!

OTHER TIPS

I don't see any different way which offers more syntactic sugar. However, you can do something like:

proc E364xA::Connect {{VisaAlias -use-default}} {
    if {$VisaAlias == "-use-default"} { 
        set VisaAlias ${E364xA::DefaultAlias}
    }

    ::VISA::Connect $VisaAlias
}

Example Usage:

E364xA::Connect                 ;# Use the default value
E364xA::Connect -use-default    ;# Use the default value
E364xA::Connect somethingElse   ;# Non default

You could use the args argument, but it's not much clearer than your code, and it hides the fact that your proc should take at most 1 argument.

proc E364xA::Connect args {
    if {[llength $args] == 0} {
        set VisaAlias $E364xA::DefaultAlias
    } else {
        set VisaAlias [lindex $args 0]
    }
    ::VISA::Connect $VisaAlias
}

or

proc E364xA::Connect args {
    ::VISA::Connect [expr {[llength $args] ? [lindex $args 0] : $E364xA::DefaultAlias}]
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top