-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
[5.6] Optional can be converted into string #23306
Conversation
Do you have a real-world use case? |
My own -- I use an external library and it uses a direct cast on the objects in a list, to populate different fields. It either takes We only implement those types of objects in a few places, but we use It can also potentially break the code if someone were to move from the instance of a specific object, to |
Based on the test, I don't think I understand the use case. An optional, after having a method called on it, is going to return either the expected result or swallow it and return nothing. Casting |
The goal was to make the optional class as much compatible as possible with the underlying object. |
Yes, but optional is used when you don't know if your variable contains an object or not. With your use case, optional doesn't actually do anything because the behavior is the same whether it's passed an object or null. With your code: $x = null;
(string) optional($x); // ""
(string) $x; // "" Why even use optional here? The normal usage is when you're doing something like calling a method |
@BrandonShar You only tested the null case. This is to fix the non-null case of casting optional to string.
|
Exactly what @36864 said. This is to make optional compatible with the underlying object, like I have said multiple times now. When you cast null to a string, you get “”, which is the expected behaviour. If you do not feel like making optional compatible with the underlying object as much as possible, then simply close this PR. |
I must not be being clear... Here's what you want to change optional to do: $x = null;
(string) optional($x) // ""
$x = User::find(1);
(string) optional($x) // {"id": 1, ...} And here's what happens if you don't use optional at all $x = null;
(string) $x // ""
$x = User::find(1);
(string) $x // {"id": 1, ...} So why are you using |
Consistency, mostly?
I'd also like to point out this PR, which is essentially the same type of addition (making optional work in a case where it wouldn't be needed at all): #22417 |
I wouldn't have merged #22417 either 😉 I understand your example now, I wasn't thinking of a situation like that. I would discourage storing I didn't write the original optional code, but I've always assumed it was based on the |
@TheDeadCode can you share the actual code snippet demonstrating how you want to use this? |
I'm still just confused on why you are echoing an optional result directly. Usually when optional is used it is to access some other property or method on the given value, not echo the optional itself. |
NB, in most other languages, to string is not directly proxied in an optional. They usually wrap the string up so you know you printed an optional. |
Closing pending inactivity. Feel free to re-open with more examples of how this would be used. |
Fix for #23303