Skip to content

[RFC] Support object types in BCMath #13741

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

Merged
merged 63 commits into from
Sep 4, 2024

Conversation

SakiTakamachi
Copy link
Member

@SakiTakamachi SakiTakamachi commented Mar 17, 2024

@zmitic
Copy link

zmitic commented May 4, 2024

Sorry for barging as end-user, but I am really interested if this RFC will allow lazy evaluation.

For example:

class LazyInt extends Number
{
	private int|null $cachedValue = null;

    /** 
     * @param Closure(): int $closure 
     */
    public function __construct(private Closure $closure){}

	#[Override]
    public function getValue(): int
    {
        return $this->cachedValue ??= ($this->closure)(); // one-time lazy evaluation
    }
}

and then use like:

function slow(): int
{
	sleep(5); // some slow query

	return 42;
}

$lazy1 = new LazyInt(fn() => slow()); // nothing happens
$lazy2 = new LazyInt(fn() => slow()); // nothing happens

$lazy1 + $lazy2; // sleeps 10 seconds, returns Number(84);
$lazy1 + $lazy2; // no sleep, returns Number(84);

This lazy evaluation has many use-cases, but it would require something like this:

class Number extends \Stringable
{
	public function getValue(): string|int;
}

@SakiTakamachi
Copy link
Member Author

@zmitic

After much discussion, it was decided that this class should be marked final, so it cannot be inherited :)

https://wiki.php.net/rfc/support_object_type_in_bcmath

@zmitic
Copy link

zmitic commented May 5, 2024

@SakiTakamachi
Thanks, I thought it was still under discussion but externals are too hard to read with nested comments.

Last one, I promise: will you be open to create NumberInterface that users could implement? This would open the door to many new things. But by locking users from custom implementations, I think that operator overload will never become a thing.

@SakiTakamachi SakiTakamachi force-pushed the rfc/bcmath-support-object branch from 1dcfbc7 to e118e60 Compare May 22, 2024 13:56
@SakiTakamachi
Copy link
Member Author

SakiTakamachi commented May 22, 2024

I'll merge this after CI passes

I made a mistake in the PR 😱

@SakiTakamachi
Copy link
Member Author

Editing the comment was not enough so I will comment again. The following comment is incorrect.

#13741 (comment)

Actually, I was trying to comment on the next PR, but accidentally commented on this one.

#14297 (comment)

@SakiTakamachi SakiTakamachi force-pushed the rfc/bcmath-support-object branch 6 times, most recently from 4693919 to 96c2ada Compare July 4, 2024 13:30
@SakiTakamachi SakiTakamachi changed the title [WIP] Prototype: Support object types in BCMath [WIP] [RFC] Support object types in BCMath Jul 4, 2024
@SakiTakamachi SakiTakamachi force-pushed the rfc/bcmath-support-object branch 4 times, most recently from 6946f43 to 0deefed Compare July 9, 2024 01:42
@SakiTakamachi SakiTakamachi force-pushed the rfc/bcmath-support-object branch 7 times, most recently from 4c5c237 to ad96f50 Compare July 17, 2024 04:51
@SakiTakamachi SakiTakamachi force-pushed the rfc/bcmath-support-object branch 2 times, most recently from c3e7a8d to 246219e Compare August 26, 2024 04:09
@SakiTakamachi SakiTakamachi force-pushed the rfc/bcmath-support-object branch from bf73b54 to 0afc106 Compare September 4, 2024 02:04
@SakiTakamachi SakiTakamachi merged commit fad899e into php:master Sep 4, 2024
1 of 2 checks passed
@SakiTakamachi SakiTakamachi deleted the rfc/bcmath-support-object branch September 4, 2024 02:12
Comment on lines +1121 to +1123
case IS_NULL:
*lval = 0;
return SUCCESS;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is null accepted here? This doesn't seem to be the case, looking at the signatures?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Girgias

This is for the same reason that there are problems with operators on GMP objects and null.

When merged this, it was intended to match the behavior of regular operators.

Since this behavior should be consistent, it would be best to discuss it together in my PR thread on GMP.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK.

derrabus added a commit to doctrine/dbal that referenced this pull request Jan 6, 2025
|      Q       |   A
|------------- | -----------
| Type         | feature
| Fixed issues | #6644

#### Summary

PHP 8.4 introduces a new `Number` class
([RFC](https://wiki.php.net/rfc/support_object_type_in_bcmath),
php/php-src#13741) that feels like a perfect fit for `DECIMAL` values.
This PR introduces a new `NumberType` class and a corresponding
`Types::NUMBER` constant which maps decimal values to `Number` objects
instead of plain strings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants