★ IDL 8.0: Other language changes
posted Mon 9 Aug 2010 by Michael Galloy under IDLThere are many other changes to the core IDL language besides operator overloading and lists/hashes, discussed here already. These changes make IDL’s syntax more convenient to use.
A new !null
variable was introduced in IDL 8.0. There are several equivalent forms:
IDL> help, !null, [], {}
UNDEFINED = !NULL
UNDEFINED = !NULL
UNDEFINED = !NULL
!null
is an undefined variable with a couple special properties:
- it can be assigned to a variable
- it can be used in array or structure concatenation
For example, a typical use would be:
IDL> a = []
IDL> a = [a, 1]
This eliminates the need for special cases in code to check for the case of adding the first element, though this use case is generally handled better by the new list
objects.
IDL now allows the use of negative subscripts to specify indices relative to the end of an array, with -1
representing the last element:
IDL> a = findgen(10)
IDL> print, a[-1]
9.00000
IDL> print, a[-2]
8.00000
Negative indices can also be used in ranges:
IDL> print, a[-2:0:-1]
8.00000 7.00000 6.00000 5.00000 4.00000
3.00000 2.00000 1.00000 0.00000
But, negative subscripts are handled in the same manner as in older versions of IDL when placed in an array used to index another array:
IDL> print, a[[-1]]
0.00000
This behavior can also still be controlled via compile_opt strictarrsubs
.
Object-oriented programming has had several important updates. Most important of these features is automatic garbage collection. This will automatically free heap variables, both pointers and objects, which no longer have a reference to them. For example, in the following example, an object is created and then its reference is lost by reassignment to another value:
IDL> o = obj_new('IDL_Container')
IDL> help, /heap
Heap Variables:
# Pointer: 0
# Object : 1
refcount=1
STRUCT = -> IDL_CONTAINER Array[1]
IDL> o = 2
IDL> help, /heap
Heap Variables:
# Pointer: 0
# Object : 0
This situation would have resulted in leaked memory—the help, /heap
command would have reported 1
object at the end, but it would not have been accessible through the o
variable any longer. Now, memory that can no longer be accessed is automatically freed. This feature can be turned off globally or for a particular variable with the DISABLE
keyword to the new HEAP_REFCOUNT
function.
There are also two small syntax changes that make objects a bit more like other languages and a bit more convenient:
- method invocation can be done with the
.
operator, as well as the old->
operator - a function of the same name as the class is automatically generated to create new objects of that class, so the
OBJ_NEW
function is not required to instantiate an object
These are both shown in the following:
IDL> c = IDL_Container()
IDL> c.add, IDLgrModel()
IDL> help, c.get(position=0)
OBJREF = <ObjHeapVar5(IDLGRMODEL)>
Note that the .
operator and the ->
operator a slightly different precedence:
IDL> p = ptr_new(list())
IDL> *p->add, 1
IDL> *p.add, 1
*p.add, 1
^
% Syntax error.
IDL> (*p).add, 1
My understanding is that this is not intended and will be fixed in a release soon.
August 26th, 2010 at 2:52 pm
The last is an example of why switching to the same operator as structure dereference is a bad idea. *p.add can be interpreted as:
a) invoke method “add” of the objected pointed to by p
b) p is a structure whose field variable ‘add’ contains a pointer; return what it points to
February 19th, 2011 at 3:58 pm
Hi,
When I use the new graphic system, like:
p = PLOT(/TEST, /FILL_BACKGROUND)
and I close the window, run command:
help,/heap
There is a memeory leak. Why? Very thanks.
February 19th, 2011 at 4:01 pm
Oh, the complete command is
p = PLOT(/TEST, /FILL_BACKGROUND)
obj_destroy, p
help,/heap
February 19th, 2011 at 5:12 pm
That is just the iTools system. It is normal for that stuff to stay around once you have used any iTool or new graphic. If you really want to clean it up, use
IRESET
: