File: rosetta-unknown-method.tcl

package info (click to toggle)
nsf 2.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 12,628 kB
  • sloc: ansic: 32,245; tcl: 10,636; sh: 664; pascal: 176; lisp: 41; makefile: 24
file content (38 lines) | stat: -rw-r--r-- 913 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#
# == Rosetta Example: Respond to an unknown method call 
# For details see https://rosettacode.org/wiki/Respond_to_an_unknown_method_call
#
package req nx
package req nx::test

#
# Define a class Example modelled after the 
# Python version of Rosetta:
#

nx::Class create Example {
  
  :public method foo {} {return "This is foo."}
  :public method bar {} {return "This is bar."}

  :method unknown {method args} {
    set result "Tried to handle unknown method '$method'."
    if {[llength $args] > 0} {
      append result " It had arguments '$args'."
    }
    return $result
  }
}

# === Demonstrating the behavior in a shell: 
#
# Create an instance of the class Example:
? {set e [Example new]} "::nsf::__#0"

? {$e foo} "This is foo."

? {$e bar} "This is bar."

? {$e grill} "Tried to handle unknown method 'grill'."

? {$e ding dong} "Tried to handle unknown method 'ding'. It had arguments 'dong'."