Re: [RFH - Tcl/Tk] use of procedure before declaration?
On Tue, 17 Jan 2017 12:29:23 +0100 (CET) Johannes Schindelin 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.
Re: [RFH - Tcl/Tk] use of procedure before declaration?
From: "Johannes Schindelin" 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
Re: [RFH - Tcl/Tk] use of procedure before declaration?
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
[RFH - Tcl/Tk] use of procedure before declaration?
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-philipoak...@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