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

Allow to pass a thunk as a message factory to assertions #221

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

simPod
Copy link
Contributor

@simPod simPod commented Oct 29, 2020

It is useful in order to delay generating a message.

At the moment the assertion does not pass the message is generated. Otherwise the thunk is not executed.

@simPod simPod force-pushed the lazy branch 2 times, most recently from f1379a5 to 48fb067 Compare October 29, 2020 10:27
* @param mixed $value
* @param string $message
* @param mixed $value
* @param string|callable():string $message
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did not implement it to all assertions. If RFC is accepted, I'll add it everywhere

Copy link
Collaborator

@shadowhand shadowhand Oct 17, 2024

Choose a reason for hiding this comment

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

Actually, looking at this again, I wonder if using Stringable|string would be more appropriate, thus dropping the need for a closure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How would you expect it to work?

my proposal is

Assert::string(1, fn() => heavy compute here)

With Stringable it has to be more verbose

Assert::string(1, new class() { public function __toString() { ... } })

Copy link
Collaborator

@shadowhand shadowhand Oct 17, 2024

Choose a reason for hiding this comment

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

@simPod yes, more verbose. And also more type safe, given that callable(): string is not enforceable (without static analysis) but Stringable|string is. 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

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

In any case, I am not merging new features right now, but this is high priority for the next feature release.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as array<object|string> is not enforceable in isInstanceOfAny, that's not valid argument IMO.

I'm not asking for merge now

@zerkms
Copy link
Contributor

zerkms commented Oct 29, 2020

What would be the real life use case scenario for this?

@simPod
Copy link
Contributor Author

simPod commented Oct 29, 2020

Sometimes my error messages are

  1. heavy to compute
  2. touch object properties that are not initialised and are not needed to be initialised except for the error message

so I'd rather skip that for happy path.

@simPod
Copy link
Contributor Author

simPod commented Apr 29, 2022

@BackEndTea Hi, is there a will to add lazy messages?

@shadowhand
Copy link
Collaborator

Interesting idea, and certainly something I would consider for the next feature release.

It is useful in order to delay generating a message.

At the moment the assertion does not pass the message is generated. Otherwise the thunk is not executed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants