Tcl namespaces and private procedures and variables

Ryan Schmidt ryandesign at macports.org
Mon Oct 8 22:18:08 UTC 2018


What's the correct way to make a private procedure or variable in a portgroup -- something the portgroup will use internally but that portfiles using the portgroup should not use?

I think the answer is to use a namespace. The xcode-1.0 portgroup does:

namespace eval xcode {}
proc xcode::get_project_path {} {
    ...
}

The meson-1.0 portgroup defines its procedure directly within the namespace:

namespace eval meson {
    proc get_post_args {} {
        ...
    }
}

The cmake-1.1 portgroup defines a variable in a namespace:

namespace eval cmake {
    variable currentportgroupdir ...
}


I didn't understand namespaces and so I avoided them in my own portgroups, so for example my compiler_blacklist_versions-1.0 portgroup has procedures like:

proc compiler_blacklist_versions._matches_all_versions

The "_" prefix on the procedure name was meant to convey that it's for internal use only, but if namespaces are what we had been using for that already, I'd like to switch to using that. It looks like only 5 out of 60 portgroups use namespaces; maybe we have a lack of information about namespaces preventing the adoption of this feature. I don't see any discussion of Tcl namespaces in the MacPorts Guide.


Is there anything else I should know about using namespaces in Tcl generally or in MacPorts specifically?


Neither a namespace nor the "_" prefix I was using cause the procedures or variables to actually *be* private, as far as I can tell. Ports can still use them if they want to. If I understand correctly, the idea in MacPorts is just to convey which procedures and variables are meant to be internal and whose functionality or existence might change and should not be relied upon by outside callers.



More information about the macports-dev mailing list