after coding a custom event in AS3, i’ve come across a curious problem:
override public function toString():String
{
switch (type)
{
case CHANGE: return formatToString("HistoryEvent", "type", "action", "name", "data");
case ABILITY: return formatToString("HistoryEvent", "type", "undoable", "redoable");
}
}
the above code returns the following compile-time error:
1170: Function does not return a
value.
i can remedy the problem easily by adding return null; at the end of the function, but that’s redundant and it annoys me that it seems to be the only solution.
why is returning a value from a switch case not seen by the compiler? is this an issue specific to the AS3 compiler or am i actually attempting something here that is so completely dangerous and adverse that it shadows counterculturists the world over.
It is possible that your switch doesn’t match, and in that case the function can’t return anything. That’s why the compiler complains. Adding
default: return null;to your switch statement solves the problem as well as addingreturn nullat the end of your function.