Difference between revisions of "Typecast"

From Second Life Wiki
Jump to navigation Jump to search
(New page: {{LSL Header}}{{LSLC|Syntax}}{{RightToc}} To convert the type of a value a typecast is required. There are two types of typecasting, explicit and implicit. Explicit typecasts must be prov...)
 
m (<lsl> example)
Line 20: Line 20:
== Examples ==
== Examples ==
<div style="padding: 0.5em;">
<div style="padding: 0.5em;">
<pre>string a = "1.5";
<lsl>string a = "1.5";
float b = (float)a;
float b = (float)a;
integer c = (integer)a;</pre>
integer c = (integer)a;</lsl>
</div></div>
</div></div>



Revision as of 19:53, 31 December 2007

To convert the type of a value a typecast is required. There are two types of typecasting, explicit and implicit. Explicit typecasts must be provided by the programmer, but implicit typecasts are put in place by the compiler. LSL implicitly typecasts strings to keys and integers to floats where the latter type is required but the former is provided.


Syntax: (type)value

Converts value to type.

• expression type variable type
• expression value expression or constant

Examples

<lsl>string a = "1.5"; float b = (float)a; integer c = (integer)a;</lsl>

Caveats

  • The compiler allows explicit typecasting where it is not needed and does not optimize it out. Unnecessary typecasts will bloat code and slow it down.