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

4 messages from 2017-01-16 to 2017-01-18. Participants: Philip Oakley, Johannes Schindelin, Konstantin Khomoutov.
Thread: https://gitlist.dev/t/44895

## Philip Oakley, 2017-01-16 22:45

Subject: [RFH - Tcl/Tk] use of procedure before declaration?
Message-ID: <F9099DB3F0374D898776BD2621BF36FA@PhilipOakley>
URL: https://gitlist.dev/e/F9099DB3F0374D898776BD2621BF36FA%40PhilipOakley

```
I'm looking into a user git-gui problem
(https://github.com/git-for-windows/git/issues/1014) that I'd seen in the
past - I'd started some patches back in Dec 2015
http://public-inbox.org/git/1450310287-4936-1-git-send-email-philipoakley@iee.org/

I'm trying to make sure I have covered the corner cases correctly, and I'm
not sure if the current code actually works as advertised.

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.

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 {}` ?

--

Philip


```

## Johannes Schindelin, 2017-01-17 11:29

Subject: Re: [RFH - Tcl/Tk] use of procedure before declaration?
Message-ID: <alpine.DEB.2.20.1701171218260.3469@virtualbox>
URL: https://gitlist.dev/e/alpine.DEB.2.20.1701171218260.3469%40virtualbox
In-Reply-To: <F9099DB3F0374D898776BD2621BF36FA@PhilipOakley>

```
Hi Philip,

On Mon, 16 Jan 2017, Philip Oakley wrote:

> 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

```

## Philip Oakley, 2017-01-17 23:33

Subject: Re: [RFH - Tcl/Tk] use of procedure before declaration?
Message-ID: <826895153F394EB29B0D27F3EB9A67C9@PhilipOakley>
URL: https://gitlist.dev/e/826895153F394EB29B0D27F3EB9A67C9%40PhilipOakley
In-Reply-To: <alpine.DEB.2.20.1701171218260.3469@virtualbox>

```
From: "Johannes Schindelin" <Johannes.Schindelin@gmx.de>
> Hi Philip,
>
> On Mon, 16 Jan 2017, Philip Oakley wrote:
>
>> 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
>
Thanks for the clarification. I'll update the old patch series and see if we 
can get this fixed.

Philip 


```

## Konstantin Khomoutov, 2017-01-18 06:43

Subject: Re: [RFH - Tcl/Tk] use of procedure before declaration?
Message-ID: <20170118094306.a17083eecd94c0f39b760081@domain007.com>
URL: https://gitlist.dev/e/20170118094306.a17083eecd94c0f39b760081%40domain007.com
In-Reply-To: <alpine.DEB.2.20.1701171218260.3469@virtualbox>

```
On Tue, 17 Jan 2017 12:29:23 +0100 (CET)
Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:

> > 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.
[...]
> And it is perfectly legitimate to use not-yet-declared procedures in
> other procedures, otherwise recursion would not work.
[...]

Sorry for chiming in too late, but I'd throw a bit of theory in.

Since Tcl is an interpreter (though it automatically compiles certain
stuff to bytecode as it goes through the script, and caches this
representation), everything is interpreted in the normal script order --
top to bottom as we usually see it in a text editor.

That is, there are simply no declaration vs definition: the main script
passed to tclsh / wish is read and interpreted from top to bottom;
as soon as it calls the [source] command, the specified script is read
and interpreted from top to bottom etc; after that the control is back
to the original script and its interpretation continues.

Hence when Tcl sees a command (everything it executes is a command; this
includes stuff like [proc], [foreach] and others, which are syntax in
other languages) it looks up this command in the current list of
commands it knows and this either succeeds or fails.  The built-in
command [proc] defines a new Tcl procedure with the given name, and
registers it in that list of known commands.

So the general rule for user-defined procedures is relatively
straightforward: to call a procedure, the interpreter should have read
and executed its definition before the attempted call.

```
