Custom types in range patterns?

Currently, range patterns only allow char and primitive numeric types. Might it be possible to extend this set to certain user-defined types, possibly with a mechanism similar to StructuralEq (where only automatically derived impls, which are known to be well-behaved, are eligible)?

i.e. StructuralPartialOrd? Then the pattern can evaluate like the ranges' contains method.

1 Like

Note that floats are on their way out though, #41620, so maybe this should require a full StructuralOrd.

1 Like

One wrinkle is that, unlike with StructuralEq, there will likely be reason to change the method of comparison from the default used by the derive macro. For simple things, like changing the priority of different fields in the comparison, or reversing comparison order, adding field attributes (#[derive(PartialEq, Eq, PartialOrd, Ord)] struct Foo { #[cmp(1, reverse)] bar: i32, #[cmp(0)] baz: i32 }) could be enough. But for something like Gray code numbers you would need a way to assert that your fully-custom impl meets the requirements.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.