Hacker News new | ask | show | jobs
by AlotOfReading 1117 days ago
For some additional context, this came up for me because some other team had snuck verbiage into our style guide that mandated things like this instead of traditional initializers:

    void foo() {
      if (auto [a, b, c] = std::make_tuple(x, y, z); true) { /* [...] */ } 
    }
Yeah you can still read this, but it's stupid to support constructs creating scope, doing control flow, and initializing arbitrarily many variables simultaneously (which may invoke constructors of their own). The relatively minor benefits to things like iterators are not outweighed by the burden of supporting this stupid code.
1 comments

If the point is to limit the scope of specific variables, wouldn't it be simpler to write

    void foo() {
        {
            auto [a, b, c] = std::make_tuple(x, y, z);
            /* [...] */
        } 
    }
I think the original comment's point is exactly that. But people see the new expression type and want to apply it in this case.

I'm of two minds on this. I can see the impulse and why you'd reach for it: Block lexical scopes with no if/while/etc statement attached read a bit odd. Introducing an "unattached" block means as a reader/reviewer I want to know why the scope has been created. So in this new syntax I suppose makes it "clearer" (in some respects) that what is being done here is introducing a new lexical scope specifically for the given variables. Like I commented elsewhere, this is somewhat similar to the ML-languages "let <assignments> in <block>" syntax, which I have always found admirable, as it makes clear to the reader (and compiler) what scope and state are being dealt with.

On the other hand, this is so out of step with C/C++ style generally, and it seems so excessively "clever" that I think it's going to piss people off. And because it's bolted onto the conditional expr, you get the pointless ;true there.

Having a with ( ... ) syntax would have been nicer?

  with (auto [a, b, c] = std::make_tuple(x, y, z)) {
  }
I'm curious what Titus Winters and the Google C++ style guide is saying about this.
I'm actually not curious about what Google's guides say, and don't accept them as technical leaders.

Having said that: It's not really unlike C++, since you have it in for loops:

    for (int x = 0; x < n; x++) { do_stuff(); }
which is like

    {
        int x;
        for (x = 0; x < n; x++) { do_stuff(); }
    }

anyway, I wouldn't mind the syntactic sugar of "with X=Y do Z" or "let X=Y in Z"