[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
namespace support revisited
From: |
jaeandersson |
Subject: |
namespace support revisited |
Date: |
Mon, 20 Jun 2016 16:59:09 -0700 (PDT) |
Hi,
I'd like to revisit the topic of namespace support, cf. the following thread
from early 2010:
http://octave.1599824.n4.nabble.com/namespace-support-td1638758.html
Has there been any progress on this since then? The proposed solution in
that thread is rather hacky and does not work if you try to alias a class
instead of a function. I think a proper "import.m" function can be
implemented much simpler than that, namely as follows:
function varargout = import(varargin)
persistent imported;
if ~iscell(imported)
imported = cell(0, 1);
end
if nargin==0
varargout{1} = imported;
else
for i=1:nargin
imported{end+1} = varargin{i};
end
end
end
This would need to be coupled to some mechanism that calls "import" with no
arguments whenever a symbol is missing. But I don't know how such a
mechanism can be implemented. Or if it can be implemented at all without
changing the Octave source.
I also came up with a hacky solution that works when you want to issue a
command such as "import mypackage.*", which unlike the solution in the older
thread handles the wildcard chararcters and (sort of) works when the package
contains classdef classes:
function import(varargin)
error(nargchk(1, inf, nargin, 'struct'));
for i=1:nargin
import1(varargin{i});
end
end
function import1(pkgname)
% Split up name into parts
pkgname_parts = strsplit(pkgname, '.');
% Fallback to system import command if not entire package (i.e. failure)
if length(pkgname_parts)~= 2 || ~strcmp(pkgname_parts{end}, '*')
error('Only the syntax ''import package_name.*'' is currently
supported')
end
% Get path for package
pkgpath = locatepkg(pkgname_parts{1});
% Add to path
addpath(pkgpath);
end
function pkgpath = locatepkg(pkgname)
pathdirs = strsplit(path, pathsep);
for iPath=1:length(pathdirs)
pkgpath = [pathdirs{iPath} filesep '+' pkgname];
if exist(pkgpath, 'dir')
return;
end
end
error('Package ''%s'' cannot be located in the path', pkgname);
end
But again, this is a hacky solution. I think that a proper solution, that
works for "import mypackage.*" as well as "import mypackage.myclass" and
"import mypackage.myfunction", should look like the first snippet above,
coupled to some mechanism for alias lookup.
Best regards,
Joel
--
View this message in context:
http://octave.1599824.n4.nabble.com/namespace-support-revisited-tp4677858.html
Sent from the Octave - General mailing list archive at Nabble.com.
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- namespace support revisited,
jaeandersson <=