use generic type U in replace function