Static Methods and Properties
Static Methods
In addition to calling methods on an object instance, you can also call static (or class) methods on a class. In order to call a method as "static," you will need to mark the method with a special "compile_opt static" flag. Usually, the documentation for the class will indicate which methods are object methods and which are static class methods.
Static Procedure Methods
IDL static procedure methods have the syntax:
Classname.ProcedureName[, Arguments] [, KEYWORDS=Keywords]
where:
- Classname is a valid class name
- ProcedureName is the name of the static procedure method
- Arguments are the required or optional positional parameters
- Keywords are any optional keywords.
Note: The square brackets are not used in the actual call. They are used in the documentation to denote that the arguments and keywords are optional.
For example:
MyClass.DoSomething, a, b, /SPECIAL
This will execute the DoSomething
class method on the MyClass
class, passing in two arguments a and b, as well as a keyword set equal to 1.
Static Function Methods
IDL static function methods have the syntax:
Result = Classname.FunctionName( [, Arguments] [, KEYWORDS=Keywords] )
where:
- Result is the returned value
- Classname is a valid class name
- FunctionName is the name of the static function method
- Arguments are the required or optional positional parameters
- Keywords are any optional keywords
Note: Again, the square brackets are not used in the actual call, they are used to denote that the arguments and keywords are optional.
For example:
Result = MyClass.Calculate(a, b, /ALL)
This statement executes the Calculate
class method on the MyClass
class, passing in two arguments a and b, as well as a keyword set equal to 1.
Static Properties
Classes can also have static properties, which can be accessed using the "dot" syntax:
Value = Classname.PropertyName
For example:
value = MyClass.Color
PRINT, MyClass.Linestyle
Internally, IDL routes the class properties to the class' GetProperty method. Because static classes have no "state" and do not persist, you will need to hard-code any static property values within the GetProperty method, or you will need to compute them "on the fly."
Static Method Calls versus Array Indexing
In IDL, by default both square brackets [ ] and parentheses ( ) can be used for array indexing. However, parentheses are also used for function calls. Because of this duplication, the IDL compiler cannot distinguish between function calls and array indexing. For example, you might have a static method call:
Result = MyClass.Calculate(a, b)
However, it is just as likely that you might be doing an array index into a structure:
Result = MyStruct.Field(a, b)
By default, IDL will be unable to distinguish between these two calls and in certain cases will throw a syntax error. To avoid any confusion, you should always use compile_opt strictarr
(or compile_opt idl2
) at the top of your procedures and functions. This will force you to use square brackets for all array indexing within that routine. The IDL compiler will then treat any parentheses as function calls.
Use of Static Methods at the Main Program Level
Normally, if you are doing static method calls within a routine, it is straightforward to insert compile_opt strictarr
to avoid any confusion with parentheses. However, if you are executing ad-hoc IDL commands at the main program level, it might be confusing to suddenly receive syntax errors when trying to make static method calls.
Because of this issue, if you use a static method call at the main program level (either at the command prompt or in a $MAIN program), IDL will then automatically turn on compile_opt strictarr
for the main level. From that point on, you must then be sure to use square brackets for array indexing. See compile_opt for details.
Examples of Static Methods
The Clipboard::Get and Clipboard::Set methods are static. For example, enter the following commands at the IDL prompt:
Clipboard.Set, "My text on the system clipboard"
print, Clipboard.Get()
IDL prints:
My text on the system clipboard
The IDLffVideoRead and IDLffVideoWrite classes have static methods for retrieving the list of available formats and codecs:
print, IDLffVideoRead.GetFormats()
print, IDLffVideoRead.GetCodecs()
print, IDLffVideoWrite.GetFormats()
print, IDLffVideoWrite.GetCodecs()
IDL prints:
avi flv gif mjpeg mov,mp4,m4a,3gp,3g2,mj2 swf wav
flv gif h263 h264 mjpeg mpeg4 ...
avi flv gif mjpeg mov mp4 swf wav webm
flv gif h263 mjpeg mpeg4 msmpeg4v2 msmpeg4 ...
The IDLUnit class has methods for adding, listing, and removing physical units:
; Add "shake" to the list of units.
IDLUnit.AddUnit, 'shake', '10 nanoseconds', PLURAL='shakes', SYMBOL='sh'
; Now that we have added the unit, we can use it:
PRINT, IDLUnit('2 shake to picoseconds')
IDL prints:
20000 picoseconds
See Also
Creating Static Methods, Using Variable Functions and Attributes