• failing to understand/use lambda

    From Mark Summerfield@m.n.summerfield@gmail.com to comp.lang.tcl on Wed Jun 25 08:05:51 2025
    From Newsgroup: comp.lang.tcl

    I want to create a class which I can provide a "reporter" callable
    so it can notify me of what it is doing. Unfortunately, I can't get
    it to work and don't understand what I'm doing wrong.
    Below is a cut-down example to illustrate.

    package require lambda 1

    oo::class create Store {
    variable Filename
    variable Reporter
    }

    oo::define Store constructor {filename {reporter ""}} {
    set Filename $filename
    if {$reporter eq ""} {
    set Reporter [lambda {message} {}]
    } else {
    set Reporter $reporter
    }
    }

    oo::define Store method add {args} {
    puts "action: adding [llength $args] new files"
    $Reporter "adding [llength $args] new files"
    }

    set str [Store new test.dat [lambda {message} { puts "reporter: $message" }]]

    $str add one.txt two.txt three.txt

    Expected output:

    action: adding 3 new files
    reporter: adding 3 new files

    Actual output:

    action: adding 3 new files
    invalid command name "::apply {message { puts "reporter: $message" }}"
    while executing
    "$Reporter "adding [llength $args] new files""
    (class "::Store" method "add" line 3)
    invoked from within
    "$str add one.txt two.txt three.txt"
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Ralf Fassel@ralfixx@gmx.de to comp.lang.tcl on Wed Jun 25 11:02:09 2025
    From Newsgroup: comp.lang.tcl

    * Mark Summerfield <m.n.summerfield@gmail.com>
    | oo::define Store method add {args} {
    | puts "action: adding [llength $args] new files"
    | $Reporter "adding [llength $args] new files"
    | }
    --<snip-snip>--
    | Actual output:

    | action: adding 3 new files
    | invalid command name "::apply {message { puts "reporter: $message" }}"

    Note that I have no experience with lambda(n), but the error message
    sounds like you need

    {*}$Reporter "adding ..."

    when calling the reporter (i.e. expand the command being called).

    HTH
    R'
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Mark Summerfield@m.n.summerfield@gmail.com to comp.lang.tcl on Wed Jun 25 09:26:01 2025
    From Newsgroup: comp.lang.tcl

    On Wed, 25 Jun 2025 11:02:09 +0200, Ralf Fassel wrote:

    * Mark Summerfield <m.n.summerfield@gmail.com>
    | oo::define Store method add {args} {
    | puts "action: adding [llength $args] new files"
    | $Reporter "adding [llength $args] new files"
    | }
    --<snip-snip>--
    | Actual output:

    | action: adding 3 new files
    | invalid command name "::apply {message { puts "reporter: $message" }}"

    Note that I have no experience with lambda(n), but the error message
    sounds like you need

    {*}$Reporter "adding ..."

    when calling the reporter (i.e. expand the command being called).

    HTH
    R'

    Fabulous! That's all it took. _And_ I now understand the error message;
    Tcl was getting the entire string as the command, but using {*} broke
    it down into its constituents.

    PS I use the Pan newsreader and ever with wrap off it still messes up
    my posts when it comes to code (e.g., seemingly randomly ignoring
    newlines).
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Ralf Fassel@ralfixx@gmx.de to comp.lang.tcl on Wed Jun 25 11:47:22 2025
    From Newsgroup: comp.lang.tcl

    * Mark Summerfield <m.n.summerfield@gmail.com>
    | PS I use the Pan newsreader and ever with wrap off it still messes up
    | my posts when it comes to code (e.g., seemingly randomly ignoring
    | newlines).

    Your original post looked fine on my side (no
    additional/superfluous/missing newlines) ...

    R'
    --- Synchronet 3.21a-Linux NewsLink 1.2