New Issue: meaning of cast ':' and 'safeCast' for domains and ranges

19269, "vasslitvinov", "meaning of cast ':' and 'safeCast' for domains and ranges", "2022-02-18T05:44:51Z"

This issue asks to define the semantics of cast (operator :) from a rectangular domain to a rectangular domain type and whether we need a separate safeCast. These decisions will carry over to ranges.

This issue also considers casts between other combinations of domain types.

Rectangular domains

Here are the things that we may want a cast or "safeCast" to do:

  • If the from-domain is stridable and the to-domain is non-stridable:

    • issue a compiler error
    • run-time check that the from-domain's stride == 1, otherwise:
      • halt the program, or
      • throw an exception
      • if this runtime check be performed only when boundsChecking or castChecking is true, throwing an exception is not appropriate just like it is not for the array bounds check
    • or skip the runtime check and always drop the from-domain's stride
  • If the from-domain's idxType is coercible, i.e. convertible implicitly, to the to-domain's idxType:

    • allow such a cast, or
    • issue a compiler error
  • If the from-domain's idxType is cast-able to the to-domain's idxType:

    • allow such a cast always
    • allow such a cast with a runtime check, under the same considerations as the stride check above
    • issue a compiler error

The straw-person proposal is to follow the precedent of integer casts:

  • the cast operator : drops the stride and casts the indices, if needed for a different idxType, without a runtime check
  • the safeCast method performs a runtime check for the stride and a safeCast() for the indices, calling HaltWrappers.safeCastCheckHalt() if the check fails

A straw-person counter-proposal is to have the cast operator : do what what the proposal's safeCast method does, and avoid defining safeCast on domains altogether.

We can initially postpone implementing support for differing idxTypes and issue a compiler error instead. Such support can be added later upon user interest while maintaining backwards compatibility.

Irregular domains

Casting from an irregular domain to a (non-sparse) rectangular domain should be a compiler error, as such an operation is not straightforward and unlikely to be sought by the user.

Casting between other combinations of domain types is well defined semantically, however is also unlikely needed by the user. We can have such casts issue a compiler error initially and implement as needed.

When casting between two domains with different idxType, we should follow the same strategy that we choose for rectangular domains.