Index: TODO =================================================================== diff -u -r562cc31a003aac134390dd343849f428994186f1 -r40c99702db40bd86761bfea1f1209cc761e61e62 --- TODO (.../TODO) (revision 562cc31a003aac134390dd343849f428994186f1) +++ TODO (.../TODO) (revision 40c99702db40bd86761bfea1f1209cc761e61e62) @@ -3162,12 +3162,18 @@ set x [o copy] - extended regression test -TODO: - - implementation of [C copy] (without a 2nd argument, - create object via new) - - look at "require method" + public / protected - => "require public method"? +- nx.tcl: + * added protected and public for "require method" + The following forms are now valid + "... require public method" + "... require protected method" + "... require method" + "... require public class method" + "... require protected class method" + "... require class method" + * extended regression test +TODO: - zzz why is the method recompiled for /tmp/sp.tcl ? debug output with VAR_RESOLVER_TRACE ================================================= Index: generic/nsf.tcl =================================================================== diff -u -rc6057c18970d5bc19fe0f1f760ef0d29898ecfdd -r40c99702db40bd86761bfea1f1209cc761e61e62 --- generic/nsf.tcl (.../nsf.tcl) (revision c6057c18970d5bc19fe0f1f760ef0d29898ecfdd) +++ generic/nsf.tcl (.../nsf.tcl) (revision 40c99702db40bd86761bfea1f1209cc761e61e62) @@ -22,6 +22,14 @@ } proc ::nsf::method::require {object name {per_object 0}} { + # + # On a method require, the optional script is evaluated and the + # "definition" gets inserted + # - on posiiton 1 the actual object + # - on posiiton 2 optionally "-per-object" + # + # The definition cmd must return the method handle. + # set key ::nsf::methodIndex($name) if {[info exists $key]} { array set "" [set $key] Index: library/nx/nx.tcl =================================================================== diff -u -r562cc31a003aac134390dd343849f428994186f1 -r40c99702db40bd86761bfea1f1209cc761e61e62 --- library/nx/nx.tcl (.../nx.tcl) (revision 562cc31a003aac134390dd343849f428994186f1) +++ library/nx/nx.tcl (.../nx.tcl) (revision 40c99702db40bd86761bfea1f1209cc761e61e62) @@ -392,14 +392,29 @@ Object method require {what args} { switch -- $what { + public - + protected { + set next [lindex $args 0] + if {$next ni {"class" "method"}} { + error "public or procected must be followed by 'class' or 'method'" + } + set result [:require {*}$args] + #puts stderr "[list :require {*}$args] => $result" + ::nsf::method::property [self] $result call-protected [expr {$what eq "protected"}] + return $result + } class { set what [lindex $args 0] - if {$what eq "method"} { - ::nsf::method::require [::nsf::self] [lindex $args 1] 1 + if {$what ne "method"} { + error "'class' must be followed by 'method'" } + set methodName [lindex $args 1] + ::nsf::method::require [::nsf::self] $methodName 1 + return [:info lookup method $methodName] } method { - ::nsf::method::require [::nsf::self] [lindex $args 0] 0 + set methodName [lindex $args 0] + return [::nsf::method::require [::nsf::self] $methodName 0] } namespace { ::nsf::object::dispatch [::nsf::self] ::nsf::methods::object::requirenamespace Index: tests/method-require.test =================================================================== diff -u -rc6057c18970d5bc19fe0f1f760ef0d29898ecfdd -r40c99702db40bd86761bfea1f1209cc761e61e62 --- tests/method-require.test (.../method-require.test) (revision c6057c18970d5bc19fe0f1f760ef0d29898ecfdd) +++ tests/method-require.test (.../method-require.test) (revision 40c99702db40bd86761bfea1f1209cc761e61e62) @@ -19,7 +19,16 @@ nsf::method::provide tcl::set {::nsf::method::alias set -frame object ::set} nsf::method::provide exists {::nsf::method::alias exists ::nsf::methods::object::exists} nsf::method::provide foo {::nsf::method::create foo {x y} {return x=$x,y=$y}} - nsf::method::provide x {::nsf::mixin ::MIX} { + + # + # Provide an example for an application defined method provider + # + nsf::proc ::get_mixin {object -per-object:switch mixinClass methodName} { + ::nsf::mixin $object -per-object=${per-object} $mixinClass + return [$mixinClass info method handle $methodName] + } + # use the method provider + nsf::method::provide x {::get_mixin ::MIX x} { # here could be as well a package require, etc. ::nx::Class create ::MIX {:public method x {} {return x}} } @@ -34,26 +43,54 @@ # required names can be different from registered names; if there # are multiple set methods, we could point to the right one - :require method tcl::set + ? [list [self] require method tcl::set] "::nsf::classes::C::set" - # object methods: - :require class method lappend + # class methods + ? [list [self] require class method lappend] "::C::lappend" - # a scripted method - :require class method foo + # a scripted class method + ? [list [self] require class method foo] "::C::foo" + ? [list [self] require class method x] "::nsf::classes::MIX::x" + ? [list [self] require method x] "::nsf::classes::MIX::x" - :require class method x - # looks as well ok: - :require namespace + ? [list [self] require namespace] "" } + + # + # Try protected and public + # + ? {C require public method lappend} ::nsf::classes::C::lappend + ? {::nsf::method::property C lappend call-protected} 0 + + ? {C require protected method lappend} ::nsf::classes::C::lappend + ? {::nsf::method::property C lappend call-protected} 1 + ? {C require protected class method set} ::C::set + ? {::nsf::method::property C ::C::set call-protected} 1 + # + # call these methods + # C create c1 ? {c1 set x 100} 100 ? {c1 exists x} 1 ? {C lappend some_list e1 e2} "e1 e2" ? {C foo 1 2} x=1,y=2 ? {C x} x + + # + # Definitions directly on object + # + Object create o1 + ? {o1 require method set} ::o1::set + ? {o1 require method x} ::nsf::classes::MIX::x + + ? {o1 require public method lappend} ::o1::lappend + ? {::nsf::method::property o1 lappend call-protected} 0 + + ? {o1 require protected method lappend} ::o1::lappend + ? {::nsf::method::property o1 lappend call-protected} 1 + } nx::Test case parent-require { @@ -90,5 +127,4 @@ nx::Object create o ::nsf::method::require o alloc ? {o alloc x} {Method alloc not dispatched on valid class} - -} \ No newline at end of file +}