Skip to content

Strict equality operator overloading for the \GMP class #13826

Closed

Description

@Rafagd

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.

Activity

  1. SakiTakamachi commented on Mar 28, 2024

    @SakiTakamachi
    Member

    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.

  2. Rafagd commented on Mar 28, 2024

    @Rafagd
    Author

    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.

  3. iluuu1994 commented on Mar 28, 2024

    @iluuu1994
    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.

  4. Rafagd commented on Mar 29, 2024

    @Rafagd
    Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions