Local object references

Anything that doesn't fit elsewhere.
Post Reply
miller

Local object references

Post by miller »

Hi

Can I declare local object references in procedures and functions? Something like

proc p() { local objref ref
}

??

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

define a new class to hide an objref in a proc or func

Post by ted »

Unfortunately that won't work. The first declaration that a name is an objref
must occur outside of a proc or func. Once a name has been declared an
objref, it can appear in another objref statement inside a proc or func, like this

Code: Select all

objref ref

proc p() {
  objref ref
}
If you really need to hide an object that is used inside a func or proc, put the func or
proc inside a new object class and make it public, but leave the objref private.

Code: Select all

begintemplate Hiddenobject
  public p, test
  objref ref, null  // these will be private

  proc p() { local i
    // let's do something we can test later
    ref = new List()
    for i=0,$1-1 ref.append(new Vector())
  }

  proc test() { local i
    print "ref is ", ref
    if (ref!=null) {
      print "its members are"
      for i=0,ref.count()-1 print i, ref.object(i)
    }
  }
endtemplate Hiddenobject

objref foo
foo = new Hiddenobject()

print " "
print "Before calling foo.p(3)"
foo.test()
print " "

foo.p(3)
print " "

print "After calling foo.p(3)"
foo.test()

print " "
print "Now convince yourself that ref is hidden"
print "by typing ref or foo.ref at the oc> prompt."
miller

Hidden objref

Post by miller »

Thanks a lot for your answer.
It is a really good workaround.
ted
Site Admin
Posts: 6305
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Post by ted »

Glad to help. Thanks for submitting your question to the NEURON
Forum, where it can benefit a wide audience.

Please be sure to cite NEURON if your work results in publications.
Of the papers currently listed at
http://www.neuron.yale.edu/neuron/bib/nrnpubs.html
the best general-purpose citation would be one of these:

Hines, M.L. and Carnevale, N.T. The NEURON simulation environment.
In: The Handbook of Brain Theory and Neural Networks, 2nd ed, edited by
M.A. Arbib. Cambridge, MA: MIT Press, 2003, pp. 769-773

Hines, M.L. and Carnevale, N.T. NEURON: a tool for neuroscientists.
The Neuroscientist 7:123-135, 2001

Hines, M.L. and Carnevale, N.T. The NEURON simulation environment.
Neural Computation 9:1179-1209, 1997
Johan

Re: define a new class to hide an objref in a proc or func

Post by Johan »

ted wrote: If you really need to hide an object that is used inside a func or proc, put the func or
proc inside a new object class and make it public, but leave the objref private.
In my build, Neuron 5.7.159, I can declare local objects within procedures and functions. This is done with the identifier localobj. It works like a charm and has been a VERY helpfull extension to NEURON. The following hoc code:

Code: Select all

objref s
s = new String("I am global!")

proc foo1(){ localobj s 
  s = new String("I am local!") 
  print s.s
}

proc foo2(){ 
  print s.s
}

foo1()
foo2()
will produce the followig output:
I am local!
I am global!

Cheers Johan
miller

Extensions

Post by miller »

That's really cool.
Another cool extension would be static localobj references like in C/C++ :-)
eacheon
Posts: 97
Joined: Wed Jan 18, 2006 2:20 pm

Re: define a new class to hide an objref in a proc or func

Post by eacheon »

Johan wrote:
ted wrote: If you really need to hide an object that is used inside a func or proc, put the func or
proc inside a new object class and make it public, but leave the objref private.
In my build, Neuron 5.7.159, I can declare local objects within procedures and functions. This is done with the identifier localobj. It works like a charm and has been a VERY helpfull extension to NEURON. The following hoc code:

Code: Select all

objref s
s = new String("I am global!")

proc foo1(){ localobj s 
  s = new String("I am local!") 
  print s.s
}

proc foo2(){ 
  print s.s
}

foo1()
foo2()
will produce the followig output:
I am local!
I am global!

Cheers Johan
Why did this get removed in NEURON 5.8?
ted
Site Admin
Posts: 6305
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: define a new class to hide an objref in a proc or func

Post by ted »

eacheon wrote:Why did this get removed in NEURON 5.8?
??
It works in 5.8. Have you encountered an instance in which it doesn't?
eacheon
Posts: 97
Joined: Wed Jan 18, 2006 2:20 pm

Re: define a new class to hide an objref in a proc or func

Post by eacheon »

ted wrote:
eacheon wrote:Why did this get removed in NEURON 5.8?
??
It works in 5.8. Have you encountered an instance in which it doesn't?
Oh, I thought it is removed. In my case it does not work:

Code: Select all

20:29:53 xxw@alots:~/nrn/lgntc$ nrniv
NEURON -- Version 5.8 2005-10-14 12:36:20 Main (88)
by John W. Moore, Michael Hines, and Ted Carnevale
Duke and Yale University -- Copyright 1984-2005

oc>objref s
oc>s = new String("I am global!")
nrniv: String is not a template
 near line 2
 s = new String("I am global!")
eacheon
Posts: 97
Joined: Wed Jan 18, 2006 2:20 pm

Re: define a new class to hide an objref in a proc or func

Post by eacheon »

eacheon wrote:
ted wrote:
eacheon wrote:Why did this get removed in NEURON 5.8?
??
It works in 5.8. Have you encountered an instance in which it doesn't?

Oh, sorry and thanks. String is defined in nrngui.hoc, if I start with nrngui it would be available.
Post Reply