tryConvert - multiple declarations
Function tryConvert
Checked component-wise conversion of a point.
Source and target scalar types must belong to the geo-d scalar domain.
Conversion is explicit and checked for range, but precision loss within the representable target range is permitted.
In particular:
- integral-to-integral narrowing fails outside the target range; - integral-to-floating conversion may lose precision; - floating-to-integral conversion requires every coordinate to be finite, already integral-valued, and inside the target integral range; - floating-to-floating conversion permits NaN and infinity; - floating-to-floating conversion fails when a finite source would become infinity in the target type.
No implicit rounding or quantisation is performed. Use rounded, floored, ceiled, or truncated explicitly before an integral conversion when those semantics are required.
Returns false when any coordinate cannot be converted according to these rules.
On failure, result is Point2!To.init.
No allocation is performed.
Complexity
O(1) time and O(1) auxiliary space.
Function tryConvert
Checked component-wise conversion of a vector.
Conversion follows exactly the same scalar rules as Point2 conversion: range overflow is rejected, precision loss within the representable target range is permitted, and no implicit rounding is performed.
Returns false when either component cannot be converted.
On failure, result is Vector2!To.init.
No allocation is performed.
Complexity
O(1) time and O(1) auxiliary space.
Function tryConvert
Checked component-wise conversion of a segment.
Both endpoints are converted using the Point2 conversion rules. The complete operation succeeds only when both endpoints convert successfully.
Precision loss within the representable target range is permitted. No implicit rounding or quantisation is performed.
On failure, result is Segment2!To.init.
No allocation is performed.
Complexity
O(1) time and O(1) auxiliary space.
Example
Example of checked conversion without implicit rounding.
import geo;
Point2!int result;
assert(
Point2!double(
3.0,
-2.0
) .tryConvert(result)
);
assert(
result ==
Point2!int(
3,
-2
)
);
assert(
!Point2!double(
3.5,
-2.0
) .tryConvert(result)
);
assert(
result ==
Point2!int .init
);