Pregunta

How to know what is the name of the proc in which I am. I mean I need this:

proc nameOfTheProc {} {

    #a lot of code here
    puts "ERROR: You are using 'nameOfTheProc' proc wrongly"
}

so I want to obtain "nameOfTheProc" but not hard-code. So that when someone will change the proc name it will still work properly.

¿Fue útil?

Solución

You can use the info level command for your issue:

proc nameOfTheProc {} {

    #a lot of code here
    puts "ERROR: You are using '[lindex [info level 0] 0]' proc wrongly"
    puts "INFO:  You specified the arguments: '[lrange [info level [info level]] 1 end]'"
}

With the inner info level you will get the level of the procedure call depth you are currently in. The outer one will return the name of the procedure itself.

Otros consejos

The correct idiomatic way to achieve what's implied in your question is to use return -code error $message like this:

proc nameOfTheProc {} {
    #a lot of code here
    return -code error "Wrong sequence of blorbs passed"
}

This way your procedure will behave exactly in a way stock Tcl commands do when they're not satisfied with what they've been called with: it would cause an error at the call site.

If your running Tcl 8.5 or later the info frame command will return a dict rather than a list. So modify the code as follows:

proc nameOfTheProc {} {
   puts "This is [dict get [info frame [info frame]] proc]"
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top