tcl

TclOO : Export/Import Class


Is there any way to export my class, like we can do for a command with namespace export/import ?
My intention is to be able to use the name of my class without its namespace.

namespace eval ns {}
oo::class create ns::myCLass {...}

proc ns::myfunc {} {
    set c [myCLass new] ; It works
    return $c
}

# it doesn't work, outside my namespace 
set c [myCLass new]

Solution

  • You can use namespace export in the namespace to mark myCLass as an exportable name, and then use namespace import in the namespace you want to use it with an unqualified name in.

    namespace eval ns {
        # Can be used with names that haven't been defined yet
        namespace export myCLass
    }
    
    oo::class create ns::myCLass {}
    
    namespace import ns::myCLass
    set c [myCLass new]