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

[5.5] Make Optional@__call really macroable #20898

Merged
merged 1 commit into from
Sep 1, 2017

Conversation

n7olkachev
Copy link
Contributor

@n7olkachev n7olkachev commented Aug 31, 2017

We need to firstly check existence of macro and only then proxy __call to the value. Otherwise it won't be really macroable (because macros won't work for Optional with value inside).

We need to firstly check existence of macro and only then pass it to the value. Otherwise it won't be really macroable (because macros won't work for Optional with value inside).
@thecrypticace
Copy link
Contributor

This is an interesting case. I would think the check should use is_object && method_exists first before the macro. Otherwise you could register a macro and accidentally break tons of stuff. And maybe by something you don't directly control, like an auto-registered service provider.

Though, this way of doing it wouldn't work for objects implementing __call. Honestly, it feels wrong that this class is even Macroable because it's meant only as a "do this if not null" pass through.

Do with this what you wish. This is just me putting my two cents in.

@n7olkachev
Copy link
Contributor Author

n7olkachev commented Aug 31, 2017

Indeed, but we already have similar Macroable functionality in, for example, Eloquent\Builder class, which is also could be easily broken with global macros.

@thecrypticace
Copy link
Contributor

thecrypticace commented Aug 31, 2017

True but the builder is only delegated to by a model if a method isn't found. Here optional isn't going to define any methods except the ones necessary to delegate to the underlying value.

It's mainly about how it's going to be used. ¯\_(ツ)_/¯

@taylorotwell
Copy link
Member

Tempted to just make it not macroable at this point.

@taylorotwell taylorotwell merged commit 979d693 into laravel:5.5 Sep 1, 2017
Ellrion added a commit to Ellrion/framework that referenced this pull request Sep 1, 2017
@n7olkachev
Copy link
Contributor Author

n7olkachev commented Sep 1, 2017

But you can just move this behavior to your macro:

Optional::macro('present', function () {
    if (is_object($this->value)) {
        return $this->value->present();
    }
    return new Optional(null);
});

@n7olkachev
Copy link
Contributor Author

You are saying it like you are going to have tons of macros and it is really painful to write 3 lines of code. Do you really think that it is good idea to remove feature just not to write 3 lines of code in your macro?

You can checkout similar monads or Optional from Java for possible macros. Without this PR, it won't be possible.

@n7olkachev
Copy link
Contributor Author

n7olkachev commented Sep 1, 2017

Optional::macro('orElse', function ($value) {
    if (is_object($this->value)) {
        return $this->value; // nope, I don't want to proxy orElse
    }
    return value($value);
});

@n7olkachev n7olkachev deleted the patch-1 branch September 1, 2017 15:39
@n7olkachev
Copy link
Contributor Author

@derekmd jfyi 1 year+ passed and there were no PRs. There were no need to be rude.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants