[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: namespace support?
From: |
Leo Singer |
Subject: |
Re: namespace support? |
Date: |
Sun, 7 Feb 2010 00:29:18 -0800 |
Sorry, I forgot to cc the list on this.
How about the following import.m? Although I have not been able to
try it on the development version yet, it should work provided that
function handles off the path are supported. In a function that you
want to use just add
import mypackage
to bring all the functions in the "+mypackage" directory (anywhere on
the path) into the local scope
or
import mypackage.myfunction
to bring in just the function declared by "+mypackage/myfunction.m".
Leo
function import(varargin)
error(nargchk(1, inf, nargin, "struct"));
for i=1:nargin
[names, funcs] = import1(varargin{i});
for j=1:length(names)
assignin("caller", names{j}, funcs{j});
endfor
endfor
endfunction
function [names, funcs] = import1(pkgname)
pkgname_parts = strsplit(pkgname, ".");
if length(pkgname_parts) > 2
error("invalid package name: %s", pkgname);
endif
pkgpath = locatepkg(pkgname_parts{1});
unwind_protect
cwd = pwd;
cd(pkgpath);
names = what(pwd);
names = {names.m{:}, names.mex{:}, names.oct{:}};
names = cellfun(@stripExtension, names, "UniformOutput", false);
if length(pkgname_parts) == 2
if any(strcmp(pkgname_parts{2}, names))
names = {pkgname_parts{2}};
else
error("function `%s' not found in package `%s'", ...
pkgname_parts{2}, pkgname_parts{1});
endif
endif
funcs = cellfun(@str2func, names, "UniformOutput", false);
unwind_protect_cleanup
cd(cwd);
end_unwind_protect
endfunction
function pkgpath = locatepkg(pkgname)
pathdirs = strsplit(path, pathsep);
for iPath=1:length(pathdirs)
pkgpath = [pathdirs{iPath} filesep "+" pkgname];
if exist(pkgpath, "dir")
return;
endif
endfor
error("package `%s' cannot be located in the path", pkgname);
endfunction
function fileName = stripExtension(fileName)
dotIndices = strfind(fileName, ".");
fileName = fileName(1:(dotIndices(end)-1));
endfunction
On Sat, Feb 6, 2010 at 9:13 PM, Jaroslav Hajek <address@hidden> wrote:
> On Sun, Feb 7, 2010 at 1:45 AM, Leo Singer <address@hidden> wrote:
>> One could almost write an M-file function called "import" to implement
>> this in Octave without modifying the interpreter.
>>
>> It might work by creating function handles to functions that are not
>> on the path. However, attempting to invoke a function handle that
>> points to an M-file that is no longer on the path results in the
>> following error message:
>>
>> error: invalid function handle
>>
>> Matlab does support function handles to functions that are no longer
>> on the path. How hard would it be to implement this in Octave?
>>
>> Leo Singer
>>
>
> No, this is not true in the development version:
>
> lt-octave:1> a = @somefunc
> a =
>
> @somefunc
>
> lt-octave:2> a()
> somefunc
> lt-octave:3> cd ..
> lt-octave:4> a()
> somefunc
>
> But I think that "import" will be very easy once the support for the
> package lookup will be done. My idea is that calling just the name
> "package_name" will return a special "package" object, much like class
> constructors do, that can be further indexed to get functions or
> classes (because Octave allows this and always did).
>
> This will even enable us to do
>
> local_name = package_name;
> local_name.package_func ()
>
> The problem seems to be with function handles.
> In any case, I won't work on it prior to 3.4.0 release, and I'm not
> even sure it is a good idea given that I have no access to a Matlab
> copy having this feature. So maybe someone else will have to do it.
>
> --
> RNDr. Jaroslav Hajek, PhD
> computing expert & GNU Octave developer
> Aeronautical Research and Test Institute (VZLU)
> Prague, Czech Republic
> url: www.highegg.matfyz.cz
>