What is Type?

In a programming language, type is often related to how much storage space a variable takes up. For example, storing a short integer takes up less space than storing a long integer, so the language allots a certain range depending on what type you declare your variable to be. Some examples of type available are:

Integers: numbers without a decimal point. These generally take less memory to store and are popular in representing the index of an array. Some languages distinguish between a Short (often just called integer) and a Long integer, which has a larger value rane (usually just called long).

Floating point: numbers with a decimal point. There are two precisions, the single-precision 32-bit, generally referred to as a Float and the double-precision 64-bit format, often called a Double.

String: a single character or collection of single characters. The length of the string is variable. The number of characters being stored in the string determines how much memory is used.

Boolean: yes or no value usually represented with 1 bit (1=yes or 0=no). These are often used as the return value of a function to indicate whether it failed or succeeded.

Variant: a non-specific data type, especially useful for parameters that may hold either objects, numbers, or strings. This type is used predominantly by Windows Scripting Host languages based on ActiveX such as VBScript and JScript. (The C# type System.Object maps to the Variant type.)

Object: reference to an object. What is actually stored is a reference to an address in memory.

Empty/Undefined/Null: differs between languages. Some languages distinguish between types that are intentionally left empty, have yet to be defined, or have zero-length strings ("").

User-defined: customized data type. The programmer/user defines a data type as part of the program.

Array: list of other data values. While some languages do not treat arrays as discrete data types, they are closely related to data type. For more information, see Working with Arrays (and Other Collections) in Softimage.

 

This list is only a general summary of possible types. For more specific information, see Comparing Data Types across Languages or the documentation for the scripting or programming language of your choice.

Type in VBScript

There is only one type in VBScript, called Variant, which consists of several different subtypes that correspond with many of the types in the list above. For example, VBScript supports both integer and long subtypes, as well as object, boolean, and string subtypes.

VBScript automatically performs type coercion, or casting, but also allows you to explicitly convert between data types using special pre-defined functions (CStr, Clng, CDbl, etc.).

When you create a variable (with the Dimension keyword) in VBScript, it is always the variant type:

   ' First you declare the variable (always variant)
   dim myIndex
   dim myNumber, myLetter

   ' When you assign a value, VBScript decides what 
   ' subtype it is based on the value
   myIndex = 0
   myNumber = 365.77351
   myLetter = "A"

Testing for Type in VBScript

VBScript provides the TypeName() function which takes the variable as input and returns its sub-type:

   myIndex = 0
   myNumber = 365.77351
   myLetter = "A"
   ' NB: You have to use the set keyword for objects
   set my3DObject = ActiveSceneRoot

   Application.LogMessage TypeName(myIndex)
   Application.LogMessage TypeName(myNumber)
   Application.LogMessage TypeName(myLetter)
   Application.LogMessage TypeName(my3DObject)

   'INFO : Integer
   'INFO : Double
   'INFO : String
   ' TypeName can also figure out the class name
   ' of object variables
   'INFO : Model

Type in JScript

Type in JScript is similar to VBScript, in that all variables are declared with a generic data type, but when you assign a value to a variable (with the variable keyword), it is automatically coerced to one of several data types (String, Number, Boolean, Object, Array, Null, and Undefined):

   // You can declare and assign separately
   var myIndex;
   myIndex = 0;

   // You can declare and assign at the same time
   var myNumber = 365.77351;
   var myLetter = "A";

Testing for Type in JScript

JScript provides the typeof() operator which, like the VBScript TypeName() function, takes the variable as input and returns its type. However, unlike TypeName, it only returns the names of basic data types, not class names. If you use this operator on an ActiveX object as used in Softimage or other applications, it returns “object”. For this reason, it is much more effective to use the ClassName (Application) method when you are dealing with native Softimage objects:

   // You can declare and assign separately
   var myIndex = 0;
   var myNumber = 365.77351;
   var myLetter = "A";
   var my3DObject = ActiveSceneRoot;

   Application.LogMessage( typeof(myIndex) );
   Application.LogMessage( typeof(myNumber) );
   Application.LogMessage( typeof(myLetter) );
   Application.LogMessage( typeof(my3DObject) );

   // Once we know we are dealing with an 'object'
   // we can test it with Application.ClassName
   if ( typeof(my3DObject) == "object" ) 
   {
       Application.LogMessage( ClassName(my3DObject) );
   } else {
       Application.LogMessage( typeof(my3DObject) );
   }

   //INFO : number
   //INFO : number
   //INFO : string
   //INFO : object
   //INFO : Model

Type in PerlScript

PerlScript doesn’t recognize data type in the same way as other scripting languages. Of course, it understands the differences between strings and integers, for example, but what is a more important distinction in PerlScript is the difference between scalars, arrays and hashes.

Basically, these differences can be summed up as follows:

scalars: simple data (numbers, characters, true/false, etc.). These types of variables are prefixed by the dollar sign ($).

arrays: collections of values, which can be any combination of scalars, arrays, hashes and/or references to arrays and hashes. These types of variables are prefixed by the at sign (@).

hashes: key-value pairs, sometimes called an associative arrays. These types of variables are prefixed by the percentage sign (%).

All variables are declared with either the my keyword (for a local variable) or the our keyword (for a global variable):

   # You can declare and assign separately
   my $integer;
   $integer = 0;

   # You can declare and assign at the same time
   my $number = 365.77351;
   $letter = "A";

   # You can add almost anything to an array
   my @array = ( 1, 2, "Nancy", $number );

Type in Python

Python takes data type very seriously and does not provide automatic type coercion like VBScript and JScript. However, you do not declare variables with special keywords: all you need to indicate in the assignment statement is the name of the new variable and Python decides what kind of type it is when it is initialized (assigned a value for the first time):

   # You can assign a single value to a variable
   myIndex = 0

   # You can also assign multiple values at once 
   myNumber, myLetter = 365.77351, "A"

Converting Types in Python

Once a variable is initialized, you can assign a different type of value to that variable without using an explicit type conversion operator. However, if you are performing operations on variables with different types, you must use one of the type-conversion functions (int(), str() and float()):

   val = 123
   stmt = "sally has "

   #    msg = stmt + val + " marbles"

   # Note: the above commented statement will generate
   # the following error message 

   #ERROR : Traceback (most recent call last):
   #  File "<Script Block >", line 3, in ?
   #    msg = stmt + val + " marbles"
   #TypeError: cannot concatenate 'str' and 'int' 
   #objects - [line 3]

   msg = stmt + str(val) + " marbles"

   # The above statement converts the numeric value 
   # into a string to avoid a mismatch while
   # concatenating:

   Application.LogMessage( msg )
   #INFO : sally has 123 marbles

Testing for Type in Python

Python provides the type() function which, like the VBScript TypeName() function, takes the variable as input and returns its type. However, unlike TypeName, it only returns the names of basic data types, not class names. If you use this function on an ActiveX object as used in Softimage or other applications, it returns “<type 'instance'>”. For this reason, it is much more effective to use the ClassName (Application) method when you are dealing with native Softimage objects:

   myIndex, myNumber, myLetter = 0, 365.77351, "A"
   my3DObject = Application.ActiveSceneRoot

   Application.LogMessage( str(type(myIndex)) )
   Application.LogMessage( str(type(myNumber)) )
   Application.LogMessage( str(type(myLetter)) )
   Application.LogMessage( str(type(my3DObject)) )

   # Once we know we are dealing with an 'instance'
   # we can test it with Application.ClassName
   if str(type(my3DObject)) == "<type 'instance'>":
       Application.LogMessage( \
          Application.ClassName(my3DObject) )
   else:
       Application.LogMessage( str(type(my3DObject)) )

   #INFO : <type 'int'>
   #INFO : <type 'float'>
   #INFO : <type 'str'>
   #INFO : <type 'instance'>
   #INFO : Model

Type in C++ (Comparison)

Type is very specific in C++. For example, there are several kinds of integers and floating point number types. C++ also supports user-defined types.

C++ performs some implicit type coercion, or casting, but imposes strict regulations on how it does this. It is much better to explicitly convert between data types than rely on C++ to do it for you.

When you create a variable in C++, you include the type as part of the expression:

   // You can declare and assign separately
   int myIndex;
   myIndex = 0;

   // You can declare and assign at the same time
   double myNumber = 365.77351;
   char myLetter = "A";

   // You must explicitly declare the class of each 
   // Softimage object instance you create
   Application app;
   app.LogMessage( L"Hello, World!" );

   // The scene root is an instance of the Model class
   Model root = app.GetActiveSceneRoot();

Testing for Type in C++ (Comparison)

Since C++ is so heavily typed, you always have to declare what type of item you have assigned to a variable and thus there is no equivalent to ClassName or any other type-testing functions.

 

You can use the sizeof() operator which returns the number of bytes used by a basic type or struct; however, you can’t depend on consistency between different platforms or machines.

For information on the sizeof() operator, see msdn2.microsoft.com/en-us/library/ms860979.aspx.

Type in C# (Comparison)

Type is very specific in C#, there are several kinds of integers and floating point number types. C# also supports user-defined types.

C# is quite strict about some kinds of type casting (conversion between data types). In addition, C# arrays cannot contain members of different types.

 

The bool type is set apart from the other basic data types (int, char, float, etc.) in that there is no conversion allowed between bool and the other types. This means that an empty string or 0 is not equivalent to false, and likewise a populated string or a non-zero value is also not equivalent to true.

Therefore, all condition tests (ie., tests to be evaluated in the context of if- or while-clauses) must strictly evaluate to a true bool value (other data types cannot be coerced/are not implicitly converted).

Testing for Type in C# (Comparison)

Since C# is so heavily typed, you always have to declare what type of item you have assigned to a variable and thus there is no equivalent to ClassName or any other type-testing functions.

 

For more information on the C# language, see msdn2.microsoft.com/vcsharp/Aa336809.



Autodesk Softimage 2011