Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[String] add LazyString to provide memoizing stringable objects#34298

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:masterfromnicolas-grekas:string-lazy
Feb 3, 2020

Conversation

nicolas-grekas
Copy link
Member

@nicolas-grekasnicolas-grekas commentedNov 8, 2019
edited
Loading

QA
Branch?master
Bug fix?no
New feature?yes
Deprecations?no
Tickets-
LicenseMIT
Doc PR-

Replaces#34190

The proposedLazyString class is a value object that can be used in type declarations of our libraries/apps.

Right now, when a method accepts or returns "strings|stringable-objects", the most accurate type declaration one can use isstring|object (either in docblocks or in union types in PHP 8). The goal ofLazyString is to allow one to usestring|LazyString instead and gain type-accuracy, thus type-safety while doing so.

Another defining property of the proposed class is also that it memoizes the computed string value so that the computation happens only once.

Two factories are provided to create aLazyString instance:

  • LazyString::fromStringable($value): self -> turns any object with__toString() into aLazyString
  • LazyString::fromCallable($callback, ...$arguments): self -> delegates the computation of the string value to a callback (optionally calling it with arguments).

Two generic helpers are also provided to help deal with stringables:

  • LazyString::isStringable($value): bool -> checks whether a value can be safely cast to string, considering__toString() too. This replaces the boilerplate we all have to write currently (is_string($value) || is_scalar($value) || is_callable([$value, '__toString']))
  • LazyString::resolve($value): string -> casts a stringable value into a string. This is similar to the casting(string) operator or tostrval(), but it throws aTypeError instead of a PHP notice when a non stringable is passed. This helps e.g. with code that enabled strict types and want to maintain compatibility with stringable objects.

An additional feature ofLazyString instances is that they allow exceptions thrown from the wrapped__toString() methods or callbacks to be propagated. This requires having theErrorHandler class from theDebug orErrorHandler components registered as a PHP error handler (already the case for any Symfony apps by default). As a reminder, throwing from__toString() is not possible natively before PHP 7.4.

Devristo reacted with thumbs up emoji
@nicolas-grekasnicolas-grekas added this to the5.0 milestoneNov 8, 2019
@nicolas-grekasnicolas-grekas modified the milestones:5.0,nextNov 8, 2019
@nicolas-grekasnicolas-grekas changed the title[String] add LazyString to the component[String] add LazyString to provide generic stringable objectsDec 2, 2019
@nicolas-grekasnicolas-grekasforce-pushed thestring-lazy branch 2 times, most recently from04a8350 to6bffaf5CompareDecember 2, 2019 09:43
@nicolas-grekas
Copy link
MemberAuthor

@symfony/mergers PR is ready. I updated the description to better explain what this does.

@nicolas-grekasnicolas-grekas changed the title[String] add LazyString to provide generic stringable objects[String] add LazyString to provide memoized stringable objectsJan 23, 2020
@nicolas-grekasnicolas-grekas changed the title[String] add LazyString to provide memoized stringable objects[String] add LazyString to provide memoizing stringable objectsJan 23, 2020
@nicolas-grekasnicolas-grekasforce-pushed thestring-lazy branch 3 times, most recently from2670fac to3fa9d12CompareJanuary 24, 2020 09:42
@fabpot
Copy link
Member

Thank you@nicolas-grekas.

fabpot added a commit that referenced this pull requestFeb 3, 2020
…e objects (nicolas-grekas)This PR was merged into the 5.1-dev branch.Discussion----------[String] add LazyString to provide memoizing stringable objects| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes| Deprecations? | no| Tickets       | -| License       | MIT| Doc PR        | -Replaces#34190The proposed `LazyString` class is a value object that can be used in type declarations of our libraries/apps.Right now, when a method accepts or returns "strings|stringable-objects", the most accurate type declaration one can use is `string|object` (either in docblocks or in union types in PHP 8). The goal of `LazyString` is to allow one to use `string|LazyString` instead and gain type-accuracy, thus type-safety while doing so.Another defining property of the proposed class is also that it memoizes the computed string value so that the computation happens only once.Two factories are provided to create a `LazyString` instance:- `LazyString::fromStringable($value): self` -> turns any object with `__toString()` into a `LazyString`- `LazyString::fromCallable($callback, ...$arguments): self` -> delegates the computation of the string value to a callback (optionally calling it with arguments).Two generic helpers are also provided to help deal with stringables:- `LazyString::isStringable($value): bool` -> checks whether a value can be safely cast to string, considering `__toString()` too. This replaces the boilerplate we all have to write currently (`is_string($value) || is_scalar($value) || is_callable([$value, '__toString'])`)- `LazyString::resolve($value): string` -> casts a stringable value into a string. This is similar to the casting `(string)` operator or to `strval()`, but it throws a `TypeError` instead of a PHP notice when a non stringable is passed. This helps e.g. with code that enabled strict types and want to maintain compatibility with stringable objects.An additional feature of `LazyString` instances is that they allow exceptions thrown from the wrapped `__toString()` methods or callbacks to be propagated. This requires having the `ErrorHandler` class from the `Debug` or `ErrorHandler` components registered as a PHP error handler (already the case for any Symfony apps by default). As a reminder, throwing from `__toString()` is not possible natively before PHP 7.4.Commits-------4bb19c6 [String] add LazyString to provide generic stringable objects
@fabpotfabpot merged commit4bb19c6 intosymfony:masterFeb 3, 2020
@nicolas-grekasnicolas-grekas deleted the string-lazy branchFebruary 5, 2020 12:31
@nicolas-grekasnicolas-grekas modified the milestones:next,5.1May 4, 2020
@fabpotfabpot mentioned this pull requestMay 5, 2020
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@stofstofstof left review comments

@fabpotfabpotfabpot approved these changes

@lyrixxlyrixxlyrixx approved these changes

@chalasrchalasrchalasr approved these changes

Assignees
No one assigned
Projects
None yet
Milestone
5.1
Development

Successfully merging this pull request may close these issues.

6 participants
@nicolas-grekas@fabpot@lyrixx@stof@chalasr@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp