Re: Brainstorming idea: inline syntax for lexical (captured) variables
| From: | Michał Marcin Brzuchalski | Date: | Fri, 14 Apr 2023 04:35:39 +0000 |
| Subject: | Re: Brainstorming idea: inline syntax for lexical (captured) variables | ||
| References: | 1 2 3 4 5 6 7 8 9 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-120036@lists.php.net to get a copy of this message | ||
Hi Nicolas,
czw., 13 kwi 2023 o 14:40 Nicolas Grekas <nicolas.grekas+php@gmail.com>
napisał(a):
> Hi Rowan, hi all!
>
> Le ven. 17 mars 2023 à 15:51, Larry Garfield <larry@garfieldtech.com> a
> écrit :
>
> > On Thu, Mar 16, 2023, at 6:05 PM, Rowan Tommins wrote:
> > > On 16/03/2023 22:14, Larry Garfield wrote:
> > >> Wouldn't the functionality described boil down to essentially just
> > materializing into a few extra lines in the constructor? At least to my
> > ignorant non-engine brain it seems straightforward to have this:
> > >>
> > >> $a = 1;
> > >> $b = 2;
> > >> $c = 3;
> > >>
> > >> $o = new class ($a, $b) use ($c) {
> > >> public function __construct(private int $a, private int $b) {}
> > >> public function something() {}
> > >> }
> > >>
> > >> Desugar to this:
> > >>
> > >> $c = class ($a, $b) use ($c) {
> > >> private $c;
> > >> public function __construct(private int $a, private int $b) {
> > >> $this->c = 3; // By value only, so this should be fine?
> > >> }
> > >> public function something() {}
> > >> }
>
Have you thought about not populating property by default but instead:
* adding "use" language construct as default to all methods?
* adding ability to assign variable values from "use" to property if needed?
Like desugar to
$c = class ($a, $b) use ($c) {
private $c = $c; // optional, not required, no conflicts
public function __construct(private int $a, private int $b) use ($c) {
$this->c = $c % 2 ? 3 : 5;
}
public function something() use ($c) {
return $c;
}
}
Or there is something so wrong with this thinking I cannot see yet.
Cheers,
Michał Marcin Brzuchalski