Doubles are not objrefs
double x[n]
declares an array of doubles.
objref y[n]
declares n objrefs whose names are y[0]...y[n-1].
I though all variables were by default doubles?
False. To paraphrase Orwell, "the slovenliness of our language makes it easier tor us to
make programming errors."
The type of a user-defined symbol is determined by its first appearance in a program.
Code: Select all
oc>y = sin(0.1) // first appearance of y
oc>y
0.099833417
oc>y[0]
/usr/local/nrn/i686/bin/nrniv: y not an array variable
near line 9
y[0]
^
So y is a scalar with double precision. The declaration
double y[n]
would make y be an array of n double precision values. Although variables that are
explicitly declared with
double varname
syntax are actually arrays, they are generally called "doubles".
objrefs are objrefs, not scalars or arrays of scalars.
Code: Select all
oc>objref z
oc>z = x[0]
bad stack access: expecting (Object **); really (double)
/usr/local/nrn/i686/bin/nrniv: interpreter stack type error
near line 14
z = x[0]
^
oc>z = y
bad stack access: expecting (Object **); really (double)
/usr/local/nrn/i686/bin/nrniv: interpreter stack type error
near line 15
z = y
^
hoc has arrays and Vectors as an accident of history. It started as a toy programming
language with a c-like syntax in the mid 1970s, at which time it had scalars and arrays of
scalars. Objects (which include Vectors) were added later.
Finally, how do I declare an array without stating how many elements it has and/or adding elements on the fly?
To the extent possible, avoid using "doubles" (variables declared with
double varname
syntax). Stick with Vectors. The Vector class has several methods for changing the
size of a Vector--append, insrt, resize. Read about them here
http://www.neuron.yale.edu/neuron/stati ... tml#Vector
For the greatest flexibility in dealing with collections of things, it is best to use the List class--
see
http://www.neuron.yale.edu/neuron/stati ... .html#List