Skip to content

[OptionsResolver] Support callable as lazy option default #31527

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

Closed
wants to merge 1 commit into from
Closed

[OptionsResolver] Support callable as lazy option default #31527

wants to merge 1 commit into from

Conversation

ogizanagi
Copy link
Contributor

@ogizanagi ogizanagi commented May 17, 2019

Q A
Branch? 4.4
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets N/A
License MIT
Doc PR Todo (complete https://symfony.com/doc/current/components/options_resolver.html#default-values-that-depend-on-another-option)

While I'm not aware of any limitation justifying it, OptionResolvers defaults as lazy value are limited to \Closure instances. This PRs adds support for any callable, by creating a \Closure instance from it (and keep checking the Options typehint is set on first argument):

$object = new class {
    public function resolve(Options $options): string {
        return 'lazy';
    }
};

$this->resolver->setDefault('foo', [$object, 'resolve']);

@ogizanagi ogizanagi added this to the next milestone May 17, 2019
@yceruto
Copy link
Member

yceruto commented May 17, 2019

IMHO you should use instead:

$this->resolver->setDefault('foo', \Closure::fromCallable([$object, 'resolve']));

See discussion about this topic in #28129.

@ogizanagi
Copy link
Contributor Author

ogizanagi commented May 22, 2019

Thank you @yceruto, I missed this issue.

While I understand the arguments against in linked issue, I'm not pretty much convinced myself here, and some mates were expecting this to work. As soon as the ambiguity is solved by the typehint check and thus cannot lead to any weird situation (nor BC break), this seems fine to me.

I can't remember the option(s) for which we dropped callables in the past mentioned by @xabbuh, but I suspect it's about #17993 which was a slighlty different problem (though we could still ignore callable strings here IMHO).

This PR was motivated by seeing people & myself expecting it to work this way at first glance. Anyway, the alternative is short and simple. Let's reject & close this PR if this still does not make sense to you & the community.

@nicolas-grekas
Copy link
Member

nicolas-grekas commented May 22, 2019

Reading the linked issue, this is not about making sense or not: there are technical arguments against (BC break by ambiguity).

👎 for this reason (without trying to discuss anything else :) )

@ogizanagi
Copy link
Contributor Author

The technical arguments expressed in #17993 are about setAllowedValues and are mitigated here by the fact we're talking about setDefault for which we already ignore any signature not matching the Options typehint requirement for the first argument.

@xabbuh
Copy link
Member

xabbuh commented May 23, 2019

How would you decide whether the user wanted to use a string or a callable if the string represents both?

@ogizanagi
Copy link
Contributor Author

ogizanagi commented May 23, 2019

@xabbuh : I'd suggest to remove the callable strings support from this PR anyway. Does not seem much common to me and would be consistent with #17993.
But even if we don't, to answer your question: the same way it is for closures right now, i.e we check the first arg typehint.

  • If it's Options, it's a lazy default value.
  • If not, it is used directly as default value.

@ogizanagi ogizanagi changed the base branch from master to 4.4 May 28, 2019 16:39
@ogizanagi
Copy link
Contributor Author

Alright. No new approval and still a -1 from a core team member. Let's close :)

@ogizanagi ogizanagi closed this Jun 14, 2019
@ogizanagi ogizanagi deleted the feat/options-resolver-lazy-callable branch June 14, 2019 08:15
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.4 Oct 27, 2019
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.

6 participants