Re: Cascade Operator (was Re: [PHP-DEV] Proposal for php7 class method return)
| From: | Jesse Schalken | Date: | Mon, 11 Jul 2016 23:14:52 +0000 |
| Subject: | Re: Cascade Operator (was Re: [PHP-DEV] Proposal for php7 class method return) | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-94482@lists.php.net to get a copy of this message | ||
On Tue, Jul 12, 2016 at 2:30 AM, Rasmus Schultz <rasmus@mindplay.dk> wrote:
>
> Would something like ->> be ambiguous as well? That's fairly close too
> - a double-headed arrow, not unlike the double dots of other
> languages...
>
>
A few weeks ago I proposed a syntax for setting multiple object properties
in an expression with the result as the object (
http://news.php.net/php.internals/93662). Both
that feature and the cascade
operator can be considered instances of a broader need to manipulate an
object as an expression with the result being the object itself, and either
feature could support both calling a method and setting a property:
$this->setBlah(
Blah::create(4)
->>foo = $foo
->>baz = ((new Baz())
->>markFixed()
->>label = "Hello"
)
->>setBot(9)
);
$this->setBlah(
Blah::create(4) {
foo = $foo,
baz = new Baz() {
markFixed(),
label = "Hello",
},
setBot(9),
}
);
Would it be incohesive to have two different syntaxes for setting
properties and calling methods, like below?
$this->setBlah(
Blah::create(4) {
foo = $foo,
baz = (new Baz())
->>markFixed()
{ label = "Hello" },
}
->>setBot(9)
);
If so, some consideration should be made as to which syntax is preferred to
solve this problem generally for both setting properties and calling
methods.
I prefer the "obj { .. }" syntax because it mirrors object literals in
JSON/JavaScript, object initializers in C# and C/C++ and Haskell record
syntax, is particularly concise when nested to create large trees of
objects, and avoids the noise of "->>" on each line.