Difference between revisions of "Typecast"

From Second Life Wiki
Jump to navigation Jump to search
m (<lsl> example)
m (Noted efficiency of typcasting as a list using (list)var syntax rather than [var] syntax)
 
(18 intermediate revisions by 11 users not shown)
Line 1: Line 1:
{{LSL Header}}{{LSLC|Syntax}}{{RightToc}}
{{LSL Header|ml=*}}
{{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 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.
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.
 
{| {{Prettytable|style=float:right; clear:right; margin:0em; line-height:1em; font-size:75%;}}
|-
!colspan="9"| Supported Typecasts
|-
|colspan="2" rowspan="2"|
! colspan="7" {{Hl2}}|To
|- align="center" {{Hl2}}
![[integer]]
![[float]]
![[string]]
![[key]]
![[list]]
![[vector]]
![[rotation]]
|- align="center"
! rowspan="7" {{Hl2}}|From
!{{Hl2}}| [[integer]]
| x
| x
| x
|
| x
|
|
|- align="center"
!{{Hl2}}| [[float]]
| x
| x
| x
|
| x
|
|
|- align="center"
!{{Hl2}}| [[string]]
| x
| x
| x
| x
| x
| x
| x
|- align="center"
!{{Hl2}}| [[key]]
|
|
| x
| x
| x
|
|
|- align="center"
!{{Hl2}}| [[list]]
|
|
| x
|
| x
|
|
|- align="center"
!{{Hl2}}| [[vector]]
|
|
| x
|
| x
| x
|
|- align="center"
!{{Hl2}}| [[rotation]]
|
|
| x
|
| x
|
| x
|}


{{#vardefine:p_type_desc|variable type
{{#vardefine:p_type_desc|variable type
Line 10: Line 90:
== Syntax: ({{LSL Param|type}}){{LSL Param|value}} ==
== Syntax: ({{LSL Param|type}}){{LSL Param|value}} ==
<div style="padding: 0.5em;">
<div style="padding: 0.5em;">
Converts '''value''' to '''type'''.
Converts {{LSLP|value}} to {{LSLP|type}}.
{|
{|
{{LSL DefineRow|expression|type|{{#var:p_type_desc}}}}
{{LSL DefineRow|expression|type|{{#var:p_type_desc}}}}
{{LSL DefineRow|expression|value|{{#var:p_value_desc}}}}
{{LSL DefineRow|expression|value|{{#var:p_value_desc}}}}
|}
|}
If {{LSL Param|value}} is a complex expression, it may be beneficial to wrap it in parentheses. ({{LSL Param|type}})({{LSL Param|value}})
</div></div>
</div></div>


Line 20: Line 101:
== Examples ==
== Examples ==
<div style="padding: 0.5em;">
<div style="padding: 0.5em;">
<lsl>string a = "1.5";
<source lang="lsl2">
string a = "1.5";
float b = (float)a;
float b = (float)a;
integer c = (integer)a;</lsl>
integer c = (integer)a;
 
integer i;
i = (integer) 1.23;    // 1
i = (integer) -1.23;    // -1
i = (integer) "0123";  // 123
i = (integer) "0x12A";  // 298
i = (integer) "  -5  ";  // -5; leading white space is ignored
i = (integer) "105 degrees here, it is a nice day"; // 105; non-numeric text which follows numeric text is ignored
i = (integer) "String with no numbers"; // 0
 
float f;
f = (float) "6.2e1";  // 62.0
f = (float) "  -16.2°C is seriously cold!"; // -16.2; (float)string also ignores leading whitespace and trailing non-numeric characters
// "6.2e1", "6.2e+1", "6.2E1", "6.2E+1" are all equivalent.
f = (float) "Nancy";  // any string beginning with "nan" (case insensitive) --
                      //  Mono only: NaN (not a number); LSO: causes a math error
f = (float) "infallible LSL!";  // any string beginning with "inf" (case insensitive) --
                                //  Mono only: Infinity; LSO: causes a math error
 
string s;
s = (string) -PI;              // "-3.141593" with precision set to 6 decimal places by zero padding or rounding
s = (string) [1, 2.3, "a"];    // "12.300000a" again 6 decimal places, but ...
s = (string) <1.0, 2.3, 4.56>; // "<1.00000, 2.30000, 4.56000>" .. now with precision set to 5 decimal places
s = (string) <2, 4, 0.0, PI >;  // "<2.00000, 4.00000, 0.00000, 3.14159>"
 
list l;
l = (list) "";                // [""]
l = (list) <1.0, 2.3, 4.56>;  // ["<1.00000, 2.30000, 4.56000>"]
 
vector v;
v = (vector) "<1.0, 2.3, 4.56>";  // <1.0, 2.3, 4.56>
v = (vector) "<1.0, 2.3>";        // ZERO_VECTOR  (Due to insufficient value)
 
rotation r;
r = (rotation) "<1.0, 2.3, 4.56, 1.0>";  // <1.0, 2.3, 4.56, 1.0>
r = (rotation) "<1.0, 2.3, 4.56>";      // ZERO_ROTATION  (Due to insufficient value)
</source>
 
===Applied===
<source lang="lsl2">default
{
    state_entry()
    {
        integer iUnixTime  = llGetUnixTime();
        string  sUnixTime  = (string)unixTime;
        string  sRegionTime = (string)llGetTimeOfDay();
        llSetObjectDesc(sUnixTime);
        llSetText(sRegionTime, <1.0, 0.0 ,0.0>, 1.0);
    }
}</source>
</div></div>
</div></div>


Line 29: Line 161:
<div style="padding: 0.5em;">
<div style="padding: 0.5em;">
*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.
*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.
</div></div>
<div id="box">
== Notes ==
<div style="padding: 0.5em;">
*Typecasting from a float to an integer merely removes the portion of the number following the decimal point.  To round to the nearest whole integer, use [[llRound]].
*For getting at the elements of a list use the {{LSLGC|List/Read Element|llList2*}} functions.
*For casting a variable <code>myVar</code> to a single-item list, the syntax <code>(list)myVar</code> is about 30% more efficient under LSL Mono than the syntax <code>[myVar]</code>. The same <code>(list)myVar</code> syntax is also slightly more efficient under LSL-LSO.
</div></div>
</div></div>

Latest revision as of 18:15, 21 November 2018

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.

Supported Typecasts
To
integer float string key list vector rotation
From integer x x x x
float x x x x
string x x x x x x x
key x x x
list x x
vector x x x
rotation x x x


Syntax: (type)value

Converts value to type.

• expression type variable type
• expression value expression or constant

If value is a complex expression, it may be beneficial to wrap it in parentheses. (type)(value)

Examples

string a = "1.5";
float b = (float)a;
integer c = (integer)a;

integer i;
i = (integer) 1.23;     // 1
i = (integer) -1.23;    // -1
i = (integer) "0123";   // 123
i = (integer) "0x12A";  // 298
i = (integer) "   -5  ";  // -5; leading white space is ignored
i = (integer) "105 degrees here, it is a nice day"; // 105; non-numeric text which follows numeric text is ignored
i = (integer) "String with no numbers"; // 0

float f;
f = (float) "6.2e1";   // 62.0
f = (float) "  -16.2°C is seriously cold!"; // -16.2; (float)string also ignores leading whitespace and trailing non-numeric characters
// "6.2e1", "6.2e+1", "6.2E1", "6.2E+1" are all equivalent.
f = (float) "Nancy";   // any string beginning with "nan" (case insensitive) --
                       //   Mono only: NaN (not a number); LSO: causes a math error
f = (float) "infallible LSL!";   // any string beginning with "inf" (case insensitive) --
                                 //   Mono only: Infinity; LSO: causes a math error

string s;
s = (string) -PI;              // "-3.141593" with precision set to 6 decimal places by zero padding or rounding
s = (string) [1, 2.3, "a"];    // "12.300000a" again 6 decimal places, but ...
s = (string) <1.0, 2.3, 4.56>; // "<1.00000, 2.30000, 4.56000>" .. now with precision set to 5 decimal places
s = (string) <2, 4, 0.0, PI >;  // "<2.00000, 4.00000, 0.00000, 3.14159>"

list l;
l = (list) "";                // [""]
l = (list) <1.0, 2.3, 4.56>;  // ["<1.00000, 2.30000, 4.56000>"]

vector v;
v = (vector) "<1.0, 2.3, 4.56>";  // <1.0, 2.3, 4.56>
v = (vector) "<1.0, 2.3>";        // ZERO_VECTOR  (Due to insufficient value)

rotation r;
r = (rotation) "<1.0, 2.3, 4.56, 1.0>";  // <1.0, 2.3, 4.56, 1.0>
r = (rotation) "<1.0, 2.3, 4.56>";       // ZERO_ROTATION  (Due to insufficient value)

Applied

default
{
    state_entry()
    {
        integer iUnixTime   = llGetUnixTime(); 
        string  sUnixTime   = (string)unixTime;
        string  sRegionTime = (string)llGetTimeOfDay();
        llSetObjectDesc(sUnixTime);
        llSetText(sRegionTime, <1.0, 0.0 ,0.0>, 1.0);
    }
}

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.

Notes

  • Typecasting from a float to an integer merely removes the portion of the number following the decimal point. To round to the nearest whole integer, use llRound.
  • For getting at the elements of a list use the llList2* functions.
  • For casting a variable myVar to a single-item list, the syntax (list)myVar is about 30% more efficient under LSL Mono than the syntax [myVar]. The same (list)myVar syntax is also slightly more efficient under LSL-LSO.