Add a `within` method to `PartialOrd`

I like the idea, but I'd like to say that it is better to implement for Ord.

I have concern:

Let's define L as result of lower_inclusive.partial_cmp(&arg), and H as upper_inclusive.partial_cmp(&arg).

The result is obvious when both L and H is Some(_) (implies comparison was successful), but what if not? Would it return false due to not contained?

Just my two cents for naming of methods:

  • x.is_in(y)
  • x.is_not_in(y)

This is assuming that

  • This goes into the direction where we want it to be very general, with parallels to features in existing languages like Python.
  • We want a have a short name that is not too specific
  • We want a name that is not too short or could conflict with potential keywords
  • We want something that would hopefully not need to be changed if we wanted to upgrade it to an actual language-level operator later on

We should also be careful about conflicts with other potential language features. For example, keywords like in or is could also potentially used for pattern matching operators:

  • x in (1 | 3 | 4)
  • x is Some(5)

To be clear, I'm just talking about similar semantic to the matches!() macro here. But I see a lot of similiarities between the (un)ergonomics of it and the .contains() (un)ergonomics that spawned this discussion. Though, looking at this, it seems unlikely that in would be end up as the best choice for this regardless.

5 Likes

IIRC libs team does not want to add such methods. For example, it's why we don't have is_not_empty() methods.

5 Likes

Agreed. I think !x.is_in(0.0..=1.0) is perfectly readable.

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