def classof(A, B):
    """
    Get the type of the result when combining matrices of different types.

    Currently the strategy is that immutability is contagious.

    Examples
    ========

    >>> M = Matrix([[1, 2], [3, 4]])  # a Mutable Matrix
    >>> IM = ImmutableMatrix([[1, 2], [3, 4]])
    >>> classof(M, IM)
    <class 'diofant.matrices.immutable.ImmutableMatrix'>

    """
    try:
        if A._class_priority > B._class_priority:
            return A.__class__
        else:
            return B.__class__
    except AttributeError:
        pass
    raise TypeError(f'Incompatible classes {A.__class__}, {B.__class__}')
