git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [RFH - Tcl/Tk] use of procedure before declaration?
@ 2017-01-16 22:44 Philip Oakley
  2017-01-17 11:29 ` Johannes Schindelin
  0 siblings, 1 reply; 4+ messages in thread
From: Philip Oakley @ 2017-01-16 22:44 UTC (permalink / raw)
  To: Git List; +Cc: Pat Thoyts

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFH - Tcl/Tk] use of procedure before declaration?
  2017-01-16 22:44 [RFH - Tcl/Tk] use of procedure before declaration? Philip Oakley
@ 2017-01-17 11:29 ` Johannes Schindelin
  2017-01-17 23:32   ` Philip Oakley
  2017-01-18  6:43   ` Konstantin Khomoutov
  0 siblings, 2 replies; 4+ messages in thread
From: Johannes Schindelin @ 2017-01-17 11:29 UTC (permalink / raw)
  To: Philip Oakley; +Cc: Git List, Pat Thoyts

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFH - Tcl/Tk] use of procedure before declaration?
  2017-01-17 11:29 ` Johannes Schindelin
@ 2017-01-17 23:32   ` Philip Oakley
  2017-01-18  6:43   ` Konstantin Khomoutov
  1 sibling, 0 replies; 4+ messages in thread
From: Philip Oakley @ 2017-01-17 23:32 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: Git List, Pat Thoyts

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 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFH - Tcl/Tk] use of procedure before declaration?
  2017-01-17 11:29 ` Johannes Schindelin
  2017-01-17 23:32   ` Philip Oakley
@ 2017-01-18  6:43   ` Konstantin Khomoutov
  1 sibling, 0 replies; 4+ messages in thread
From: Konstantin Khomoutov @ 2017-01-18  6:43 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: Philip Oakley, Git List, Pat Thoyts

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.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-01-18  6:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-16 22:44 [RFH - Tcl/Tk] use of procedure before declaration? Philip Oakley
2017-01-17 11:29 ` Johannes Schindelin
2017-01-17 23:32   ` Philip Oakley
2017-01-18  6:43   ` Konstantin Khomoutov

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).