castSwitch

fnauto castSwitch(choices...)(Object switchObject)

Executes and returns one of a collection of handlers based on the type of the switch object.

The first choice that switchObject can be casted to the type of argument it accepts will be called with switchObject casted to that type, and the value it'll return will be returned by castSwitch.

If a choice's return type is void, the choice must throw an exception, unless all the choices are void. In that case, castSwitch itself will return void.

Throws

If none of the choice matches, a SwitchError will be thrown. SwitchError will also be thrown if not all the choices are void and a void

choice was executed without throwing anything.

Parameters

choicesThe choices needs to be composed of function or delegate handlers that accept one argument. There can also be a choice that accepts zero arguments. That choice will be invoked if the switchObject is null.
switchObjectthe object against which the tests are being made.

Returns

The value of the selected choice.

Note

castSwitch can only be used with object types.