[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
namespaces, etc.
From: |
John W. Eaton |
Subject: |
namespaces, etc. |
Date: |
Fri, 01 Feb 2008 02:48:23 -0500 |
On 29-Jan-2008, Przemek Klosowski wrote:
| This posting is inspired by a recent request from the help list, asking
| for a way to load into octave variables that are named at runtime. The
| orginal idea was to eval([varName '=' varValue]), and I suggested using
|
| myVars.(varName)=varValue
|
| I commented that this looks like a namespace, and then I spoke to Paul
| Kienzle and we came up with the idea that this technique is useful to
| import foreign namespace into local function symbol table, like so:
|
| function y=f(myVars,x) <-- pass the 'namespaced' variables, or use a global
var
| for [val,key]=myVars
| eval([key '=' val])
| endfor
|
| i.e. make a caller-defined related variables available in local
| variables of the function. The problem with the above approach is that
| it doesn't work out the box: val is a numerical object, and needs to
| be massaged before the eval() argument is concatenated: eval([key '='
| num2str(val)]), or even something more complex for array values.
|
| Paul suggested that things like that would simplify if the local
| symbol table could be accessed as a struct:
|
| function y=f(myVars,x)
| for [val,key]=myVars
| __locals__.(key) = val;
| endfor
| or even
| __locals__ = myVars
|
| Anyway, I thought it is an interesting idea, which would simplify
| moving state variables into and out of subroutines. Maybe even it
| could be useful in some way for your properties database in the
| graphics subsystem? I haven't even began looking at the implementation
| issues--I am just sending this for a reality check.
I could be missing something, but I don't see a simple way to expose
the symbol table (or even just the variables in the current scope) as
a structure that would work for both reading and writing. It might be
possible to do it, but I don't think it would be trivial given the
current way that symbols are stored and structures in Octave work.
OTOH, I think it would be fairly easy to provide functions like
create_symbol (NAME) ## place NAME in current symbol table scope
symbol_value (NAME) ## value of symbol in current scope (could be
## variable, function, private function, class
## method, etc.)
variable_value (NAME) ## like symbol_value, but only look at the
## variable slot corresponding to the symbol
## NAME in the current scope
set_variable (NAME, VALUE) ## set the variable slot corresponding
## to the symbol NAME in the current
## scope to VALUE
and so on (there might be other useful get/set functions in addition
to these).
Perhaps it's obvious, but I think I should point out that a function
that adds an arbitrary symbol name to the symbol table would cause
trouble for an Octave compiler, the same as eval would.
jwe
- namespaces, etc.,
John W. Eaton <=