Code covered by the BSD License  

Highlights from
NameSpace scoping operator

Be the first to rate this file! 1 Download (last 30 days) File Size: 5.63 KB File ID: #26221

NameSpace scoping operator

by Matt J

 

29 Dec 2009

Generates namespace scoping operators for interfacing with 3rd party mfile libraries

| Watch this File

File Information
Description

Sometimes, I will receive a directory full of many interdependent mfiles from a colleague. I must install all the files on the path for the main files to work, which means I must also worry that some of these files have the same name as one of my own and will result in file name conflict.

As a remedy, this FEX submission defines the NameSpace class, a user-defined class of namespace scoping operators. Objects of this class effectively allow different MATLAB directory search paths to act as namespaces, so that when a function in a particular colelction of directories is called, MATLAB will only see files in these directories while the function is executing. Hence, my colleague's mfile library can be executed in isolation from mine.
 
Although MATLAB defines package directories (of the form +mypackage/ ) for this purpose, I find this less convenient, because the package must be prepared with all packaged functions and variables referenced with the package scoping syntax mypackage.blablabla

Conversely, objects of class NameSpace can manipulate a conventionally written library of mfiles as its own self-contained package. To do so, the NameSpace object makes an automatic path change at the time of the function call to a temporary path that includes the library. After the function call is completed, the NameSpace object automatically restores the original path.
 
 The temporary search path contains the desired function library and is specified when the NameSpace object is constructed. The temporary path also contains native MATLAB files/functions but (in the default case) contains no other user-added directories. This means that, at the time of the function call, MATLAB will only be aware of user-defined mfiles in the temporary path, so that name conflict between files in the library and files on the original search path cannot occur.
 
 There are certain hazards/limitations to this scheme, as noted below, but in simple cases, it can provide a useful way to interface to 3rd party file libraries.
 
 
 EXAMPLE 1: Applying scoping operator to function
 
 
           Obj=NameSpace(DirName);
                    %Construct the object.
                    %DirName is the name of a directory containing,
                    %along with its subdirectories, a series of
                    %MATLAB function files.
  
           argout=Obj.func(arg1,arg2);
                    %func() is implemented in an mfile within DirName/
                    %or one of its subdirectories.
 
 This will invoke func(arg1,arg2), but after implictly changing the MATLAB path to include DirName/, its subdirectories, and native MATLAB directories.
  
 
 
 EXAMPLE 2: The above could also be done equivalently as follows
 
   
            Obj=NameSpace(DirName); %Construct object.
  
            newspace(Obj); %switches to temporary path
  
               argout=func(arg1,arg2);
  
            oldspace(Obj); %switches back to original path

 
 LIMITATIONS
 
 (1) Path switching is somewhat slow. Execution is slower than if these
     operations are done normally, without path switching.
 
 (2) If a function called through the NameSpace object Obj.f() is a
     path-altering operation, it will not work. The path always reverts to
     what it was at time of object construction.
 
 (3) Name conflict between classes cannot, unfortunately, be prevented. Once a class
     of a particular name is first referenced, MATLAB will only recognize this
     class definition, independently of whether this first reference occurs in
     the temporary function space, or the original one.
 
 (4) If execution crashes with an error during a call Obj.f(), the temporary path will
     not revert back to the original one. Use reset(Obj) or oldspace(Obj) to restore
     it.
 
 

MATLAB release MATLAB 7.9 (2009b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
namespace Matt J 30 Dec 2009 11:28:21
scope Matt J 30 Dec 2009 11:28:21
package Matt J 30 Dec 2009 11:28:21
scoping Matt J 30 Dec 2009 11:28:21
path Matt J 30 Dec 2009 11:28:21

Contact us at files@mathworks.com