Hacker News new | past | comments | ask | show | jobs | submit login

Note to the author: Since you specifically point out that C# 7.0 is supported, this example:

    if (ev is InputEventMouse)
    {
        var mouseEvent = (InputEventMouse)ev;
        Vector2 mousePos = mouseEvent.Position;
    }
    else if (ev is InputEventKey)
    {
        var keyEvent = (InputEventKey)ev;
        bool aPressed = keyEvent.Pressed && keyEvent.Scancode == GD.KEY_A;
    }
would be better written as:

    switch (ev)
    {
        case InputEventMouse mouseEvent:
            Vector2 mousePos = mouseEvent.Position;
            break;

        case InputEventKey keyEvent:
            bool aPressed = keyEvent.Pressed && keyEvent.Scancode == GD.KEY_A;
            break;
    }



Or:

    if (ev is InputEventMouse mouseEvent)
    {
        Vector2 mousePos = mouseEvent.Position;
    }
    else if (ev is InputEventKey keyEvent)
    {
        bool aPressed = keyEvent.Pressed && keyEvent.Scancode == GD.KEY_A;
    }


I think switch blocks are only worth it when you have 2 or more `else if`, otherwise a normal `if` is more readable IMHO.


Normally I'd agree, but I really like the concise but readable way this handles the casting for you.


It might be if they used the One True Brace style ;)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: