variable names and data types

The basics of how to develop, test, and use models.
Post Reply
JohnAnderson
Posts: 8
Joined: Sun Jun 12, 2005 10:41 am
Location: University of North Florida

variable names and data types

Post by JohnAnderson »

I have a couple of questions about the following hoc code and a modified version of it:

objectvar f, randx
f = new File()
f.ropen("randnums/rsx0.dat")
irand = f.scanvar()
randx = f.scanvar()

The code above, extracted from a larger program based on demofig1.hoc from patdemo.zip, gives the following error when I run the simulation from mosinit.hoc within a nrnzip file:

bad stack access: expecting (Object **); really (double)
nrniv: interpreter stack type error

The modified version of it below, however, does not give this error:

objectvar f
f = new File()
f.ropen("randnums/rsx0.dat")
irand = f.scanvar()
irandx = f.scanvar()

So it looks like randx is automatically declared as a double, whereas irandx is not. Is this correct?

I haven't been able to find information in the documentation about the rules concerning variable names and data types. Is there some documentation where really basic things like this are covered?

Thanks for your help...

ja
ted
Site Admin
Posts: 5810
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: variable names and data types

Post by ted »

it looks like randx is automatically declared as a double, whereas irandx is not. Is this correct?
No. There are two differences between the code examples. In the first example randx is declared to be an objref
objectvar f, randx
then (mis)used in an assignment statement
randx = f.scanvar()
whose RHS returns a scalar instead of an objref. In the second example irandx is _not_ declared to be an objref, therefore is treated as a scalar when it first appears in the assignment statement
irandx = f.scanvar()
and no error results.
rules concerning variable names and data types
The basic types are scalars, arrays of scalars, and objrefs. A scalar can be redefined as an array of scalars, e.g.

Code: Select all

oc>x = 3  // creates scalar x and assigns a value to it
first instance of x
oc>print x
3 
oc>double x[2] // produces no error msg, but x now means x[0] and old value is discarded
oc>print x, x[0]
0 0
However, once a variable has been used as a scalar, it cannot be redeclared as an objref, nor can a variable declared as an objref be used later as a scalar. Ditto for strdefs.

Chapters 12 and 13 of The NEURON Book go into much more detail about hoc syntax and object oriented programming in hoc, but much of this information is also in the Programmer's Reference. A good place to start is the hierarchical index of topics
http://www.neuron.yale.edu/neuron/stati ... /hier.html
especially the items in this part of the table:

Code: Select all

      neuron.exe
         general
            ObjectOrientedProgramming  functions  syntax  
            button-menu-panel  keywords   
            classes        predeclared-variables  
         neuron
            CompileTimeOptions  functions  keywords      
            Section        globals        mechanisms     
            classes        ion            pointprocesses
Post Reply