Do you think if statement as expression would be nice to have in D?
H. S. Teoh
hsteoh at qfbox.info
Mon Jun 6 17:43:47 UTC 2022
On Mon, Jun 06, 2022 at 05:07:30PM +0000, deadalnix via Digitalmars-d wrote:
> On Saturday, 4 June 2022 at 21:17:56 UTC, SealabJaster wrote:
> > On Saturday, 4 June 2022 at 19:54:48 UTC, Dukc wrote:
> > > ```D
> > > auto number = 200;
> > > auto myVar = type == "as-is"?
> > > number:
> > > { number *= 2;
> > > number += 2;
> > > number /= 2;
> > > number = number > 300 ? 200 : 100;
> > > return number;
> > > }();
> > > ```
> >
> > This next question comes from a place of ignorance: What is the
> > codegen like for this code? Would is allocate a closure on the GC
> > before performing the execution, or are the compilers smart enough
> > to inline the entire thing?
>
> LDC is able to inline the lambda and then optimize away the
> allocation. DMD is not.
>
> https://godbolt.org/z/5vMcz4s14
Yeah, when in doubt, trust LDC to do the "right thing". :-P Well, that,
and take a look at the generated assembly to see what it actually does.
For questions of performance or codegen quality, I usually don't even
bother looking at DMD output.
T
--
Unix was not designed to stop people from doing stupid things, because that would also stop them from doing clever things. -- Doug Gwyn
More information about the Digitalmars-d
mailing list