Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

Make a wish, it might come true.


comp / comp.lang.tcl / How to pass an object from inside its own method to a helper function

SubjectAuthor
* How to pass an object from inside its own method to a helper functionMark Summerfield
`* Re: How to pass an object from inside its own method to a helper functionMark Summerfield
 `* Re: How to pass an object from inside its own method to a helper functionSchelte
  `* Re: How to pass an object from inside its own method to a helper functionMark Summerfield
   `- Re: How to pass an object from inside its own method to a helper functionRalf Fassel

1
Subject: How to pass an object from inside its own method to a helper function
From: Mark Summerfield
Newsgroups: comp.lang.tcl
Date: Thu, 11 Jul 2024 09:44 UTC
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!border-1.nntp.ord.giganews.com!local-4.nntp.ord.giganews.com!border-4.nntp.ord.giganews.com!nntp.giganews.com!local-1.nntp.ord.giganews.com!Xl.tags.giganews.com!local-2.nntp.ord.giganews.com!nntp.brightview.co.uk!news.brightview.co.uk.POSTED!not-for-mail
NNTP-Posting-Date: Thu, 11 Jul 2024 09:44:07 +0000
From: mark@qtrac.eu (Mark Summerfield)
Subject: How to pass an object from inside its own method to a helper function
Newsgroups: comp.lang.tcl
MIME-Version: 1.0
User-Agent: Pan/0.154 (Izium; 517acf4)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Message-ID: <mwSdnbvrgID6NBL7nZ2dnZfqnPednZ2d@brightview.co.uk>
Date: Thu, 11 Jul 2024 09:44:07 +0000
Lines: 50
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-fvsTsUFLGCbiVpMnctRxs0+yvre6XQyqIKT89d/e9FnxDIHahqrz+N6T6R5Mz3AipPOPQhD/NVHQcZS!wHgs1bEd5uKQqhBQsR2ZVGHLmsw16Uwu3Utb2ZauW1doTvwCgFYj4Wo8EIC51Vg3NfVgYmEjAjkt!FPjCdPG+tx1Y8H/+40Z+lVm/dg==
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
View all headers

I want to pass an object from inside its own method to a helper function.
(I'd also like to know how to pass a bound method.)

In the example below I have tried using [self] and [self object] and [self
namespace] but none of them works.

#!/usr/bin/env wish9
tk appname "Test App"
proc make_file_menu {app} {
.menu.file add command -command {$app on_quit} -label Quit \
-underline 0 -accelerator Ctrl+Q
} oo::class create App {
constructor {} {
wm withdraw .
wm title . [tk appname]
grid [ttk::button .quitButton -text Quit -underline 0 \
-command [callback on_quit]]
bind . <Escape> [callback on_quit]
bind . <Alt-q> [callback on_quit]
menu .menu
menu .menu.file
.menu add cascade -menu .menu.file -label File -underline 0
make_file_menu [self] ;# BUG what do I pass here as "this"
. configure -menu .menu
}
method on_quit {} {destroy .}
method show {} {
wm deiconify .
raise .
}
} set application [App new]
$application show

The error I get is:

can't read "app": no such variable
while executing
"$app on_quit"
invoked from within
".#menu.#menu#file invoke active"
("uplevel" body line 1)
invoked from within
"uplevel #0 [list $w invoke active]"
(procedure "tk::MenuInvoke" line 49)
invoked from within
"tk::MenuInvoke .#menu.#menu#file 1"
(command bound to event)

Subject: Re: How to pass an object from inside its own method to a helper function
From: Mark Summerfield
Newsgroups: comp.lang.tcl
Date: Thu, 11 Jul 2024 10:03 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!border-2.nntp.ord.giganews.com!border-3.nntp.ord.giganews.com!nntp.giganews.com!local-2.nntp.ord.giganews.com!local-3.nntp.ord.giganews.com!Xl.tags.giganews.com!local-1.nntp.ord.giganews.com!nntp.brightview.co.uk!news.brightview.co.uk.POSTED!not-for-mail
NNTP-Posting-Date: Thu, 11 Jul 2024 10:03:27 +0000
From: mark@qtrac.eu (Mark Summerfield)
Subject: Re: How to pass an object from inside its own method to a helper
function
Newsgroups: comp.lang.tcl
References: <mwSdnbvrgID6NBL7nZ2dnZfqnPednZ2d@brightview.co.uk>
MIME-Version: 1.0
User-Agent: Pan/0.154 (Izium; 517acf4)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Message-ID: <mwSdnbrrgIByMBL7nZ2dnZfqnPcAAAAA@brightview.co.uk>
Date: Thu, 11 Jul 2024 10:03:27 +0000
Lines: 10
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-Ast8dwi4GupelUzsRRmbyd5WwxJ7szbBzk8DoUha/emTRcghdJqGjYuBM+wwDSnvt+WHfqsYtqvEyuv!vdDLJKb9MfXsAqbQIbqj4Bj/JmAOy5yXIATMhb3e6qC3qMUYesQ+HCNNqk4I71mDfKk9oYtp4OpQ!O0n8aZhGB9jkyReweHxzqUEpMg==
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
View all headers

I worked out how to do it:

proc make_file_menu {app} {
.menu.file add command -command [list ${app}::my on_quit] \
-label Quit -underline 0 -accelerator Ctrl+Q
}

For the caller I used:

make_file_menu [self]

Subject: Re: How to pass an object from inside its own method to a helper function
From: Schelte
Newsgroups: comp.lang.tcl
Organization: KPN B.V.
Date: Thu, 11 Jul 2024 11:12 UTC
References: 1 2
Date: Thu, 11 Jul 2024 13:12:52 +0200
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
Subject: Re: How to pass an object from inside its own method to a helper
function
Newsgroups: comp.lang.tcl
References: <mwSdnbvrgID6NBL7nZ2dnZfqnPednZ2d@brightview.co.uk>
<mwSdnbrrgIByMBL7nZ2dnZfqnPcAAAAA@brightview.co.uk>
Content-Language: en-US
From: nospam@wanadoo.nl (Schelte)
In-Reply-To: <mwSdnbrrgIByMBL7nZ2dnZfqnPcAAAAA@brightview.co.uk>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <nnd$51f688df$79eeaed5@f984d21cd0a14d8f>
Organization: KPN B.V.
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!news.swapon.de!2.eu.feeder.erje.net!feeder.erje.net!feed.abavia.com!abe007.abavia.com!abp002.abavia.com!news.kpn.nl!not-for-mail
Lines: 23
Injection-Date: Thu, 11 Jul 2024 13:12:56 +0200
Injection-Info: news.kpn.nl; mail-complaints-to="abuse@kpn.com"
View all headers

On 11/07/2024 12:03, Mark Summerfield wrote:
> I worked out how to do it:
>
> proc make_file_menu {app} {
> .menu.file add command -command [list ${app}::my on_quit] \
> -label Quit -underline 0 -accelerator Ctrl+Q
> }
>
Or less hacky:

proc make_file_menu {app} {
.menu.file add command -command [list $app on_quit] \
-label Quit -underline 0 -accelerator Ctrl+Q
}

Originally you had $app inside curly braces, which prevented it from
being substituted at definition time. At execution time, the app
variable was out of scope.

Schelte.

Subject: Re: How to pass an object from inside its own method to a helper function
From: Mark Summerfield
Newsgroups: comp.lang.tcl
Date: Thu, 11 Jul 2024 11:42 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!border-2.nntp.ord.giganews.com!border-1.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-2.nntp.ord.giganews.com!nntp.brightview.co.uk!news.brightview.co.uk.POSTED!not-for-mail
NNTP-Posting-Date: Thu, 11 Jul 2024 11:42:36 +0000
From: mark@qtrac.eu (Mark Summerfield)
Subject: Re: How to pass an object from inside its own method to a helper
function
Newsgroups: comp.lang.tcl
References: <mwSdnbvrgID6NBL7nZ2dnZfqnPednZ2d@brightview.co.uk>
<mwSdnbrrgIByMBL7nZ2dnZfqnPcAAAAA@brightview.co.uk>
<nnd$51f688df$79eeaed5@f984d21cd0a14d8f>
MIME-Version: 1.0
User-Agent: Pan/0.154 (Izium; 517acf4)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Message-ID: <1C6dnZup-smxWBL7nZ2dnZfqnPudnZ2d@brightview.co.uk>
Date: Thu, 11 Jul 2024 11:42:36 +0000
Lines: 15
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-IAouoDKysyPrOrSjVE4keflS2FR1ZOpf+8KqN1kKNVkfpVgrlWPmPvV2ioaWA2xTEVJ/wKJTepkM1MN!xgL5U8ixTvnCoP/lrTp7QU4/PiM+nZEdFa8klKIFip1/JBjzoUpUY5ChmyvccqvJUnEINyzM3ba2!X9mOmLPzvLMPUrGvkTT39sjGag==
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
View all headers

On Thu, 11 Jul 2024 13:12:52 +0200, Schelte wrote:

[snip]
> Or less hacky:
>
> proc make_file_menu {app} {
> .menu.file add command -command [list $app on_quit] \
> -label Quit -underline 0 -accelerator Ctrl+Q
> }
>
> Originally you had $app inside curly braces, which prevented it from
> being substituted at definition time. At execution time, the app
> variable was out of scope.

Thank you, that works great.

Subject: Re: How to pass an object from inside its own method to a helper function
From: Ralf Fassel
Newsgroups: comp.lang.tcl
Date: Thu, 11 Jul 2024 12:55 UTC
References: 1 2 3 4
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: ralfixx@gmx.de (Ralf Fassel)
Newsgroups: comp.lang.tcl
Subject: Re: How to pass an object from inside its own method to a helper function
Date: Thu, 11 Jul 2024 14:55:53 +0200
Lines: 31
Message-ID: <ygamsmo2ili.fsf@akutech.de>
References: <mwSdnbvrgID6NBL7nZ2dnZfqnPednZ2d@brightview.co.uk>
<mwSdnbrrgIByMBL7nZ2dnZfqnPcAAAAA@brightview.co.uk>
<nnd$51f688df$79eeaed5@f984d21cd0a14d8f>
<1C6dnZup-smxWBL7nZ2dnZfqnPudnZ2d@brightview.co.uk>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net 2q2zQunZ69nwrlgM/EmneAdMLbotZ+RNiPUHdLpSNV/AIIhH0=
Cancel-Lock: sha1:0D+F3XKm8tgGC4Lr6CNcn/BNOns= sha1:LAUIbj/h81K5VtWGFSN0+bmBkGg= sha256:lyd80+owSASgUOGL6sr1k56jhZpChQrKuz4kqomPVZs=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
View all headers

* Mark Summerfield <mark@qtrac.eu>
| On Thu, 11 Jul 2024 13:12:52 +0200, Schelte wrote:
| > proc make_file_menu {app} {
| > .menu.file add command -command [list $app on_quit] \
| > -label Quit -underline 0 -accelerator Ctrl+Q
| > }
| >
| > Originally you had $app inside curly braces, which prevented it from
| > being substituted at definition time. At execution time, the app
| > variable was out of scope.
>
| Thank you, that works great.

To save you trouble on your TCL-road ahead, make sure you understand the
difference between

-command {$app on_quit}

which you had originally in your code and

-command [list $app on_quit]

which is the correct way of doing things and

-command "$app on_quit"

which also most probably would "work" right now,
until some day it won't :-)...

HTH
R'

1

rocksolid light 0.9.8
clearnet tor