Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A question about procedures

2 views
Skip to first unread message

ZB

unread,
Nov 12, 2007, 7:39:32 PM11/12/07
to
As I understand, the procedure names are always seen in global scope? I mean:

#v+
tclsh8.5 [~]proc aaa {} { puts "Hey" }
tclsh8.5 [~]aaa
Hey
tclsh8.5 [~]proc bbb {} { proc ccc {} { puts "Hi" } ; ccc }
tclsh8.5 [~]aaa
Hey
tclsh8.5 [~]bbb
Hi
tclsh8.5 [~]ccc
Hi
tclsh8.5 [~]
#v-

So, although I was trying to set "local" procedure ccc, it'll be still
seen "outside"?
--
ZB

tom.rmadilo

unread,
Nov 12, 2007, 8:25:41 PM11/12/07
to

What is a local procedure? What you have done is to use a procedure to
create another procedure. Very useful, but it would be totally useless
if it was local to the procedure doing the creating. The procedure is
located in the namespace given by the name you give the proc.

ZB

unread,
Nov 12, 2007, 8:06:40 PM11/12/07
to
Dnia 13.11.2007 tom.rmadilo <tom.r...@gmail.com> napisał/a:

> What is a local procedure?

Kind of analogue of global/local variable.

> What you have done is to use a procedure to create another procedure.

It was just a (very) simplistic example. Of course, the "bbb" procedure
should contain a lot more code, beside the definition of "ccc" procedure.

> Very useful, but it would be totally useless if it was local to the
> procedure doing the creating. The procedure is located in the namespace
> given by the name you give the proc.

What I tried to make was to enclose some block of code as "local" to another
block - just because I know in advance, that it never will be used somewhere
else. So, according to the rule "to not pollute the global namespace" I was
hoping, that "ccc" procedure will not be seen outside "bbb".

Not a big problem, it still is, anyway.
--
ZB

miguel

unread,
Nov 12, 2007, 8:52:03 PM11/12/07
to

If you want ccc to disappear when bbb returns, and you are using 8.5,
[apply] is your friend:

proc bbb {} {
set ccc [list ::apply [list {} {puts Hi]]
$ccc
}

See http://www.tcl.tk/man/tcl8.5/TclCmd/apply.htm

tom.rmadilo

unread,
Nov 12, 2007, 8:56:00 PM11/12/07
to
On Nov 12, 5:06 pm, ZB <zbREMOVE_THIS@AND_THISispid.com.pl> wrote:
> Dnia 13.11.2007 tom.rmadilo <tom.rmad...@gmail.com> napisa³/a:


A procedure named ::my::procedure is not in the global namespace, it
is in the ::my namespace. Your reasoning would mean that pointers
pollute some global namespace in C.

What you really want to look at is namespaces. And, also forget about
other languages and what they do. You'll likely go crazy and write
another OO extension.

You can do some interesting things in Tcl with namespaces, but like
lambda, there is zero reason to think in terms of local procedures. If
the point of writing procedures is reuse, why scope the procedure so
narrowly? Also, any procedure which requires such a construct is by
definition too difficult to maintain, for instance I usually branch
out loops or switch bodies to helper procs, but adding more junk into
a procedure body is not the way to develop, just like you should not
execute [proc] commands inside a [namespace eval].

Of course developers can do whatever they want, fortunately Tcl makes
it difficult to do useless things like this.

0 new messages