0

I'd like to create a procedure that exists only within a scope of a different proc in TCL.
I.e. that just 1 proc can call it. Is it possible? According to this following link, no. :http://wiki.tcl.tk/463
But maybe someone knows another way to do it.
Thanks.

user1134991
  • 3,003
  • 2
  • 25
  • 35

2 Answers2

1

You can't limit procedures like that, but you can use a lambda term which is almost as good:

proc outside {a b c} {
    # Lambda terms are two- or three-element lists.
    set inside {{d e f} {
        return [expr {$d + $e * $f}]
    }}

    set total 0
    for {set i $a} {$i < $b} {incr i} {
        # Lambdas have to be explicitly applied with [apply]
        set total [apply $inside $total $c $i]
    }
    return $total
}

puts [outside 3 7 18]

First element of lambda: list of formal arguments (as for proc)
Second element of lambda: body (as for proc)
Third OPTIONAL element of lambda: context namespace, defaults to the global namespace (::)

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
0

The philosophy of Tcl is enabling rather than restricting. The programmer is trusted to do the right thing.

One can use namespaces, same-interpreter aliases, or OO to soft-restrict procedures. They can't be called by mistake, but they can still be accessed by deliberate action. Module procedures are often restricted this way: look at the code in e.g. struct::matrix to see how it can be done.

One can hard-restrict procedures by running the program in a sandbox, an interpreter that withholds a procedure completely or only permits it to be called under special circumstances, such as if called from a certain procedure.

(One can also write a procedure that simply checks who the caller is, but that's easy to spoof.)

Restricting access to a procedure by making its identifier local is a feature of lexical scoping, which Tcl doesn't use. The nearest corresponding mechanism is namespaces.

Peter Lewerin
  • 13,140
  • 1
  • 24
  • 27