Hello Internals!
I'd like to gauge a reaction on a potential RFC proposal.
As per the RFC howto, I shall state that this would be a "concept" as I
don't have anyone lined up that would implement the feature, and I'm not a
C dev myself. (Though if no one volunteers I may take this as a learning
opportunity ;-) ).
The proposed feature is regarding method chaining.
There appear to be two design patterns that use it:
- Where you deliberately act on very different objects returned in a
"flow" like way, e.g.$boo = $App->getConfig()->getId('foobar')->getValue();
- For configuration, saving typing the original object multiple times
$FooBar ->loadConfig($Config) ->loadExtensions($Extensions) ->run() ;
For the former, it relies on you knowing what is returned, and makes a nice
way of using what was returned easily.
For the latter, the actual method itself returning the instance seems a bit
useless as actual information (unless dealing with immutable objects –
where it in-fact returns a new instance. But that's really just the
former case – since the method was designed with returning something in
mind anyway).
So I wonder whether for the latter case, it would almost be better to
implement that kind of method chaining as a language feature (instead of
(ab)using a return statement that doesn't have anything to do with the
method's role).
So I would propose a new operator, perhaps &>
is decent notation – but
that could be discussed further if we like the concept. Assuming it's &>
in the following though:
&>
would work almost exactly like ->
, except that instead of evaluating
to the value returned by the method call, it would evaluate to the object
on the LHS. A kind of "pass-through" operator if you will.
So instead of the author of the object adding support for this
configuration method chaining style (they might not be able to for all
methods), you could instead use the &>
operator to do it – this all in a
way that is clear to the reader too. You could even use it on methods that
do return things (if you don't care about what's returned).
I.e. write this (knowing that it would work: because language feature).
$Foo
&>bar()
&>baz()
;
Which would have the written meaning: "call $Foo
's bar()
and baz()
methods".
Other examples:
You want to instantiate an object and call a couple of methods, and also
get a reference to that object.
Before:
($Foo = new Foo)->bar();
$Foo->baz();
With the "pass-through" operator:
$Foo = (new Foo)&>bar()&>baz();
Thanks for reading if you got this far, I shall look forward to hearing
feedback.
Kind regards,
Aidan