JavaScript switch returns warn for unreachable code.

I am getting a weird warning in a switch statement that return back values based on the type of parameter, but it makes no sense to me why this happen! Is it because it expect fall-through to the other cases? When I double click the error it highlights the following line of code: switch (parameterType) {.

Obviously I can't put break in there either since it would warn for the break being unreachable. Is the only solution to set a return variable and break out of it or can I squelch the warning otherwise?

In C# it's possible to ignore selective warnings:

#pragma warning disable 414, 3021

BCW0015: WARNING: Unreachable code detected.

function GetParam(gameObject : GameObject, interactor : GameObject) {
    switch (parameterType) {
    case ParameterType.NoParameter:
        return null;
    case ParameterType.GameObject:
        return gameObject;
    case ParameterType.Interactor:
        return interactor;
    case ParameterType.Str:
        return parameter;
    case ParameterType.Float:
        return float.Parse(parameter);
    case ParameterType.Integer:
        return int.Parse(parameter);
    case ParameterType.FindChild:
        return gameObject.transform.Find(parameter);
    case ParameterType.FindGameObject:
        return GameObject.Find(parameter);
    case ParameterType.GetComponent:
        return gameObject.GetComponent(parameter);
    }
}

No warnings :P

function GetParam(gameObject : GameObject, interactor : GameObject) {
    var result;
    switch (parameterType) {
    case ParameterType.NoParameter:
        result = null;
        break;
    case ParameterType.GameObject:
        result = gameObject;
        break;
    case ParameterType.Interactor:
        result = interactor;
        break;
    case ParameterType.Str:
        result = parameter;
        break;
    case ParameterType.Float:
        result = float.Parse(parameter);
        break;
    case ParameterType.Integer:
        result = int.Parse(parameter);
        break;
    case ParameterType.FindChild:
        result = gameObject.transform.Find(parameter);
        break;
    case ParameterType.FindGameObject:
        result = GameObject.Find(parameter);
        break;
    case ParameterType.GetComponent:
        result = gameObject.GetComponent(parameter);
        break;
    }
    return result;
}

I have a vague recollection of this topic (or a similar one) being discussed recently, but I don't know where or when that was. What if you add 'default: return null;' to the first example; does that get rid of the warning?

No, default doesn't help. In C# you can ignore warnings selectively. I was hoping to be able to do the same in JS, but the syntax won't work in JS (see updated question).

1 Answer

1

I don't believe UnityScript has the ability to do it - the preprocessor directives it exposes are pretty lacking

I didn't start out with JS as I learned Unity so I don't know where to find the proper resources, but maybe you do? Is there any place where I can get a specification of the JS implementation? I'm sick of guessing and I thought I'd learn it once and for all.

There isn't one - the stuff I know is a mix of the data on the wiki, the documentation, and a few years of helping in IRC :S

Great! Really surprised that it uses Automatic by default. Glad it helped.