Re: RFC: Add `final class Vector` to PHP
| From: | Levi Morrison via internals | Date: | Fri, 17 Sep 2021 20:56:22 +0000 |
| Subject: | Re: RFC: Add `final class Vector` to PHP | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-116079@lists.php.net to get a copy of this message | ||
> * Whether or not contains() needs a comparison callback in my mind depends mainly on whether or
> not the operator overloading RFC passes. If it does, then contains() can/should use the
> __compareTo() method on objects. If it doesn't, then there needs to be some other way to
> compare non-identical objects or else that method becomes mostly useless.
This is only partly true. Let's say we have a vector of some complex
type A. There are legitimate reasons for using different ways of
comparing As, such as when projecting sub-fields (for example, sorting
by each member's name this time, but next time sorting by each
member's location).
Of course, if it passes, then using a type's built-in comparison
overloading is a sensible default, but it doesn't remove the need of
having a custom comparator.
-----
I was tired when I originally pointed it out the comparator/equatable
stuff, but Tyson was rightly saying that
any solves this need, e.g.
<?php
if ($vec->contains($value, $eq)) {/**/}
// translates to
if ($vec->any(fn ($x) => $eq($x, $value))) {/**/}
?>
However, it's not as clear what to do for indexOf where you care
about the index it was found at.