Skip to content
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

Strict equality operator overloading for the \GMP class #13826

Closed
Rafagd opened this issue Mar 28, 2024 · 4 comments
Closed

Strict equality operator overloading for the \GMP class #13826

Rafagd opened this issue Mar 28, 2024 · 4 comments

Comments

@Rafagd
Copy link

Rafagd commented Mar 28, 2024

Description

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:

gmp_init(0) + gmp_init(1); // $x = \GMP { 1 }
gmp_init(0) + 1; // $x = \GMP { 1 }
gmp_init(0xFFFF_FFFF) & 1; // 0x1
gmp_init(0) < 1; // true
gmp_init(0) > 1; // false
gmp_init(133) == 133; // true 

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.

@SakiTakamachi
Copy link
Member

SakiTakamachi commented 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
Copy link
Author

Rafagd commented 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 == $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.

@iluuu1994
Copy link
Member

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.

@Rafagd
Copy link
Author

Rafagd commented Mar 29, 2024

I'm closing the issue, as it's a subset of the one mentioned by @iluuu1994

@Rafagd Rafagd closed this as completed Mar 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants