1

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!

jjno91
  • 653
  • 7
  • 19
  • Some clarifications were made to my answer. You can keep @HaiVu's accepted answer--I agree that that is a clearer way of doing things--but if it's concision you want, then mine might help. – Andrew Cheong Jan 02 '13 at 17:09
  • Do you want the content of the variable at the time the procedure is _created_ or at the time the procedure is _called_? In Tcl, this is a very important distinction. – Donal Fellows Jan 02 '13 at 23:01
  • @acheong87 I've accepted your answer since it was closer to answering the question I asked. I do believe HaiVu's is probably the best way to solve the issue overall, but it will evaluate the variable at the time it is called. I need this value to be set at time of creation since it refers to a value that is hard coded into devices of a certain series. I also wanted the line to be visually similar to {{VisaAlias "E364xA"}} my consciense just wouldn't let me hardcode the value. – jjno91 Jan 03 '13 at 16:42
  • @donal I believe my last comment answers your question as well. – jjno91 Jan 03 '13 at 16:43

3 Answers3

4

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
Hai Vu
  • 37,849
  • 11
  • 66
  • 93
  • Thanks for the response, I think this gives the best idea of what the code is doing to someone looking over it. Doesn't look like TCL has the functionality I was wanting to use, but I believe this accomplishes the same goal. – jjno91 Jan 02 '13 at 17:03
  • Actually acheong87 posted a very nice answer that I have accepted that uses double quotes to solve this problem. Take a look at it. – jjno91 Jan 02 '13 at 17:22
  • 1
    For Tcl 8.5 and above, use `eq` for string comparison: http://tcl.tk/man/tcl8.5/TclCmd/expr.htm#M14 – glenn jackman Jan 02 '13 at 19:08
1

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!
Andrew Cheong
  • 29,362
  • 15
  • 90
  • 145
1

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}]
}
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Thanks, I hadn't thought thought to use args since I'm pretty new to TCL. However, I do agree with you on it not helping clarity very much. I tried a few ways with args and it just didn't look too nice. Thanks for the idea though! +1 – jjno91 Jan 02 '13 at 17:09
  • Thank you, Glenn. At work, we are still at Tcl 8.4 without any plan to upgrade to 8.5. I did not know about eq until now. – Hai Vu Jan 02 '13 at 19:12