Optimizing a function over multiple types for its arguments