<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def safe_cast(invar, totype):
 """Performs a "safe" typecast. Ensures that `invar` properly casts to `totype`. Checks after casting that the result is actually of type `totype`. Any exceptions raised by the typecast itself are unhandled. Parameters invar (arbitrary) -- Value to be typecast. totype |type| -- Type to which `invar` is to be cast. Returns ------- outvar `type 'totype'` -- Typecast version of `invar` Raises ------ ~exceptions.TypeError If result of typecast is not of type `totype` """


    # Make the typecast. Just use Python built-in exceptioning
    outvar = totype(invar)

    # Check that the cast type matches
    if not isinstance(outvar, totype):
        raise TypeError("Result of cast to '{0}' is '{1}'"
                                            .format(totype, type(outvar)))
    ## end if

    # Success; return the cast value
    return outvar