Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Conversions

This section describes which type conversions are available.

Casts may occur as either t(v) (strict conversion) or v as t (optional conversion). The behavior of the call operator over a type may not always be a conversion depending on if t implements the self-attached meta::invoke() meta-method.

v as t     // returns t?. failure returns null
v as! t    // failure throws a TypeError (as-strict)
t(v)       // same as "v as! t"

Constant coercions

Constant coercions occur implicitly both at compile-time and runtime, converting a constant into another constant.

KindResult
undefined to flag enumerationInterned instance whose value is zero (0).
null to flag enumerationInterned instance whose value is zero (0).
undefined to t containing both undefined and nullundefined
undefined to t containing undefined and no nullundefined
undefined to t containing null and no undefinednull
null to t containing undefined but not nullundefined
null to t containing null but not undefinednull
null to t containing both undefined or nullnull
Numeric constant to * or ObjectEquivalent constant of the target type.
String constant to * or Object or union containing stringEquivalent constant of the target type.
Boolean constant to * or Object or union containing booleanEquivalent constant of the target type.
Namespace constant to * or Object or union containing NamespaceEquivalent constant of the target type.
Type constant to * or Object or union containing ClassEquivalent constant of the target type.
Numeric constant to another compatible numeric typeNumeric constant with value coerced to target type.
Numeric constant to union containing at least one compatible numeric typeNumeric constant of the target type containing value coerced to the containing numeric type, preferring the same numeric type or otherwise the first numeric type found.
NaN to floatNaN
NaN to decimalNaN
-Infinity to float-Infinity
+Infinity to float+Infinity
-Infinity to decimal-Infinity
+Infinity to decimal+Infinity

Implicit coercion

Implicit coercions occur implicitly both at compile-time and runtime, after trying a constant coercion.

KindResult
From *
To *
From numeric type to compatible numeric type
To covariant (includes base classes, same parameterized type (if current type arguments implicitly coerce to the target type arguments), implemented interfaces, unions and inherited record type)
From union to compatible union
From union member to union
From union to a common base typeFor example, given type U = (B, C), a var a:A = u; declaration is valid as long as B and C share A as a base type.
From structural function type to another compatible function type
From t type parameter to t?

Note: interface types inherit Object.

Parameterized types

ShockScript allows implicit coercions from t.<...> to t.<...> where t is a parameterized type, where the final type contains type arguments which the original type’s type arguments implicitly coerce to.

Note: Implicitly coercing an Array, Map or Set type to use covariant element types is allowed; however overwriting the collection later with unexpected element values may throw a TypeError during runtime.

For other types, using an unexpected type somewhere in place of type parameters may lead to an internal error during runtime.

Casts

Casts occur when resolving v as t or t(v), after trying an implicit coercion.

KindResult
To contravariant (from interface to interface subtype, from class to subclass, or record type subtype)
To union member
From * or Object to interface
To a contravariant [t] typeA new Array filtering out incompatible elements.
To a possibly incompatible Map.<K, V> typeA new Map filtering out incompatible fields.
To a contravariant Set.<t> typeA new Set filtering out incompatible elements.
To same parameterized type if not Array, nor Map and nor Set and if current type arguments can cast to the target type argumentsE.g. c.<*> to c.<double>
string to enumerationIdentification of an enumeration variant by its string name.
Number to enumeration (using the same numeric type)For regular enumerations, identifies a variant by its numeric value. For flag enumerations, identifies variant bits.
To stringFor undefined, returns "undefined"; for null, returns "null"; for other types, invokes toString().
To booleanEvaluates truthy value.
To doubleForced conversion to double-precision floating point.
To floatForced conversion to single-precision floating point.
To decimalForced conversion to 128-bit decimal number.
To byteForced conversion to 8-bit unsigned integer.
To shortForced conversion to 16-bit signed integer.
To intForced conversion to 32-bit signed integer.
To uintForced conversion to 32-bit unsigned uninteger.
To longForced conversion to 64-bit signed integer.
To bigintForced conversion to an arbitrary range integer.
Record type into equivalent record type of non-uniform field order
From type parameter

Note: interface types inherit Object.

Parameterized types

ShockScript allows casts from t.<...> to t.<...> where t is a parameterized type, where the final type contains type arguments which the original type’s type arguments may cast to.

Note: These conversions are always safe for Array, Map and Set types, as they create new objects.

For other types, the resulting object is the same, therefore unexpected type or property related errors can occur during runtimes.