Index: doc/next-tutorial.html =================================================================== diff -u -rb3018d3be0f1524a3f1709edc0e2ddb5d8bc4c0b -r542e84bd66ce4c3cd28e4ba1fd41f2151d8cb043 --- doc/next-tutorial.html (.../next-tutorial.html) (revision b3018d3be0f1524a3f1709edc0e2ddb5d8bc4c0b) +++ doc/next-tutorial.html (.../next-tutorial.html) (revision 542e84bd66ce4c3cd28e4ba1fd41f2151d8cb043) @@ -2323,6 +2323,157 @@

+ + + + +
+

4. Miscellaneous

+
+
+

4.1. Unknown Handlers

+

NX provides two kinds of unknown handlers:

+
    +
  • +

    +Unknown handlers for methods +

    +
  • +
  • +

    +Unknown handlers for objects and classes +

    +
  • +
+
+

4.1.1. Unknown Handlers for Methods

+

Object and classes might be equipped +with a method unknown which is called in cases, where an unknown +method is called. The method unknown receives as first argument the +called method followed by the provided arguments

+
Listing 29: Unknown Method Handler

+
+
+
  1
+  2
+  3
+  4
+  5
+  6
+  7
+  8
+  9
+ 10
+
::nx::Object create o {
+  :method unknown {called_method args} {
+    puts "Unknown method '$called_method' called"
+  }
+}
+
+# Invoke an unknown method for object o:
+o foo 1 2 3
+
+# Output will be: "Unknown method 'foo' called"
+

Without any provision of an unknown method handler, an error will be +raised, when an unknown method is called.

+
+
+

4.1.2. Unknown Handlers for Objects and Classes

+

The next scripting framework provides in addition to unknown method +handlers also a means to dynamically create objects and classes, when +these are referenced. This happens e.g. when superclasses, mixins, or +parent objects are referenced. This mechanism can be used to implement +e.g. lazy loading of these classes. Nsf allows to register multiple +unknown handlers, each identified by a key (a unique name, different +from the keys of other unknown handlers).

+
Listing 30: Unknown Class Handler

+
+
+
  1
+  2
+  3
+  4
+  5
+  6
+  7
+  8
+  9
+ 10
+ 11
+ 12
+ 13
+ 14
+ 15
+ 16
+ 17
+ 18
+ 19
+ 20
+ 21
+ 22
+
::nx::Class public class method __unknown {name} {
+  # A very simple unknown handler, showing just how
+  # the mechanism works.
+  puts "***** __unknown called with <$name>"
+  ::nx::Class create $name
+}
+
+# Register an unknown handler as a method of ::nx::Class
+::nsf::unknown::add nx {::nx::Class __unknown}
+
+::nx::Object create o {
+  # The class M is unknown at this point
+
+  :mixin add M
+  # The line above has triggered the unknown class handler,
+  # class M is now defined
+
+  puts [:info mixin classes]
+  # The output will be:
+  #     ***** __unknown called with <::M>
+  #     ::M
+}
+

The Next Scripting Framework allows to add, query, delete abd list unknown handlers.

+
Listing 31: Unknown Handler registration

+
+
+
  1
+  2
+  3
+  4
+  5
+
# Interface for unknown handlers:
+# nsf::unknown::add /key/ /handler/
+# nsf::unknown::get /key/
+# nsf::unknown::delete /key/
+# nsf::unknown::keys
References
  • @@ -2406,7 +2557,7 @@