On Friday, 4 November 2016 at 16:15:21 UTC, Steven Schveighoffer wrote:
It's just a strawman type, I'm sure there's ways to handle these things, I just didn't put a lot of effort into all the cases.

But really it's just syntax to separate the bool check from the value itself. You can get more elaborate if you want with the comparison. The difficult thing is having a type that you use normally, but that when you use in an if statement, it gives what you want. And declaring that type in the if clause itself (which is allowed in restricted circumstances).

This also isn't exactly ideal for things like int, as if(i) would now be unexpected.

It is just a basic type but ultimately you will probably need more than one type with a different way of operating to get the same behavior. That and having part of the code as a string which makes it harder to read and no syntax highlighting for the code in text editors/ides.

What do you mean if(i) would be unexpected?

I thought you could only declare variables in the first clause?

In any case, I think Stefan really has the best answer:

{int value; if(auto i = someFunc(&value))
{
    // ...
}}

Sure, it doesn't look great. But my expectation is that your proposal doesn't pull enough weight to be integrated into the language. There are enough ways to solve this problem that don't involve language changes.

-Steve

It follows the same rules for an if statement condition which an if statement allows "if(auto i = foo())".

It's syntactic sugar, most of the existing language features don't serve a functional purpose. They simply serve to simplify another syntax. Every "foreach" statement could be written as a for-statement. Every for-statement could be written as a while-statement. And so on so forth. It doesn't add anything that new either. It follows the same syntax as a for-statement.

It's not that big of a language feature, it's just a small syntactic change, which comes down to. Is this easier to read compared to this.

    {int value; if(auto i = someFunc(&value))
    {
        // ...
    }}

vs.

    if(int value; auto i = someFunc(&value))
    {
        // ...
    }


I think it makes it easier to read and it fits with how the for-statement operates. I write code everyday that could utilize this if-statement syntax, so I thought I might as well bring it up. But if there isn't that much interest in it then I won't bother with a DIP.

Reply via email to