Difference between revisions of "Typecast"
Jump to navigation
Jump to search
m (Multi-lang) |
|||
Line 5: | Line 5: | ||
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;}} | |||
|- | |||
!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 18: | Line 96: | ||
{{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> | ||
Revision as of 03:35, 16 May 2008
LSL Portal | Functions | Events | Types | Operators | Constants | Flow Control | Script Library | Categorized Library | Tutorials |
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
<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.