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

[WIP] Prototype: Support object types in BCMath #13741

Draft
wants to merge 18 commits into
base: master
Choose a base branch
from

Conversation

SakiTakamachi
Copy link
Member

@SakiTakamachi SakiTakamachi commented Mar 17, 2024

Please see https://externals.io/message/122651

(This is a branch derived from #13096.)

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

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.

None yet

2 participants