Revisiting SE-0177: Adding `clamped(to:)`

I've created a preview package with an alternative design:

  • The primary API is a generic clamped(to:) method on Comparable.
  • The secondary API is a new RangeClamping protocol.
  • There are (conditional) conformances for the existing range types.
  • The tests use an internal @Clamped(to:) property wrapper.

This addresses the Core Team feedback where "a generic solution is preferred if possible".

I've since discovered that similar designs have already been suggested (in 2017 and 2018).

The protocol requirement has an in-out parameter, which I'm having second thoughts about.

4 Likes