Advertisement

Syntax suggestion for if statements

Started by February 02, 2017 12:34 PM
3 comments, last by Miss 7 years, 9 months ago
This code snippet currently doesn't work, but would be very helpful syntax, which I believe is valid in a lot of other languages. Thoughts?
        if ((auto wText = cast<TextWidget>(w)) !is null)
            wText.SetColor(col);
        else if ((auto wButton = cast<ButtonWidget>(w)) !is null)
            wButton.SetColor(col);

I'll think about it.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement

I don't actually know a language where the above is valid but I can imagine that some exist. However, it may be worth considering that C++17 introduces a similar but more consistent and, to me, far better looking syntax, for if and switch statements, similar to that of for loops:


if (auto it = m.find(10); it != m.end()) {
    return it->size();
}

See init-statement here and here.

funny. I had considered exactly the analogy of declaring variables in for() loops to accomplish the suggestion that Ansjh brought up. It makes me pleased to know that this same idea has already been introduced in C++17.

I agree that the syntax introduced in C++17 is better and also consistent with the already existing syntax in for-loops.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Ah, right, I knew it was more like for() and I think I've seen it in several languages. That C++17 syntax is indeed a lot nicer.

This topic is closed to new replies.

Advertisement