-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hashable::equals signature #140
Comments
forgot to mention: if |
Currently the equals method won't be called (assumed false) if the value is not an instance of the same class. |
|
There is some bad design here because the equals method is not meant to be called directly, which is crazy. Thanks for bringing this to my attention. |
When checking equality, ext-ds takes a shortcut to avoid invoking a callback unnecessarily. But any value should be a valid argument so the docs are wrong. A good solution here might be to |
not sure a |
leads to
|
And to state that "the $obj must implement Hashable" makes no sense. You try to state that "the $obj must be |
That is correct. There should then be no typehint at all and the implementation should check that the object is of the same class using |
i'm sure the |
ah. or do you mean that calling |
or even something like
? |
It can be any value. If you use a string as a key and an object that happens to hash into the same bucket, you must be able to do an equality check. You must be able to equate any two values. The |
|
i understood now. ok, so this is a docs mistake. could you fix that or should i file a bug report there? (i don't know if this is you who maintains that docs or someone else) |
I think the best thing here would be to specify in the docs that object equality MUST be restricted to objects of the same class, or return false otherwise. The implementation should not assume this. The internal shortcut to not invoke We have to support calling |
I'll update the docs 👍 |
it'd be useful to explain there what "guaranteed" means (that you |
not sure about inheritance though. what if someone treats an object and an object of a child class as equal? they could have same hash, same id, but their class names differ! |
have i understood you correctly in the following? you think that ext-ds should make it impossible to create a class that will implement Hashable and that will treat equal |
They should then wrap them both in something that adapts them to the same class. I believe we should be strict here and enforce that when two objects are equated, they must be instances of the same class to be considered equal. Otherwise it is a breach of the contract. The problem is then how do we enforce that? I think we may be able to magically intercept a call to the equals function of any object that implements This is a difficult problem. |
I think we should try to be as flexible as possible.
|
agree. the less magic, the less invisible and not obvious operations - the better a developer understands how it works, the more reliable code they write, the more they're happy with the ext - imo |
Sorry to weigh in on this over a year later, but I have to say as a simple PHP developer (not a C developer) this My 2 cents are: Surely the type should be This is basically how I implement it on most classes:
I would like to remove the docblock as I don't like pointless docs like that, it is only there to serve the static analysis of the code. Therefore I would like to see:
|
Thanks for weighing in, I would like to come up with a solution for this. Going by your example, would The difficulty with this method is that a hash table can store mixed keys, and if the hash is equal, we have to call equals to determine equality. I'll have to read through this thread again. |
I don't use PHPStorm so I wouldn't know about it particularly.
Do you mean instead of |
Good call re: specific class. So if we were to typehint equals, and a mixed key comes long, we can't pass it to equals or the type check will fail. I think the ideal is for the implementor to be able to typehint however they like, and a mixed key would then fail. I've drafted separating equals out and have hashable extend that. I'll look into whether we can relax the extension of equals. |
fyi, $this instanceof $that and $that instanceof $this |
Just noting that
I'm not sure if an explicit |
Current
Hashable::equals
signature isequals($obj): bool
, but in php docs it's said to bewhich differs. This difference leads to fatal for me:
First, I decided that this is a docs mistake, and was going to report it there. But then I understood that the docs is right in that the type declaration has to be there, because
and how do you suppose to implement this interface in scalars? Thus the
object
type is guaranteed.But it's still possible to call
Therefore, the signature has to be:
equals(?object $obj))
i.e. nullable object to prevent null errors.I propose you to add the type declaration to your code. Or at least, if you stick to current version, could you fix it in the docs?
And secondly, I'd appreciate if you explain the meaning of the phrase
Which code will guarantee this? I've just checked it:
doesn't produce any error or something...
Does the author mean that this is me who must must guarantee it? Do I have to write something like the following when implementing Hashable?
or is it supposed that ext-ds will somehow magically check this for me? If the latter, then I'd also like to file a bug report ('cause it doesn't check this)
The text was updated successfully, but these errors were encountered: