git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [RFH - Tcl/Tk] use of procedure before declaration?

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Jan 17, 2017, 11:29 UTC
Message-ID
<alpine.DEB.2.20.1701171218260.3469@virtualbox>
In-Reply-To
<F9099DB3F0374D898776BD2621BF36FA@PhilipOakley>
Hi Philip,
On Mon, 16 Jan 2017, Philip Oakley wrote:
Show 5 quoted lines
> In
> https://github.com/git/git/blob/master/git-gui/lib/choose_repository.tcl#L242
> the procedure `_unset_recentrepo` is called, however the procedure isn't
> declared until line 248. My reading of the various Tcl tutorials suggest
> (but not explictly) that this isn't the right way.
Indeed, calling a procedure before it is declared sounds incorrect.

Since documentation can be treacherous, let's just test it. With a `tclsh` whose `$tcl_version` variable claims that this is version 8.6, this script:

```tcl hello Philip

proc hello {arg} {
        puts "Hi, $arg"
}
```
... yields the error message:
	invalid command name "hello"
	    while executing
	"hello Philip"
... while this script:
```tcl
proc hello {arg} {
        puts "Hi, $arg"
}

hello Philip ```

... prints the expected "Hi, Philip".

Having said that, in the code to which you linked, the procedure is not actually called before it is declared, as the call is inside another procedure.

Indeed, the entire file declares one object-oriented class, so no code gets executed in that file:

https://github.com/git/git/blob/d7dffce1c/git-gui/lib/choose_repository.tcl#L4

(I guess proper indentation would make it easier to understand that this file is defining a class, not executing anything yet).

And it is perfectly legitimate to use not-yet-declared procedures in other procedures, otherwise recursion would not work.

> Should 3c6a287 ("git-gui: Keep repo_config(gui.recentrepos) and .gitconfig
> in sync", 2010-01-23) have declared `proc _unset_recentrepo {p}` before
> `proc _get_recentrepos {}` ?
Given the findings above, I believe that the patch is actually correct.

Ciao, Dscho

Previous: Philip OakleyNext: Philip Oakley
Message 2 of 4 in “[RFH - Tcl/Tk] use of procedure before declaration?”
  1. Philip OakleyJan 16, 2017
  2. Johannes SchindelinJan 17, 2017
  3. Philip OakleyJan 17, 2017
  4. Konstantin KhomoutovJan 18, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.