You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Strict equality operator overloading for the \GMP class #13826
The GMP class already enjoys a lot of privileges in regards to having the language operators being overloaded under the hood to behave in a sensible way:
This is all good but the strict equality operator is the one that behaves in a weird way. While I do understand it would be bad to have \GMP { 0 } === int { 0 } to be true, I do think it's very odd that gmp_init(0) === gmp_init(0) evaluates to false.
While I do understand they are different objects that have different pointers associated to them, I think this is just an interpreter implementation detail that shouldn't surface to the PHP user.
That kinda shuts down a more useful usage [IMO]: these are both the same number with the same type.
A GMP object is just a big integer representation of a number, and as such, the identity of one single instance of the object is a bit of a commodity. Any copy of the object that hold the same value is somewhat indistinguishable of another.
Currently $x == $y behaves with the loose integer comparison semantics. If both $x and $y are GMP, true if the value they hold is the same, if one of them is not, it will be converted into GMP implicitly using gmp_init if it's int or string and then compared.
The $x === $y uses the strict comparison semantics, but it uses the standard rules for objects, as in "Are they exactly the same object in memory? then true, otherwise false". Which is a bit of an implementation detail, given most operators are overloaded to pretend GMP is just a normal number.
I'll post it to the mailing list later, I agree it would be a breaking change that requires a proper discussion.
Strict equality is not currently customizable in PHP, even in extensions. There's definitely a larger discussion required for something like this. Coincidentally, I'm working on data classes (i.e. classes as value types) which includes this feature (#13800). GMP would be a good use-case for value type semantics.
Activity
SakiTakamachi commentedon Mar 28, 2024
If do make such changes, how can we be sure that the GMP objects are exactly the same?
Also, what is the difference between
==and===?In any case, GitHub issues are not suitable for such discussions, so I encourage you to post your ideas on the mailing list.
Rafagd commentedon Mar 28, 2024
A GMP object is just a big integer representation of a number, and as such, the identity of one single instance of the object is a bit of a commodity. Any copy of the object that hold the same value is somewhat indistinguishable of another.
Currently
$x == $ybehaves with the loose integer comparison semantics. If both $x and $y are GMP, true if the value they hold is the same, if one of them is not, it will be converted into GMP implicitly using gmp_init if it's int or string and then compared.The
$x === $yuses the strict comparison semantics, but it uses the standard rules for objects, as in "Are they exactly the same object in memory? then true, otherwise false". Which is a bit of an implementation detail, given most operators are overloaded to pretend GMP is just a normal number.I'll post it to the mailing list later, I agree it would be a breaking change that requires a proper discussion.
iluuu1994 commentedon Mar 28, 2024
Strict equality is not currently customizable in PHP, even in extensions. There's definitely a larger discussion required for something like this. Coincidentally, I'm working on data classes (i.e. classes as value types) which includes this feature (#13800).
GMPwould be a good use-case for value type semantics.Rafagd commentedon Mar 29, 2024
I'm closing the issue, as it's a subset of the one mentioned by @iluuu1994