Sunday, May 1, 2011

What is Explicit Typecasting?

One of the basics of scripting and programming in general that you'll learn is variables: a nametag for a piece of data that you can store, modify, and use.  Some of you may be familiar with the "box" example, where a variable is a labeled box with a piece of paper containing the information it has.


Now, variables can hold different kinds of information, or data types.  Under LSL, there's seven data types:

  • Integer: A whole number, such as 42.
  • Float: A non-whole or decimal-point number, such as 2.5, 1.25, or 3.141592654
  • String: Text data, such as "Hello, world!"
  • Key: A special kind of string, used to reference the data for avatars, inventory, objects, textures, and just about everything.  They are also known as a UUID, short for Universally Unique ID.  This is the key for the default plywood texture: "89556747-24cb-43ed-920b-47caed15465f"
  • Vector: A series of three float values stored in the same variable.  Used for colors, Euler rotations, and positions.  Example: <1.2, 2.5, 8.6>.
  • Rotation: A series of four float values stored in the same variable.  Used for quaternion rotations.
  • List: A special variable that allows you to store many other variables of any other kind under one variable.  Example:  ["hello", "world", 3.141, 42]
However, sometimes one variable may need to be used with another variable of a different type.  So, what happens?  What happens is Type Casting, where you convert one variable type to another.  There's two different ways to do this: implicit typecasting, and explicit typecasting.

What's the difference?  Implicit typecasting is where the variable type is converted to another type automatically.  Explicit typecasting is where you give an explicit instruction for the conversion.  So, if you casted an integer to a float, it would go from being 42 to 42.00.  And if you casted that same integer to a string, it would become "42".

Under the old, buggy script engine from the original opensim code, implicit typecasting was allowed all over the place.  This is problematic, however, because it allowed for sloppier code and is not meant for use in programming languages that have strong typing, such as LSL.  So, under the new Phlox script engine at InWorldz, explicit typecasting is now being enforced, bringing it in line with Linden Lab's implementation.

This means that some scripts may break, if they were written in Opensim or InWorldz.  But scripts written in Second Life won't break as a result of this, because explicit typecasting is already enforced.

However, the fix is rather easy: you just have to do explicit typecasting.  Say, if you want to convert 3.14 to a string.

//This won't work under Phlox:
float pi = 3.14;
string text = pi;

//This will work under Phlox:
float pi = 3.14;
string text = (string)pi;

Pretty simple, isn't it?  By putting the variable type in parenthesis before the variable, you can explicitly typecast most variables.  However, there are a couple of exceptions to this:
  • Rotations and Vectors cannot be cast from one to the other.
  • Keys can only be cast to strings, and only strings can be cast to keys.
  • Anything can be cast to a list, but the reverse isn't true.

As well, there is one remaining element of implicit typecasting, which also exists in Second Life: integer values may be implicitly typecast to floats, but not the reverse. So, this piece of code is valid:

//This will work under Phlox:
float number = 42;


//This won't work under Phlox:
integer number = 42.00;


Also, there are two ways to cast normal variables into lists, though the 2nd method shown below is technically creating a single-element list:

//First Method:
float number = 3.14;
list data = [number];


//Second Method:
float number = 3.14;
list data = (list)number;


This blog post is a work in progress and will be updated as the explanations are improved, as needed.  If anyone needs clarifications on parts of this or has suggestions on how to improve it, please contact me by IM at InWorldz, or post your thoughts in the comments section.