Re: [RFC][Draft] Body-less __construct

From: Date: Mon, 10 May 2021 16:58:01 +0000
Subject: Re: [RFC][Draft] Body-less __construct
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-114345@lists.php.net to get a copy of this message
On Mon, May 10, 2021, at 9:40 AM, Chase Peeler wrote: > On Mon, May 10, 2021 at 6:31 AM Guilliam Xavier <guilliam.xavier@gmail..com> > wrote: > > > On Mon, May 10, 2021 at 10:29 AM Matīss Treinis <mrtreinis@gmail.com> > > wrote: > > > > > Hi everyone, > > > > > > Since constructor property promotion is now implemented, and it looks > > > like it could become a widely used feature, I am proposing a small, > > > cosmetic change in syntax for constructors in concrete classes to do > > > away with empty constructor body. > > > > > > Here's an example of how this would work: > > > > > > <?php > > > namespace App; > > > > > > class Foo { > > > public function __construct( > > > private Bar $bar, > > > private Baz $baz > > > ); > > > } > > > > > > Some notes to this: > > > > > > - Since this is similar to already existing syntax for body-less > > > methods, parser should not be affected that much. I hope. I really have > > > no idea. > > > - Syntax would be optional - meaning, you can as well continue using > > > empty body, just that in this case the body would be implied empty.. > > > > > > Thoughts? > > > Regards, > > > - Matīss > > > > > > > > Hi, > > > > To me ; means not "empty body" (that's > > {}) but really "no definition, > > only declaration" (or "no body, only signature", and also "no code > > executed"), i.e. an *abstract* method (either explicitly declared so in a > > class, or implicitly in an interface). > > > > Granted, property promotion is already special (cannot be used in an > > abstract constructor), but it can also be mixed with non-promoted > > parameters and body, so I feat that your proposed alternative syntax would > > bring more confusion than convenience :s > > > > Regards, > > > > -- > > Guilliam Xavier > > > > I'm ambivalent about this proposal, but figured I'd throw out one advantage > to ; over {}. In my opinion, ending with a semicolon is a very clear way of > saying there is no body to the method, while having {} could indicate that > implementation of the body was intended but never actually done. I know > that when I'm writing new classes I often will set up the method signature > but leave the method body empty while I finish the code that utilizes that > method. I don't know if that is justification for the proposal, but it is > one reason why ; might be preferred over {}. > > > -- > Chase Peeler > chasepeeler@gmail.com I agree that in the grand scheme of things this would be a minor matter, but aesthetically I would prefer it as well. The {} annoys me, CS tools or no. Related: I feel the same way about empty-classes and interfaces, which is often the case for exceptions where all you're doing is declaring a type. I figure that would get even more pushback, though. :-) As a data point, you can already do the same "semi-colon for no body" for foreach, for, and while loops. I've only ever done it on foreach to runout an iterator, but it works on all three today. --Larry Garfield

« previous php.internals (#114345) next »