Add cast function for casting objects.