link(n)

πŸ“¦
version 9.0.3 Β· 3 versions
, in alpine 3.23, alpine 3.24, nixos 26.05

okay so bestie, welcome to the link(n) page from the TclOO Commands manual. This one is literally about making methods callable as commands inside your methods, no cap. Think less β€œraw object name” and more β€œshortcut era.” ✨

🌸 NAME 🌸

link β€” create a link from a command to a method of an object 🫢

πŸŽ€ SYNOPSIS πŸŽ€

package require tcl::oo

link methodName ?...?
link {commandName methodName} ?...?

πŸ’… DESCRIPTION πŸ’…

The link command is available inside methods. It takes a series of one or more method names (methodName ...) and/or pairs of command-name and method-name ({commandName methodName}), and it makes those named methods available as commands without making you spell out the object name or use the my command. Iconic. πŸ‘‘

The method does not need to exist at the time the link is made, bestie. If link is invoked when the method does not exist, the standard unknown method handling system is used. No extra drama invented here. πŸ’€

The command name under which the method becomes available defaults to the method name, except where it is explicitly specified through an alias/method pair. Formally, every argument must be a list: if the list has two elements, the first element is the name of the command to create and the second element is the name of the method of the current object to which the command links; otherwise, the name of the command and the name of the method are the same string (the first element of the list).

If the name of the command is not a fully-qualified command name, it will be resolved with respect to the current namespace (i.e., the object namespace). Slay. ✨

πŸ¦‹ EXAMPLES πŸ¦‹

This demonstrates linking a single method in various ways. First it makes a simple link, then a renamed link, then an external link. Note that the method itself is unexported, but it can still be called directly from outside the class. So yes, it’s giving sneaky-but-legal. 😌

oo::class create ABC {
    method Foo {} {
        puts "This is Foo in [self]"
    }

    constructor {} {
        link Foo
        # The method Foo is now directly accessible as Foo here
        link {bar Foo}
        # The method Foo is now directly accessible as bar
        link {::ExternalCall Foo}
        # The method Foo is now directly accessible in the global
        # namespace as ExternalCall
    }

    method grill {} {
        puts "Step 1:"
        Foo
        puts "Step 2:"
        bar
    }
}

ABC create abc
abc grill
        β†’ Step 1:
        β†’ This is Foo in ::abc
        β†’ Step 2:
        β†’ This is Foo in ::abc
# Direct access via the linked command
puts "Step 3:"; ExternalCall
        β†’ Step 3:
        β†’ This is Foo in ::abc

This example shows that multiple linked commands can be made in a call to link, and that they can handle arguments. Multi-link slay. πŸŽ€

oo::class create Ex {
    constructor {} {
        link a b c
        # The methods a, b, and c (defined below) are all now
        # directly accessible within methods under their own names.
    }

    method a {} {
        puts "This is a"
    }
    method b {x} {
        puts "This is b($x)"
    }
    method c {y z} {
        puts "This is c($y,$z)"
    }

    method call {p q r} {
        a
        b $p
        c $q $r
    }
}

set o [Ex new]
$o call 3 5 7
        β†’ This is a
        β†’ This is b(3)
        β†’ This is c(5,7)

πŸ‘€ SEE ALSO πŸ‘€

πŸ”‘ KEYWORDS πŸ”‘

  • command
  • method
  • object

okay that’s the whole link(n) vibe, bestie. Signing off with the footer facts: this is from TclOO, version 0.3, and the footer doesn’t show a date β€” so no fake date energy from me, periodt. πŸ’œπŸŒˆ

πŸ“¦ where this page comes from