Jump to content
  • Advertisement
Sign in to follow this  
Miss

Syntax suggestion for if statements

This topic is 625 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!