Conversions¶
Some types in NDCell are implicitly converted (coerced) to other types when used with various operators or passed to functions.
Subtype coercion¶
Some types are subtypes of other types; this is implemented by coercing the subtype to the supertype when necessary. For example, a Cell is implicitly converted to a CellSet when used where a CellSet is required. Here are the rules for subtype coercion:
Boolean conversion¶
Values of some types can be converted to a boolean, which is represented using an Int. This can happen implicitly (when used in a place where a boolean is required) or explicitly (using bool()).
- An
Intis truthy if it is not equal to0. - A
Cellis truthy if it is not equal to#0. - A
Vecis truthy if any of its components is not equal to0. - A
Patternis truthy if any of its cells is not equal to#0.
“Truthy” values become TRUE (1) and “falsey” values (anything not truthy) become FALSE (0).
Vector to vector conversion¶
A Vec of one length can be converted to a Vec of a different length. This can happen implicitly (when used in a place where a Vec of a different length is required) or explicitly (using vec()).
- If the new length is shorter than the original length, the vector is truncated and extra components are removed.
- Example:
vec2([10, 20, 30, 40])→[10, 20]
- Example:
- If the new length is longer than the original length, the vector is extended with zeros.
- Example:
vec4([10, 20])→[10, 20, 0, 0]
- Example:
Integer to vector conversion¶
An Int can be converted to a Vec of any length. This can happen implicitly (when used in a place where a Vec is required) or explicitly (using vec()).
A new vector is constructed with the value of the original integer for each component.
- Example:
vec3(-5)→[-5, -5, -5]