Version 1 of my oh my

Updated 2014-06-12 02:06:47 by pooryorick

my oh my is a response to a question by jbr in Ask, and it shall be given # 11

In tcloo can I create a new "slot" type class variable?

Specifically I'd like to create a "method" declaration adjective, similar to "public" and "private" from JBR's tcloo.tcl. The adjective I'm thinking of is "linked" that will perform the function of the "procs" procedure in tcloo.tcl, and is called "link" in some other code (forgot where (PYK: probably in ooutil)). It links procs in an object's namespace to the class methods such that they can be called as commands.

The current implementations require the set of linked methods to be mentioned twice, once in their method declarations and once in the link/procs call in the class constructor. I'd like something like:

    linked method name { args } { body }

The linked method would add the method name to the classes linked slot and then at constructor time (maybe automatically) the links would be made using the "linked" slot list. Is it possible to override or filter the class constructor to handle this automatic at constructor time part? Without subclassing??

Ideas??

Thanks - JBR

my oh my

PYK 2014-06-11: I put together a mixin class that tries to do what you've described. The purpose of string map here is to find a nice private location for the bookkeeping that is done to track the my methods. It's a little ugly, but hey, it works.

Using namespace uknown as the redirector was the only way I could find at the script level to get this job done.

Overloading the word my for this purpose leads to a heaping helping of my, but other ideas such as my! and nomy seemed even more forced. Better ideas are welomce, and anyone wants to improve this code, please just dig in!

oo::class create my

{*}[string map [list {{{my}}} [list [
    info object namespace [::oo::object new]]]] {apply {{} {

    oo::define my constructor args {
        namespace eval [self namespace] {
            namespace unknown [list apply [list args {
                set clsns [uplevel {info object namespace [self class]}]
                if {[lindex $args 0] in [set {{my}}::${clsns}::my]} {
                    return [uplevel [list my {*}$args]]
                }
                return -code error [list {not found} $args]
            }]]
        }
    }

    proc oo::define::my {method name margs body} {
        uplevel [list method $name $margs $body]
        set clsname [lindex [info level -1] 1]
        set ns [uplevel [list info object namespace $clsname]]
        namespace eval {{my}}::$ns {}
        lappend {{my}}::${ns}::my $name

    }
}}}]

example:

oo::class create class1
oo::define class1 {
    mixin -append my
    my method method1 args {
        return {hi from method1}
    }
    method method2 args {
        method1
    }
}

class1 create inst1
puts [inst1 method2]