Skip to content

Force? Coerce? Just add a policy argument instead #715

@chiphogg

Description

@chiphogg

What API should override conversion safety checks?

mp-units uses "force" as a word to denote this, despite the fact that "force" already has an unrelated meaning in the context of units libraries. Au uses "coerce" for this purpose, which avoids this confusion, but has problems of its own. For example, it's a blunt hammer: it turns of all safety checks, and the reader can only guess what specific problem the author was trying to circumvent.

The upcoming Au 0.5.0 release provides a new solution. Conversion functions can take a second argument, which is a conversion risk policy. We have conversion risk sets, such as:

  • OVERFLOW_RISK
  • TRUNCATION_RISK
  • ALL_RISKS (equal to OVERFLOW_RISK | TRUNCATION_RISK)

To turn a risk set into a policy, pass it to ignore(), or check().

A key benefit is that it expresses the author's intent much more clearly and directly. Additionally, it only opts out of the specified risk(s).

To see this in action, try fixing the individual compiler errors in https://godbolt.org/z/M1YPEP9fT.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions