• Re: add additional subcommand for the "chan" tcl command?

    From Schelte@21:1/5 to All on Mon Jun 17 16:11:14 2024
    On 17/06/2024 15:20, aotto1968 wrote:
     I would like to add "chan exists str" just to signal a boolean return
    on exists and I want to do this
     *without* to touch the tcl distribution.

     there is already a feature like "::tcl:mathfunc::????" to add an additional function to "expr" and I
     ask me now it is possible to do such a kind of "addition" to the
    "chan" command as well ?

    The chan command is an ensemble, so you can add your own subcommands. It
    is a bit more work than adding a math function, but not too much:

    proc chanexists {fd} {
    return [expr {$fd in [chan names]}]
    }
    namespace ensemble configure chan -map [dict replace \
    [namespace ensemble configure chan -map] exists ::chanexists]

    After that, you get the following results:
    % chan help
    unknown or ambiguous subcommand "help": must be blocked, close,
    configure, copy, create, eof, event, exists, flush, gets, names,
    pending, pipe, pop, postevent, push, puts, read, seek, tell, or truncate
    % chan exists stdin
    1
    % chan exists nosuchchan
    0


    Schelte.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From aotto1968@21:1/5 to All on Mon Jun 17 15:20:35 2024
    Hi,

    I would like to add "chan exists str" just to signal a boolean return on exists and I want to do this
    *without* to touch the tcl distribution.

    there is already a feature like "::tcl:mathfunc::????" to add an additional function to "expr" and I
    ask me now it is possible to do such a kind of "addition" to the "chan" command as well ?


    mfg ao

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From aotto1968@21:1/5 to Schelte on Mon Jun 17 21:39:31 2024
    On 17.06.24 16:11, Schelte wrote:
    chan exists stdin

    question: is this possible with an additional level?

    proc chanexists {fd} {
    return [expr {$fd in [chan names]}]
    }
    namespace ensemble configure chan -map [dict replace \
    [namespace ensemble configure chan -map] exists ::chanexists]

    #chan help

    puts [chan exists stdin] ; # → OK

    # problem: "ensemble" is an ensemble IN the "namespace" ensemble
    # goal: namespace ensemble add ENSEMBLE NAME CMD

    proc ::namespace_ensemble_add {ensb name cmd} {
    namespace ensemble configure $ensb -map [dict replace \
    [namespace ensemble configure $ensb -map] $name $cmd]
    }

    → how I do this for a *two* level ensemble (ensemble in a ensemble) ?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From aotto1968@21:1/5 to Schelte on Mon Jun 17 21:21:18 2024
    thanks … the technology to add my own ensemble-command into an existing ensemble should be more used.


    On 17.06.24 16:11, Schelte wrote:
    On 17/06/2024 15:20, aotto1968 wrote:
      I would like to add "chan exists str" just to signal a boolean return on exists and I want to do this
      *without* to touch the tcl distribution.

      there is already a feature like "::tcl:mathfunc::????" to add an additional function to "expr" and I
      ask me now it is possible to do such a kind of "addition" to the "chan" command as well ?

    The chan command is an ensemble, so you can add your own subcommands. It is a bit more work than adding a math function, but not
    too much:

    proc chanexists {fd} {
        return [expr {$fd in [chan names]}]
    }
    namespace ensemble configure chan -map [dict replace \
      [namespace ensemble configure chan -map] exists ::chanexists]

    After that, you get the following results:
    % chan help
    unknown or ambiguous subcommand "help": must be blocked, close, configure, copy, create, eof, event, exists, flush, gets, names,
    pending, pipe, pop, postevent, push, puts, read, seek, tell, or truncate
    % chan exists stdin
    1
    % chan exists nosuchchan
    0


    Schelte.


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From aotto1968@21:1/5 to All on Tue Jun 18 08:10:47 2024
    ok… "namespace ensemble" is not an ensemble"
    namespace ensemble configure namespace -map =
    children = ::tcl::namespace::children
    code = ::tcl::namespace::code
    current = ::tcl::namespace::current
    delete = ::tcl::namespace::delete
    ensemble = ::tcl::namespace::ensemble
    eval = ::tcl::namespace::eval
    exists = ::tcl::namespace::exists
    export = ::tcl::namespace::export
    forget = ::tcl::namespace::forget
    import = ::tcl::namespace::import
    inscope = ::tcl::namespace::inscope
    origin = ::tcl::namespace::origin
    parent = ::tcl::namespace::parent
    path = ::tcl::namespace::path
    qualifiers = ::tcl::namespace::qualifiers
    tail = ::tcl::namespace::tail
    unknown = ::tcl::namespace::unknown
    upvar = ::tcl::namespace::upvar
    which = ::tcl::namespace::which
    namespace ensemble configure ::tcl::namespace::ensemble -map = "::tcl::namespace::ensemble" is not an ensemble command
    info body ::tcl::namespace::ensemble = "::tcl::namespace::ensemble" isn't a procedure

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)