🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Syntax suggestion for if statements

Started by
3 comments, last by Miss 7 years, 4 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);
Advertisement

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

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